blob: 7a6afbd560e1292bf4e43200f3ec49faad6a51fa [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
Douglas Gregor89cc9d62010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor1b2949d2010-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 Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
Douglas Gregord5dc83a2010-12-01 01:36:18 +000072 QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
Abramo Bagnara465d41b2010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor1b2949d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCallc12c5bb2010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnara25777432010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas Gregor083a8212010-02-21 18:24:45 +000086 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000087 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregor040afae2010-11-30 19:14:50 +000088 TemplateParameterList *ImportTemplateParameterList(
89 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-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 Gregor96a01b42010-02-11 00:48:18 +000094 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000095 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +000096 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000097 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +000098 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor9e5d9962010-02-10 21:10:29 +000099 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000100 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000102 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000103 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-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 Gregor96a01b42010-02-11 00:48:18 +0000108 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000109 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000110 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000111 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000112 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000113 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000114 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000115 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000116 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000117 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000118 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000119 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000120 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000121 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000122 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000123 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-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 Gregord5dc83a2010-12-01 01:36:18 +0000128 Decl *VisitClassTemplateSpecializationDecl(
129 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000130
Douglas Gregor4800d952010-02-11 19:21:55 +0000131 // Importing statements
132 Stmt *VisitStmt(Stmt *S);
133
134 // Importing expressions
135 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000136 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000137 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000138 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000139 Expr *VisitParenExpr(ParenExpr *E);
140 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregorbd249a52010-02-19 01:24:23 +0000141 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000142 Expr *VisitBinaryOperator(BinaryOperator *E);
143 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000144 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000145 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000146 };
147}
148
149//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-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 Gregor73dc30b2010-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 Gregorea35d112010-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 Gregor73dc30b2010-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 Gregorea35d112010-02-15 23:54:17 +0000176 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000177 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000178 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000179 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-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 Kyrtzidis33e4e702010-11-18 20:06:41 +0000196 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000197 }
198
199 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000200 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-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 Foad9f71a8f2010-12-07 08:25:34 +0000217 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000218
Jay Foad9f71a8f2010-12-07 08:25:34 +0000219 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-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 Foad9f71a8f2010-12-07 08:25:34 +0000230 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000231 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000232 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-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 Gregord5dc83a2010-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());
304
305 case TemplateArgument::Expression:
306 return IsStructurallyEquivalent(Context,
307 Arg1.getAsExpr(), Arg2.getAsExpr());
308
309 case TemplateArgument::Pack:
310 if (Arg1.pack_size() != Arg2.pack_size())
311 return false;
312
313 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
314 if (!IsStructurallyEquivalent(Context,
315 Arg1.pack_begin()[I],
316 Arg2.pack_begin()[I]))
317 return false;
318
319 return true;
320 }
321
322 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000323 return true;
324}
325
326/// \brief Determine structural equivalence for the common part of array
327/// types.
328static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
329 const ArrayType *Array1,
330 const ArrayType *Array2) {
331 if (!IsStructurallyEquivalent(Context,
332 Array1->getElementType(),
333 Array2->getElementType()))
334 return false;
335 if (Array1->getSizeModifier() != Array2->getSizeModifier())
336 return false;
337 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
338 return false;
339
340 return true;
341}
342
343/// \brief Determine structural equivalence of two types.
344static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
345 QualType T1, QualType T2) {
346 if (T1.isNull() || T2.isNull())
347 return T1.isNull() && T2.isNull();
348
349 if (!Context.StrictTypeSpelling) {
350 // We aren't being strict about token-to-token equivalence of types,
351 // so map down to the canonical type.
352 T1 = Context.C1.getCanonicalType(T1);
353 T2 = Context.C2.getCanonicalType(T2);
354 }
355
356 if (T1.getQualifiers() != T2.getQualifiers())
357 return false;
358
Douglas Gregorea35d112010-02-15 23:54:17 +0000359 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000360
Douglas Gregorea35d112010-02-15 23:54:17 +0000361 if (T1->getTypeClass() != T2->getTypeClass()) {
362 // Compare function types with prototypes vs. without prototypes as if
363 // both did not have prototypes.
364 if (T1->getTypeClass() == Type::FunctionProto &&
365 T2->getTypeClass() == Type::FunctionNoProto)
366 TC = Type::FunctionNoProto;
367 else if (T1->getTypeClass() == Type::FunctionNoProto &&
368 T2->getTypeClass() == Type::FunctionProto)
369 TC = Type::FunctionNoProto;
370 else
371 return false;
372 }
373
374 switch (TC) {
375 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000376 // FIXME: Deal with Char_S/Char_U.
377 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
378 return false;
379 break;
380
381 case Type::Complex:
382 if (!IsStructurallyEquivalent(Context,
383 cast<ComplexType>(T1)->getElementType(),
384 cast<ComplexType>(T2)->getElementType()))
385 return false;
386 break;
387
388 case Type::Pointer:
389 if (!IsStructurallyEquivalent(Context,
390 cast<PointerType>(T1)->getPointeeType(),
391 cast<PointerType>(T2)->getPointeeType()))
392 return false;
393 break;
394
395 case Type::BlockPointer:
396 if (!IsStructurallyEquivalent(Context,
397 cast<BlockPointerType>(T1)->getPointeeType(),
398 cast<BlockPointerType>(T2)->getPointeeType()))
399 return false;
400 break;
401
402 case Type::LValueReference:
403 case Type::RValueReference: {
404 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
405 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
406 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
407 return false;
408 if (Ref1->isInnerRef() != Ref2->isInnerRef())
409 return false;
410 if (!IsStructurallyEquivalent(Context,
411 Ref1->getPointeeTypeAsWritten(),
412 Ref2->getPointeeTypeAsWritten()))
413 return false;
414 break;
415 }
416
417 case Type::MemberPointer: {
418 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
419 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
420 if (!IsStructurallyEquivalent(Context,
421 MemPtr1->getPointeeType(),
422 MemPtr2->getPointeeType()))
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 QualType(MemPtr1->getClass(), 0),
426 QualType(MemPtr2->getClass(), 0)))
427 return false;
428 break;
429 }
430
431 case Type::ConstantArray: {
432 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
433 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
434 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
435 return false;
436
437 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
438 return false;
439 break;
440 }
441
442 case Type::IncompleteArray:
443 if (!IsArrayStructurallyEquivalent(Context,
444 cast<ArrayType>(T1),
445 cast<ArrayType>(T2)))
446 return false;
447 break;
448
449 case Type::VariableArray: {
450 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
451 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
452 if (!IsStructurallyEquivalent(Context,
453 Array1->getSizeExpr(), Array2->getSizeExpr()))
454 return false;
455
456 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
457 return false;
458
459 break;
460 }
461
462 case Type::DependentSizedArray: {
463 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
464 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
465 if (!IsStructurallyEquivalent(Context,
466 Array1->getSizeExpr(), Array2->getSizeExpr()))
467 return false;
468
469 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
470 return false;
471
472 break;
473 }
474
475 case Type::DependentSizedExtVector: {
476 const DependentSizedExtVectorType *Vec1
477 = cast<DependentSizedExtVectorType>(T1);
478 const DependentSizedExtVectorType *Vec2
479 = cast<DependentSizedExtVectorType>(T2);
480 if (!IsStructurallyEquivalent(Context,
481 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
482 return false;
483 if (!IsStructurallyEquivalent(Context,
484 Vec1->getElementType(),
485 Vec2->getElementType()))
486 return false;
487 break;
488 }
489
490 case Type::Vector:
491 case Type::ExtVector: {
492 const VectorType *Vec1 = cast<VectorType>(T1);
493 const VectorType *Vec2 = cast<VectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getElementType(),
496 Vec2->getElementType()))
497 return false;
498 if (Vec1->getNumElements() != Vec2->getNumElements())
499 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000500 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000501 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000502 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000503 }
504
505 case Type::FunctionProto: {
506 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
507 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
508 if (Proto1->getNumArgs() != Proto2->getNumArgs())
509 return false;
510 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
511 if (!IsStructurallyEquivalent(Context,
512 Proto1->getArgType(I),
513 Proto2->getArgType(I)))
514 return false;
515 }
516 if (Proto1->isVariadic() != Proto2->isVariadic())
517 return false;
518 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
519 return false;
520 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
521 return false;
522 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getExceptionType(I),
527 Proto2->getExceptionType(I)))
528 return false;
529 }
530 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
531 return false;
532
533 // Fall through to check the bits common with FunctionNoProtoType.
534 }
535
536 case Type::FunctionNoProto: {
537 const FunctionType *Function1 = cast<FunctionType>(T1);
538 const FunctionType *Function2 = cast<FunctionType>(T2);
539 if (!IsStructurallyEquivalent(Context,
540 Function1->getResultType(),
541 Function2->getResultType()))
542 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000543 if (Function1->getExtInfo() != Function2->getExtInfo())
544 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 break;
546 }
547
548 case Type::UnresolvedUsing:
549 if (!IsStructurallyEquivalent(Context,
550 cast<UnresolvedUsingType>(T1)->getDecl(),
551 cast<UnresolvedUsingType>(T2)->getDecl()))
552 return false;
553
554 break;
555
556 case Type::Typedef:
557 if (!IsStructurallyEquivalent(Context,
558 cast<TypedefType>(T1)->getDecl(),
559 cast<TypedefType>(T2)->getDecl()))
560 return false;
561 break;
562
563 case Type::TypeOfExpr:
564 if (!IsStructurallyEquivalent(Context,
565 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
566 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
567 return false;
568 break;
569
570 case Type::TypeOf:
571 if (!IsStructurallyEquivalent(Context,
572 cast<TypeOfType>(T1)->getUnderlyingType(),
573 cast<TypeOfType>(T2)->getUnderlyingType()))
574 return false;
575 break;
576
577 case Type::Decltype:
578 if (!IsStructurallyEquivalent(Context,
579 cast<DecltypeType>(T1)->getUnderlyingExpr(),
580 cast<DecltypeType>(T2)->getUnderlyingExpr()))
581 return false;
582 break;
583
584 case Type::Record:
585 case Type::Enum:
586 if (!IsStructurallyEquivalent(Context,
587 cast<TagType>(T1)->getDecl(),
588 cast<TagType>(T2)->getDecl()))
589 return false;
590 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000591
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000592 case Type::TemplateTypeParm: {
593 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
594 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
595 if (Parm1->getDepth() != Parm2->getDepth())
596 return false;
597 if (Parm1->getIndex() != Parm2->getIndex())
598 return false;
599 if (Parm1->isParameterPack() != Parm2->isParameterPack())
600 return false;
601
602 // Names of template type parameters are never significant.
603 break;
604 }
605
606 case Type::SubstTemplateTypeParm: {
607 const SubstTemplateTypeParmType *Subst1
608 = cast<SubstTemplateTypeParmType>(T1);
609 const SubstTemplateTypeParmType *Subst2
610 = cast<SubstTemplateTypeParmType>(T2);
611 if (!IsStructurallyEquivalent(Context,
612 QualType(Subst1->getReplacedParameter(), 0),
613 QualType(Subst2->getReplacedParameter(), 0)))
614 return false;
615 if (!IsStructurallyEquivalent(Context,
616 Subst1->getReplacementType(),
617 Subst2->getReplacementType()))
618 return false;
619 break;
620 }
621
622 case Type::TemplateSpecialization: {
623 const TemplateSpecializationType *Spec1
624 = cast<TemplateSpecializationType>(T1);
625 const TemplateSpecializationType *Spec2
626 = cast<TemplateSpecializationType>(T2);
627 if (!IsStructurallyEquivalent(Context,
628 Spec1->getTemplateName(),
629 Spec2->getTemplateName()))
630 return false;
631 if (Spec1->getNumArgs() != Spec2->getNumArgs())
632 return false;
633 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
634 if (!IsStructurallyEquivalent(Context,
635 Spec1->getArg(I), Spec2->getArg(I)))
636 return false;
637 }
638 break;
639 }
640
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000641 case Type::Elaborated: {
642 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
643 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
644 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
645 if (Elab1->getKeyword() != Elab2->getKeyword())
646 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000647 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000648 Elab1->getQualifier(),
649 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000650 return false;
651 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000652 Elab1->getNamedType(),
653 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000654 return false;
655 break;
656 }
657
John McCall3cb0ebd2010-03-10 03:28:59 +0000658 case Type::InjectedClassName: {
659 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
660 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
661 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000662 Inj1->getInjectedSpecializationType(),
663 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000664 return false;
665 break;
666 }
667
Douglas Gregor4714c122010-03-31 17:34:00 +0000668 case Type::DependentName: {
669 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
670 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000671 if (!IsStructurallyEquivalent(Context,
672 Typename1->getQualifier(),
673 Typename2->getQualifier()))
674 return false;
675 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
676 Typename2->getIdentifier()))
677 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000678
679 break;
680 }
681
John McCall33500952010-06-11 00:33:02 +0000682 case Type::DependentTemplateSpecialization: {
683 const DependentTemplateSpecializationType *Spec1 =
684 cast<DependentTemplateSpecializationType>(T1);
685 const DependentTemplateSpecializationType *Spec2 =
686 cast<DependentTemplateSpecializationType>(T2);
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getQualifier(),
689 Spec2->getQualifier()))
690 return false;
691 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
692 Spec2->getIdentifier()))
693 return false;
694 if (Spec1->getNumArgs() != Spec2->getNumArgs())
695 return false;
696 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
697 if (!IsStructurallyEquivalent(Context,
698 Spec1->getArg(I), Spec2->getArg(I)))
699 return false;
700 }
701 break;
702 }
703
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000704 case Type::ObjCInterface: {
705 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
706 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
707 if (!IsStructurallyEquivalent(Context,
708 Iface1->getDecl(), Iface2->getDecl()))
709 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000710 break;
711 }
712
713 case Type::ObjCObject: {
714 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
715 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
716 if (!IsStructurallyEquivalent(Context,
717 Obj1->getBaseType(),
718 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000719 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000720 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
721 return false;
722 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000723 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000724 Obj1->getProtocol(I),
725 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000726 return false;
727 }
728 break;
729 }
730
731 case Type::ObjCObjectPointer: {
732 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
733 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
734 if (!IsStructurallyEquivalent(Context,
735 Ptr1->getPointeeType(),
736 Ptr2->getPointeeType()))
737 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000738 break;
739 }
740
741 } // end switch
742
743 return true;
744}
745
746/// \brief Determine structural equivalence of two records.
747static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
748 RecordDecl *D1, RecordDecl *D2) {
749 if (D1->isUnion() != D2->isUnion()) {
750 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
751 << Context.C2.getTypeDeclType(D2);
752 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
753 << D1->getDeclName() << (unsigned)D1->getTagKind();
754 return false;
755 }
756
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000757 // If both declarations are class template specializations, we know
758 // the ODR applies, so check the template and template arguments.
759 ClassTemplateSpecializationDecl *Spec1
760 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
761 ClassTemplateSpecializationDecl *Spec2
762 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
763 if (Spec1 && Spec2) {
764 // Check that the specialized templates are the same.
765 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
766 Spec2->getSpecializedTemplate()))
767 return false;
768
769 // Check that the template arguments are the same.
770 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
771 return false;
772
773 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
774 if (!IsStructurallyEquivalent(Context,
775 Spec1->getTemplateArgs().get(I),
776 Spec2->getTemplateArgs().get(I)))
777 return false;
778 }
779 // If one is a class template specialization and the other is not, these
780 // structures are diferent.
781 else if (Spec1 || Spec2)
782 return false;
783
Douglas Gregorea35d112010-02-15 23:54:17 +0000784 // Compare the definitions of these two records. If either or both are
785 // incomplete, we assume that they are equivalent.
786 D1 = D1->getDefinition();
787 D2 = D2->getDefinition();
788 if (!D1 || !D2)
789 return true;
790
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000791 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
792 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
793 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
794 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000795 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000797 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000799 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000800 return false;
801 }
802
803 // Check the base classes.
804 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
805 BaseEnd1 = D1CXX->bases_end(),
806 Base2 = D2CXX->bases_begin();
807 Base1 != BaseEnd1;
808 ++Base1, ++Base2) {
809 if (!IsStructurallyEquivalent(Context,
810 Base1->getType(), Base2->getType())) {
811 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
812 << Context.C2.getTypeDeclType(D2);
813 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
814 << Base2->getType()
815 << Base2->getSourceRange();
816 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
817 << Base1->getType()
818 << Base1->getSourceRange();
819 return false;
820 }
821
822 // Check virtual vs. non-virtual inheritance mismatch.
823 if (Base1->isVirtual() != Base2->isVirtual()) {
824 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
825 << Context.C2.getTypeDeclType(D2);
826 Context.Diag2(Base2->getSourceRange().getBegin(),
827 diag::note_odr_virtual_base)
828 << Base2->isVirtual() << Base2->getSourceRange();
829 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
830 << Base1->isVirtual()
831 << Base1->getSourceRange();
832 return false;
833 }
834 }
835 } else if (D1CXX->getNumBases() > 0) {
836 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
837 << Context.C2.getTypeDeclType(D2);
838 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
839 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
840 << Base1->getType()
841 << Base1->getSourceRange();
842 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
843 return false;
844 }
845 }
846
847 // Check the fields for consistency.
848 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
849 Field2End = D2->field_end();
850 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
851 Field1End = D1->field_end();
852 Field1 != Field1End;
853 ++Field1, ++Field2) {
854 if (Field2 == Field2End) {
855 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
856 << Context.C2.getTypeDeclType(D2);
857 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
858 << Field1->getDeclName() << Field1->getType();
859 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
860 return false;
861 }
862
863 if (!IsStructurallyEquivalent(Context,
864 Field1->getType(), Field2->getType())) {
865 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(D2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
868 << Field2->getDeclName() << Field2->getType();
869 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
870 << Field1->getDeclName() << Field1->getType();
871 return false;
872 }
873
874 if (Field1->isBitField() != Field2->isBitField()) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
876 << Context.C2.getTypeDeclType(D2);
877 if (Field1->isBitField()) {
878 llvm::APSInt Bits;
879 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
880 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
881 << Field1->getDeclName() << Field1->getType()
882 << Bits.toString(10, false);
883 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
884 << Field2->getDeclName();
885 } else {
886 llvm::APSInt Bits;
887 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
888 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
889 << Field2->getDeclName() << Field2->getType()
890 << Bits.toString(10, false);
891 Context.Diag1(Field1->getLocation(),
892 diag::note_odr_not_bit_field)
893 << Field1->getDeclName();
894 }
895 return false;
896 }
897
898 if (Field1->isBitField()) {
899 // Make sure that the bit-fields are the same length.
900 llvm::APSInt Bits1, Bits2;
901 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
902 return false;
903 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
904 return false;
905
906 if (!IsSameValue(Bits1, Bits2)) {
907 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
908 << Context.C2.getTypeDeclType(D2);
909 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
910 << Field2->getDeclName() << Field2->getType()
911 << Bits2.toString(10, false);
912 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
913 << Field1->getDeclName() << Field1->getType()
914 << Bits1.toString(10, false);
915 return false;
916 }
917 }
918 }
919
920 if (Field2 != Field2End) {
921 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
922 << Context.C2.getTypeDeclType(D2);
923 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
924 << Field2->getDeclName() << Field2->getType();
925 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
926 return false;
927 }
928
929 return true;
930}
931
932/// \brief Determine structural equivalence of two enums.
933static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
934 EnumDecl *D1, EnumDecl *D2) {
935 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
936 EC2End = D2->enumerator_end();
937 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
938 EC1End = D1->enumerator_end();
939 EC1 != EC1End; ++EC1, ++EC2) {
940 if (EC2 == EC2End) {
941 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
942 << Context.C2.getTypeDeclType(D2);
943 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
944 << EC1->getDeclName()
945 << EC1->getInitVal().toString(10);
946 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
947 return false;
948 }
949
950 llvm::APSInt Val1 = EC1->getInitVal();
951 llvm::APSInt Val2 = EC2->getInitVal();
952 if (!IsSameValue(Val1, Val2) ||
953 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
956 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
957 << EC2->getDeclName()
958 << EC2->getInitVal().toString(10);
959 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
960 << EC1->getDeclName()
961 << EC1->getInitVal().toString(10);
962 return false;
963 }
964 }
965
966 if (EC2 != EC2End) {
967 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
968 << Context.C2.getTypeDeclType(D2);
969 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
970 << EC2->getDeclName()
971 << EC2->getInitVal().toString(10);
972 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
973 return false;
974 }
975
976 return true;
977}
Douglas Gregor040afae2010-11-30 19:14:50 +0000978
979static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
980 TemplateParameterList *Params1,
981 TemplateParameterList *Params2) {
982 if (Params1->size() != Params2->size()) {
983 Context.Diag2(Params2->getTemplateLoc(),
984 diag::err_odr_different_num_template_parameters)
985 << Params1->size() << Params2->size();
986 Context.Diag1(Params1->getTemplateLoc(),
987 diag::note_odr_template_parameter_list);
988 return false;
989 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000990
Douglas Gregor040afae2010-11-30 19:14:50 +0000991 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
992 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
993 Context.Diag2(Params2->getParam(I)->getLocation(),
994 diag::err_odr_different_template_parameter_kind);
995 Context.Diag1(Params1->getParam(I)->getLocation(),
996 diag::note_odr_template_parameter_here);
997 return false;
998 }
999
1000 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1001 Params2->getParam(I))) {
1002
1003 return false;
1004 }
1005 }
1006
1007 return true;
1008}
1009
1010static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1011 TemplateTypeParmDecl *D1,
1012 TemplateTypeParmDecl *D2) {
1013 if (D1->isParameterPack() != D2->isParameterPack()) {
1014 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1015 << D2->isParameterPack();
1016 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1017 << D1->isParameterPack();
1018 return false;
1019 }
1020
1021 return true;
1022}
1023
1024static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1025 NonTypeTemplateParmDecl *D1,
1026 NonTypeTemplateParmDecl *D2) {
1027 // FIXME: Enable once we have variadic templates.
1028#if 0
1029 if (D1->isParameterPack() != D2->isParameterPack()) {
1030 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1031 << D2->isParameterPack();
1032 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1033 << D1->isParameterPack();
1034 return false;
1035 }
1036#endif
1037
1038 // Check types.
1039 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1040 Context.Diag2(D2->getLocation(),
1041 diag::err_odr_non_type_parameter_type_inconsistent)
1042 << D2->getType() << D1->getType();
1043 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1044 << D1->getType();
1045 return false;
1046 }
1047
1048 return true;
1049}
1050
1051static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1052 TemplateTemplateParmDecl *D1,
1053 TemplateTemplateParmDecl *D2) {
1054 // FIXME: Enable once we have variadic templates.
1055#if 0
1056 if (D1->isParameterPack() != D2->isParameterPack()) {
1057 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1058 << D2->isParameterPack();
1059 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1060 << D1->isParameterPack();
1061 return false;
1062 }
1063#endif
1064
1065 // Check template parameter lists.
1066 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1067 D2->getTemplateParameters());
1068}
1069
1070static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1071 ClassTemplateDecl *D1,
1072 ClassTemplateDecl *D2) {
1073 // Check template parameters.
1074 if (!IsStructurallyEquivalent(Context,
1075 D1->getTemplateParameters(),
1076 D2->getTemplateParameters()))
1077 return false;
1078
1079 // Check the templated declaration.
1080 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1081 D2->getTemplatedDecl());
1082}
1083
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001084/// \brief Determine structural equivalence of two declarations.
1085static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1086 Decl *D1, Decl *D2) {
1087 // FIXME: Check for known structural equivalences via a callback of some sort.
1088
Douglas Gregorea35d112010-02-15 23:54:17 +00001089 // Check whether we already know that these two declarations are not
1090 // structurally equivalent.
1091 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1092 D2->getCanonicalDecl())))
1093 return false;
1094
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001095 // Determine whether we've already produced a tentative equivalence for D1.
1096 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1097 if (EquivToD1)
1098 return EquivToD1 == D2->getCanonicalDecl();
1099
1100 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1101 EquivToD1 = D2->getCanonicalDecl();
1102 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1103 return true;
1104}
1105
1106bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1107 Decl *D2) {
1108 if (!::IsStructurallyEquivalent(*this, D1, D2))
1109 return false;
1110
1111 return !Finish();
1112}
1113
1114bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1115 QualType T2) {
1116 if (!::IsStructurallyEquivalent(*this, T1, T2))
1117 return false;
1118
1119 return !Finish();
1120}
1121
1122bool StructuralEquivalenceContext::Finish() {
1123 while (!DeclsToCheck.empty()) {
1124 // Check the next declaration.
1125 Decl *D1 = DeclsToCheck.front();
1126 DeclsToCheck.pop_front();
1127
1128 Decl *D2 = TentativeEquivalences[D1];
1129 assert(D2 && "Unrecorded tentative equivalence?");
1130
Douglas Gregorea35d112010-02-15 23:54:17 +00001131 bool Equivalent = true;
1132
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001133 // FIXME: Switch on all declaration kinds. For now, we're just going to
1134 // check the obvious ones.
1135 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1136 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1137 // Check for equivalent structure names.
1138 IdentifierInfo *Name1 = Record1->getIdentifier();
1139 if (!Name1 && Record1->getTypedefForAnonDecl())
1140 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1141 IdentifierInfo *Name2 = Record2->getIdentifier();
1142 if (!Name2 && Record2->getTypedefForAnonDecl())
1143 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001144 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1145 !::IsStructurallyEquivalent(*this, Record1, Record2))
1146 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001147 } else {
1148 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001149 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001150 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001151 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001152 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1153 // Check for equivalent enum names.
1154 IdentifierInfo *Name1 = Enum1->getIdentifier();
1155 if (!Name1 && Enum1->getTypedefForAnonDecl())
1156 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1157 IdentifierInfo *Name2 = Enum2->getIdentifier();
1158 if (!Name2 && Enum2->getTypedefForAnonDecl())
1159 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001160 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1161 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1162 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001163 } else {
1164 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001165 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001166 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001167 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001168 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1169 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001170 Typedef2->getIdentifier()) ||
1171 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001172 Typedef1->getUnderlyingType(),
1173 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001174 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001175 } else {
1176 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001177 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001178 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001179 } else if (ClassTemplateDecl *ClassTemplate1
1180 = dyn_cast<ClassTemplateDecl>(D1)) {
1181 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1182 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1183 ClassTemplate2->getIdentifier()) ||
1184 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1185 Equivalent = false;
1186 } else {
1187 // Class template/non-class-template mismatch.
1188 Equivalent = false;
1189 }
1190 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1191 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1192 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1193 Equivalent = false;
1194 } else {
1195 // Kind mismatch.
1196 Equivalent = false;
1197 }
1198 } else if (NonTypeTemplateParmDecl *NTTP1
1199 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1200 if (NonTypeTemplateParmDecl *NTTP2
1201 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1202 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1203 Equivalent = false;
1204 } else {
1205 // Kind mismatch.
1206 Equivalent = false;
1207 }
1208 } else if (TemplateTemplateParmDecl *TTP1
1209 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1210 if (TemplateTemplateParmDecl *TTP2
1211 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1212 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1213 Equivalent = false;
1214 } else {
1215 // Kind mismatch.
1216 Equivalent = false;
1217 }
1218 }
1219
Douglas Gregorea35d112010-02-15 23:54:17 +00001220 if (!Equivalent) {
1221 // Note that these two declarations are not equivalent (and we already
1222 // know about it).
1223 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1224 D2->getCanonicalDecl()));
1225 return true;
1226 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001227 // FIXME: Check other declaration kinds!
1228 }
1229
1230 return false;
1231}
1232
1233//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001234// Import Types
1235//----------------------------------------------------------------------------
1236
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001237QualType ASTNodeImporter::VisitType(Type *T) {
1238 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1239 << T->getTypeClassName();
1240 return QualType();
1241}
1242
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001243QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1244 switch (T->getKind()) {
1245 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1246 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1247
1248 case BuiltinType::Char_U:
1249 // The context we're importing from has an unsigned 'char'. If we're
1250 // importing into a context with a signed 'char', translate to
1251 // 'unsigned char' instead.
1252 if (Importer.getToContext().getLangOptions().CharIsSigned)
1253 return Importer.getToContext().UnsignedCharTy;
1254
1255 return Importer.getToContext().CharTy;
1256
1257 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1258
1259 case BuiltinType::Char16:
1260 // FIXME: Make sure that the "to" context supports C++!
1261 return Importer.getToContext().Char16Ty;
1262
1263 case BuiltinType::Char32:
1264 // FIXME: Make sure that the "to" context supports C++!
1265 return Importer.getToContext().Char32Ty;
1266
1267 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1268 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1269 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1270 case BuiltinType::ULongLong:
1271 return Importer.getToContext().UnsignedLongLongTy;
1272 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1273
1274 case BuiltinType::Char_S:
1275 // The context we're importing from has an unsigned 'char'. If we're
1276 // importing into a context with a signed 'char', translate to
1277 // 'unsigned char' instead.
1278 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1279 return Importer.getToContext().SignedCharTy;
1280
1281 return Importer.getToContext().CharTy;
1282
1283 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1284 case BuiltinType::WChar:
1285 // FIXME: If not in C++, shall we translate to the C equivalent of
1286 // wchar_t?
1287 return Importer.getToContext().WCharTy;
1288
1289 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1290 case BuiltinType::Int : return Importer.getToContext().IntTy;
1291 case BuiltinType::Long : return Importer.getToContext().LongTy;
1292 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1293 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1294 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1295 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1296 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1297
1298 case BuiltinType::NullPtr:
1299 // FIXME: Make sure that the "to" context supports C++0x!
1300 return Importer.getToContext().NullPtrTy;
1301
1302 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1303 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1304 case BuiltinType::UndeducedAuto:
1305 // FIXME: Make sure that the "to" context supports C++0x!
1306 return Importer.getToContext().UndeducedAutoTy;
1307
1308 case BuiltinType::ObjCId:
1309 // FIXME: Make sure that the "to" context supports Objective-C!
1310 return Importer.getToContext().ObjCBuiltinIdTy;
1311
1312 case BuiltinType::ObjCClass:
1313 return Importer.getToContext().ObjCBuiltinClassTy;
1314
1315 case BuiltinType::ObjCSel:
1316 return Importer.getToContext().ObjCBuiltinSelTy;
1317 }
1318
1319 return QualType();
1320}
1321
1322QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1323 QualType ToElementType = Importer.Import(T->getElementType());
1324 if (ToElementType.isNull())
1325 return QualType();
1326
1327 return Importer.getToContext().getComplexType(ToElementType);
1328}
1329
1330QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1331 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1332 if (ToPointeeType.isNull())
1333 return QualType();
1334
1335 return Importer.getToContext().getPointerType(ToPointeeType);
1336}
1337
1338QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1339 // FIXME: Check for blocks support in "to" context.
1340 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1341 if (ToPointeeType.isNull())
1342 return QualType();
1343
1344 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1345}
1346
1347QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1348 // FIXME: Check for C++ support in "to" context.
1349 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1350 if (ToPointeeType.isNull())
1351 return QualType();
1352
1353 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1354}
1355
1356QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1357 // FIXME: Check for C++0x support in "to" context.
1358 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1359 if (ToPointeeType.isNull())
1360 return QualType();
1361
1362 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1363}
1364
1365QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1366 // FIXME: Check for C++ support in "to" context.
1367 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1368 if (ToPointeeType.isNull())
1369 return QualType();
1370
1371 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1372 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1373 ClassType.getTypePtr());
1374}
1375
1376QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1377 QualType ToElementType = Importer.Import(T->getElementType());
1378 if (ToElementType.isNull())
1379 return QualType();
1380
1381 return Importer.getToContext().getConstantArrayType(ToElementType,
1382 T->getSize(),
1383 T->getSizeModifier(),
1384 T->getIndexTypeCVRQualifiers());
1385}
1386
1387QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1388 QualType ToElementType = Importer.Import(T->getElementType());
1389 if (ToElementType.isNull())
1390 return QualType();
1391
1392 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1393 T->getSizeModifier(),
1394 T->getIndexTypeCVRQualifiers());
1395}
1396
1397QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1398 QualType ToElementType = Importer.Import(T->getElementType());
1399 if (ToElementType.isNull())
1400 return QualType();
1401
1402 Expr *Size = Importer.Import(T->getSizeExpr());
1403 if (!Size)
1404 return QualType();
1405
1406 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1407 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1408 T->getSizeModifier(),
1409 T->getIndexTypeCVRQualifiers(),
1410 Brackets);
1411}
1412
1413QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1414 QualType ToElementType = Importer.Import(T->getElementType());
1415 if (ToElementType.isNull())
1416 return QualType();
1417
1418 return Importer.getToContext().getVectorType(ToElementType,
1419 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001420 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421}
1422
1423QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1424 QualType ToElementType = Importer.Import(T->getElementType());
1425 if (ToElementType.isNull())
1426 return QualType();
1427
1428 return Importer.getToContext().getExtVectorType(ToElementType,
1429 T->getNumElements());
1430}
1431
1432QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1433 // FIXME: What happens if we're importing a function without a prototype
1434 // into C++? Should we make it variadic?
1435 QualType ToResultType = Importer.Import(T->getResultType());
1436 if (ToResultType.isNull())
1437 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001438
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001439 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001440 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001441}
1442
1443QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1444 QualType ToResultType = Importer.Import(T->getResultType());
1445 if (ToResultType.isNull())
1446 return QualType();
1447
1448 // Import argument types
1449 llvm::SmallVector<QualType, 4> ArgTypes;
1450 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1451 AEnd = T->arg_type_end();
1452 A != AEnd; ++A) {
1453 QualType ArgType = Importer.Import(*A);
1454 if (ArgType.isNull())
1455 return QualType();
1456 ArgTypes.push_back(ArgType);
1457 }
1458
1459 // Import exception types
1460 llvm::SmallVector<QualType, 4> ExceptionTypes;
1461 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1462 EEnd = T->exception_end();
1463 E != EEnd; ++E) {
1464 QualType ExceptionType = Importer.Import(*E);
1465 if (ExceptionType.isNull())
1466 return QualType();
1467 ExceptionTypes.push_back(ExceptionType);
1468 }
1469
1470 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1471 ArgTypes.size(),
1472 T->isVariadic(),
1473 T->getTypeQuals(),
1474 T->hasExceptionSpec(),
1475 T->hasAnyExceptionSpec(),
1476 ExceptionTypes.size(),
1477 ExceptionTypes.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001478 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001479}
1480
1481QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1482 TypedefDecl *ToDecl
1483 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1484 if (!ToDecl)
1485 return QualType();
1486
1487 return Importer.getToContext().getTypeDeclType(ToDecl);
1488}
1489
1490QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1491 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1492 if (!ToExpr)
1493 return QualType();
1494
1495 return Importer.getToContext().getTypeOfExprType(ToExpr);
1496}
1497
1498QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1499 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1500 if (ToUnderlyingType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1504}
1505
1506QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1507 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1508 if (!ToExpr)
1509 return QualType();
1510
1511 return Importer.getToContext().getDecltypeType(ToExpr);
1512}
1513
1514QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1515 RecordDecl *ToDecl
1516 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1517 if (!ToDecl)
1518 return QualType();
1519
1520 return Importer.getToContext().getTagDeclType(ToDecl);
1521}
1522
1523QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1524 EnumDecl *ToDecl
1525 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1526 if (!ToDecl)
1527 return QualType();
1528
1529 return Importer.getToContext().getTagDeclType(ToDecl);
1530}
1531
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001532QualType ASTNodeImporter::VisitTemplateSpecializationType(
1533 TemplateSpecializationType *T) {
1534 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1535 if (ToTemplate.isNull())
1536 return QualType();
1537
1538 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1539 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1540 return QualType();
1541
1542 QualType ToCanonType;
1543 if (!QualType(T, 0).isCanonical()) {
1544 QualType FromCanonType
1545 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1546 ToCanonType =Importer.Import(FromCanonType);
1547 if (ToCanonType.isNull())
1548 return QualType();
1549 }
1550 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1551 ToTemplateArgs.data(),
1552 ToTemplateArgs.size(),
1553 ToCanonType);
1554}
1555
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001556QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001557 NestedNameSpecifier *ToQualifier = 0;
1558 // Note: the qualifier in an ElaboratedType is optional.
1559 if (T->getQualifier()) {
1560 ToQualifier = Importer.Import(T->getQualifier());
1561 if (!ToQualifier)
1562 return QualType();
1563 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001564
1565 QualType ToNamedType = Importer.Import(T->getNamedType());
1566 if (ToNamedType.isNull())
1567 return QualType();
1568
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001569 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1570 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001571}
1572
1573QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1574 ObjCInterfaceDecl *Class
1575 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1576 if (!Class)
1577 return QualType();
1578
John McCallc12c5bb2010-05-15 11:32:37 +00001579 return Importer.getToContext().getObjCInterfaceType(Class);
1580}
1581
1582QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1583 QualType ToBaseType = Importer.Import(T->getBaseType());
1584 if (ToBaseType.isNull())
1585 return QualType();
1586
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001587 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001588 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 PEnd = T->qual_end();
1590 P != PEnd; ++P) {
1591 ObjCProtocolDecl *Protocol
1592 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1593 if (!Protocol)
1594 return QualType();
1595 Protocols.push_back(Protocol);
1596 }
1597
John McCallc12c5bb2010-05-15 11:32:37 +00001598 return Importer.getToContext().getObjCObjectType(ToBaseType,
1599 Protocols.data(),
1600 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001601}
1602
1603QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1604 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1605 if (ToPointeeType.isNull())
1606 return QualType();
1607
John McCallc12c5bb2010-05-15 11:32:37 +00001608 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001609}
1610
Douglas Gregor089459a2010-02-08 21:09:39 +00001611//----------------------------------------------------------------------------
1612// Import Declarations
1613//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001614bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1615 DeclContext *&LexicalDC,
1616 DeclarationName &Name,
1617 SourceLocation &Loc) {
1618 // Import the context of this declaration.
1619 DC = Importer.ImportContext(D->getDeclContext());
1620 if (!DC)
1621 return true;
1622
1623 LexicalDC = DC;
1624 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1625 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1626 if (!LexicalDC)
1627 return true;
1628 }
1629
1630 // Import the name of this declaration.
1631 Name = Importer.Import(D->getDeclName());
1632 if (D->getDeclName() && !Name)
1633 return true;
1634
1635 // Import the location of this declaration.
1636 Loc = Importer.Import(D->getLocation());
1637 return false;
1638}
1639
Abramo Bagnara25777432010-08-11 22:01:17 +00001640void
1641ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1642 DeclarationNameInfo& To) {
1643 // NOTE: To.Name and To.Loc are already imported.
1644 // We only have to import To.LocInfo.
1645 switch (To.getName().getNameKind()) {
1646 case DeclarationName::Identifier:
1647 case DeclarationName::ObjCZeroArgSelector:
1648 case DeclarationName::ObjCOneArgSelector:
1649 case DeclarationName::ObjCMultiArgSelector:
1650 case DeclarationName::CXXUsingDirective:
1651 return;
1652
1653 case DeclarationName::CXXOperatorName: {
1654 SourceRange Range = From.getCXXOperatorNameRange();
1655 To.setCXXOperatorNameRange(Importer.Import(Range));
1656 return;
1657 }
1658 case DeclarationName::CXXLiteralOperatorName: {
1659 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1660 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1661 return;
1662 }
1663 case DeclarationName::CXXConstructorName:
1664 case DeclarationName::CXXDestructorName:
1665 case DeclarationName::CXXConversionFunctionName: {
1666 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1667 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1668 return;
1669 }
1670 assert(0 && "Unknown name kind.");
1671 }
1672}
1673
Douglas Gregor083a8212010-02-21 18:24:45 +00001674void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1675 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1676 FromEnd = FromDC->decls_end();
1677 From != FromEnd;
1678 ++From)
1679 Importer.Import(*From);
1680}
1681
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001682bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1683 if (To->getDefinition())
1684 return false;
1685
1686 To->startDefinition();
1687
1688 // Add base classes.
1689 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1690 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1691
1692 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1693 for (CXXRecordDecl::base_class_iterator
1694 Base1 = FromCXX->bases_begin(),
1695 FromBaseEnd = FromCXX->bases_end();
1696 Base1 != FromBaseEnd;
1697 ++Base1) {
1698 QualType T = Importer.Import(Base1->getType());
1699 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001700 return true;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001701
1702 Bases.push_back(
1703 new (Importer.getToContext())
1704 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1705 Base1->isVirtual(),
1706 Base1->isBaseOfClass(),
1707 Base1->getAccessSpecifierAsWritten(),
1708 Importer.Import(Base1->getTypeSourceInfo())));
1709 }
1710 if (!Bases.empty())
1711 ToCXX->setBases(Bases.data(), Bases.size());
1712 }
1713
1714 ImportDeclContext(From);
1715 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001716 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001717}
1718
Douglas Gregor040afae2010-11-30 19:14:50 +00001719TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1720 TemplateParameterList *Params) {
1721 llvm::SmallVector<NamedDecl *, 4> ToParams;
1722 ToParams.reserve(Params->size());
1723 for (TemplateParameterList::iterator P = Params->begin(),
1724 PEnd = Params->end();
1725 P != PEnd; ++P) {
1726 Decl *To = Importer.Import(*P);
1727 if (!To)
1728 return 0;
1729
1730 ToParams.push_back(cast<NamedDecl>(To));
1731 }
1732
1733 return TemplateParameterList::Create(Importer.getToContext(),
1734 Importer.Import(Params->getTemplateLoc()),
1735 Importer.Import(Params->getLAngleLoc()),
1736 ToParams.data(), ToParams.size(),
1737 Importer.Import(Params->getRAngleLoc()));
1738}
1739
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001740TemplateArgument
1741ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1742 switch (From.getKind()) {
1743 case TemplateArgument::Null:
1744 return TemplateArgument();
1745
1746 case TemplateArgument::Type: {
1747 QualType ToType = Importer.Import(From.getAsType());
1748 if (ToType.isNull())
1749 return TemplateArgument();
1750 return TemplateArgument(ToType);
1751 }
1752
1753 case TemplateArgument::Integral: {
1754 QualType ToType = Importer.Import(From.getIntegralType());
1755 if (ToType.isNull())
1756 return TemplateArgument();
1757 return TemplateArgument(*From.getAsIntegral(), ToType);
1758 }
1759
1760 case TemplateArgument::Declaration:
1761 if (Decl *To = Importer.Import(From.getAsDecl()))
1762 return TemplateArgument(To);
1763 return TemplateArgument();
1764
1765 case TemplateArgument::Template: {
1766 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1767 if (ToTemplate.isNull())
1768 return TemplateArgument();
1769
1770 return TemplateArgument(ToTemplate);
1771 }
1772
1773 case TemplateArgument::Expression:
1774 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1775 return TemplateArgument(ToExpr);
1776 return TemplateArgument();
1777
1778 case TemplateArgument::Pack: {
1779 llvm::SmallVector<TemplateArgument, 2> ToPack;
1780 ToPack.reserve(From.pack_size());
1781 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1782 return TemplateArgument();
1783
1784 TemplateArgument *ToArgs
1785 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1786 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1787 return TemplateArgument(ToArgs, ToPack.size());
1788 }
1789 }
1790
1791 llvm_unreachable("Invalid template argument kind");
1792 return TemplateArgument();
1793}
1794
1795bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1796 unsigned NumFromArgs,
1797 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1798 for (unsigned I = 0; I != NumFromArgs; ++I) {
1799 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1800 if (To.isNull() && !FromArgs[I].isNull())
1801 return true;
1802
1803 ToArgs.push_back(To);
1804 }
1805
1806 return false;
1807}
1808
Douglas Gregor96a01b42010-02-11 00:48:18 +00001809bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001810 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001811 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001812 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001813 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001814 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001815}
1816
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001817bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001818 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001819 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001820 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001821 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001822}
1823
Douglas Gregor040afae2010-11-30 19:14:50 +00001824bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1825 ClassTemplateDecl *To) {
1826 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1827 Importer.getToContext(),
1828 Importer.getNonEquivalentDecls());
1829 return Ctx.IsStructurallyEquivalent(From, To);
1830}
1831
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001832Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001833 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001834 << D->getDeclKindName();
1835 return 0;
1836}
1837
Douglas Gregor788c62d2010-02-21 18:26:36 +00001838Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1839 // Import the major distinguishing characteristics of this namespace.
1840 DeclContext *DC, *LexicalDC;
1841 DeclarationName Name;
1842 SourceLocation Loc;
1843 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1844 return 0;
1845
1846 NamespaceDecl *MergeWithNamespace = 0;
1847 if (!Name) {
1848 // This is an anonymous namespace. Adopt an existing anonymous
1849 // namespace if we can.
1850 // FIXME: Not testable.
1851 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1852 MergeWithNamespace = TU->getAnonymousNamespace();
1853 else
1854 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1855 } else {
1856 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1857 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1858 Lookup.first != Lookup.second;
1859 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001860 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001861 continue;
1862
1863 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1864 MergeWithNamespace = FoundNS;
1865 ConflictingDecls.clear();
1866 break;
1867 }
1868
1869 ConflictingDecls.push_back(*Lookup.first);
1870 }
1871
1872 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001873 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001874 ConflictingDecls.data(),
1875 ConflictingDecls.size());
1876 }
1877 }
1878
1879 // Create the "to" namespace, if needed.
1880 NamespaceDecl *ToNamespace = MergeWithNamespace;
1881 if (!ToNamespace) {
1882 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1883 Name.getAsIdentifierInfo());
1884 ToNamespace->setLexicalDeclContext(LexicalDC);
1885 LexicalDC->addDecl(ToNamespace);
1886
1887 // If this is an anonymous namespace, register it as the anonymous
1888 // namespace within its context.
1889 if (!Name) {
1890 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1891 TU->setAnonymousNamespace(ToNamespace);
1892 else
1893 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1894 }
1895 }
1896 Importer.Imported(D, ToNamespace);
1897
1898 ImportDeclContext(D);
1899
1900 return ToNamespace;
1901}
1902
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001903Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1904 // Import the major distinguishing characteristics of this typedef.
1905 DeclContext *DC, *LexicalDC;
1906 DeclarationName Name;
1907 SourceLocation Loc;
1908 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1909 return 0;
1910
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001911 // If this typedef is not in block scope, determine whether we've
1912 // seen a typedef with the same name (that we can merge with) or any
1913 // other entity by that name (which name lookup could conflict with).
1914 if (!DC->isFunctionOrMethod()) {
1915 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1916 unsigned IDNS = Decl::IDNS_Ordinary;
1917 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1918 Lookup.first != Lookup.second;
1919 ++Lookup.first) {
1920 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1921 continue;
1922 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001923 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1924 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001925 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001926 }
1927
1928 ConflictingDecls.push_back(*Lookup.first);
1929 }
1930
1931 if (!ConflictingDecls.empty()) {
1932 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1933 ConflictingDecls.data(),
1934 ConflictingDecls.size());
1935 if (!Name)
1936 return 0;
1937 }
1938 }
1939
Douglas Gregorea35d112010-02-15 23:54:17 +00001940 // Import the underlying type of this typedef;
1941 QualType T = Importer.Import(D->getUnderlyingType());
1942 if (T.isNull())
1943 return 0;
1944
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001945 // Create the new typedef node.
1946 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1947 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1948 Loc, Name.getAsIdentifierInfo(),
1949 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001950 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001951 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001952 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001953 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001954
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001955 return ToTypedef;
1956}
1957
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001958Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1959 // Import the major distinguishing characteristics of this enum.
1960 DeclContext *DC, *LexicalDC;
1961 DeclarationName Name;
1962 SourceLocation Loc;
1963 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1964 return 0;
1965
1966 // Figure out what enum name we're looking for.
1967 unsigned IDNS = Decl::IDNS_Tag;
1968 DeclarationName SearchName = Name;
1969 if (!SearchName && D->getTypedefForAnonDecl()) {
1970 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1971 IDNS = Decl::IDNS_Ordinary;
1972 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1973 IDNS |= Decl::IDNS_Ordinary;
1974
1975 // We may already have an enum of the same name; try to find and match it.
1976 if (!DC->isFunctionOrMethod() && SearchName) {
1977 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1978 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1979 Lookup.first != Lookup.second;
1980 ++Lookup.first) {
1981 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1982 continue;
1983
1984 Decl *Found = *Lookup.first;
1985 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1986 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1987 Found = Tag->getDecl();
1988 }
1989
1990 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001991 if (IsStructuralMatch(D, FoundEnum))
1992 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001993 }
1994
1995 ConflictingDecls.push_back(*Lookup.first);
1996 }
1997
1998 if (!ConflictingDecls.empty()) {
1999 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2000 ConflictingDecls.data(),
2001 ConflictingDecls.size());
2002 }
2003 }
2004
2005 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002007 Name.getAsIdentifierInfo(),
2008 Importer.Import(D->getTagKeywordLoc()), 0,
2009 D->isScoped(), D->isScopedUsingClassTag(),
2010 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002011 // Import the qualifier, if any.
2012 if (D->getQualifier()) {
2013 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2014 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2015 D2->setQualifierInfo(NNS, NNSRange);
2016 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002017 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002018 D2->setLexicalDeclContext(LexicalDC);
2019 Importer.Imported(D, D2);
2020 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002021
2022 // Import the integer type.
2023 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2024 if (ToIntegerType.isNull())
2025 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002026 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002027
2028 // Import the definition
2029 if (D->isDefinition()) {
2030 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2031 if (T.isNull())
2032 return 0;
2033
2034 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2035 if (ToPromotionType.isNull())
2036 return 0;
2037
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002038 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002039 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002040
2041 // FIXME: we might need to merge the number of positive or negative bits
2042 // if the enumerator lists don't match.
2043 D2->completeDefinition(T, ToPromotionType,
2044 D->getNumPositiveBits(),
2045 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002046 }
2047
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002048 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002049}
2050
Douglas Gregor96a01b42010-02-11 00:48:18 +00002051Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2052 // If this record has a definition in the translation unit we're coming from,
2053 // but this particular declaration is not that definition, import the
2054 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002055 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002056 if (Definition && Definition != D) {
2057 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002058 if (!ImportedDef)
2059 return 0;
2060
2061 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002062 }
2063
2064 // Import the major distinguishing characteristics of this record.
2065 DeclContext *DC, *LexicalDC;
2066 DeclarationName Name;
2067 SourceLocation Loc;
2068 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2069 return 0;
2070
2071 // Figure out what structure name we're looking for.
2072 unsigned IDNS = Decl::IDNS_Tag;
2073 DeclarationName SearchName = Name;
2074 if (!SearchName && D->getTypedefForAnonDecl()) {
2075 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2076 IDNS = Decl::IDNS_Ordinary;
2077 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2078 IDNS |= Decl::IDNS_Ordinary;
2079
2080 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002081 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002082 if (!DC->isFunctionOrMethod() && SearchName) {
2083 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2084 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2085 Lookup.first != Lookup.second;
2086 ++Lookup.first) {
2087 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2088 continue;
2089
2090 Decl *Found = *Lookup.first;
2091 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2092 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2093 Found = Tag->getDecl();
2094 }
2095
2096 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002097 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2098 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2099 // The record types structurally match, or the "from" translation
2100 // unit only had a forward declaration anyway; call it the same
2101 // function.
2102 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002103 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002104 }
2105 } else {
2106 // We have a forward declaration of this type, so adopt that forward
2107 // declaration rather than building a new one.
2108 AdoptDecl = FoundRecord;
2109 continue;
2110 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002111 }
2112
2113 ConflictingDecls.push_back(*Lookup.first);
2114 }
2115
2116 if (!ConflictingDecls.empty()) {
2117 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2118 ConflictingDecls.data(),
2119 ConflictingDecls.size());
2120 }
2121 }
2122
2123 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002124 RecordDecl *D2 = AdoptDecl;
2125 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002126 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002127 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002128 D->getTagKind(),
2129 DC, Loc,
2130 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002131 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002132 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002133 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002134 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002135 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002136 DC, Loc,
2137 Name.getAsIdentifierInfo(),
2138 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002139 }
John McCallb6217662010-03-15 10:12:16 +00002140 // Import the qualifier, if any.
2141 if (D->getQualifier()) {
2142 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2143 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2144 D2->setQualifierInfo(NNS, NNSRange);
2145 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002146 D2->setLexicalDeclContext(LexicalDC);
2147 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002148 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002149
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002150 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002151
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002152 if (D->isDefinition() && ImportDefinition(D, D2))
2153 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002154
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002155 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002156}
2157
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002158Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2159 // Import the major distinguishing characteristics of this enumerator.
2160 DeclContext *DC, *LexicalDC;
2161 DeclarationName Name;
2162 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002163 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002164 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002165
2166 QualType T = Importer.Import(D->getType());
2167 if (T.isNull())
2168 return 0;
2169
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002170 // Determine whether there are any other declarations with the same name and
2171 // in the same context.
2172 if (!LexicalDC->isFunctionOrMethod()) {
2173 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2174 unsigned IDNS = Decl::IDNS_Ordinary;
2175 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2176 Lookup.first != Lookup.second;
2177 ++Lookup.first) {
2178 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2179 continue;
2180
2181 ConflictingDecls.push_back(*Lookup.first);
2182 }
2183
2184 if (!ConflictingDecls.empty()) {
2185 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2186 ConflictingDecls.data(),
2187 ConflictingDecls.size());
2188 if (!Name)
2189 return 0;
2190 }
2191 }
2192
2193 Expr *Init = Importer.Import(D->getInitExpr());
2194 if (D->getInitExpr() && !Init)
2195 return 0;
2196
2197 EnumConstantDecl *ToEnumerator
2198 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2199 Name.getAsIdentifierInfo(), T,
2200 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002201 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002202 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002203 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002204 LexicalDC->addDecl(ToEnumerator);
2205 return ToEnumerator;
2206}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002207
Douglas Gregora404ea62010-02-10 19:54:31 +00002208Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2209 // Import the major distinguishing characteristics of this function.
2210 DeclContext *DC, *LexicalDC;
2211 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002212 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002213 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002214 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002215
Douglas Gregora404ea62010-02-10 19:54:31 +00002216 // Try to find a function in our own ("to") context with the same name, same
2217 // type, and in the same context as the function we're importing.
2218 if (!LexicalDC->isFunctionOrMethod()) {
2219 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2220 unsigned IDNS = Decl::IDNS_Ordinary;
2221 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2222 Lookup.first != Lookup.second;
2223 ++Lookup.first) {
2224 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2225 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002226
Douglas Gregora404ea62010-02-10 19:54:31 +00002227 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2228 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2229 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002230 if (Importer.IsStructurallyEquivalent(D->getType(),
2231 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002232 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002233 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002234 }
2235
2236 // FIXME: Check for overloading more carefully, e.g., by boosting
2237 // Sema::IsOverload out to the AST library.
2238
2239 // Function overloading is okay in C++.
2240 if (Importer.getToContext().getLangOptions().CPlusPlus)
2241 continue;
2242
2243 // Complain about inconsistent function types.
2244 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002245 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002246 Importer.ToDiag(FoundFunction->getLocation(),
2247 diag::note_odr_value_here)
2248 << FoundFunction->getType();
2249 }
2250 }
2251
2252 ConflictingDecls.push_back(*Lookup.first);
2253 }
2254
2255 if (!ConflictingDecls.empty()) {
2256 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2257 ConflictingDecls.data(),
2258 ConflictingDecls.size());
2259 if (!Name)
2260 return 0;
2261 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002262 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002263
Abramo Bagnara25777432010-08-11 22:01:17 +00002264 DeclarationNameInfo NameInfo(Name, Loc);
2265 // Import additional name location/type info.
2266 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2267
Douglas Gregorea35d112010-02-15 23:54:17 +00002268 // Import the type.
2269 QualType T = Importer.Import(D->getType());
2270 if (T.isNull())
2271 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002272
2273 // Import the function parameters.
2274 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2275 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2276 P != PEnd; ++P) {
2277 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2278 if (!ToP)
2279 return 0;
2280
2281 Parameters.push_back(ToP);
2282 }
2283
2284 // Create the imported function.
2285 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002286 FunctionDecl *ToFunction = 0;
2287 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2288 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2289 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002290 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002291 FromConstructor->isExplicit(),
2292 D->isInlineSpecified(),
2293 D->isImplicit());
2294 } else if (isa<CXXDestructorDecl>(D)) {
2295 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2296 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002297 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002298 D->isInlineSpecified(),
2299 D->isImplicit());
2300 } else if (CXXConversionDecl *FromConversion
2301 = dyn_cast<CXXConversionDecl>(D)) {
2302 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2303 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002304 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002305 D->isInlineSpecified(),
2306 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002307 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2308 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2309 cast<CXXRecordDecl>(DC),
2310 NameInfo, T, TInfo,
2311 Method->isStatic(),
2312 Method->getStorageClassAsWritten(),
2313 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002314 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002315 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2316 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002317 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002318 D->isInlineSpecified(),
2319 D->hasWrittenPrototype());
2320 }
John McCallb6217662010-03-15 10:12:16 +00002321
2322 // Import the qualifier, if any.
2323 if (D->getQualifier()) {
2324 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2325 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2326 ToFunction->setQualifierInfo(NNS, NNSRange);
2327 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002328 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002329 ToFunction->setLexicalDeclContext(LexicalDC);
2330 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002331
Douglas Gregora404ea62010-02-10 19:54:31 +00002332 // Set the parameters.
2333 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002334 Parameters[I]->setOwningFunction(ToFunction);
2335 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002336 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002337 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002338
2339 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002340
2341 // Add this function to the lexical context.
2342 LexicalDC->addDecl(ToFunction);
2343
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002344 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002345}
2346
Douglas Gregorc144f352010-02-21 18:29:16 +00002347Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2348 return VisitFunctionDecl(D);
2349}
2350
2351Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2352 return VisitCXXMethodDecl(D);
2353}
2354
2355Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2356 return VisitCXXMethodDecl(D);
2357}
2358
2359Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2360 return VisitCXXMethodDecl(D);
2361}
2362
Douglas Gregor96a01b42010-02-11 00:48:18 +00002363Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2364 // Import the major distinguishing characteristics of a variable.
2365 DeclContext *DC, *LexicalDC;
2366 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002367 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002368 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2369 return 0;
2370
2371 // Import the type.
2372 QualType T = Importer.Import(D->getType());
2373 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002374 return 0;
2375
2376 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2377 Expr *BitWidth = Importer.Import(D->getBitWidth());
2378 if (!BitWidth && D->getBitWidth())
2379 return 0;
2380
2381 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2382 Loc, Name.getAsIdentifierInfo(),
2383 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002384 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002385 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002386 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002387 LexicalDC->addDecl(ToField);
2388 return ToField;
2389}
2390
Francois Pichet87c2e122010-11-21 06:08:52 +00002391Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2392 // Import the major distinguishing characteristics of a variable.
2393 DeclContext *DC, *LexicalDC;
2394 DeclarationName Name;
2395 SourceLocation Loc;
2396 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2397 return 0;
2398
2399 // Import the type.
2400 QualType T = Importer.Import(D->getType());
2401 if (T.isNull())
2402 return 0;
2403
2404 NamedDecl **NamedChain =
2405 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2406
2407 unsigned i = 0;
2408 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2409 PE = D->chain_end(); PI != PE; ++PI) {
2410 Decl* D = Importer.Import(*PI);
2411 if (!D)
2412 return 0;
2413 NamedChain[i++] = cast<NamedDecl>(D);
2414 }
2415
2416 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2417 Importer.getToContext(), DC,
2418 Loc, Name.getAsIdentifierInfo(), T,
2419 NamedChain, D->getChainingSize());
2420 ToIndirectField->setAccess(D->getAccess());
2421 ToIndirectField->setLexicalDeclContext(LexicalDC);
2422 Importer.Imported(D, ToIndirectField);
2423 LexicalDC->addDecl(ToIndirectField);
2424 return ToIndirectField;
2425}
2426
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002427Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2428 // Import the major distinguishing characteristics of an ivar.
2429 DeclContext *DC, *LexicalDC;
2430 DeclarationName Name;
2431 SourceLocation Loc;
2432 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2433 return 0;
2434
2435 // Determine whether we've already imported this ivar
2436 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2437 Lookup.first != Lookup.second;
2438 ++Lookup.first) {
2439 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2440 if (Importer.IsStructurallyEquivalent(D->getType(),
2441 FoundIvar->getType())) {
2442 Importer.Imported(D, FoundIvar);
2443 return FoundIvar;
2444 }
2445
2446 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2447 << Name << D->getType() << FoundIvar->getType();
2448 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2449 << FoundIvar->getType();
2450 return 0;
2451 }
2452 }
2453
2454 // Import the type.
2455 QualType T = Importer.Import(D->getType());
2456 if (T.isNull())
2457 return 0;
2458
2459 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2460 Expr *BitWidth = Importer.Import(D->getBitWidth());
2461 if (!BitWidth && D->getBitWidth())
2462 return 0;
2463
Daniel Dunbara0654922010-04-02 20:10:03 +00002464 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2465 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002466 Loc, Name.getAsIdentifierInfo(),
2467 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002468 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002469 ToIvar->setLexicalDeclContext(LexicalDC);
2470 Importer.Imported(D, ToIvar);
2471 LexicalDC->addDecl(ToIvar);
2472 return ToIvar;
2473
2474}
2475
Douglas Gregora404ea62010-02-10 19:54:31 +00002476Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2477 // Import the major distinguishing characteristics of a variable.
2478 DeclContext *DC, *LexicalDC;
2479 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002480 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002481 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002482 return 0;
2483
Douglas Gregor089459a2010-02-08 21:09:39 +00002484 // Try to find a variable in our own ("to") context with the same name and
2485 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002486 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002487 VarDecl *MergeWithVar = 0;
2488 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2489 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002490 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002491 Lookup.first != Lookup.second;
2492 ++Lookup.first) {
2493 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2494 continue;
2495
2496 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2497 // We have found a variable that we may need to merge with. Check it.
2498 if (isExternalLinkage(FoundVar->getLinkage()) &&
2499 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002500 if (Importer.IsStructurallyEquivalent(D->getType(),
2501 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002502 MergeWithVar = FoundVar;
2503 break;
2504 }
2505
Douglas Gregord0145422010-02-12 17:23:39 +00002506 const ArrayType *FoundArray
2507 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2508 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002509 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002510 if (FoundArray && TArray) {
2511 if (isa<IncompleteArrayType>(FoundArray) &&
2512 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002513 // Import the type.
2514 QualType T = Importer.Import(D->getType());
2515 if (T.isNull())
2516 return 0;
2517
Douglas Gregord0145422010-02-12 17:23:39 +00002518 FoundVar->setType(T);
2519 MergeWithVar = FoundVar;
2520 break;
2521 } else if (isa<IncompleteArrayType>(TArray) &&
2522 isa<ConstantArrayType>(FoundArray)) {
2523 MergeWithVar = FoundVar;
2524 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002525 }
2526 }
2527
Douglas Gregor089459a2010-02-08 21:09:39 +00002528 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002529 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002530 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2531 << FoundVar->getType();
2532 }
2533 }
2534
2535 ConflictingDecls.push_back(*Lookup.first);
2536 }
2537
2538 if (MergeWithVar) {
2539 // An equivalent variable with external linkage has been found. Link
2540 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002541 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002542
2543 if (VarDecl *DDef = D->getDefinition()) {
2544 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2545 Importer.ToDiag(ExistingDef->getLocation(),
2546 diag::err_odr_variable_multiple_def)
2547 << Name;
2548 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2549 } else {
2550 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002551 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002552 }
2553 }
2554
2555 return MergeWithVar;
2556 }
2557
2558 if (!ConflictingDecls.empty()) {
2559 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2560 ConflictingDecls.data(),
2561 ConflictingDecls.size());
2562 if (!Name)
2563 return 0;
2564 }
2565 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002566
Douglas Gregorea35d112010-02-15 23:54:17 +00002567 // Import the type.
2568 QualType T = Importer.Import(D->getType());
2569 if (T.isNull())
2570 return 0;
2571
Douglas Gregor089459a2010-02-08 21:09:39 +00002572 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002573 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002574 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2575 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002576 D->getStorageClass(),
2577 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002578 // Import the qualifier, if any.
2579 if (D->getQualifier()) {
2580 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2581 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2582 ToVar->setQualifierInfo(NNS, NNSRange);
2583 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002584 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002585 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002586 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002587 LexicalDC->addDecl(ToVar);
2588
Douglas Gregor089459a2010-02-08 21:09:39 +00002589 // Merge the initializer.
2590 // FIXME: Can we really import any initializer? Alternatively, we could force
2591 // ourselves to import every declaration of a variable and then only use
2592 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002593 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002594
2595 // FIXME: Other bits to merge?
2596
2597 return ToVar;
2598}
2599
Douglas Gregor2cd00932010-02-17 21:22:52 +00002600Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2601 // Parameters are created in the translation unit's context, then moved
2602 // into the function declaration's context afterward.
2603 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2604
2605 // Import the name of this declaration.
2606 DeclarationName Name = Importer.Import(D->getDeclName());
2607 if (D->getDeclName() && !Name)
2608 return 0;
2609
2610 // Import the location of this declaration.
2611 SourceLocation Loc = Importer.Import(D->getLocation());
2612
2613 // Import the parameter's type.
2614 QualType T = Importer.Import(D->getType());
2615 if (T.isNull())
2616 return 0;
2617
2618 // Create the imported parameter.
2619 ImplicitParamDecl *ToParm
2620 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2621 Loc, Name.getAsIdentifierInfo(),
2622 T);
2623 return Importer.Imported(D, ToParm);
2624}
2625
Douglas Gregora404ea62010-02-10 19:54:31 +00002626Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2627 // Parameters are created in the translation unit's context, then moved
2628 // into the function declaration's context afterward.
2629 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2630
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002631 // Import the name of this declaration.
2632 DeclarationName Name = Importer.Import(D->getDeclName());
2633 if (D->getDeclName() && !Name)
2634 return 0;
2635
Douglas Gregora404ea62010-02-10 19:54:31 +00002636 // Import the location of this declaration.
2637 SourceLocation Loc = Importer.Import(D->getLocation());
2638
2639 // Import the parameter's type.
2640 QualType T = Importer.Import(D->getType());
2641 if (T.isNull())
2642 return 0;
2643
2644 // Create the imported parameter.
2645 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2646 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2647 Loc, Name.getAsIdentifierInfo(),
2648 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002649 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002650 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002651 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002652 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002653}
2654
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002655Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2656 // Import the major distinguishing characteristics of a method.
2657 DeclContext *DC, *LexicalDC;
2658 DeclarationName Name;
2659 SourceLocation Loc;
2660 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2661 return 0;
2662
2663 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2664 Lookup.first != Lookup.second;
2665 ++Lookup.first) {
2666 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2667 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2668 continue;
2669
2670 // Check return types.
2671 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2672 FoundMethod->getResultType())) {
2673 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2674 << D->isInstanceMethod() << Name
2675 << D->getResultType() << FoundMethod->getResultType();
2676 Importer.ToDiag(FoundMethod->getLocation(),
2677 diag::note_odr_objc_method_here)
2678 << D->isInstanceMethod() << Name;
2679 return 0;
2680 }
2681
2682 // Check the number of parameters.
2683 if (D->param_size() != FoundMethod->param_size()) {
2684 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2685 << D->isInstanceMethod() << Name
2686 << D->param_size() << FoundMethod->param_size();
2687 Importer.ToDiag(FoundMethod->getLocation(),
2688 diag::note_odr_objc_method_here)
2689 << D->isInstanceMethod() << Name;
2690 return 0;
2691 }
2692
2693 // Check parameter types.
2694 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2695 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2696 P != PEnd; ++P, ++FoundP) {
2697 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2698 (*FoundP)->getType())) {
2699 Importer.FromDiag((*P)->getLocation(),
2700 diag::err_odr_objc_method_param_type_inconsistent)
2701 << D->isInstanceMethod() << Name
2702 << (*P)->getType() << (*FoundP)->getType();
2703 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2704 << (*FoundP)->getType();
2705 return 0;
2706 }
2707 }
2708
2709 // Check variadic/non-variadic.
2710 // Check the number of parameters.
2711 if (D->isVariadic() != FoundMethod->isVariadic()) {
2712 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2713 << D->isInstanceMethod() << Name;
2714 Importer.ToDiag(FoundMethod->getLocation(),
2715 diag::note_odr_objc_method_here)
2716 << D->isInstanceMethod() << Name;
2717 return 0;
2718 }
2719
2720 // FIXME: Any other bits we need to merge?
2721 return Importer.Imported(D, FoundMethod);
2722 }
2723 }
2724
2725 // Import the result type.
2726 QualType ResultTy = Importer.Import(D->getResultType());
2727 if (ResultTy.isNull())
2728 return 0;
2729
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002730 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2731
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002732 ObjCMethodDecl *ToMethod
2733 = ObjCMethodDecl::Create(Importer.getToContext(),
2734 Loc,
2735 Importer.Import(D->getLocEnd()),
2736 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002737 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002738 D->isInstanceMethod(),
2739 D->isVariadic(),
2740 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002741 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002742 D->getImplementationControl());
2743
2744 // FIXME: When we decide to merge method definitions, we'll need to
2745 // deal with implicit parameters.
2746
2747 // Import the parameters
2748 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2749 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2750 FromPEnd = D->param_end();
2751 FromP != FromPEnd;
2752 ++FromP) {
2753 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2754 if (!ToP)
2755 return 0;
2756
2757 ToParams.push_back(ToP);
2758 }
2759
2760 // Set the parameters.
2761 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2762 ToParams[I]->setOwningFunction(ToMethod);
2763 ToMethod->addDecl(ToParams[I]);
2764 }
2765 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002766 ToParams.data(), ToParams.size(),
2767 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002768
2769 ToMethod->setLexicalDeclContext(LexicalDC);
2770 Importer.Imported(D, ToMethod);
2771 LexicalDC->addDecl(ToMethod);
2772 return ToMethod;
2773}
2774
Douglas Gregorb4677b62010-02-18 01:47:50 +00002775Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2776 // Import the major distinguishing characteristics of a category.
2777 DeclContext *DC, *LexicalDC;
2778 DeclarationName Name;
2779 SourceLocation Loc;
2780 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2781 return 0;
2782
2783 ObjCInterfaceDecl *ToInterface
2784 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2785 if (!ToInterface)
2786 return 0;
2787
2788 // Determine if we've already encountered this category.
2789 ObjCCategoryDecl *MergeWithCategory
2790 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2791 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2792 if (!ToCategory) {
2793 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2794 Importer.Import(D->getAtLoc()),
2795 Loc,
2796 Importer.Import(D->getCategoryNameLoc()),
2797 Name.getAsIdentifierInfo());
2798 ToCategory->setLexicalDeclContext(LexicalDC);
2799 LexicalDC->addDecl(ToCategory);
2800 Importer.Imported(D, ToCategory);
2801
2802 // Link this category into its class's category list.
2803 ToCategory->setClassInterface(ToInterface);
2804 ToCategory->insertNextClassCategory();
2805
2806 // Import protocols
2807 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2808 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2809 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2810 = D->protocol_loc_begin();
2811 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2812 FromProtoEnd = D->protocol_end();
2813 FromProto != FromProtoEnd;
2814 ++FromProto, ++FromProtoLoc) {
2815 ObjCProtocolDecl *ToProto
2816 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2817 if (!ToProto)
2818 return 0;
2819 Protocols.push_back(ToProto);
2820 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2821 }
2822
2823 // FIXME: If we're merging, make sure that the protocol list is the same.
2824 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2825 ProtocolLocs.data(), Importer.getToContext());
2826
2827 } else {
2828 Importer.Imported(D, ToCategory);
2829 }
2830
2831 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002832 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002833
2834 // If we have an implementation, import it as well.
2835 if (D->getImplementation()) {
2836 ObjCCategoryImplDecl *Impl
2837 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2838 if (!Impl)
2839 return 0;
2840
2841 ToCategory->setImplementation(Impl);
2842 }
2843
2844 return ToCategory;
2845}
2846
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002847Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002848 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002849 DeclContext *DC, *LexicalDC;
2850 DeclarationName Name;
2851 SourceLocation Loc;
2852 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2853 return 0;
2854
2855 ObjCProtocolDecl *MergeWithProtocol = 0;
2856 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2857 Lookup.first != Lookup.second;
2858 ++Lookup.first) {
2859 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2860 continue;
2861
2862 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2863 break;
2864 }
2865
2866 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2867 if (!ToProto || ToProto->isForwardDecl()) {
2868 if (!ToProto) {
2869 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2870 Name.getAsIdentifierInfo());
2871 ToProto->setForwardDecl(D->isForwardDecl());
2872 ToProto->setLexicalDeclContext(LexicalDC);
2873 LexicalDC->addDecl(ToProto);
2874 }
2875 Importer.Imported(D, ToProto);
2876
2877 // Import protocols
2878 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2879 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2880 ObjCProtocolDecl::protocol_loc_iterator
2881 FromProtoLoc = D->protocol_loc_begin();
2882 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2883 FromProtoEnd = D->protocol_end();
2884 FromProto != FromProtoEnd;
2885 ++FromProto, ++FromProtoLoc) {
2886 ObjCProtocolDecl *ToProto
2887 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2888 if (!ToProto)
2889 return 0;
2890 Protocols.push_back(ToProto);
2891 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2892 }
2893
2894 // FIXME: If we're merging, make sure that the protocol list is the same.
2895 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2896 ProtocolLocs.data(), Importer.getToContext());
2897 } else {
2898 Importer.Imported(D, ToProto);
2899 }
2900
Douglas Gregorb4677b62010-02-18 01:47:50 +00002901 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002902 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002903
2904 return ToProto;
2905}
2906
Douglas Gregora12d2942010-02-16 01:20:57 +00002907Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2908 // Import the major distinguishing characteristics of an @interface.
2909 DeclContext *DC, *LexicalDC;
2910 DeclarationName Name;
2911 SourceLocation Loc;
2912 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2913 return 0;
2914
2915 ObjCInterfaceDecl *MergeWithIface = 0;
2916 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2917 Lookup.first != Lookup.second;
2918 ++Lookup.first) {
2919 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2920 continue;
2921
2922 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2923 break;
2924 }
2925
2926 ObjCInterfaceDecl *ToIface = MergeWithIface;
2927 if (!ToIface || ToIface->isForwardDecl()) {
2928 if (!ToIface) {
2929 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2930 DC, Loc,
2931 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002932 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002933 D->isForwardDecl(),
2934 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002935 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002936 ToIface->setLexicalDeclContext(LexicalDC);
2937 LexicalDC->addDecl(ToIface);
2938 }
2939 Importer.Imported(D, ToIface);
2940
Douglas Gregora12d2942010-02-16 01:20:57 +00002941 if (D->getSuperClass()) {
2942 ObjCInterfaceDecl *Super
2943 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2944 if (!Super)
2945 return 0;
2946
2947 ToIface->setSuperClass(Super);
2948 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2949 }
2950
2951 // Import protocols
2952 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2953 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2954 ObjCInterfaceDecl::protocol_loc_iterator
2955 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002956
2957 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00002958 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2959 FromProtoEnd = D->protocol_end();
2960 FromProto != FromProtoEnd;
2961 ++FromProto, ++FromProtoLoc) {
2962 ObjCProtocolDecl *ToProto
2963 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2964 if (!ToProto)
2965 return 0;
2966 Protocols.push_back(ToProto);
2967 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2968 }
2969
2970 // FIXME: If we're merging, make sure that the protocol list is the same.
2971 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2972 ProtocolLocs.data(), Importer.getToContext());
2973
Douglas Gregora12d2942010-02-16 01:20:57 +00002974 // Import @end range
2975 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2976 } else {
2977 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002978
2979 // Check for consistency of superclasses.
2980 DeclarationName FromSuperName, ToSuperName;
2981 if (D->getSuperClass())
2982 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2983 if (ToIface->getSuperClass())
2984 ToSuperName = ToIface->getSuperClass()->getDeclName();
2985 if (FromSuperName != ToSuperName) {
2986 Importer.ToDiag(ToIface->getLocation(),
2987 diag::err_odr_objc_superclass_inconsistent)
2988 << ToIface->getDeclName();
2989 if (ToIface->getSuperClass())
2990 Importer.ToDiag(ToIface->getSuperClassLoc(),
2991 diag::note_odr_objc_superclass)
2992 << ToIface->getSuperClass()->getDeclName();
2993 else
2994 Importer.ToDiag(ToIface->getLocation(),
2995 diag::note_odr_objc_missing_superclass);
2996 if (D->getSuperClass())
2997 Importer.FromDiag(D->getSuperClassLoc(),
2998 diag::note_odr_objc_superclass)
2999 << D->getSuperClass()->getDeclName();
3000 else
3001 Importer.FromDiag(D->getLocation(),
3002 diag::note_odr_objc_missing_superclass);
3003 return 0;
3004 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003005 }
3006
Douglas Gregorb4677b62010-02-18 01:47:50 +00003007 // Import categories. When the categories themselves are imported, they'll
3008 // hook themselves into this interface.
3009 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3010 FromCat = FromCat->getNextClassCategory())
3011 Importer.Import(FromCat);
3012
Douglas Gregora12d2942010-02-16 01:20:57 +00003013 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003014 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003015
3016 // If we have an @implementation, import it as well.
3017 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003018 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3019 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003020 if (!Impl)
3021 return 0;
3022
3023 ToIface->setImplementation(Impl);
3024 }
3025
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003026 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003027}
3028
Douglas Gregor3daef292010-12-07 15:32:12 +00003029Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3030 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3031 Importer.Import(D->getCategoryDecl()));
3032 if (!Category)
3033 return 0;
3034
3035 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3036 if (!ToImpl) {
3037 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3038 if (!DC)
3039 return 0;
3040
3041 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3042 Importer.Import(D->getLocation()),
3043 Importer.Import(D->getIdentifier()),
3044 Category->getClassInterface());
3045
3046 DeclContext *LexicalDC = DC;
3047 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3048 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3049 if (!LexicalDC)
3050 return 0;
3051
3052 ToImpl->setLexicalDeclContext(LexicalDC);
3053 }
3054
3055 LexicalDC->addDecl(ToImpl);
3056 Category->setImplementation(ToImpl);
3057 }
3058
3059 Importer.Imported(D, ToImpl);
3060 ImportDeclContext(ToImpl);
3061 return ToImpl;
3062}
3063
Douglas Gregordd182ff2010-12-07 01:26:03 +00003064Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3065 // Find the corresponding interface.
3066 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3067 Importer.Import(D->getClassInterface()));
3068 if (!Iface)
3069 return 0;
3070
3071 // Import the superclass, if any.
3072 ObjCInterfaceDecl *Super = 0;
3073 if (D->getSuperClass()) {
3074 Super = cast_or_null<ObjCInterfaceDecl>(
3075 Importer.Import(D->getSuperClass()));
3076 if (!Super)
3077 return 0;
3078 }
3079
3080 ObjCImplementationDecl *Impl = Iface->getImplementation();
3081 if (!Impl) {
3082 // We haven't imported an implementation yet. Create a new @implementation
3083 // now.
3084 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3085 Importer.ImportContext(D->getDeclContext()),
3086 Importer.Import(D->getLocation()),
3087 Iface, Super);
3088
3089 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3090 DeclContext *LexicalDC
3091 = Importer.ImportContext(D->getLexicalDeclContext());
3092 if (!LexicalDC)
3093 return 0;
3094 Impl->setLexicalDeclContext(LexicalDC);
3095 }
3096
3097 // Associate the implementation with the class it implements.
3098 Iface->setImplementation(Impl);
3099 Importer.Imported(D, Iface->getImplementation());
3100 } else {
3101 Importer.Imported(D, Iface->getImplementation());
3102
3103 // Verify that the existing @implementation has the same superclass.
3104 if ((Super && !Impl->getSuperClass()) ||
3105 (!Super && Impl->getSuperClass()) ||
3106 (Super && Impl->getSuperClass() &&
3107 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3108 Importer.ToDiag(Impl->getLocation(),
3109 diag::err_odr_objc_superclass_inconsistent)
3110 << Iface->getDeclName();
3111 // FIXME: It would be nice to have the location of the superclass
3112 // below.
3113 if (Impl->getSuperClass())
3114 Importer.ToDiag(Impl->getLocation(),
3115 diag::note_odr_objc_superclass)
3116 << Impl->getSuperClass()->getDeclName();
3117 else
3118 Importer.ToDiag(Impl->getLocation(),
3119 diag::note_odr_objc_missing_superclass);
3120 if (D->getSuperClass())
3121 Importer.FromDiag(D->getLocation(),
3122 diag::note_odr_objc_superclass)
3123 << D->getSuperClass()->getDeclName();
3124 else
3125 Importer.FromDiag(D->getLocation(),
3126 diag::note_odr_objc_missing_superclass);
3127 return 0;
3128 }
3129 }
3130
3131 // Import all of the members of this @implementation.
3132 ImportDeclContext(D);
3133
3134 return Impl;
3135}
3136
Douglas Gregore3261622010-02-17 18:02:10 +00003137Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3138 // Import the major distinguishing characteristics of an @property.
3139 DeclContext *DC, *LexicalDC;
3140 DeclarationName Name;
3141 SourceLocation Loc;
3142 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3143 return 0;
3144
3145 // Check whether we have already imported this property.
3146 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3147 Lookup.first != Lookup.second;
3148 ++Lookup.first) {
3149 if (ObjCPropertyDecl *FoundProp
3150 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3151 // Check property types.
3152 if (!Importer.IsStructurallyEquivalent(D->getType(),
3153 FoundProp->getType())) {
3154 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3155 << Name << D->getType() << FoundProp->getType();
3156 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3157 << FoundProp->getType();
3158 return 0;
3159 }
3160
3161 // FIXME: Check property attributes, getters, setters, etc.?
3162
3163 // Consider these properties to be equivalent.
3164 Importer.Imported(D, FoundProp);
3165 return FoundProp;
3166 }
3167 }
3168
3169 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003170 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3171 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003172 return 0;
3173
3174 // Create the new property.
3175 ObjCPropertyDecl *ToProperty
3176 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3177 Name.getAsIdentifierInfo(),
3178 Importer.Import(D->getAtLoc()),
3179 T,
3180 D->getPropertyImplementation());
3181 Importer.Imported(D, ToProperty);
3182 ToProperty->setLexicalDeclContext(LexicalDC);
3183 LexicalDC->addDecl(ToProperty);
3184
3185 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003186 ToProperty->setPropertyAttributesAsWritten(
3187 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003188 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3189 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3190 ToProperty->setGetterMethodDecl(
3191 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3192 ToProperty->setSetterMethodDecl(
3193 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3194 ToProperty->setPropertyIvarDecl(
3195 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3196 return ToProperty;
3197}
3198
Douglas Gregor954e0c72010-12-07 18:32:03 +00003199Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3200 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3201 Importer.Import(D->getPropertyDecl()));
3202 if (!Property)
3203 return 0;
3204
3205 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3206 if (!DC)
3207 return 0;
3208
3209 // Import the lexical declaration context.
3210 DeclContext *LexicalDC = DC;
3211 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3212 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3213 if (!LexicalDC)
3214 return 0;
3215 }
3216
3217 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3218 if (!InImpl)
3219 return 0;
3220
3221 // Import the ivar (for an @synthesize).
3222 ObjCIvarDecl *Ivar = 0;
3223 if (D->getPropertyIvarDecl()) {
3224 Ivar = cast_or_null<ObjCIvarDecl>(
3225 Importer.Import(D->getPropertyIvarDecl()));
3226 if (!Ivar)
3227 return 0;
3228 }
3229
3230 ObjCPropertyImplDecl *ToImpl
3231 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3232 if (!ToImpl) {
3233 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3234 Importer.Import(D->getLocStart()),
3235 Importer.Import(D->getLocation()),
3236 Property,
3237 D->getPropertyImplementation(),
3238 Ivar,
3239 Importer.Import(D->getPropertyIvarDeclLoc()));
3240 ToImpl->setLexicalDeclContext(LexicalDC);
3241 Importer.Imported(D, ToImpl);
3242 LexicalDC->addDecl(ToImpl);
3243 } else {
3244 // Check that we have the same kind of property implementation (@synthesize
3245 // vs. @dynamic).
3246 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3247 Importer.ToDiag(ToImpl->getLocation(),
3248 diag::err_odr_objc_property_impl_kind_inconsistent)
3249 << Property->getDeclName()
3250 << (ToImpl->getPropertyImplementation()
3251 == ObjCPropertyImplDecl::Dynamic);
3252 Importer.FromDiag(D->getLocation(),
3253 diag::note_odr_objc_property_impl_kind)
3254 << D->getPropertyDecl()->getDeclName()
3255 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3256 return 0;
3257 }
3258
3259 // For @synthesize, check that we have the same
3260 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3261 Ivar != ToImpl->getPropertyIvarDecl()) {
3262 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3263 diag::err_odr_objc_synthesize_ivar_inconsistent)
3264 << Property->getDeclName()
3265 << ToImpl->getPropertyIvarDecl()->getDeclName()
3266 << Ivar->getDeclName();
3267 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3268 diag::note_odr_objc_synthesize_ivar_here)
3269 << D->getPropertyIvarDecl()->getDeclName();
3270 return 0;
3271 }
3272
3273 // Merge the existing implementation with the new implementation.
3274 Importer.Imported(D, ToImpl);
3275 }
3276
3277 return ToImpl;
3278}
3279
Douglas Gregor2b785022010-02-18 02:12:22 +00003280Decl *
3281ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3282 // Import the context of this declaration.
3283 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3284 if (!DC)
3285 return 0;
3286
3287 DeclContext *LexicalDC = DC;
3288 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3289 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3290 if (!LexicalDC)
3291 return 0;
3292 }
3293
3294 // Import the location of this declaration.
3295 SourceLocation Loc = Importer.Import(D->getLocation());
3296
3297 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3298 llvm::SmallVector<SourceLocation, 4> Locations;
3299 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3300 = D->protocol_loc_begin();
3301 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3302 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3303 FromProto != FromProtoEnd;
3304 ++FromProto, ++FromProtoLoc) {
3305 ObjCProtocolDecl *ToProto
3306 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3307 if (!ToProto)
3308 continue;
3309
3310 Protocols.push_back(ToProto);
3311 Locations.push_back(Importer.Import(*FromProtoLoc));
3312 }
3313
3314 ObjCForwardProtocolDecl *ToForward
3315 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3316 Protocols.data(), Protocols.size(),
3317 Locations.data());
3318 ToForward->setLexicalDeclContext(LexicalDC);
3319 LexicalDC->addDecl(ToForward);
3320 Importer.Imported(D, ToForward);
3321 return ToForward;
3322}
3323
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003324Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3325 // Import the context of this declaration.
3326 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3327 if (!DC)
3328 return 0;
3329
3330 DeclContext *LexicalDC = DC;
3331 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3332 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3333 if (!LexicalDC)
3334 return 0;
3335 }
3336
3337 // Import the location of this declaration.
3338 SourceLocation Loc = Importer.Import(D->getLocation());
3339
3340 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3341 llvm::SmallVector<SourceLocation, 4> Locations;
3342 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3343 From != FromEnd; ++From) {
3344 ObjCInterfaceDecl *ToIface
3345 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3346 if (!ToIface)
3347 continue;
3348
3349 Interfaces.push_back(ToIface);
3350 Locations.push_back(Importer.Import(From->getLocation()));
3351 }
3352
3353 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3354 Loc,
3355 Interfaces.data(),
3356 Locations.data(),
3357 Interfaces.size());
3358 ToClass->setLexicalDeclContext(LexicalDC);
3359 LexicalDC->addDecl(ToClass);
3360 Importer.Imported(D, ToClass);
3361 return ToClass;
3362}
3363
Douglas Gregor040afae2010-11-30 19:14:50 +00003364Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3365 // For template arguments, we adopt the translation unit as our declaration
3366 // context. This context will be fixed when the actual template declaration
3367 // is created.
3368
3369 // FIXME: Import default argument.
3370 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3371 Importer.getToContext().getTranslationUnitDecl(),
3372 Importer.Import(D->getLocation()),
3373 D->getDepth(),
3374 D->getIndex(),
3375 Importer.Import(D->getIdentifier()),
3376 D->wasDeclaredWithTypename(),
3377 D->isParameterPack());
3378}
3379
3380Decl *
3381ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3382 // Import the name of this declaration.
3383 DeclarationName Name = Importer.Import(D->getDeclName());
3384 if (D->getDeclName() && !Name)
3385 return 0;
3386
3387 // Import the location of this declaration.
3388 SourceLocation Loc = Importer.Import(D->getLocation());
3389
3390 // Import the type of this declaration.
3391 QualType T = Importer.Import(D->getType());
3392 if (T.isNull())
3393 return 0;
3394
3395 // Import type-source information.
3396 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3397 if (D->getTypeSourceInfo() && !TInfo)
3398 return 0;
3399
3400 // FIXME: Import default argument.
3401
3402 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3403 Importer.getToContext().getTranslationUnitDecl(),
3404 Loc, D->getDepth(), D->getPosition(),
3405 Name.getAsIdentifierInfo(),
3406 T, TInfo);
3407}
3408
3409Decl *
3410ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3411 // Import the name of this declaration.
3412 DeclarationName Name = Importer.Import(D->getDeclName());
3413 if (D->getDeclName() && !Name)
3414 return 0;
3415
3416 // Import the location of this declaration.
3417 SourceLocation Loc = Importer.Import(D->getLocation());
3418
3419 // Import template parameters.
3420 TemplateParameterList *TemplateParams
3421 = ImportTemplateParameterList(D->getTemplateParameters());
3422 if (!TemplateParams)
3423 return 0;
3424
3425 // FIXME: Import default argument.
3426
3427 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3428 Importer.getToContext().getTranslationUnitDecl(),
3429 Loc, D->getDepth(), D->getPosition(),
3430 Name.getAsIdentifierInfo(),
3431 TemplateParams);
3432}
3433
3434Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3435 // If this record has a definition in the translation unit we're coming from,
3436 // but this particular declaration is not that definition, import the
3437 // definition and map to that.
3438 CXXRecordDecl *Definition
3439 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3440 if (Definition && Definition != D->getTemplatedDecl()) {
3441 Decl *ImportedDef
3442 = Importer.Import(Definition->getDescribedClassTemplate());
3443 if (!ImportedDef)
3444 return 0;
3445
3446 return Importer.Imported(D, ImportedDef);
3447 }
3448
3449 // Import the major distinguishing characteristics of this class template.
3450 DeclContext *DC, *LexicalDC;
3451 DeclarationName Name;
3452 SourceLocation Loc;
3453 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3454 return 0;
3455
3456 // We may already have a template of the same name; try to find and match it.
3457 if (!DC->isFunctionOrMethod()) {
3458 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3459 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3460 Lookup.first != Lookup.second;
3461 ++Lookup.first) {
3462 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3463 continue;
3464
3465 Decl *Found = *Lookup.first;
3466 if (ClassTemplateDecl *FoundTemplate
3467 = dyn_cast<ClassTemplateDecl>(Found)) {
3468 if (IsStructuralMatch(D, FoundTemplate)) {
3469 // The class templates structurally match; call it the same template.
3470 // FIXME: We may be filling in a forward declaration here. Handle
3471 // this case!
3472 Importer.Imported(D->getTemplatedDecl(),
3473 FoundTemplate->getTemplatedDecl());
3474 return Importer.Imported(D, FoundTemplate);
3475 }
3476 }
3477
3478 ConflictingDecls.push_back(*Lookup.first);
3479 }
3480
3481 if (!ConflictingDecls.empty()) {
3482 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3483 ConflictingDecls.data(),
3484 ConflictingDecls.size());
3485 }
3486
3487 if (!Name)
3488 return 0;
3489 }
3490
3491 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3492
3493 // Create the declaration that is being templated.
3494 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3495 DTemplated->getTagKind(),
3496 DC,
3497 Importer.Import(DTemplated->getLocation()),
3498 Name.getAsIdentifierInfo(),
3499 Importer.Import(DTemplated->getTagKeywordLoc()));
3500 D2Templated->setAccess(DTemplated->getAccess());
3501
3502
3503 // Import the qualifier, if any.
3504 if (DTemplated->getQualifier()) {
3505 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3506 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3507 D2Templated->setQualifierInfo(NNS, NNSRange);
3508 }
3509 D2Templated->setLexicalDeclContext(LexicalDC);
3510
3511 // Create the class template declaration itself.
3512 TemplateParameterList *TemplateParams
3513 = ImportTemplateParameterList(D->getTemplateParameters());
3514 if (!TemplateParams)
3515 return 0;
3516
3517 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3518 Loc, Name, TemplateParams,
3519 D2Templated,
3520 /*PrevDecl=*/0);
3521 D2Templated->setDescribedClassTemplate(D2);
3522
3523 D2->setAccess(D->getAccess());
3524 D2->setLexicalDeclContext(LexicalDC);
3525 LexicalDC->addDecl(D2);
3526
3527 // Note the relationship between the class templates.
3528 Importer.Imported(D, D2);
3529 Importer.Imported(DTemplated, D2Templated);
3530
3531 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3532 // FIXME: Import definition!
3533 }
3534
3535 return D2;
3536}
3537
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003538Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3539 ClassTemplateSpecializationDecl *D) {
3540 // If this record has a definition in the translation unit we're coming from,
3541 // but this particular declaration is not that definition, import the
3542 // definition and map to that.
3543 TagDecl *Definition = D->getDefinition();
3544 if (Definition && Definition != D) {
3545 Decl *ImportedDef = Importer.Import(Definition);
3546 if (!ImportedDef)
3547 return 0;
3548
3549 return Importer.Imported(D, ImportedDef);
3550 }
3551
3552 ClassTemplateDecl *ClassTemplate
3553 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3554 D->getSpecializedTemplate()));
3555 if (!ClassTemplate)
3556 return 0;
3557
3558 // Import the context of this declaration.
3559 DeclContext *DC = ClassTemplate->getDeclContext();
3560 if (!DC)
3561 return 0;
3562
3563 DeclContext *LexicalDC = DC;
3564 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3565 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3566 if (!LexicalDC)
3567 return 0;
3568 }
3569
3570 // Import the location of this declaration.
3571 SourceLocation Loc = Importer.Import(D->getLocation());
3572
3573 // Import template arguments.
3574 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3575 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3576 D->getTemplateArgs().size(),
3577 TemplateArgs))
3578 return 0;
3579
3580 // Try to find an existing specialization with these template arguments.
3581 void *InsertPos = 0;
3582 ClassTemplateSpecializationDecl *D2
3583 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3584 TemplateArgs.size(), InsertPos);
3585 if (D2) {
3586 // We already have a class template specialization with these template
3587 // arguments.
3588
3589 // FIXME: Check for specialization vs. instantiation errors.
3590
3591 if (RecordDecl *FoundDef = D2->getDefinition()) {
3592 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3593 // The record types structurally match, or the "from" translation
3594 // unit only had a forward declaration anyway; call it the same
3595 // function.
3596 return Importer.Imported(D, FoundDef);
3597 }
3598 }
3599 } else {
3600 // Create a new specialization.
3601 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3602 D->getTagKind(), DC,
3603 Loc, ClassTemplate,
3604 TemplateArgs.data(),
3605 TemplateArgs.size(),
3606 /*PrevDecl=*/0);
3607 D2->setSpecializationKind(D->getSpecializationKind());
3608
3609 // Add this specialization to the class template.
3610 ClassTemplate->AddSpecialization(D2, InsertPos);
3611
3612 // Import the qualifier, if any.
3613 if (D->getQualifier()) {
3614 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3615 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3616 D2->setQualifierInfo(NNS, NNSRange);
3617 }
3618
3619
3620 // Add the specialization to this context.
3621 D2->setLexicalDeclContext(LexicalDC);
3622 LexicalDC->addDecl(D2);
3623 }
3624 Importer.Imported(D, D2);
3625
3626 if (D->isDefinition() && ImportDefinition(D, D2))
3627 return 0;
3628
3629 return D2;
3630}
3631
Douglas Gregor4800d952010-02-11 19:21:55 +00003632//----------------------------------------------------------------------------
3633// Import Statements
3634//----------------------------------------------------------------------------
3635
3636Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3637 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3638 << S->getStmtClassName();
3639 return 0;
3640}
3641
3642//----------------------------------------------------------------------------
3643// Import Expressions
3644//----------------------------------------------------------------------------
3645Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3646 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3647 << E->getStmtClassName();
3648 return 0;
3649}
3650
Douglas Gregor44080632010-02-19 01:17:02 +00003651Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3652 NestedNameSpecifier *Qualifier = 0;
3653 if (E->getQualifier()) {
3654 Qualifier = Importer.Import(E->getQualifier());
3655 if (!E->getQualifier())
3656 return 0;
3657 }
3658
3659 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3660 if (!ToD)
3661 return 0;
3662
3663 QualType T = Importer.Import(E->getType());
3664 if (T.isNull())
3665 return 0;
3666
3667 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3668 Importer.Import(E->getQualifierRange()),
3669 ToD,
3670 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003671 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003672 /*FIXME:TemplateArgs=*/0);
3673}
3674
Douglas Gregor4800d952010-02-11 19:21:55 +00003675Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3676 QualType T = Importer.Import(E->getType());
3677 if (T.isNull())
3678 return 0;
3679
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003680 return IntegerLiteral::Create(Importer.getToContext(),
3681 E->getValue(), T,
3682 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003683}
3684
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003685Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3686 QualType T = Importer.Import(E->getType());
3687 if (T.isNull())
3688 return 0;
3689
3690 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3691 E->isWide(), T,
3692 Importer.Import(E->getLocation()));
3693}
3694
Douglas Gregorf638f952010-02-19 01:07:06 +00003695Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3696 Expr *SubExpr = Importer.Import(E->getSubExpr());
3697 if (!SubExpr)
3698 return 0;
3699
3700 return new (Importer.getToContext())
3701 ParenExpr(Importer.Import(E->getLParen()),
3702 Importer.Import(E->getRParen()),
3703 SubExpr);
3704}
3705
3706Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3707 QualType T = Importer.Import(E->getType());
3708 if (T.isNull())
3709 return 0;
3710
3711 Expr *SubExpr = Importer.Import(E->getSubExpr());
3712 if (!SubExpr)
3713 return 0;
3714
3715 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003716 T, E->getValueKind(),
3717 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003718 Importer.Import(E->getOperatorLoc()));
3719}
3720
Douglas Gregorbd249a52010-02-19 01:24:23 +00003721Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3722 QualType ResultType = Importer.Import(E->getType());
3723
3724 if (E->isArgumentType()) {
3725 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3726 if (!TInfo)
3727 return 0;
3728
3729 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3730 TInfo, ResultType,
3731 Importer.Import(E->getOperatorLoc()),
3732 Importer.Import(E->getRParenLoc()));
3733 }
3734
3735 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3736 if (!SubExpr)
3737 return 0;
3738
3739 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3740 SubExpr, ResultType,
3741 Importer.Import(E->getOperatorLoc()),
3742 Importer.Import(E->getRParenLoc()));
3743}
3744
Douglas Gregorf638f952010-02-19 01:07:06 +00003745Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3746 QualType T = Importer.Import(E->getType());
3747 if (T.isNull())
3748 return 0;
3749
3750 Expr *LHS = Importer.Import(E->getLHS());
3751 if (!LHS)
3752 return 0;
3753
3754 Expr *RHS = Importer.Import(E->getRHS());
3755 if (!RHS)
3756 return 0;
3757
3758 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003759 T, E->getValueKind(),
3760 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003761 Importer.Import(E->getOperatorLoc()));
3762}
3763
3764Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3765 QualType T = Importer.Import(E->getType());
3766 if (T.isNull())
3767 return 0;
3768
3769 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3770 if (CompLHSType.isNull())
3771 return 0;
3772
3773 QualType CompResultType = Importer.Import(E->getComputationResultType());
3774 if (CompResultType.isNull())
3775 return 0;
3776
3777 Expr *LHS = Importer.Import(E->getLHS());
3778 if (!LHS)
3779 return 0;
3780
3781 Expr *RHS = Importer.Import(E->getRHS());
3782 if (!RHS)
3783 return 0;
3784
3785 return new (Importer.getToContext())
3786 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003787 T, E->getValueKind(),
3788 E->getObjectKind(),
3789 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003790 Importer.Import(E->getOperatorLoc()));
3791}
3792
John McCallf871d0c2010-08-07 06:22:56 +00003793bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3794 if (E->path_empty()) return false;
3795
3796 // TODO: import cast paths
3797 return true;
3798}
3799
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003800Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3801 QualType T = Importer.Import(E->getType());
3802 if (T.isNull())
3803 return 0;
3804
3805 Expr *SubExpr = Importer.Import(E->getSubExpr());
3806 if (!SubExpr)
3807 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003808
3809 CXXCastPath BasePath;
3810 if (ImportCastPath(E, BasePath))
3811 return 0;
3812
3813 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003814 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003815}
3816
Douglas Gregor008847a2010-02-19 01:32:14 +00003817Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3818 QualType T = Importer.Import(E->getType());
3819 if (T.isNull())
3820 return 0;
3821
3822 Expr *SubExpr = Importer.Import(E->getSubExpr());
3823 if (!SubExpr)
3824 return 0;
3825
3826 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3827 if (!TInfo && E->getTypeInfoAsWritten())
3828 return 0;
3829
John McCallf871d0c2010-08-07 06:22:56 +00003830 CXXCastPath BasePath;
3831 if (ImportCastPath(E, BasePath))
3832 return 0;
3833
John McCallf89e55a2010-11-18 06:31:45 +00003834 return CStyleCastExpr::Create(Importer.getToContext(), T,
3835 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003836 SubExpr, &BasePath, TInfo,
3837 Importer.Import(E->getLParenLoc()),
3838 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003839}
3840
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003841ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003842 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003843 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003844 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003845 ImportedDecls[FromContext.getTranslationUnitDecl()]
3846 = ToContext.getTranslationUnitDecl();
3847}
3848
3849ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003850
3851QualType ASTImporter::Import(QualType FromT) {
3852 if (FromT.isNull())
3853 return QualType();
3854
Douglas Gregor169fba52010-02-08 15:18:58 +00003855 // Check whether we've already imported this type.
3856 llvm::DenseMap<Type *, Type *>::iterator Pos
3857 = ImportedTypes.find(FromT.getTypePtr());
3858 if (Pos != ImportedTypes.end())
3859 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003860
Douglas Gregor169fba52010-02-08 15:18:58 +00003861 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003862 ASTNodeImporter Importer(*this);
3863 QualType ToT = Importer.Visit(FromT.getTypePtr());
3864 if (ToT.isNull())
3865 return ToT;
3866
Douglas Gregor169fba52010-02-08 15:18:58 +00003867 // Record the imported type.
3868 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3869
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003870 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3871}
3872
Douglas Gregor9bed8792010-02-09 19:21:46 +00003873TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003874 if (!FromTSI)
3875 return FromTSI;
3876
3877 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003878 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003879 QualType T = Import(FromTSI->getType());
3880 if (T.isNull())
3881 return 0;
3882
3883 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003884 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003885}
3886
3887Decl *ASTImporter::Import(Decl *FromD) {
3888 if (!FromD)
3889 return 0;
3890
3891 // Check whether we've already imported this declaration.
3892 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3893 if (Pos != ImportedDecls.end())
3894 return Pos->second;
3895
3896 // Import the type
3897 ASTNodeImporter Importer(*this);
3898 Decl *ToD = Importer.Visit(FromD);
3899 if (!ToD)
3900 return 0;
3901
3902 // Record the imported declaration.
3903 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003904
3905 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3906 // Keep track of anonymous tags that have an associated typedef.
3907 if (FromTag->getTypedefForAnonDecl())
3908 AnonTagsWithPendingTypedefs.push_back(FromTag);
3909 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3910 // When we've finished transforming a typedef, see whether it was the
3911 // typedef for an anonymous tag.
3912 for (llvm::SmallVector<TagDecl *, 4>::iterator
3913 FromTag = AnonTagsWithPendingTypedefs.begin(),
3914 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3915 FromTag != FromTagEnd; ++FromTag) {
3916 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3917 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3918 // We found the typedef for an anonymous tag; link them.
3919 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3920 AnonTagsWithPendingTypedefs.erase(FromTag);
3921 break;
3922 }
3923 }
3924 }
3925 }
3926
Douglas Gregor9bed8792010-02-09 19:21:46 +00003927 return ToD;
3928}
3929
3930DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3931 if (!FromDC)
3932 return FromDC;
3933
3934 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3935}
3936
3937Expr *ASTImporter::Import(Expr *FromE) {
3938 if (!FromE)
3939 return 0;
3940
3941 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3942}
3943
3944Stmt *ASTImporter::Import(Stmt *FromS) {
3945 if (!FromS)
3946 return 0;
3947
Douglas Gregor4800d952010-02-11 19:21:55 +00003948 // Check whether we've already imported this declaration.
3949 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3950 if (Pos != ImportedStmts.end())
3951 return Pos->second;
3952
3953 // Import the type
3954 ASTNodeImporter Importer(*this);
3955 Stmt *ToS = Importer.Visit(FromS);
3956 if (!ToS)
3957 return 0;
3958
3959 // Record the imported declaration.
3960 ImportedStmts[FromS] = ToS;
3961 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003962}
3963
3964NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3965 if (!FromNNS)
3966 return 0;
3967
3968 // FIXME: Implement!
3969 return 0;
3970}
3971
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003972TemplateName ASTImporter::Import(TemplateName From) {
3973 switch (From.getKind()) {
3974 case TemplateName::Template:
3975 if (TemplateDecl *ToTemplate
3976 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3977 return TemplateName(ToTemplate);
3978
3979 return TemplateName();
3980
3981 case TemplateName::OverloadedTemplate: {
3982 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3983 UnresolvedSet<2> ToTemplates;
3984 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3985 E = FromStorage->end();
3986 I != E; ++I) {
3987 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3988 ToTemplates.addDecl(To);
3989 else
3990 return TemplateName();
3991 }
3992 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3993 ToTemplates.end());
3994 }
3995
3996 case TemplateName::QualifiedTemplate: {
3997 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3998 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
3999 if (!Qualifier)
4000 return TemplateName();
4001
4002 if (TemplateDecl *ToTemplate
4003 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4004 return ToContext.getQualifiedTemplateName(Qualifier,
4005 QTN->hasTemplateKeyword(),
4006 ToTemplate);
4007
4008 return TemplateName();
4009 }
4010
4011 case TemplateName::DependentTemplate: {
4012 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4013 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4014 if (!Qualifier)
4015 return TemplateName();
4016
4017 if (DTN->isIdentifier()) {
4018 return ToContext.getDependentTemplateName(Qualifier,
4019 Import(DTN->getIdentifier()));
4020 }
4021
4022 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4023 }
4024 }
4025
4026 llvm_unreachable("Invalid template name kind");
4027 return TemplateName();
4028}
4029
Douglas Gregor9bed8792010-02-09 19:21:46 +00004030SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4031 if (FromLoc.isInvalid())
4032 return SourceLocation();
4033
Douglas Gregor88523732010-02-10 00:15:17 +00004034 SourceManager &FromSM = FromContext.getSourceManager();
4035
4036 // For now, map everything down to its spelling location, so that we
4037 // don't have to import macro instantiations.
4038 // FIXME: Import macro instantiations!
4039 FromLoc = FromSM.getSpellingLoc(FromLoc);
4040 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4041 SourceManager &ToSM = ToContext.getSourceManager();
4042 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4043 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004044}
4045
4046SourceRange ASTImporter::Import(SourceRange FromRange) {
4047 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4048}
4049
Douglas Gregor88523732010-02-10 00:15:17 +00004050FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004051 llvm::DenseMap<FileID, FileID>::iterator Pos
4052 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004053 if (Pos != ImportedFileIDs.end())
4054 return Pos->second;
4055
4056 SourceManager &FromSM = FromContext.getSourceManager();
4057 SourceManager &ToSM = ToContext.getSourceManager();
4058 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4059 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4060
4061 // Include location of this file.
4062 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4063
4064 // Map the FileID for to the "to" source manager.
4065 FileID ToID;
4066 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4067 if (Cache->Entry) {
4068 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4069 // disk again
4070 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4071 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004072 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004073 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4074 FromSLoc.getFile().getFileCharacteristic());
4075 } else {
4076 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004077 const llvm::MemoryBuffer *
4078 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004079 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004080 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004081 FromBuf->getBufferIdentifier());
4082 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4083 }
4084
4085
Sebastian Redl535a3e22010-09-30 01:03:06 +00004086 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004087 return ToID;
4088}
4089
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004090DeclarationName ASTImporter::Import(DeclarationName FromName) {
4091 if (!FromName)
4092 return DeclarationName();
4093
4094 switch (FromName.getNameKind()) {
4095 case DeclarationName::Identifier:
4096 return Import(FromName.getAsIdentifierInfo());
4097
4098 case DeclarationName::ObjCZeroArgSelector:
4099 case DeclarationName::ObjCOneArgSelector:
4100 case DeclarationName::ObjCMultiArgSelector:
4101 return Import(FromName.getObjCSelector());
4102
4103 case DeclarationName::CXXConstructorName: {
4104 QualType T = Import(FromName.getCXXNameType());
4105 if (T.isNull())
4106 return DeclarationName();
4107
4108 return ToContext.DeclarationNames.getCXXConstructorName(
4109 ToContext.getCanonicalType(T));
4110 }
4111
4112 case DeclarationName::CXXDestructorName: {
4113 QualType T = Import(FromName.getCXXNameType());
4114 if (T.isNull())
4115 return DeclarationName();
4116
4117 return ToContext.DeclarationNames.getCXXDestructorName(
4118 ToContext.getCanonicalType(T));
4119 }
4120
4121 case DeclarationName::CXXConversionFunctionName: {
4122 QualType T = Import(FromName.getCXXNameType());
4123 if (T.isNull())
4124 return DeclarationName();
4125
4126 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4127 ToContext.getCanonicalType(T));
4128 }
4129
4130 case DeclarationName::CXXOperatorName:
4131 return ToContext.DeclarationNames.getCXXOperatorName(
4132 FromName.getCXXOverloadedOperator());
4133
4134 case DeclarationName::CXXLiteralOperatorName:
4135 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4136 Import(FromName.getCXXLiteralIdentifier()));
4137
4138 case DeclarationName::CXXUsingDirective:
4139 // FIXME: STATICS!
4140 return DeclarationName::getUsingDirectiveName();
4141 }
4142
4143 // Silence bogus GCC warning
4144 return DeclarationName();
4145}
4146
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004147IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004148 if (!FromId)
4149 return 0;
4150
4151 return &ToContext.Idents.get(FromId->getName());
4152}
Douglas Gregor089459a2010-02-08 21:09:39 +00004153
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004154Selector ASTImporter::Import(Selector FromSel) {
4155 if (FromSel.isNull())
4156 return Selector();
4157
4158 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4159 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4160 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4161 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4162 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4163}
4164
Douglas Gregor089459a2010-02-08 21:09:39 +00004165DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4166 DeclContext *DC,
4167 unsigned IDNS,
4168 NamedDecl **Decls,
4169 unsigned NumDecls) {
4170 return Name;
4171}
4172
4173DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004174 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004175}
4176
4177DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004178 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004179}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004180
4181Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4182 ImportedDecls[From] = To;
4183 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004184}
Douglas Gregorea35d112010-02-15 23:54:17 +00004185
4186bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4187 llvm::DenseMap<Type *, Type *>::iterator Pos
4188 = ImportedTypes.find(From.getTypePtr());
4189 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4190 return true;
4191
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004192 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004193 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004194}