blob: 8415977349b3667996f1e33f115a957608b92f63 [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
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000556 case Type::Paren:
557 if (!IsStructurallyEquivalent(Context,
558 cast<ParenType>(T1)->getInnerType(),
559 cast<ParenType>(T2)->getInnerType()))
560 return false;
561 break;
562
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000563 case Type::Typedef:
564 if (!IsStructurallyEquivalent(Context,
565 cast<TypedefType>(T1)->getDecl(),
566 cast<TypedefType>(T2)->getDecl()))
567 return false;
568 break;
569
570 case Type::TypeOfExpr:
571 if (!IsStructurallyEquivalent(Context,
572 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
573 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
574 return false;
575 break;
576
577 case Type::TypeOf:
578 if (!IsStructurallyEquivalent(Context,
579 cast<TypeOfType>(T1)->getUnderlyingType(),
580 cast<TypeOfType>(T2)->getUnderlyingType()))
581 return false;
582 break;
583
584 case Type::Decltype:
585 if (!IsStructurallyEquivalent(Context,
586 cast<DecltypeType>(T1)->getUnderlyingExpr(),
587 cast<DecltypeType>(T2)->getUnderlyingExpr()))
588 return false;
589 break;
590
591 case Type::Record:
592 case Type::Enum:
593 if (!IsStructurallyEquivalent(Context,
594 cast<TagType>(T1)->getDecl(),
595 cast<TagType>(T2)->getDecl()))
596 return false;
597 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000598
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000599 case Type::TemplateTypeParm: {
600 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
601 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
602 if (Parm1->getDepth() != Parm2->getDepth())
603 return false;
604 if (Parm1->getIndex() != Parm2->getIndex())
605 return false;
606 if (Parm1->isParameterPack() != Parm2->isParameterPack())
607 return false;
608
609 // Names of template type parameters are never significant.
610 break;
611 }
612
613 case Type::SubstTemplateTypeParm: {
614 const SubstTemplateTypeParmType *Subst1
615 = cast<SubstTemplateTypeParmType>(T1);
616 const SubstTemplateTypeParmType *Subst2
617 = cast<SubstTemplateTypeParmType>(T2);
618 if (!IsStructurallyEquivalent(Context,
619 QualType(Subst1->getReplacedParameter(), 0),
620 QualType(Subst2->getReplacedParameter(), 0)))
621 return false;
622 if (!IsStructurallyEquivalent(Context,
623 Subst1->getReplacementType(),
624 Subst2->getReplacementType()))
625 return false;
626 break;
627 }
628
629 case Type::TemplateSpecialization: {
630 const TemplateSpecializationType *Spec1
631 = cast<TemplateSpecializationType>(T1);
632 const TemplateSpecializationType *Spec2
633 = cast<TemplateSpecializationType>(T2);
634 if (!IsStructurallyEquivalent(Context,
635 Spec1->getTemplateName(),
636 Spec2->getTemplateName()))
637 return false;
638 if (Spec1->getNumArgs() != Spec2->getNumArgs())
639 return false;
640 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
641 if (!IsStructurallyEquivalent(Context,
642 Spec1->getArg(I), Spec2->getArg(I)))
643 return false;
644 }
645 break;
646 }
647
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000648 case Type::Elaborated: {
649 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
650 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
651 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
652 if (Elab1->getKeyword() != Elab2->getKeyword())
653 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000654 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000655 Elab1->getQualifier(),
656 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000657 return false;
658 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000659 Elab1->getNamedType(),
660 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000661 return false;
662 break;
663 }
664
John McCall3cb0ebd2010-03-10 03:28:59 +0000665 case Type::InjectedClassName: {
666 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
667 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
668 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000669 Inj1->getInjectedSpecializationType(),
670 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000671 return false;
672 break;
673 }
674
Douglas Gregor4714c122010-03-31 17:34:00 +0000675 case Type::DependentName: {
676 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
677 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000678 if (!IsStructurallyEquivalent(Context,
679 Typename1->getQualifier(),
680 Typename2->getQualifier()))
681 return false;
682 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
683 Typename2->getIdentifier()))
684 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000685
686 break;
687 }
688
John McCall33500952010-06-11 00:33:02 +0000689 case Type::DependentTemplateSpecialization: {
690 const DependentTemplateSpecializationType *Spec1 =
691 cast<DependentTemplateSpecializationType>(T1);
692 const DependentTemplateSpecializationType *Spec2 =
693 cast<DependentTemplateSpecializationType>(T2);
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getQualifier(),
696 Spec2->getQualifier()))
697 return false;
698 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
699 Spec2->getIdentifier()))
700 return false;
701 if (Spec1->getNumArgs() != Spec2->getNumArgs())
702 return false;
703 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
704 if (!IsStructurallyEquivalent(Context,
705 Spec1->getArg(I), Spec2->getArg(I)))
706 return false;
707 }
708 break;
709 }
710
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000711 case Type::ObjCInterface: {
712 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
713 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
714 if (!IsStructurallyEquivalent(Context,
715 Iface1->getDecl(), Iface2->getDecl()))
716 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000717 break;
718 }
719
720 case Type::ObjCObject: {
721 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
722 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
723 if (!IsStructurallyEquivalent(Context,
724 Obj1->getBaseType(),
725 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000726 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000727 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
728 return false;
729 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000730 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000731 Obj1->getProtocol(I),
732 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000733 return false;
734 }
735 break;
736 }
737
738 case Type::ObjCObjectPointer: {
739 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
740 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
741 if (!IsStructurallyEquivalent(Context,
742 Ptr1->getPointeeType(),
743 Ptr2->getPointeeType()))
744 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000745 break;
746 }
747
748 } // end switch
749
750 return true;
751}
752
753/// \brief Determine structural equivalence of two records.
754static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
755 RecordDecl *D1, RecordDecl *D2) {
756 if (D1->isUnion() != D2->isUnion()) {
757 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
758 << Context.C2.getTypeDeclType(D2);
759 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
760 << D1->getDeclName() << (unsigned)D1->getTagKind();
761 return false;
762 }
763
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000764 // If both declarations are class template specializations, we know
765 // the ODR applies, so check the template and template arguments.
766 ClassTemplateSpecializationDecl *Spec1
767 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
768 ClassTemplateSpecializationDecl *Spec2
769 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
770 if (Spec1 && Spec2) {
771 // Check that the specialized templates are the same.
772 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
773 Spec2->getSpecializedTemplate()))
774 return false;
775
776 // Check that the template arguments are the same.
777 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
778 return false;
779
780 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
781 if (!IsStructurallyEquivalent(Context,
782 Spec1->getTemplateArgs().get(I),
783 Spec2->getTemplateArgs().get(I)))
784 return false;
785 }
786 // If one is a class template specialization and the other is not, these
787 // structures are diferent.
788 else if (Spec1 || Spec2)
789 return false;
790
Douglas Gregorea35d112010-02-15 23:54:17 +0000791 // Compare the definitions of these two records. If either or both are
792 // incomplete, we assume that they are equivalent.
793 D1 = D1->getDefinition();
794 D2 = D2->getDefinition();
795 if (!D1 || !D2)
796 return true;
797
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
799 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
800 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
801 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000802 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000803 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000804 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000805 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000806 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000807 return false;
808 }
809
810 // Check the base classes.
811 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
812 BaseEnd1 = D1CXX->bases_end(),
813 Base2 = D2CXX->bases_begin();
814 Base1 != BaseEnd1;
815 ++Base1, ++Base2) {
816 if (!IsStructurallyEquivalent(Context,
817 Base1->getType(), Base2->getType())) {
818 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
819 << Context.C2.getTypeDeclType(D2);
820 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
821 << Base2->getType()
822 << Base2->getSourceRange();
823 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
824 << Base1->getType()
825 << Base1->getSourceRange();
826 return false;
827 }
828
829 // Check virtual vs. non-virtual inheritance mismatch.
830 if (Base1->isVirtual() != Base2->isVirtual()) {
831 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
832 << Context.C2.getTypeDeclType(D2);
833 Context.Diag2(Base2->getSourceRange().getBegin(),
834 diag::note_odr_virtual_base)
835 << Base2->isVirtual() << Base2->getSourceRange();
836 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
837 << Base1->isVirtual()
838 << Base1->getSourceRange();
839 return false;
840 }
841 }
842 } else if (D1CXX->getNumBases() > 0) {
843 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
844 << Context.C2.getTypeDeclType(D2);
845 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
846 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
847 << Base1->getType()
848 << Base1->getSourceRange();
849 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
850 return false;
851 }
852 }
853
854 // Check the fields for consistency.
855 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
856 Field2End = D2->field_end();
857 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
858 Field1End = D1->field_end();
859 Field1 != Field1End;
860 ++Field1, ++Field2) {
861 if (Field2 == Field2End) {
862 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
863 << Context.C2.getTypeDeclType(D2);
864 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
865 << Field1->getDeclName() << Field1->getType();
866 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
867 return false;
868 }
869
870 if (!IsStructurallyEquivalent(Context,
871 Field1->getType(), Field2->getType())) {
872 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
873 << Context.C2.getTypeDeclType(D2);
874 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
875 << Field2->getDeclName() << Field2->getType();
876 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
877 << Field1->getDeclName() << Field1->getType();
878 return false;
879 }
880
881 if (Field1->isBitField() != Field2->isBitField()) {
882 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
883 << Context.C2.getTypeDeclType(D2);
884 if (Field1->isBitField()) {
885 llvm::APSInt Bits;
886 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
887 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
888 << Field1->getDeclName() << Field1->getType()
889 << Bits.toString(10, false);
890 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
891 << Field2->getDeclName();
892 } else {
893 llvm::APSInt Bits;
894 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
895 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
896 << Field2->getDeclName() << Field2->getType()
897 << Bits.toString(10, false);
898 Context.Diag1(Field1->getLocation(),
899 diag::note_odr_not_bit_field)
900 << Field1->getDeclName();
901 }
902 return false;
903 }
904
905 if (Field1->isBitField()) {
906 // Make sure that the bit-fields are the same length.
907 llvm::APSInt Bits1, Bits2;
908 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
909 return false;
910 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
911 return false;
912
913 if (!IsSameValue(Bits1, Bits2)) {
914 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
915 << Context.C2.getTypeDeclType(D2);
916 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
917 << Field2->getDeclName() << Field2->getType()
918 << Bits2.toString(10, false);
919 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
920 << Field1->getDeclName() << Field1->getType()
921 << Bits1.toString(10, false);
922 return false;
923 }
924 }
925 }
926
927 if (Field2 != Field2End) {
928 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
929 << Context.C2.getTypeDeclType(D2);
930 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
931 << Field2->getDeclName() << Field2->getType();
932 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
933 return false;
934 }
935
936 return true;
937}
938
939/// \brief Determine structural equivalence of two enums.
940static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
941 EnumDecl *D1, EnumDecl *D2) {
942 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
943 EC2End = D2->enumerator_end();
944 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
945 EC1End = D1->enumerator_end();
946 EC1 != EC1End; ++EC1, ++EC2) {
947 if (EC2 == EC2End) {
948 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
949 << Context.C2.getTypeDeclType(D2);
950 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
951 << EC1->getDeclName()
952 << EC1->getInitVal().toString(10);
953 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
954 return false;
955 }
956
957 llvm::APSInt Val1 = EC1->getInitVal();
958 llvm::APSInt Val2 = EC2->getInitVal();
959 if (!IsSameValue(Val1, Val2) ||
960 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
961 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
962 << Context.C2.getTypeDeclType(D2);
963 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
964 << EC2->getDeclName()
965 << EC2->getInitVal().toString(10);
966 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
967 << EC1->getDeclName()
968 << EC1->getInitVal().toString(10);
969 return false;
970 }
971 }
972
973 if (EC2 != EC2End) {
974 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
975 << Context.C2.getTypeDeclType(D2);
976 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
977 << EC2->getDeclName()
978 << EC2->getInitVal().toString(10);
979 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
980 return false;
981 }
982
983 return true;
984}
Douglas Gregor040afae2010-11-30 19:14:50 +0000985
986static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
987 TemplateParameterList *Params1,
988 TemplateParameterList *Params2) {
989 if (Params1->size() != Params2->size()) {
990 Context.Diag2(Params2->getTemplateLoc(),
991 diag::err_odr_different_num_template_parameters)
992 << Params1->size() << Params2->size();
993 Context.Diag1(Params1->getTemplateLoc(),
994 diag::note_odr_template_parameter_list);
995 return false;
996 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000997
Douglas Gregor040afae2010-11-30 19:14:50 +0000998 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
999 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1000 Context.Diag2(Params2->getParam(I)->getLocation(),
1001 diag::err_odr_different_template_parameter_kind);
1002 Context.Diag1(Params1->getParam(I)->getLocation(),
1003 diag::note_odr_template_parameter_here);
1004 return false;
1005 }
1006
1007 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1008 Params2->getParam(I))) {
1009
1010 return false;
1011 }
1012 }
1013
1014 return true;
1015}
1016
1017static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1018 TemplateTypeParmDecl *D1,
1019 TemplateTypeParmDecl *D2) {
1020 if (D1->isParameterPack() != D2->isParameterPack()) {
1021 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1022 << D2->isParameterPack();
1023 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1024 << D1->isParameterPack();
1025 return false;
1026 }
1027
1028 return true;
1029}
1030
1031static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1032 NonTypeTemplateParmDecl *D1,
1033 NonTypeTemplateParmDecl *D2) {
1034 // FIXME: Enable once we have variadic templates.
1035#if 0
1036 if (D1->isParameterPack() != D2->isParameterPack()) {
1037 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1038 << D2->isParameterPack();
1039 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1040 << D1->isParameterPack();
1041 return false;
1042 }
1043#endif
1044
1045 // Check types.
1046 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1047 Context.Diag2(D2->getLocation(),
1048 diag::err_odr_non_type_parameter_type_inconsistent)
1049 << D2->getType() << D1->getType();
1050 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1051 << D1->getType();
1052 return false;
1053 }
1054
1055 return true;
1056}
1057
1058static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1059 TemplateTemplateParmDecl *D1,
1060 TemplateTemplateParmDecl *D2) {
1061 // FIXME: Enable once we have variadic templates.
1062#if 0
1063 if (D1->isParameterPack() != D2->isParameterPack()) {
1064 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1065 << D2->isParameterPack();
1066 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1067 << D1->isParameterPack();
1068 return false;
1069 }
1070#endif
1071
1072 // Check template parameter lists.
1073 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1074 D2->getTemplateParameters());
1075}
1076
1077static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1078 ClassTemplateDecl *D1,
1079 ClassTemplateDecl *D2) {
1080 // Check template parameters.
1081 if (!IsStructurallyEquivalent(Context,
1082 D1->getTemplateParameters(),
1083 D2->getTemplateParameters()))
1084 return false;
1085
1086 // Check the templated declaration.
1087 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1088 D2->getTemplatedDecl());
1089}
1090
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001091/// \brief Determine structural equivalence of two declarations.
1092static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1093 Decl *D1, Decl *D2) {
1094 // FIXME: Check for known structural equivalences via a callback of some sort.
1095
Douglas Gregorea35d112010-02-15 23:54:17 +00001096 // Check whether we already know that these two declarations are not
1097 // structurally equivalent.
1098 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1099 D2->getCanonicalDecl())))
1100 return false;
1101
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001102 // Determine whether we've already produced a tentative equivalence for D1.
1103 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1104 if (EquivToD1)
1105 return EquivToD1 == D2->getCanonicalDecl();
1106
1107 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1108 EquivToD1 = D2->getCanonicalDecl();
1109 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1110 return true;
1111}
1112
1113bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1114 Decl *D2) {
1115 if (!::IsStructurallyEquivalent(*this, D1, D2))
1116 return false;
1117
1118 return !Finish();
1119}
1120
1121bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1122 QualType T2) {
1123 if (!::IsStructurallyEquivalent(*this, T1, T2))
1124 return false;
1125
1126 return !Finish();
1127}
1128
1129bool StructuralEquivalenceContext::Finish() {
1130 while (!DeclsToCheck.empty()) {
1131 // Check the next declaration.
1132 Decl *D1 = DeclsToCheck.front();
1133 DeclsToCheck.pop_front();
1134
1135 Decl *D2 = TentativeEquivalences[D1];
1136 assert(D2 && "Unrecorded tentative equivalence?");
1137
Douglas Gregorea35d112010-02-15 23:54:17 +00001138 bool Equivalent = true;
1139
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001140 // FIXME: Switch on all declaration kinds. For now, we're just going to
1141 // check the obvious ones.
1142 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1143 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1144 // Check for equivalent structure names.
1145 IdentifierInfo *Name1 = Record1->getIdentifier();
1146 if (!Name1 && Record1->getTypedefForAnonDecl())
1147 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1148 IdentifierInfo *Name2 = Record2->getIdentifier();
1149 if (!Name2 && Record2->getTypedefForAnonDecl())
1150 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001151 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1152 !::IsStructurallyEquivalent(*this, Record1, Record2))
1153 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001154 } else {
1155 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001156 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001157 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001158 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001159 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1160 // Check for equivalent enum names.
1161 IdentifierInfo *Name1 = Enum1->getIdentifier();
1162 if (!Name1 && Enum1->getTypedefForAnonDecl())
1163 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1164 IdentifierInfo *Name2 = Enum2->getIdentifier();
1165 if (!Name2 && Enum2->getTypedefForAnonDecl())
1166 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001167 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1168 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1169 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001170 } else {
1171 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001172 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001173 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001174 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001175 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1176 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001177 Typedef2->getIdentifier()) ||
1178 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001179 Typedef1->getUnderlyingType(),
1180 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001181 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001182 } else {
1183 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001184 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001185 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001186 } else if (ClassTemplateDecl *ClassTemplate1
1187 = dyn_cast<ClassTemplateDecl>(D1)) {
1188 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1189 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1190 ClassTemplate2->getIdentifier()) ||
1191 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1192 Equivalent = false;
1193 } else {
1194 // Class template/non-class-template mismatch.
1195 Equivalent = false;
1196 }
1197 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1198 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1199 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1200 Equivalent = false;
1201 } else {
1202 // Kind mismatch.
1203 Equivalent = false;
1204 }
1205 } else if (NonTypeTemplateParmDecl *NTTP1
1206 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1207 if (NonTypeTemplateParmDecl *NTTP2
1208 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1209 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1210 Equivalent = false;
1211 } else {
1212 // Kind mismatch.
1213 Equivalent = false;
1214 }
1215 } else if (TemplateTemplateParmDecl *TTP1
1216 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1217 if (TemplateTemplateParmDecl *TTP2
1218 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1219 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1220 Equivalent = false;
1221 } else {
1222 // Kind mismatch.
1223 Equivalent = false;
1224 }
1225 }
1226
Douglas Gregorea35d112010-02-15 23:54:17 +00001227 if (!Equivalent) {
1228 // Note that these two declarations are not equivalent (and we already
1229 // know about it).
1230 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1231 D2->getCanonicalDecl()));
1232 return true;
1233 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001234 // FIXME: Check other declaration kinds!
1235 }
1236
1237 return false;
1238}
1239
1240//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001241// Import Types
1242//----------------------------------------------------------------------------
1243
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001244QualType ASTNodeImporter::VisitType(Type *T) {
1245 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1246 << T->getTypeClassName();
1247 return QualType();
1248}
1249
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001250QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1251 switch (T->getKind()) {
1252 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1253 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1254
1255 case BuiltinType::Char_U:
1256 // The context we're importing from has an unsigned 'char'. If we're
1257 // importing into a context with a signed 'char', translate to
1258 // 'unsigned char' instead.
1259 if (Importer.getToContext().getLangOptions().CharIsSigned)
1260 return Importer.getToContext().UnsignedCharTy;
1261
1262 return Importer.getToContext().CharTy;
1263
1264 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1265
1266 case BuiltinType::Char16:
1267 // FIXME: Make sure that the "to" context supports C++!
1268 return Importer.getToContext().Char16Ty;
1269
1270 case BuiltinType::Char32:
1271 // FIXME: Make sure that the "to" context supports C++!
1272 return Importer.getToContext().Char32Ty;
1273
1274 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1275 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1276 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1277 case BuiltinType::ULongLong:
1278 return Importer.getToContext().UnsignedLongLongTy;
1279 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1280
1281 case BuiltinType::Char_S:
1282 // The context we're importing from has an unsigned 'char'. If we're
1283 // importing into a context with a signed 'char', translate to
1284 // 'unsigned char' instead.
1285 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1286 return Importer.getToContext().SignedCharTy;
1287
1288 return Importer.getToContext().CharTy;
1289
1290 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1291 case BuiltinType::WChar:
1292 // FIXME: If not in C++, shall we translate to the C equivalent of
1293 // wchar_t?
1294 return Importer.getToContext().WCharTy;
1295
1296 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1297 case BuiltinType::Int : return Importer.getToContext().IntTy;
1298 case BuiltinType::Long : return Importer.getToContext().LongTy;
1299 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1300 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1301 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1302 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1303 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1304
1305 case BuiltinType::NullPtr:
1306 // FIXME: Make sure that the "to" context supports C++0x!
1307 return Importer.getToContext().NullPtrTy;
1308
1309 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1310 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1311 case BuiltinType::UndeducedAuto:
1312 // FIXME: Make sure that the "to" context supports C++0x!
1313 return Importer.getToContext().UndeducedAutoTy;
1314
1315 case BuiltinType::ObjCId:
1316 // FIXME: Make sure that the "to" context supports Objective-C!
1317 return Importer.getToContext().ObjCBuiltinIdTy;
1318
1319 case BuiltinType::ObjCClass:
1320 return Importer.getToContext().ObjCBuiltinClassTy;
1321
1322 case BuiltinType::ObjCSel:
1323 return Importer.getToContext().ObjCBuiltinSelTy;
1324 }
1325
1326 return QualType();
1327}
1328
1329QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1330 QualType ToElementType = Importer.Import(T->getElementType());
1331 if (ToElementType.isNull())
1332 return QualType();
1333
1334 return Importer.getToContext().getComplexType(ToElementType);
1335}
1336
1337QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1338 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1339 if (ToPointeeType.isNull())
1340 return QualType();
1341
1342 return Importer.getToContext().getPointerType(ToPointeeType);
1343}
1344
1345QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1346 // FIXME: Check for blocks support in "to" context.
1347 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1348 if (ToPointeeType.isNull())
1349 return QualType();
1350
1351 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1352}
1353
1354QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1355 // FIXME: Check for C++ support in "to" context.
1356 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1357 if (ToPointeeType.isNull())
1358 return QualType();
1359
1360 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1361}
1362
1363QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1364 // FIXME: Check for C++0x support in "to" context.
1365 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1366 if (ToPointeeType.isNull())
1367 return QualType();
1368
1369 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1370}
1371
1372QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1373 // FIXME: Check for C++ support in "to" context.
1374 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1375 if (ToPointeeType.isNull())
1376 return QualType();
1377
1378 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1379 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1380 ClassType.getTypePtr());
1381}
1382
1383QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1384 QualType ToElementType = Importer.Import(T->getElementType());
1385 if (ToElementType.isNull())
1386 return QualType();
1387
1388 return Importer.getToContext().getConstantArrayType(ToElementType,
1389 T->getSize(),
1390 T->getSizeModifier(),
1391 T->getIndexTypeCVRQualifiers());
1392}
1393
1394QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1395 QualType ToElementType = Importer.Import(T->getElementType());
1396 if (ToElementType.isNull())
1397 return QualType();
1398
1399 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1400 T->getSizeModifier(),
1401 T->getIndexTypeCVRQualifiers());
1402}
1403
1404QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1405 QualType ToElementType = Importer.Import(T->getElementType());
1406 if (ToElementType.isNull())
1407 return QualType();
1408
1409 Expr *Size = Importer.Import(T->getSizeExpr());
1410 if (!Size)
1411 return QualType();
1412
1413 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1414 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1415 T->getSizeModifier(),
1416 T->getIndexTypeCVRQualifiers(),
1417 Brackets);
1418}
1419
1420QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1421 QualType ToElementType = Importer.Import(T->getElementType());
1422 if (ToElementType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getVectorType(ToElementType,
1426 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001427 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001428}
1429
1430QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1431 QualType ToElementType = Importer.Import(T->getElementType());
1432 if (ToElementType.isNull())
1433 return QualType();
1434
1435 return Importer.getToContext().getExtVectorType(ToElementType,
1436 T->getNumElements());
1437}
1438
1439QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1440 // FIXME: What happens if we're importing a function without a prototype
1441 // into C++? Should we make it variadic?
1442 QualType ToResultType = Importer.Import(T->getResultType());
1443 if (ToResultType.isNull())
1444 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001445
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001446 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001447 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001448}
1449
1450QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1451 QualType ToResultType = Importer.Import(T->getResultType());
1452 if (ToResultType.isNull())
1453 return QualType();
1454
1455 // Import argument types
1456 llvm::SmallVector<QualType, 4> ArgTypes;
1457 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1458 AEnd = T->arg_type_end();
1459 A != AEnd; ++A) {
1460 QualType ArgType = Importer.Import(*A);
1461 if (ArgType.isNull())
1462 return QualType();
1463 ArgTypes.push_back(ArgType);
1464 }
1465
1466 // Import exception types
1467 llvm::SmallVector<QualType, 4> ExceptionTypes;
1468 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1469 EEnd = T->exception_end();
1470 E != EEnd; ++E) {
1471 QualType ExceptionType = Importer.Import(*E);
1472 if (ExceptionType.isNull())
1473 return QualType();
1474 ExceptionTypes.push_back(ExceptionType);
1475 }
1476
1477 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1478 ArgTypes.size(),
1479 T->isVariadic(),
1480 T->getTypeQuals(),
1481 T->hasExceptionSpec(),
1482 T->hasAnyExceptionSpec(),
1483 ExceptionTypes.size(),
1484 ExceptionTypes.data(),
Rafael Espindola264ba482010-03-30 20:24:48 +00001485 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001486}
1487
1488QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1489 TypedefDecl *ToDecl
1490 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1491 if (!ToDecl)
1492 return QualType();
1493
1494 return Importer.getToContext().getTypeDeclType(ToDecl);
1495}
1496
1497QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1498 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1499 if (!ToExpr)
1500 return QualType();
1501
1502 return Importer.getToContext().getTypeOfExprType(ToExpr);
1503}
1504
1505QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1506 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1507 if (ToUnderlyingType.isNull())
1508 return QualType();
1509
1510 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1511}
1512
1513QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1514 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1515 if (!ToExpr)
1516 return QualType();
1517
1518 return Importer.getToContext().getDecltypeType(ToExpr);
1519}
1520
1521QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1522 RecordDecl *ToDecl
1523 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1524 if (!ToDecl)
1525 return QualType();
1526
1527 return Importer.getToContext().getTagDeclType(ToDecl);
1528}
1529
1530QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1531 EnumDecl *ToDecl
1532 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1533 if (!ToDecl)
1534 return QualType();
1535
1536 return Importer.getToContext().getTagDeclType(ToDecl);
1537}
1538
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001539QualType ASTNodeImporter::VisitTemplateSpecializationType(
1540 TemplateSpecializationType *T) {
1541 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1542 if (ToTemplate.isNull())
1543 return QualType();
1544
1545 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1546 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1547 return QualType();
1548
1549 QualType ToCanonType;
1550 if (!QualType(T, 0).isCanonical()) {
1551 QualType FromCanonType
1552 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1553 ToCanonType =Importer.Import(FromCanonType);
1554 if (ToCanonType.isNull())
1555 return QualType();
1556 }
1557 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1558 ToTemplateArgs.data(),
1559 ToTemplateArgs.size(),
1560 ToCanonType);
1561}
1562
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001563QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001564 NestedNameSpecifier *ToQualifier = 0;
1565 // Note: the qualifier in an ElaboratedType is optional.
1566 if (T->getQualifier()) {
1567 ToQualifier = Importer.Import(T->getQualifier());
1568 if (!ToQualifier)
1569 return QualType();
1570 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001571
1572 QualType ToNamedType = Importer.Import(T->getNamedType());
1573 if (ToNamedType.isNull())
1574 return QualType();
1575
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001576 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1577 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001578}
1579
1580QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1581 ObjCInterfaceDecl *Class
1582 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1583 if (!Class)
1584 return QualType();
1585
John McCallc12c5bb2010-05-15 11:32:37 +00001586 return Importer.getToContext().getObjCInterfaceType(Class);
1587}
1588
1589QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1590 QualType ToBaseType = Importer.Import(T->getBaseType());
1591 if (ToBaseType.isNull())
1592 return QualType();
1593
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001594 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001595 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001596 PEnd = T->qual_end();
1597 P != PEnd; ++P) {
1598 ObjCProtocolDecl *Protocol
1599 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1600 if (!Protocol)
1601 return QualType();
1602 Protocols.push_back(Protocol);
1603 }
1604
John McCallc12c5bb2010-05-15 11:32:37 +00001605 return Importer.getToContext().getObjCObjectType(ToBaseType,
1606 Protocols.data(),
1607 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001608}
1609
1610QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1611 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1612 if (ToPointeeType.isNull())
1613 return QualType();
1614
John McCallc12c5bb2010-05-15 11:32:37 +00001615 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001616}
1617
Douglas Gregor089459a2010-02-08 21:09:39 +00001618//----------------------------------------------------------------------------
1619// Import Declarations
1620//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001621bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1622 DeclContext *&LexicalDC,
1623 DeclarationName &Name,
1624 SourceLocation &Loc) {
1625 // Import the context of this declaration.
1626 DC = Importer.ImportContext(D->getDeclContext());
1627 if (!DC)
1628 return true;
1629
1630 LexicalDC = DC;
1631 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1632 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1633 if (!LexicalDC)
1634 return true;
1635 }
1636
1637 // Import the name of this declaration.
1638 Name = Importer.Import(D->getDeclName());
1639 if (D->getDeclName() && !Name)
1640 return true;
1641
1642 // Import the location of this declaration.
1643 Loc = Importer.Import(D->getLocation());
1644 return false;
1645}
1646
Abramo Bagnara25777432010-08-11 22:01:17 +00001647void
1648ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1649 DeclarationNameInfo& To) {
1650 // NOTE: To.Name and To.Loc are already imported.
1651 // We only have to import To.LocInfo.
1652 switch (To.getName().getNameKind()) {
1653 case DeclarationName::Identifier:
1654 case DeclarationName::ObjCZeroArgSelector:
1655 case DeclarationName::ObjCOneArgSelector:
1656 case DeclarationName::ObjCMultiArgSelector:
1657 case DeclarationName::CXXUsingDirective:
1658 return;
1659
1660 case DeclarationName::CXXOperatorName: {
1661 SourceRange Range = From.getCXXOperatorNameRange();
1662 To.setCXXOperatorNameRange(Importer.Import(Range));
1663 return;
1664 }
1665 case DeclarationName::CXXLiteralOperatorName: {
1666 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1667 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1668 return;
1669 }
1670 case DeclarationName::CXXConstructorName:
1671 case DeclarationName::CXXDestructorName:
1672 case DeclarationName::CXXConversionFunctionName: {
1673 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1674 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1675 return;
1676 }
1677 assert(0 && "Unknown name kind.");
1678 }
1679}
1680
Douglas Gregor083a8212010-02-21 18:24:45 +00001681void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1682 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1683 FromEnd = FromDC->decls_end();
1684 From != FromEnd;
1685 ++From)
1686 Importer.Import(*From);
1687}
1688
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001689bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1690 if (To->getDefinition())
1691 return false;
1692
1693 To->startDefinition();
1694
1695 // Add base classes.
1696 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1697 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1698
1699 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1700 for (CXXRecordDecl::base_class_iterator
1701 Base1 = FromCXX->bases_begin(),
1702 FromBaseEnd = FromCXX->bases_end();
1703 Base1 != FromBaseEnd;
1704 ++Base1) {
1705 QualType T = Importer.Import(Base1->getType());
1706 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001707 return true;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001708
1709 Bases.push_back(
1710 new (Importer.getToContext())
1711 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1712 Base1->isVirtual(),
1713 Base1->isBaseOfClass(),
1714 Base1->getAccessSpecifierAsWritten(),
1715 Importer.Import(Base1->getTypeSourceInfo())));
1716 }
1717 if (!Bases.empty())
1718 ToCXX->setBases(Bases.data(), Bases.size());
1719 }
1720
1721 ImportDeclContext(From);
1722 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001723 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001724}
1725
Douglas Gregor040afae2010-11-30 19:14:50 +00001726TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1727 TemplateParameterList *Params) {
1728 llvm::SmallVector<NamedDecl *, 4> ToParams;
1729 ToParams.reserve(Params->size());
1730 for (TemplateParameterList::iterator P = Params->begin(),
1731 PEnd = Params->end();
1732 P != PEnd; ++P) {
1733 Decl *To = Importer.Import(*P);
1734 if (!To)
1735 return 0;
1736
1737 ToParams.push_back(cast<NamedDecl>(To));
1738 }
1739
1740 return TemplateParameterList::Create(Importer.getToContext(),
1741 Importer.Import(Params->getTemplateLoc()),
1742 Importer.Import(Params->getLAngleLoc()),
1743 ToParams.data(), ToParams.size(),
1744 Importer.Import(Params->getRAngleLoc()));
1745}
1746
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001747TemplateArgument
1748ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1749 switch (From.getKind()) {
1750 case TemplateArgument::Null:
1751 return TemplateArgument();
1752
1753 case TemplateArgument::Type: {
1754 QualType ToType = Importer.Import(From.getAsType());
1755 if (ToType.isNull())
1756 return TemplateArgument();
1757 return TemplateArgument(ToType);
1758 }
1759
1760 case TemplateArgument::Integral: {
1761 QualType ToType = Importer.Import(From.getIntegralType());
1762 if (ToType.isNull())
1763 return TemplateArgument();
1764 return TemplateArgument(*From.getAsIntegral(), ToType);
1765 }
1766
1767 case TemplateArgument::Declaration:
1768 if (Decl *To = Importer.Import(From.getAsDecl()))
1769 return TemplateArgument(To);
1770 return TemplateArgument();
1771
1772 case TemplateArgument::Template: {
1773 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1774 if (ToTemplate.isNull())
1775 return TemplateArgument();
1776
1777 return TemplateArgument(ToTemplate);
1778 }
1779
1780 case TemplateArgument::Expression:
1781 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1782 return TemplateArgument(ToExpr);
1783 return TemplateArgument();
1784
1785 case TemplateArgument::Pack: {
1786 llvm::SmallVector<TemplateArgument, 2> ToPack;
1787 ToPack.reserve(From.pack_size());
1788 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1789 return TemplateArgument();
1790
1791 TemplateArgument *ToArgs
1792 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1793 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1794 return TemplateArgument(ToArgs, ToPack.size());
1795 }
1796 }
1797
1798 llvm_unreachable("Invalid template argument kind");
1799 return TemplateArgument();
1800}
1801
1802bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1803 unsigned NumFromArgs,
1804 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1805 for (unsigned I = 0; I != NumFromArgs; ++I) {
1806 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1807 if (To.isNull() && !FromArgs[I].isNull())
1808 return true;
1809
1810 ToArgs.push_back(To);
1811 }
1812
1813 return false;
1814}
1815
Douglas Gregor96a01b42010-02-11 00:48:18 +00001816bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001817 RecordDecl *ToRecord) {
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(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001822}
1823
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001824bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001825 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001826 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001827 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001828 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001829}
1830
Douglas Gregor040afae2010-11-30 19:14:50 +00001831bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1832 ClassTemplateDecl *To) {
1833 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1834 Importer.getToContext(),
1835 Importer.getNonEquivalentDecls());
1836 return Ctx.IsStructurallyEquivalent(From, To);
1837}
1838
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001839Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001840 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001841 << D->getDeclKindName();
1842 return 0;
1843}
1844
Douglas Gregor788c62d2010-02-21 18:26:36 +00001845Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1846 // Import the major distinguishing characteristics of this namespace.
1847 DeclContext *DC, *LexicalDC;
1848 DeclarationName Name;
1849 SourceLocation Loc;
1850 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1851 return 0;
1852
1853 NamespaceDecl *MergeWithNamespace = 0;
1854 if (!Name) {
1855 // This is an anonymous namespace. Adopt an existing anonymous
1856 // namespace if we can.
1857 // FIXME: Not testable.
1858 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1859 MergeWithNamespace = TU->getAnonymousNamespace();
1860 else
1861 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1862 } else {
1863 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1864 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1865 Lookup.first != Lookup.second;
1866 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001867 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001868 continue;
1869
1870 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1871 MergeWithNamespace = FoundNS;
1872 ConflictingDecls.clear();
1873 break;
1874 }
1875
1876 ConflictingDecls.push_back(*Lookup.first);
1877 }
1878
1879 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001880 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001881 ConflictingDecls.data(),
1882 ConflictingDecls.size());
1883 }
1884 }
1885
1886 // Create the "to" namespace, if needed.
1887 NamespaceDecl *ToNamespace = MergeWithNamespace;
1888 if (!ToNamespace) {
1889 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1890 Name.getAsIdentifierInfo());
1891 ToNamespace->setLexicalDeclContext(LexicalDC);
1892 LexicalDC->addDecl(ToNamespace);
1893
1894 // If this is an anonymous namespace, register it as the anonymous
1895 // namespace within its context.
1896 if (!Name) {
1897 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1898 TU->setAnonymousNamespace(ToNamespace);
1899 else
1900 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1901 }
1902 }
1903 Importer.Imported(D, ToNamespace);
1904
1905 ImportDeclContext(D);
1906
1907 return ToNamespace;
1908}
1909
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001910Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1911 // Import the major distinguishing characteristics of this typedef.
1912 DeclContext *DC, *LexicalDC;
1913 DeclarationName Name;
1914 SourceLocation Loc;
1915 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1916 return 0;
1917
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001918 // If this typedef is not in block scope, determine whether we've
1919 // seen a typedef with the same name (that we can merge with) or any
1920 // other entity by that name (which name lookup could conflict with).
1921 if (!DC->isFunctionOrMethod()) {
1922 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1923 unsigned IDNS = Decl::IDNS_Ordinary;
1924 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1925 Lookup.first != Lookup.second;
1926 ++Lookup.first) {
1927 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1928 continue;
1929 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001930 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1931 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001932 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001933 }
1934
1935 ConflictingDecls.push_back(*Lookup.first);
1936 }
1937
1938 if (!ConflictingDecls.empty()) {
1939 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1940 ConflictingDecls.data(),
1941 ConflictingDecls.size());
1942 if (!Name)
1943 return 0;
1944 }
1945 }
1946
Douglas Gregorea35d112010-02-15 23:54:17 +00001947 // Import the underlying type of this typedef;
1948 QualType T = Importer.Import(D->getUnderlyingType());
1949 if (T.isNull())
1950 return 0;
1951
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001952 // Create the new typedef node.
1953 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1954 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1955 Loc, Name.getAsIdentifierInfo(),
1956 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001957 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001958 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001959 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001960 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001961
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001962 return ToTypedef;
1963}
1964
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001965Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1966 // Import the major distinguishing characteristics of this enum.
1967 DeclContext *DC, *LexicalDC;
1968 DeclarationName Name;
1969 SourceLocation Loc;
1970 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1971 return 0;
1972
1973 // Figure out what enum name we're looking for.
1974 unsigned IDNS = Decl::IDNS_Tag;
1975 DeclarationName SearchName = Name;
1976 if (!SearchName && D->getTypedefForAnonDecl()) {
1977 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1978 IDNS = Decl::IDNS_Ordinary;
1979 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1980 IDNS |= Decl::IDNS_Ordinary;
1981
1982 // We may already have an enum of the same name; try to find and match it.
1983 if (!DC->isFunctionOrMethod() && SearchName) {
1984 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1985 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1986 Lookup.first != Lookup.second;
1987 ++Lookup.first) {
1988 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1989 continue;
1990
1991 Decl *Found = *Lookup.first;
1992 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1993 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1994 Found = Tag->getDecl();
1995 }
1996
1997 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001998 if (IsStructuralMatch(D, FoundEnum))
1999 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002000 }
2001
2002 ConflictingDecls.push_back(*Lookup.first);
2003 }
2004
2005 if (!ConflictingDecls.empty()) {
2006 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2007 ConflictingDecls.data(),
2008 ConflictingDecls.size());
2009 }
2010 }
2011
2012 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002013 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002014 Name.getAsIdentifierInfo(),
2015 Importer.Import(D->getTagKeywordLoc()), 0,
2016 D->isScoped(), D->isScopedUsingClassTag(),
2017 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002018 // Import the qualifier, if any.
2019 if (D->getQualifier()) {
2020 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2021 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2022 D2->setQualifierInfo(NNS, NNSRange);
2023 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002024 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002025 D2->setLexicalDeclContext(LexicalDC);
2026 Importer.Imported(D, D2);
2027 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002028
2029 // Import the integer type.
2030 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2031 if (ToIntegerType.isNull())
2032 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002033 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002034
2035 // Import the definition
2036 if (D->isDefinition()) {
2037 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2038 if (T.isNull())
2039 return 0;
2040
2041 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2042 if (ToPromotionType.isNull())
2043 return 0;
2044
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002045 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002046 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002047
2048 // FIXME: we might need to merge the number of positive or negative bits
2049 // if the enumerator lists don't match.
2050 D2->completeDefinition(T, ToPromotionType,
2051 D->getNumPositiveBits(),
2052 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002053 }
2054
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002055 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002056}
2057
Douglas Gregor96a01b42010-02-11 00:48:18 +00002058Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2059 // If this record has a definition in the translation unit we're coming from,
2060 // but this particular declaration is not that definition, import the
2061 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002062 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002063 if (Definition && Definition != D) {
2064 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002065 if (!ImportedDef)
2066 return 0;
2067
2068 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002069 }
2070
2071 // Import the major distinguishing characteristics of this record.
2072 DeclContext *DC, *LexicalDC;
2073 DeclarationName Name;
2074 SourceLocation Loc;
2075 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2076 return 0;
2077
2078 // Figure out what structure name we're looking for.
2079 unsigned IDNS = Decl::IDNS_Tag;
2080 DeclarationName SearchName = Name;
2081 if (!SearchName && D->getTypedefForAnonDecl()) {
2082 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2083 IDNS = Decl::IDNS_Ordinary;
2084 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2085 IDNS |= Decl::IDNS_Ordinary;
2086
2087 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002088 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002089 if (!DC->isFunctionOrMethod() && SearchName) {
2090 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2091 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2092 Lookup.first != Lookup.second;
2093 ++Lookup.first) {
2094 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2095 continue;
2096
2097 Decl *Found = *Lookup.first;
2098 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2099 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2100 Found = Tag->getDecl();
2101 }
2102
2103 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002104 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2105 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2106 // The record types structurally match, or the "from" translation
2107 // unit only had a forward declaration anyway; call it the same
2108 // function.
2109 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002110 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002111 }
2112 } else {
2113 // We have a forward declaration of this type, so adopt that forward
2114 // declaration rather than building a new one.
2115 AdoptDecl = FoundRecord;
2116 continue;
2117 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002118 }
2119
2120 ConflictingDecls.push_back(*Lookup.first);
2121 }
2122
2123 if (!ConflictingDecls.empty()) {
2124 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2125 ConflictingDecls.data(),
2126 ConflictingDecls.size());
2127 }
2128 }
2129
2130 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002131 RecordDecl *D2 = AdoptDecl;
2132 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002133 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002134 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002135 D->getTagKind(),
2136 DC, Loc,
2137 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002138 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002139 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002140 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002141 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002142 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002143 DC, Loc,
2144 Name.getAsIdentifierInfo(),
2145 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002146 }
John McCallb6217662010-03-15 10:12:16 +00002147 // Import the qualifier, if any.
2148 if (D->getQualifier()) {
2149 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2150 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2151 D2->setQualifierInfo(NNS, NNSRange);
2152 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002153 D2->setLexicalDeclContext(LexicalDC);
2154 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002155 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002156
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002157 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002158
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002159 if (D->isDefinition() && ImportDefinition(D, D2))
2160 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002161
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002162 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002163}
2164
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002165Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2166 // Import the major distinguishing characteristics of this enumerator.
2167 DeclContext *DC, *LexicalDC;
2168 DeclarationName Name;
2169 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002170 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002171 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002172
2173 QualType T = Importer.Import(D->getType());
2174 if (T.isNull())
2175 return 0;
2176
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002177 // Determine whether there are any other declarations with the same name and
2178 // in the same context.
2179 if (!LexicalDC->isFunctionOrMethod()) {
2180 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2181 unsigned IDNS = Decl::IDNS_Ordinary;
2182 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2183 Lookup.first != Lookup.second;
2184 ++Lookup.first) {
2185 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2186 continue;
2187
2188 ConflictingDecls.push_back(*Lookup.first);
2189 }
2190
2191 if (!ConflictingDecls.empty()) {
2192 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2193 ConflictingDecls.data(),
2194 ConflictingDecls.size());
2195 if (!Name)
2196 return 0;
2197 }
2198 }
2199
2200 Expr *Init = Importer.Import(D->getInitExpr());
2201 if (D->getInitExpr() && !Init)
2202 return 0;
2203
2204 EnumConstantDecl *ToEnumerator
2205 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2206 Name.getAsIdentifierInfo(), T,
2207 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002208 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002209 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002210 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002211 LexicalDC->addDecl(ToEnumerator);
2212 return ToEnumerator;
2213}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002214
Douglas Gregora404ea62010-02-10 19:54:31 +00002215Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2216 // Import the major distinguishing characteristics of this function.
2217 DeclContext *DC, *LexicalDC;
2218 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002219 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002220 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002221 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002222
Douglas Gregora404ea62010-02-10 19:54:31 +00002223 // Try to find a function in our own ("to") context with the same name, same
2224 // type, and in the same context as the function we're importing.
2225 if (!LexicalDC->isFunctionOrMethod()) {
2226 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2227 unsigned IDNS = Decl::IDNS_Ordinary;
2228 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2229 Lookup.first != Lookup.second;
2230 ++Lookup.first) {
2231 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2232 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002233
Douglas Gregora404ea62010-02-10 19:54:31 +00002234 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2235 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2236 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002237 if (Importer.IsStructurallyEquivalent(D->getType(),
2238 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002239 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002240 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002241 }
2242
2243 // FIXME: Check for overloading more carefully, e.g., by boosting
2244 // Sema::IsOverload out to the AST library.
2245
2246 // Function overloading is okay in C++.
2247 if (Importer.getToContext().getLangOptions().CPlusPlus)
2248 continue;
2249
2250 // Complain about inconsistent function types.
2251 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002252 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002253 Importer.ToDiag(FoundFunction->getLocation(),
2254 diag::note_odr_value_here)
2255 << FoundFunction->getType();
2256 }
2257 }
2258
2259 ConflictingDecls.push_back(*Lookup.first);
2260 }
2261
2262 if (!ConflictingDecls.empty()) {
2263 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2264 ConflictingDecls.data(),
2265 ConflictingDecls.size());
2266 if (!Name)
2267 return 0;
2268 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002269 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002270
Abramo Bagnara25777432010-08-11 22:01:17 +00002271 DeclarationNameInfo NameInfo(Name, Loc);
2272 // Import additional name location/type info.
2273 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2274
Douglas Gregorea35d112010-02-15 23:54:17 +00002275 // Import the type.
2276 QualType T = Importer.Import(D->getType());
2277 if (T.isNull())
2278 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002279
2280 // Import the function parameters.
2281 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2282 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2283 P != PEnd; ++P) {
2284 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2285 if (!ToP)
2286 return 0;
2287
2288 Parameters.push_back(ToP);
2289 }
2290
2291 // Create the imported function.
2292 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002293 FunctionDecl *ToFunction = 0;
2294 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2295 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2296 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002297 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002298 FromConstructor->isExplicit(),
2299 D->isInlineSpecified(),
2300 D->isImplicit());
2301 } else if (isa<CXXDestructorDecl>(D)) {
2302 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2303 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002304 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002305 D->isInlineSpecified(),
2306 D->isImplicit());
2307 } else if (CXXConversionDecl *FromConversion
2308 = dyn_cast<CXXConversionDecl>(D)) {
2309 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2310 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002311 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002312 D->isInlineSpecified(),
2313 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002314 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2315 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2316 cast<CXXRecordDecl>(DC),
2317 NameInfo, T, TInfo,
2318 Method->isStatic(),
2319 Method->getStorageClassAsWritten(),
2320 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002321 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002322 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2323 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002324 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002325 D->isInlineSpecified(),
2326 D->hasWrittenPrototype());
2327 }
John McCallb6217662010-03-15 10:12:16 +00002328
2329 // Import the qualifier, if any.
2330 if (D->getQualifier()) {
2331 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2332 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2333 ToFunction->setQualifierInfo(NNS, NNSRange);
2334 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002335 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002336 ToFunction->setLexicalDeclContext(LexicalDC);
2337 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002338
Douglas Gregora404ea62010-02-10 19:54:31 +00002339 // Set the parameters.
2340 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002341 Parameters[I]->setOwningFunction(ToFunction);
2342 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002343 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002344 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002345
2346 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002347
2348 // Add this function to the lexical context.
2349 LexicalDC->addDecl(ToFunction);
2350
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002351 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002352}
2353
Douglas Gregorc144f352010-02-21 18:29:16 +00002354Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2355 return VisitFunctionDecl(D);
2356}
2357
2358Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2359 return VisitCXXMethodDecl(D);
2360}
2361
2362Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2363 return VisitCXXMethodDecl(D);
2364}
2365
2366Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2367 return VisitCXXMethodDecl(D);
2368}
2369
Douglas Gregor96a01b42010-02-11 00:48:18 +00002370Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2371 // Import the major distinguishing characteristics of a variable.
2372 DeclContext *DC, *LexicalDC;
2373 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002374 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002375 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2376 return 0;
2377
2378 // Import the type.
2379 QualType T = Importer.Import(D->getType());
2380 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002381 return 0;
2382
2383 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2384 Expr *BitWidth = Importer.Import(D->getBitWidth());
2385 if (!BitWidth && D->getBitWidth())
2386 return 0;
2387
2388 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2389 Loc, Name.getAsIdentifierInfo(),
2390 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002391 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002392 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002393 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002394 LexicalDC->addDecl(ToField);
2395 return ToField;
2396}
2397
Francois Pichet87c2e122010-11-21 06:08:52 +00002398Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2399 // Import the major distinguishing characteristics of a variable.
2400 DeclContext *DC, *LexicalDC;
2401 DeclarationName Name;
2402 SourceLocation Loc;
2403 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2404 return 0;
2405
2406 // Import the type.
2407 QualType T = Importer.Import(D->getType());
2408 if (T.isNull())
2409 return 0;
2410
2411 NamedDecl **NamedChain =
2412 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2413
2414 unsigned i = 0;
2415 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2416 PE = D->chain_end(); PI != PE; ++PI) {
2417 Decl* D = Importer.Import(*PI);
2418 if (!D)
2419 return 0;
2420 NamedChain[i++] = cast<NamedDecl>(D);
2421 }
2422
2423 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2424 Importer.getToContext(), DC,
2425 Loc, Name.getAsIdentifierInfo(), T,
2426 NamedChain, D->getChainingSize());
2427 ToIndirectField->setAccess(D->getAccess());
2428 ToIndirectField->setLexicalDeclContext(LexicalDC);
2429 Importer.Imported(D, ToIndirectField);
2430 LexicalDC->addDecl(ToIndirectField);
2431 return ToIndirectField;
2432}
2433
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002434Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2435 // Import the major distinguishing characteristics of an ivar.
2436 DeclContext *DC, *LexicalDC;
2437 DeclarationName Name;
2438 SourceLocation Loc;
2439 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2440 return 0;
2441
2442 // Determine whether we've already imported this ivar
2443 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2444 Lookup.first != Lookup.second;
2445 ++Lookup.first) {
2446 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2447 if (Importer.IsStructurallyEquivalent(D->getType(),
2448 FoundIvar->getType())) {
2449 Importer.Imported(D, FoundIvar);
2450 return FoundIvar;
2451 }
2452
2453 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2454 << Name << D->getType() << FoundIvar->getType();
2455 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2456 << FoundIvar->getType();
2457 return 0;
2458 }
2459 }
2460
2461 // Import the type.
2462 QualType T = Importer.Import(D->getType());
2463 if (T.isNull())
2464 return 0;
2465
2466 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2467 Expr *BitWidth = Importer.Import(D->getBitWidth());
2468 if (!BitWidth && D->getBitWidth())
2469 return 0;
2470
Daniel Dunbara0654922010-04-02 20:10:03 +00002471 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2472 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002473 Loc, Name.getAsIdentifierInfo(),
2474 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002475 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002476 ToIvar->setLexicalDeclContext(LexicalDC);
2477 Importer.Imported(D, ToIvar);
2478 LexicalDC->addDecl(ToIvar);
2479 return ToIvar;
2480
2481}
2482
Douglas Gregora404ea62010-02-10 19:54:31 +00002483Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2484 // Import the major distinguishing characteristics of a variable.
2485 DeclContext *DC, *LexicalDC;
2486 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002487 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002488 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002489 return 0;
2490
Douglas Gregor089459a2010-02-08 21:09:39 +00002491 // Try to find a variable in our own ("to") context with the same name and
2492 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002493 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002494 VarDecl *MergeWithVar = 0;
2495 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2496 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002497 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002498 Lookup.first != Lookup.second;
2499 ++Lookup.first) {
2500 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2501 continue;
2502
2503 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2504 // We have found a variable that we may need to merge with. Check it.
2505 if (isExternalLinkage(FoundVar->getLinkage()) &&
2506 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002507 if (Importer.IsStructurallyEquivalent(D->getType(),
2508 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002509 MergeWithVar = FoundVar;
2510 break;
2511 }
2512
Douglas Gregord0145422010-02-12 17:23:39 +00002513 const ArrayType *FoundArray
2514 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2515 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002516 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002517 if (FoundArray && TArray) {
2518 if (isa<IncompleteArrayType>(FoundArray) &&
2519 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002520 // Import the type.
2521 QualType T = Importer.Import(D->getType());
2522 if (T.isNull())
2523 return 0;
2524
Douglas Gregord0145422010-02-12 17:23:39 +00002525 FoundVar->setType(T);
2526 MergeWithVar = FoundVar;
2527 break;
2528 } else if (isa<IncompleteArrayType>(TArray) &&
2529 isa<ConstantArrayType>(FoundArray)) {
2530 MergeWithVar = FoundVar;
2531 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002532 }
2533 }
2534
Douglas Gregor089459a2010-02-08 21:09:39 +00002535 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002536 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002537 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2538 << FoundVar->getType();
2539 }
2540 }
2541
2542 ConflictingDecls.push_back(*Lookup.first);
2543 }
2544
2545 if (MergeWithVar) {
2546 // An equivalent variable with external linkage has been found. Link
2547 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002548 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002549
2550 if (VarDecl *DDef = D->getDefinition()) {
2551 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2552 Importer.ToDiag(ExistingDef->getLocation(),
2553 diag::err_odr_variable_multiple_def)
2554 << Name;
2555 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2556 } else {
2557 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002558 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002559 }
2560 }
2561
2562 return MergeWithVar;
2563 }
2564
2565 if (!ConflictingDecls.empty()) {
2566 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2567 ConflictingDecls.data(),
2568 ConflictingDecls.size());
2569 if (!Name)
2570 return 0;
2571 }
2572 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002573
Douglas Gregorea35d112010-02-15 23:54:17 +00002574 // Import the type.
2575 QualType T = Importer.Import(D->getType());
2576 if (T.isNull())
2577 return 0;
2578
Douglas Gregor089459a2010-02-08 21:09:39 +00002579 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002580 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002581 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2582 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002583 D->getStorageClass(),
2584 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002585 // Import the qualifier, if any.
2586 if (D->getQualifier()) {
2587 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2588 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2589 ToVar->setQualifierInfo(NNS, NNSRange);
2590 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002591 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002592 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002593 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002594 LexicalDC->addDecl(ToVar);
2595
Douglas Gregor089459a2010-02-08 21:09:39 +00002596 // Merge the initializer.
2597 // FIXME: Can we really import any initializer? Alternatively, we could force
2598 // ourselves to import every declaration of a variable and then only use
2599 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002600 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002601
2602 // FIXME: Other bits to merge?
2603
2604 return ToVar;
2605}
2606
Douglas Gregor2cd00932010-02-17 21:22:52 +00002607Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2608 // Parameters are created in the translation unit's context, then moved
2609 // into the function declaration's context afterward.
2610 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2611
2612 // Import the name of this declaration.
2613 DeclarationName Name = Importer.Import(D->getDeclName());
2614 if (D->getDeclName() && !Name)
2615 return 0;
2616
2617 // Import the location of this declaration.
2618 SourceLocation Loc = Importer.Import(D->getLocation());
2619
2620 // Import the parameter's type.
2621 QualType T = Importer.Import(D->getType());
2622 if (T.isNull())
2623 return 0;
2624
2625 // Create the imported parameter.
2626 ImplicitParamDecl *ToParm
2627 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2628 Loc, Name.getAsIdentifierInfo(),
2629 T);
2630 return Importer.Imported(D, ToParm);
2631}
2632
Douglas Gregora404ea62010-02-10 19:54:31 +00002633Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2634 // Parameters are created in the translation unit's context, then moved
2635 // into the function declaration's context afterward.
2636 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2637
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002638 // Import the name of this declaration.
2639 DeclarationName Name = Importer.Import(D->getDeclName());
2640 if (D->getDeclName() && !Name)
2641 return 0;
2642
Douglas Gregora404ea62010-02-10 19:54:31 +00002643 // Import the location of this declaration.
2644 SourceLocation Loc = Importer.Import(D->getLocation());
2645
2646 // Import the parameter's type.
2647 QualType T = Importer.Import(D->getType());
2648 if (T.isNull())
2649 return 0;
2650
2651 // Create the imported parameter.
2652 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2653 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2654 Loc, Name.getAsIdentifierInfo(),
2655 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002656 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002657 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002658 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002659 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002660}
2661
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002662Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2663 // Import the major distinguishing characteristics of a method.
2664 DeclContext *DC, *LexicalDC;
2665 DeclarationName Name;
2666 SourceLocation Loc;
2667 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2668 return 0;
2669
2670 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2671 Lookup.first != Lookup.second;
2672 ++Lookup.first) {
2673 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2674 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2675 continue;
2676
2677 // Check return types.
2678 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2679 FoundMethod->getResultType())) {
2680 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2681 << D->isInstanceMethod() << Name
2682 << D->getResultType() << FoundMethod->getResultType();
2683 Importer.ToDiag(FoundMethod->getLocation(),
2684 diag::note_odr_objc_method_here)
2685 << D->isInstanceMethod() << Name;
2686 return 0;
2687 }
2688
2689 // Check the number of parameters.
2690 if (D->param_size() != FoundMethod->param_size()) {
2691 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2692 << D->isInstanceMethod() << Name
2693 << D->param_size() << FoundMethod->param_size();
2694 Importer.ToDiag(FoundMethod->getLocation(),
2695 diag::note_odr_objc_method_here)
2696 << D->isInstanceMethod() << Name;
2697 return 0;
2698 }
2699
2700 // Check parameter types.
2701 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2702 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2703 P != PEnd; ++P, ++FoundP) {
2704 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2705 (*FoundP)->getType())) {
2706 Importer.FromDiag((*P)->getLocation(),
2707 diag::err_odr_objc_method_param_type_inconsistent)
2708 << D->isInstanceMethod() << Name
2709 << (*P)->getType() << (*FoundP)->getType();
2710 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2711 << (*FoundP)->getType();
2712 return 0;
2713 }
2714 }
2715
2716 // Check variadic/non-variadic.
2717 // Check the number of parameters.
2718 if (D->isVariadic() != FoundMethod->isVariadic()) {
2719 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2720 << D->isInstanceMethod() << Name;
2721 Importer.ToDiag(FoundMethod->getLocation(),
2722 diag::note_odr_objc_method_here)
2723 << D->isInstanceMethod() << Name;
2724 return 0;
2725 }
2726
2727 // FIXME: Any other bits we need to merge?
2728 return Importer.Imported(D, FoundMethod);
2729 }
2730 }
2731
2732 // Import the result type.
2733 QualType ResultTy = Importer.Import(D->getResultType());
2734 if (ResultTy.isNull())
2735 return 0;
2736
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002737 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2738
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002739 ObjCMethodDecl *ToMethod
2740 = ObjCMethodDecl::Create(Importer.getToContext(),
2741 Loc,
2742 Importer.Import(D->getLocEnd()),
2743 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002744 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002745 D->isInstanceMethod(),
2746 D->isVariadic(),
2747 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002748 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002749 D->getImplementationControl());
2750
2751 // FIXME: When we decide to merge method definitions, we'll need to
2752 // deal with implicit parameters.
2753
2754 // Import the parameters
2755 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2756 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2757 FromPEnd = D->param_end();
2758 FromP != FromPEnd;
2759 ++FromP) {
2760 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2761 if (!ToP)
2762 return 0;
2763
2764 ToParams.push_back(ToP);
2765 }
2766
2767 // Set the parameters.
2768 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2769 ToParams[I]->setOwningFunction(ToMethod);
2770 ToMethod->addDecl(ToParams[I]);
2771 }
2772 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002773 ToParams.data(), ToParams.size(),
2774 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002775
2776 ToMethod->setLexicalDeclContext(LexicalDC);
2777 Importer.Imported(D, ToMethod);
2778 LexicalDC->addDecl(ToMethod);
2779 return ToMethod;
2780}
2781
Douglas Gregorb4677b62010-02-18 01:47:50 +00002782Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2783 // Import the major distinguishing characteristics of a category.
2784 DeclContext *DC, *LexicalDC;
2785 DeclarationName Name;
2786 SourceLocation Loc;
2787 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2788 return 0;
2789
2790 ObjCInterfaceDecl *ToInterface
2791 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2792 if (!ToInterface)
2793 return 0;
2794
2795 // Determine if we've already encountered this category.
2796 ObjCCategoryDecl *MergeWithCategory
2797 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2798 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2799 if (!ToCategory) {
2800 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2801 Importer.Import(D->getAtLoc()),
2802 Loc,
2803 Importer.Import(D->getCategoryNameLoc()),
2804 Name.getAsIdentifierInfo());
2805 ToCategory->setLexicalDeclContext(LexicalDC);
2806 LexicalDC->addDecl(ToCategory);
2807 Importer.Imported(D, ToCategory);
2808
2809 // Link this category into its class's category list.
2810 ToCategory->setClassInterface(ToInterface);
2811 ToCategory->insertNextClassCategory();
2812
2813 // Import protocols
2814 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2815 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2816 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2817 = D->protocol_loc_begin();
2818 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2819 FromProtoEnd = D->protocol_end();
2820 FromProto != FromProtoEnd;
2821 ++FromProto, ++FromProtoLoc) {
2822 ObjCProtocolDecl *ToProto
2823 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2824 if (!ToProto)
2825 return 0;
2826 Protocols.push_back(ToProto);
2827 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2828 }
2829
2830 // FIXME: If we're merging, make sure that the protocol list is the same.
2831 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2832 ProtocolLocs.data(), Importer.getToContext());
2833
2834 } else {
2835 Importer.Imported(D, ToCategory);
2836 }
2837
2838 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002839 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002840
2841 // If we have an implementation, import it as well.
2842 if (D->getImplementation()) {
2843 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002844 = cast_or_null<ObjCCategoryImplDecl>(
2845 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002846 if (!Impl)
2847 return 0;
2848
2849 ToCategory->setImplementation(Impl);
2850 }
2851
2852 return ToCategory;
2853}
2854
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002855Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002856 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002857 DeclContext *DC, *LexicalDC;
2858 DeclarationName Name;
2859 SourceLocation Loc;
2860 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2861 return 0;
2862
2863 ObjCProtocolDecl *MergeWithProtocol = 0;
2864 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2865 Lookup.first != Lookup.second;
2866 ++Lookup.first) {
2867 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2868 continue;
2869
2870 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2871 break;
2872 }
2873
2874 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2875 if (!ToProto || ToProto->isForwardDecl()) {
2876 if (!ToProto) {
2877 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2878 Name.getAsIdentifierInfo());
2879 ToProto->setForwardDecl(D->isForwardDecl());
2880 ToProto->setLexicalDeclContext(LexicalDC);
2881 LexicalDC->addDecl(ToProto);
2882 }
2883 Importer.Imported(D, ToProto);
2884
2885 // Import protocols
2886 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2887 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2888 ObjCProtocolDecl::protocol_loc_iterator
2889 FromProtoLoc = D->protocol_loc_begin();
2890 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2891 FromProtoEnd = D->protocol_end();
2892 FromProto != FromProtoEnd;
2893 ++FromProto, ++FromProtoLoc) {
2894 ObjCProtocolDecl *ToProto
2895 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2896 if (!ToProto)
2897 return 0;
2898 Protocols.push_back(ToProto);
2899 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2900 }
2901
2902 // FIXME: If we're merging, make sure that the protocol list is the same.
2903 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2904 ProtocolLocs.data(), Importer.getToContext());
2905 } else {
2906 Importer.Imported(D, ToProto);
2907 }
2908
Douglas Gregorb4677b62010-02-18 01:47:50 +00002909 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002910 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002911
2912 return ToProto;
2913}
2914
Douglas Gregora12d2942010-02-16 01:20:57 +00002915Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2916 // Import the major distinguishing characteristics of an @interface.
2917 DeclContext *DC, *LexicalDC;
2918 DeclarationName Name;
2919 SourceLocation Loc;
2920 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2921 return 0;
2922
2923 ObjCInterfaceDecl *MergeWithIface = 0;
2924 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2925 Lookup.first != Lookup.second;
2926 ++Lookup.first) {
2927 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2928 continue;
2929
2930 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2931 break;
2932 }
2933
2934 ObjCInterfaceDecl *ToIface = MergeWithIface;
2935 if (!ToIface || ToIface->isForwardDecl()) {
2936 if (!ToIface) {
2937 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2938 DC, Loc,
2939 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002940 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002941 D->isForwardDecl(),
2942 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002943 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002944 ToIface->setLexicalDeclContext(LexicalDC);
2945 LexicalDC->addDecl(ToIface);
2946 }
2947 Importer.Imported(D, ToIface);
2948
Douglas Gregora12d2942010-02-16 01:20:57 +00002949 if (D->getSuperClass()) {
2950 ObjCInterfaceDecl *Super
2951 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2952 if (!Super)
2953 return 0;
2954
2955 ToIface->setSuperClass(Super);
2956 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2957 }
2958
2959 // Import protocols
2960 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2961 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2962 ObjCInterfaceDecl::protocol_loc_iterator
2963 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002964
2965 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00002966 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2967 FromProtoEnd = D->protocol_end();
2968 FromProto != FromProtoEnd;
2969 ++FromProto, ++FromProtoLoc) {
2970 ObjCProtocolDecl *ToProto
2971 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2972 if (!ToProto)
2973 return 0;
2974 Protocols.push_back(ToProto);
2975 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2976 }
2977
2978 // FIXME: If we're merging, make sure that the protocol list is the same.
2979 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2980 ProtocolLocs.data(), Importer.getToContext());
2981
Douglas Gregora12d2942010-02-16 01:20:57 +00002982 // Import @end range
2983 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2984 } else {
2985 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002986
2987 // Check for consistency of superclasses.
2988 DeclarationName FromSuperName, ToSuperName;
2989 if (D->getSuperClass())
2990 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2991 if (ToIface->getSuperClass())
2992 ToSuperName = ToIface->getSuperClass()->getDeclName();
2993 if (FromSuperName != ToSuperName) {
2994 Importer.ToDiag(ToIface->getLocation(),
2995 diag::err_odr_objc_superclass_inconsistent)
2996 << ToIface->getDeclName();
2997 if (ToIface->getSuperClass())
2998 Importer.ToDiag(ToIface->getSuperClassLoc(),
2999 diag::note_odr_objc_superclass)
3000 << ToIface->getSuperClass()->getDeclName();
3001 else
3002 Importer.ToDiag(ToIface->getLocation(),
3003 diag::note_odr_objc_missing_superclass);
3004 if (D->getSuperClass())
3005 Importer.FromDiag(D->getSuperClassLoc(),
3006 diag::note_odr_objc_superclass)
3007 << D->getSuperClass()->getDeclName();
3008 else
3009 Importer.FromDiag(D->getLocation(),
3010 diag::note_odr_objc_missing_superclass);
3011 return 0;
3012 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003013 }
3014
Douglas Gregorb4677b62010-02-18 01:47:50 +00003015 // Import categories. When the categories themselves are imported, they'll
3016 // hook themselves into this interface.
3017 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3018 FromCat = FromCat->getNextClassCategory())
3019 Importer.Import(FromCat);
3020
Douglas Gregora12d2942010-02-16 01:20:57 +00003021 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003022 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003023
3024 // If we have an @implementation, import it as well.
3025 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003026 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3027 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003028 if (!Impl)
3029 return 0;
3030
3031 ToIface->setImplementation(Impl);
3032 }
3033
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003034 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003035}
3036
Douglas Gregor3daef292010-12-07 15:32:12 +00003037Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3038 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3039 Importer.Import(D->getCategoryDecl()));
3040 if (!Category)
3041 return 0;
3042
3043 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3044 if (!ToImpl) {
3045 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3046 if (!DC)
3047 return 0;
3048
3049 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3050 Importer.Import(D->getLocation()),
3051 Importer.Import(D->getIdentifier()),
3052 Category->getClassInterface());
3053
3054 DeclContext *LexicalDC = DC;
3055 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3056 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3057 if (!LexicalDC)
3058 return 0;
3059
3060 ToImpl->setLexicalDeclContext(LexicalDC);
3061 }
3062
3063 LexicalDC->addDecl(ToImpl);
3064 Category->setImplementation(ToImpl);
3065 }
3066
3067 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003068 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003069 return ToImpl;
3070}
3071
Douglas Gregordd182ff2010-12-07 01:26:03 +00003072Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3073 // Find the corresponding interface.
3074 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3075 Importer.Import(D->getClassInterface()));
3076 if (!Iface)
3077 return 0;
3078
3079 // Import the superclass, if any.
3080 ObjCInterfaceDecl *Super = 0;
3081 if (D->getSuperClass()) {
3082 Super = cast_or_null<ObjCInterfaceDecl>(
3083 Importer.Import(D->getSuperClass()));
3084 if (!Super)
3085 return 0;
3086 }
3087
3088 ObjCImplementationDecl *Impl = Iface->getImplementation();
3089 if (!Impl) {
3090 // We haven't imported an implementation yet. Create a new @implementation
3091 // now.
3092 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3093 Importer.ImportContext(D->getDeclContext()),
3094 Importer.Import(D->getLocation()),
3095 Iface, Super);
3096
3097 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3098 DeclContext *LexicalDC
3099 = Importer.ImportContext(D->getLexicalDeclContext());
3100 if (!LexicalDC)
3101 return 0;
3102 Impl->setLexicalDeclContext(LexicalDC);
3103 }
3104
3105 // Associate the implementation with the class it implements.
3106 Iface->setImplementation(Impl);
3107 Importer.Imported(D, Iface->getImplementation());
3108 } else {
3109 Importer.Imported(D, Iface->getImplementation());
3110
3111 // Verify that the existing @implementation has the same superclass.
3112 if ((Super && !Impl->getSuperClass()) ||
3113 (!Super && Impl->getSuperClass()) ||
3114 (Super && Impl->getSuperClass() &&
3115 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3116 Importer.ToDiag(Impl->getLocation(),
3117 diag::err_odr_objc_superclass_inconsistent)
3118 << Iface->getDeclName();
3119 // FIXME: It would be nice to have the location of the superclass
3120 // below.
3121 if (Impl->getSuperClass())
3122 Importer.ToDiag(Impl->getLocation(),
3123 diag::note_odr_objc_superclass)
3124 << Impl->getSuperClass()->getDeclName();
3125 else
3126 Importer.ToDiag(Impl->getLocation(),
3127 diag::note_odr_objc_missing_superclass);
3128 if (D->getSuperClass())
3129 Importer.FromDiag(D->getLocation(),
3130 diag::note_odr_objc_superclass)
3131 << D->getSuperClass()->getDeclName();
3132 else
3133 Importer.FromDiag(D->getLocation(),
3134 diag::note_odr_objc_missing_superclass);
3135 return 0;
3136 }
3137 }
3138
3139 // Import all of the members of this @implementation.
3140 ImportDeclContext(D);
3141
3142 return Impl;
3143}
3144
Douglas Gregore3261622010-02-17 18:02:10 +00003145Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3146 // Import the major distinguishing characteristics of an @property.
3147 DeclContext *DC, *LexicalDC;
3148 DeclarationName Name;
3149 SourceLocation Loc;
3150 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3151 return 0;
3152
3153 // Check whether we have already imported this property.
3154 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3155 Lookup.first != Lookup.second;
3156 ++Lookup.first) {
3157 if (ObjCPropertyDecl *FoundProp
3158 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3159 // Check property types.
3160 if (!Importer.IsStructurallyEquivalent(D->getType(),
3161 FoundProp->getType())) {
3162 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3163 << Name << D->getType() << FoundProp->getType();
3164 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3165 << FoundProp->getType();
3166 return 0;
3167 }
3168
3169 // FIXME: Check property attributes, getters, setters, etc.?
3170
3171 // Consider these properties to be equivalent.
3172 Importer.Imported(D, FoundProp);
3173 return FoundProp;
3174 }
3175 }
3176
3177 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003178 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3179 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003180 return 0;
3181
3182 // Create the new property.
3183 ObjCPropertyDecl *ToProperty
3184 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3185 Name.getAsIdentifierInfo(),
3186 Importer.Import(D->getAtLoc()),
3187 T,
3188 D->getPropertyImplementation());
3189 Importer.Imported(D, ToProperty);
3190 ToProperty->setLexicalDeclContext(LexicalDC);
3191 LexicalDC->addDecl(ToProperty);
3192
3193 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003194 ToProperty->setPropertyAttributesAsWritten(
3195 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003196 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3197 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3198 ToProperty->setGetterMethodDecl(
3199 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3200 ToProperty->setSetterMethodDecl(
3201 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3202 ToProperty->setPropertyIvarDecl(
3203 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3204 return ToProperty;
3205}
3206
Douglas Gregor954e0c72010-12-07 18:32:03 +00003207Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3208 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3209 Importer.Import(D->getPropertyDecl()));
3210 if (!Property)
3211 return 0;
3212
3213 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3214 if (!DC)
3215 return 0;
3216
3217 // Import the lexical declaration context.
3218 DeclContext *LexicalDC = DC;
3219 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3220 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3221 if (!LexicalDC)
3222 return 0;
3223 }
3224
3225 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3226 if (!InImpl)
3227 return 0;
3228
3229 // Import the ivar (for an @synthesize).
3230 ObjCIvarDecl *Ivar = 0;
3231 if (D->getPropertyIvarDecl()) {
3232 Ivar = cast_or_null<ObjCIvarDecl>(
3233 Importer.Import(D->getPropertyIvarDecl()));
3234 if (!Ivar)
3235 return 0;
3236 }
3237
3238 ObjCPropertyImplDecl *ToImpl
3239 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3240 if (!ToImpl) {
3241 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3242 Importer.Import(D->getLocStart()),
3243 Importer.Import(D->getLocation()),
3244 Property,
3245 D->getPropertyImplementation(),
3246 Ivar,
3247 Importer.Import(D->getPropertyIvarDeclLoc()));
3248 ToImpl->setLexicalDeclContext(LexicalDC);
3249 Importer.Imported(D, ToImpl);
3250 LexicalDC->addDecl(ToImpl);
3251 } else {
3252 // Check that we have the same kind of property implementation (@synthesize
3253 // vs. @dynamic).
3254 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3255 Importer.ToDiag(ToImpl->getLocation(),
3256 diag::err_odr_objc_property_impl_kind_inconsistent)
3257 << Property->getDeclName()
3258 << (ToImpl->getPropertyImplementation()
3259 == ObjCPropertyImplDecl::Dynamic);
3260 Importer.FromDiag(D->getLocation(),
3261 diag::note_odr_objc_property_impl_kind)
3262 << D->getPropertyDecl()->getDeclName()
3263 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3264 return 0;
3265 }
3266
3267 // For @synthesize, check that we have the same
3268 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3269 Ivar != ToImpl->getPropertyIvarDecl()) {
3270 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3271 diag::err_odr_objc_synthesize_ivar_inconsistent)
3272 << Property->getDeclName()
3273 << ToImpl->getPropertyIvarDecl()->getDeclName()
3274 << Ivar->getDeclName();
3275 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3276 diag::note_odr_objc_synthesize_ivar_here)
3277 << D->getPropertyIvarDecl()->getDeclName();
3278 return 0;
3279 }
3280
3281 // Merge the existing implementation with the new implementation.
3282 Importer.Imported(D, ToImpl);
3283 }
3284
3285 return ToImpl;
3286}
3287
Douglas Gregor2b785022010-02-18 02:12:22 +00003288Decl *
3289ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3290 // Import the context of this declaration.
3291 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3292 if (!DC)
3293 return 0;
3294
3295 DeclContext *LexicalDC = DC;
3296 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3297 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3298 if (!LexicalDC)
3299 return 0;
3300 }
3301
3302 // Import the location of this declaration.
3303 SourceLocation Loc = Importer.Import(D->getLocation());
3304
3305 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3306 llvm::SmallVector<SourceLocation, 4> Locations;
3307 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3308 = D->protocol_loc_begin();
3309 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3310 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3311 FromProto != FromProtoEnd;
3312 ++FromProto, ++FromProtoLoc) {
3313 ObjCProtocolDecl *ToProto
3314 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3315 if (!ToProto)
3316 continue;
3317
3318 Protocols.push_back(ToProto);
3319 Locations.push_back(Importer.Import(*FromProtoLoc));
3320 }
3321
3322 ObjCForwardProtocolDecl *ToForward
3323 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3324 Protocols.data(), Protocols.size(),
3325 Locations.data());
3326 ToForward->setLexicalDeclContext(LexicalDC);
3327 LexicalDC->addDecl(ToForward);
3328 Importer.Imported(D, ToForward);
3329 return ToForward;
3330}
3331
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003332Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3333 // Import the context of this declaration.
3334 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3335 if (!DC)
3336 return 0;
3337
3338 DeclContext *LexicalDC = DC;
3339 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3340 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3341 if (!LexicalDC)
3342 return 0;
3343 }
3344
3345 // Import the location of this declaration.
3346 SourceLocation Loc = Importer.Import(D->getLocation());
3347
3348 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3349 llvm::SmallVector<SourceLocation, 4> Locations;
3350 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3351 From != FromEnd; ++From) {
3352 ObjCInterfaceDecl *ToIface
3353 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3354 if (!ToIface)
3355 continue;
3356
3357 Interfaces.push_back(ToIface);
3358 Locations.push_back(Importer.Import(From->getLocation()));
3359 }
3360
3361 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3362 Loc,
3363 Interfaces.data(),
3364 Locations.data(),
3365 Interfaces.size());
3366 ToClass->setLexicalDeclContext(LexicalDC);
3367 LexicalDC->addDecl(ToClass);
3368 Importer.Imported(D, ToClass);
3369 return ToClass;
3370}
3371
Douglas Gregor040afae2010-11-30 19:14:50 +00003372Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3373 // For template arguments, we adopt the translation unit as our declaration
3374 // context. This context will be fixed when the actual template declaration
3375 // is created.
3376
3377 // FIXME: Import default argument.
3378 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3379 Importer.getToContext().getTranslationUnitDecl(),
3380 Importer.Import(D->getLocation()),
3381 D->getDepth(),
3382 D->getIndex(),
3383 Importer.Import(D->getIdentifier()),
3384 D->wasDeclaredWithTypename(),
3385 D->isParameterPack());
3386}
3387
3388Decl *
3389ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3390 // Import the name of this declaration.
3391 DeclarationName Name = Importer.Import(D->getDeclName());
3392 if (D->getDeclName() && !Name)
3393 return 0;
3394
3395 // Import the location of this declaration.
3396 SourceLocation Loc = Importer.Import(D->getLocation());
3397
3398 // Import the type of this declaration.
3399 QualType T = Importer.Import(D->getType());
3400 if (T.isNull())
3401 return 0;
3402
3403 // Import type-source information.
3404 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3405 if (D->getTypeSourceInfo() && !TInfo)
3406 return 0;
3407
3408 // FIXME: Import default argument.
3409
3410 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3411 Importer.getToContext().getTranslationUnitDecl(),
3412 Loc, D->getDepth(), D->getPosition(),
3413 Name.getAsIdentifierInfo(),
3414 T, TInfo);
3415}
3416
3417Decl *
3418ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3419 // Import the name of this declaration.
3420 DeclarationName Name = Importer.Import(D->getDeclName());
3421 if (D->getDeclName() && !Name)
3422 return 0;
3423
3424 // Import the location of this declaration.
3425 SourceLocation Loc = Importer.Import(D->getLocation());
3426
3427 // Import template parameters.
3428 TemplateParameterList *TemplateParams
3429 = ImportTemplateParameterList(D->getTemplateParameters());
3430 if (!TemplateParams)
3431 return 0;
3432
3433 // FIXME: Import default argument.
3434
3435 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3436 Importer.getToContext().getTranslationUnitDecl(),
3437 Loc, D->getDepth(), D->getPosition(),
3438 Name.getAsIdentifierInfo(),
3439 TemplateParams);
3440}
3441
3442Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3443 // If this record has a definition in the translation unit we're coming from,
3444 // but this particular declaration is not that definition, import the
3445 // definition and map to that.
3446 CXXRecordDecl *Definition
3447 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3448 if (Definition && Definition != D->getTemplatedDecl()) {
3449 Decl *ImportedDef
3450 = Importer.Import(Definition->getDescribedClassTemplate());
3451 if (!ImportedDef)
3452 return 0;
3453
3454 return Importer.Imported(D, ImportedDef);
3455 }
3456
3457 // Import the major distinguishing characteristics of this class template.
3458 DeclContext *DC, *LexicalDC;
3459 DeclarationName Name;
3460 SourceLocation Loc;
3461 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3462 return 0;
3463
3464 // We may already have a template of the same name; try to find and match it.
3465 if (!DC->isFunctionOrMethod()) {
3466 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3467 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3468 Lookup.first != Lookup.second;
3469 ++Lookup.first) {
3470 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3471 continue;
3472
3473 Decl *Found = *Lookup.first;
3474 if (ClassTemplateDecl *FoundTemplate
3475 = dyn_cast<ClassTemplateDecl>(Found)) {
3476 if (IsStructuralMatch(D, FoundTemplate)) {
3477 // The class templates structurally match; call it the same template.
3478 // FIXME: We may be filling in a forward declaration here. Handle
3479 // this case!
3480 Importer.Imported(D->getTemplatedDecl(),
3481 FoundTemplate->getTemplatedDecl());
3482 return Importer.Imported(D, FoundTemplate);
3483 }
3484 }
3485
3486 ConflictingDecls.push_back(*Lookup.first);
3487 }
3488
3489 if (!ConflictingDecls.empty()) {
3490 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3491 ConflictingDecls.data(),
3492 ConflictingDecls.size());
3493 }
3494
3495 if (!Name)
3496 return 0;
3497 }
3498
3499 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3500
3501 // Create the declaration that is being templated.
3502 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3503 DTemplated->getTagKind(),
3504 DC,
3505 Importer.Import(DTemplated->getLocation()),
3506 Name.getAsIdentifierInfo(),
3507 Importer.Import(DTemplated->getTagKeywordLoc()));
3508 D2Templated->setAccess(DTemplated->getAccess());
3509
3510
3511 // Import the qualifier, if any.
3512 if (DTemplated->getQualifier()) {
3513 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3514 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3515 D2Templated->setQualifierInfo(NNS, NNSRange);
3516 }
3517 D2Templated->setLexicalDeclContext(LexicalDC);
3518
3519 // Create the class template declaration itself.
3520 TemplateParameterList *TemplateParams
3521 = ImportTemplateParameterList(D->getTemplateParameters());
3522 if (!TemplateParams)
3523 return 0;
3524
3525 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3526 Loc, Name, TemplateParams,
3527 D2Templated,
3528 /*PrevDecl=*/0);
3529 D2Templated->setDescribedClassTemplate(D2);
3530
3531 D2->setAccess(D->getAccess());
3532 D2->setLexicalDeclContext(LexicalDC);
3533 LexicalDC->addDecl(D2);
3534
3535 // Note the relationship between the class templates.
3536 Importer.Imported(D, D2);
3537 Importer.Imported(DTemplated, D2Templated);
3538
3539 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3540 // FIXME: Import definition!
3541 }
3542
3543 return D2;
3544}
3545
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003546Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3547 ClassTemplateSpecializationDecl *D) {
3548 // If this record has a definition in the translation unit we're coming from,
3549 // but this particular declaration is not that definition, import the
3550 // definition and map to that.
3551 TagDecl *Definition = D->getDefinition();
3552 if (Definition && Definition != D) {
3553 Decl *ImportedDef = Importer.Import(Definition);
3554 if (!ImportedDef)
3555 return 0;
3556
3557 return Importer.Imported(D, ImportedDef);
3558 }
3559
3560 ClassTemplateDecl *ClassTemplate
3561 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3562 D->getSpecializedTemplate()));
3563 if (!ClassTemplate)
3564 return 0;
3565
3566 // Import the context of this declaration.
3567 DeclContext *DC = ClassTemplate->getDeclContext();
3568 if (!DC)
3569 return 0;
3570
3571 DeclContext *LexicalDC = DC;
3572 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3573 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3574 if (!LexicalDC)
3575 return 0;
3576 }
3577
3578 // Import the location of this declaration.
3579 SourceLocation Loc = Importer.Import(D->getLocation());
3580
3581 // Import template arguments.
3582 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3583 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3584 D->getTemplateArgs().size(),
3585 TemplateArgs))
3586 return 0;
3587
3588 // Try to find an existing specialization with these template arguments.
3589 void *InsertPos = 0;
3590 ClassTemplateSpecializationDecl *D2
3591 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3592 TemplateArgs.size(), InsertPos);
3593 if (D2) {
3594 // We already have a class template specialization with these template
3595 // arguments.
3596
3597 // FIXME: Check for specialization vs. instantiation errors.
3598
3599 if (RecordDecl *FoundDef = D2->getDefinition()) {
3600 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3601 // The record types structurally match, or the "from" translation
3602 // unit only had a forward declaration anyway; call it the same
3603 // function.
3604 return Importer.Imported(D, FoundDef);
3605 }
3606 }
3607 } else {
3608 // Create a new specialization.
3609 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3610 D->getTagKind(), DC,
3611 Loc, ClassTemplate,
3612 TemplateArgs.data(),
3613 TemplateArgs.size(),
3614 /*PrevDecl=*/0);
3615 D2->setSpecializationKind(D->getSpecializationKind());
3616
3617 // Add this specialization to the class template.
3618 ClassTemplate->AddSpecialization(D2, InsertPos);
3619
3620 // Import the qualifier, if any.
3621 if (D->getQualifier()) {
3622 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3623 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3624 D2->setQualifierInfo(NNS, NNSRange);
3625 }
3626
3627
3628 // Add the specialization to this context.
3629 D2->setLexicalDeclContext(LexicalDC);
3630 LexicalDC->addDecl(D2);
3631 }
3632 Importer.Imported(D, D2);
3633
3634 if (D->isDefinition() && ImportDefinition(D, D2))
3635 return 0;
3636
3637 return D2;
3638}
3639
Douglas Gregor4800d952010-02-11 19:21:55 +00003640//----------------------------------------------------------------------------
3641// Import Statements
3642//----------------------------------------------------------------------------
3643
3644Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3645 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3646 << S->getStmtClassName();
3647 return 0;
3648}
3649
3650//----------------------------------------------------------------------------
3651// Import Expressions
3652//----------------------------------------------------------------------------
3653Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3654 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3655 << E->getStmtClassName();
3656 return 0;
3657}
3658
Douglas Gregor44080632010-02-19 01:17:02 +00003659Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3660 NestedNameSpecifier *Qualifier = 0;
3661 if (E->getQualifier()) {
3662 Qualifier = Importer.Import(E->getQualifier());
3663 if (!E->getQualifier())
3664 return 0;
3665 }
3666
3667 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3668 if (!ToD)
3669 return 0;
3670
3671 QualType T = Importer.Import(E->getType());
3672 if (T.isNull())
3673 return 0;
3674
3675 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3676 Importer.Import(E->getQualifierRange()),
3677 ToD,
3678 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003679 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003680 /*FIXME:TemplateArgs=*/0);
3681}
3682
Douglas Gregor4800d952010-02-11 19:21:55 +00003683Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3684 QualType T = Importer.Import(E->getType());
3685 if (T.isNull())
3686 return 0;
3687
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003688 return IntegerLiteral::Create(Importer.getToContext(),
3689 E->getValue(), T,
3690 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003691}
3692
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003693Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3694 QualType T = Importer.Import(E->getType());
3695 if (T.isNull())
3696 return 0;
3697
3698 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3699 E->isWide(), T,
3700 Importer.Import(E->getLocation()));
3701}
3702
Douglas Gregorf638f952010-02-19 01:07:06 +00003703Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3704 Expr *SubExpr = Importer.Import(E->getSubExpr());
3705 if (!SubExpr)
3706 return 0;
3707
3708 return new (Importer.getToContext())
3709 ParenExpr(Importer.Import(E->getLParen()),
3710 Importer.Import(E->getRParen()),
3711 SubExpr);
3712}
3713
3714Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3715 QualType T = Importer.Import(E->getType());
3716 if (T.isNull())
3717 return 0;
3718
3719 Expr *SubExpr = Importer.Import(E->getSubExpr());
3720 if (!SubExpr)
3721 return 0;
3722
3723 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003724 T, E->getValueKind(),
3725 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003726 Importer.Import(E->getOperatorLoc()));
3727}
3728
Douglas Gregorbd249a52010-02-19 01:24:23 +00003729Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3730 QualType ResultType = Importer.Import(E->getType());
3731
3732 if (E->isArgumentType()) {
3733 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3734 if (!TInfo)
3735 return 0;
3736
3737 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3738 TInfo, ResultType,
3739 Importer.Import(E->getOperatorLoc()),
3740 Importer.Import(E->getRParenLoc()));
3741 }
3742
3743 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3744 if (!SubExpr)
3745 return 0;
3746
3747 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3748 SubExpr, ResultType,
3749 Importer.Import(E->getOperatorLoc()),
3750 Importer.Import(E->getRParenLoc()));
3751}
3752
Douglas Gregorf638f952010-02-19 01:07:06 +00003753Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3754 QualType T = Importer.Import(E->getType());
3755 if (T.isNull())
3756 return 0;
3757
3758 Expr *LHS = Importer.Import(E->getLHS());
3759 if (!LHS)
3760 return 0;
3761
3762 Expr *RHS = Importer.Import(E->getRHS());
3763 if (!RHS)
3764 return 0;
3765
3766 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003767 T, E->getValueKind(),
3768 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003769 Importer.Import(E->getOperatorLoc()));
3770}
3771
3772Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3773 QualType T = Importer.Import(E->getType());
3774 if (T.isNull())
3775 return 0;
3776
3777 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3778 if (CompLHSType.isNull())
3779 return 0;
3780
3781 QualType CompResultType = Importer.Import(E->getComputationResultType());
3782 if (CompResultType.isNull())
3783 return 0;
3784
3785 Expr *LHS = Importer.Import(E->getLHS());
3786 if (!LHS)
3787 return 0;
3788
3789 Expr *RHS = Importer.Import(E->getRHS());
3790 if (!RHS)
3791 return 0;
3792
3793 return new (Importer.getToContext())
3794 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003795 T, E->getValueKind(),
3796 E->getObjectKind(),
3797 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003798 Importer.Import(E->getOperatorLoc()));
3799}
3800
John McCallf871d0c2010-08-07 06:22:56 +00003801bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3802 if (E->path_empty()) return false;
3803
3804 // TODO: import cast paths
3805 return true;
3806}
3807
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003808Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3809 QualType T = Importer.Import(E->getType());
3810 if (T.isNull())
3811 return 0;
3812
3813 Expr *SubExpr = Importer.Import(E->getSubExpr());
3814 if (!SubExpr)
3815 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003816
3817 CXXCastPath BasePath;
3818 if (ImportCastPath(E, BasePath))
3819 return 0;
3820
3821 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003822 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003823}
3824
Douglas Gregor008847a2010-02-19 01:32:14 +00003825Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3826 QualType T = Importer.Import(E->getType());
3827 if (T.isNull())
3828 return 0;
3829
3830 Expr *SubExpr = Importer.Import(E->getSubExpr());
3831 if (!SubExpr)
3832 return 0;
3833
3834 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3835 if (!TInfo && E->getTypeInfoAsWritten())
3836 return 0;
3837
John McCallf871d0c2010-08-07 06:22:56 +00003838 CXXCastPath BasePath;
3839 if (ImportCastPath(E, BasePath))
3840 return 0;
3841
John McCallf89e55a2010-11-18 06:31:45 +00003842 return CStyleCastExpr::Create(Importer.getToContext(), T,
3843 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003844 SubExpr, &BasePath, TInfo,
3845 Importer.Import(E->getLParenLoc()),
3846 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003847}
3848
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003849ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003850 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003851 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003852 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003853 ImportedDecls[FromContext.getTranslationUnitDecl()]
3854 = ToContext.getTranslationUnitDecl();
3855}
3856
3857ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003858
3859QualType ASTImporter::Import(QualType FromT) {
3860 if (FromT.isNull())
3861 return QualType();
3862
Douglas Gregor169fba52010-02-08 15:18:58 +00003863 // Check whether we've already imported this type.
3864 llvm::DenseMap<Type *, Type *>::iterator Pos
3865 = ImportedTypes.find(FromT.getTypePtr());
3866 if (Pos != ImportedTypes.end())
3867 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003868
Douglas Gregor169fba52010-02-08 15:18:58 +00003869 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003870 ASTNodeImporter Importer(*this);
3871 QualType ToT = Importer.Visit(FromT.getTypePtr());
3872 if (ToT.isNull())
3873 return ToT;
3874
Douglas Gregor169fba52010-02-08 15:18:58 +00003875 // Record the imported type.
3876 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3877
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003878 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3879}
3880
Douglas Gregor9bed8792010-02-09 19:21:46 +00003881TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003882 if (!FromTSI)
3883 return FromTSI;
3884
3885 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003886 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003887 QualType T = Import(FromTSI->getType());
3888 if (T.isNull())
3889 return 0;
3890
3891 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003892 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003893}
3894
3895Decl *ASTImporter::Import(Decl *FromD) {
3896 if (!FromD)
3897 return 0;
3898
3899 // Check whether we've already imported this declaration.
3900 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3901 if (Pos != ImportedDecls.end())
3902 return Pos->second;
3903
3904 // Import the type
3905 ASTNodeImporter Importer(*this);
3906 Decl *ToD = Importer.Visit(FromD);
3907 if (!ToD)
3908 return 0;
3909
3910 // Record the imported declaration.
3911 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003912
3913 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3914 // Keep track of anonymous tags that have an associated typedef.
3915 if (FromTag->getTypedefForAnonDecl())
3916 AnonTagsWithPendingTypedefs.push_back(FromTag);
3917 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3918 // When we've finished transforming a typedef, see whether it was the
3919 // typedef for an anonymous tag.
3920 for (llvm::SmallVector<TagDecl *, 4>::iterator
3921 FromTag = AnonTagsWithPendingTypedefs.begin(),
3922 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3923 FromTag != FromTagEnd; ++FromTag) {
3924 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3925 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3926 // We found the typedef for an anonymous tag; link them.
3927 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3928 AnonTagsWithPendingTypedefs.erase(FromTag);
3929 break;
3930 }
3931 }
3932 }
3933 }
3934
Douglas Gregor9bed8792010-02-09 19:21:46 +00003935 return ToD;
3936}
3937
3938DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3939 if (!FromDC)
3940 return FromDC;
3941
3942 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3943}
3944
3945Expr *ASTImporter::Import(Expr *FromE) {
3946 if (!FromE)
3947 return 0;
3948
3949 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3950}
3951
3952Stmt *ASTImporter::Import(Stmt *FromS) {
3953 if (!FromS)
3954 return 0;
3955
Douglas Gregor4800d952010-02-11 19:21:55 +00003956 // Check whether we've already imported this declaration.
3957 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3958 if (Pos != ImportedStmts.end())
3959 return Pos->second;
3960
3961 // Import the type
3962 ASTNodeImporter Importer(*this);
3963 Stmt *ToS = Importer.Visit(FromS);
3964 if (!ToS)
3965 return 0;
3966
3967 // Record the imported declaration.
3968 ImportedStmts[FromS] = ToS;
3969 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003970}
3971
3972NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3973 if (!FromNNS)
3974 return 0;
3975
3976 // FIXME: Implement!
3977 return 0;
3978}
3979
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003980TemplateName ASTImporter::Import(TemplateName From) {
3981 switch (From.getKind()) {
3982 case TemplateName::Template:
3983 if (TemplateDecl *ToTemplate
3984 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3985 return TemplateName(ToTemplate);
3986
3987 return TemplateName();
3988
3989 case TemplateName::OverloadedTemplate: {
3990 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3991 UnresolvedSet<2> ToTemplates;
3992 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3993 E = FromStorage->end();
3994 I != E; ++I) {
3995 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3996 ToTemplates.addDecl(To);
3997 else
3998 return TemplateName();
3999 }
4000 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4001 ToTemplates.end());
4002 }
4003
4004 case TemplateName::QualifiedTemplate: {
4005 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4006 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4007 if (!Qualifier)
4008 return TemplateName();
4009
4010 if (TemplateDecl *ToTemplate
4011 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4012 return ToContext.getQualifiedTemplateName(Qualifier,
4013 QTN->hasTemplateKeyword(),
4014 ToTemplate);
4015
4016 return TemplateName();
4017 }
4018
4019 case TemplateName::DependentTemplate: {
4020 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4021 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4022 if (!Qualifier)
4023 return TemplateName();
4024
4025 if (DTN->isIdentifier()) {
4026 return ToContext.getDependentTemplateName(Qualifier,
4027 Import(DTN->getIdentifier()));
4028 }
4029
4030 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4031 }
4032 }
4033
4034 llvm_unreachable("Invalid template name kind");
4035 return TemplateName();
4036}
4037
Douglas Gregor9bed8792010-02-09 19:21:46 +00004038SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4039 if (FromLoc.isInvalid())
4040 return SourceLocation();
4041
Douglas Gregor88523732010-02-10 00:15:17 +00004042 SourceManager &FromSM = FromContext.getSourceManager();
4043
4044 // For now, map everything down to its spelling location, so that we
4045 // don't have to import macro instantiations.
4046 // FIXME: Import macro instantiations!
4047 FromLoc = FromSM.getSpellingLoc(FromLoc);
4048 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4049 SourceManager &ToSM = ToContext.getSourceManager();
4050 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4051 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004052}
4053
4054SourceRange ASTImporter::Import(SourceRange FromRange) {
4055 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4056}
4057
Douglas Gregor88523732010-02-10 00:15:17 +00004058FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004059 llvm::DenseMap<FileID, FileID>::iterator Pos
4060 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004061 if (Pos != ImportedFileIDs.end())
4062 return Pos->second;
4063
4064 SourceManager &FromSM = FromContext.getSourceManager();
4065 SourceManager &ToSM = ToContext.getSourceManager();
4066 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4067 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4068
4069 // Include location of this file.
4070 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4071
4072 // Map the FileID for to the "to" source manager.
4073 FileID ToID;
4074 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4075 if (Cache->Entry) {
4076 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4077 // disk again
4078 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4079 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004080 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004081 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4082 FromSLoc.getFile().getFileCharacteristic());
4083 } else {
4084 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004085 const llvm::MemoryBuffer *
4086 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004087 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004088 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004089 FromBuf->getBufferIdentifier());
4090 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4091 }
4092
4093
Sebastian Redl535a3e22010-09-30 01:03:06 +00004094 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004095 return ToID;
4096}
4097
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004098DeclarationName ASTImporter::Import(DeclarationName FromName) {
4099 if (!FromName)
4100 return DeclarationName();
4101
4102 switch (FromName.getNameKind()) {
4103 case DeclarationName::Identifier:
4104 return Import(FromName.getAsIdentifierInfo());
4105
4106 case DeclarationName::ObjCZeroArgSelector:
4107 case DeclarationName::ObjCOneArgSelector:
4108 case DeclarationName::ObjCMultiArgSelector:
4109 return Import(FromName.getObjCSelector());
4110
4111 case DeclarationName::CXXConstructorName: {
4112 QualType T = Import(FromName.getCXXNameType());
4113 if (T.isNull())
4114 return DeclarationName();
4115
4116 return ToContext.DeclarationNames.getCXXConstructorName(
4117 ToContext.getCanonicalType(T));
4118 }
4119
4120 case DeclarationName::CXXDestructorName: {
4121 QualType T = Import(FromName.getCXXNameType());
4122 if (T.isNull())
4123 return DeclarationName();
4124
4125 return ToContext.DeclarationNames.getCXXDestructorName(
4126 ToContext.getCanonicalType(T));
4127 }
4128
4129 case DeclarationName::CXXConversionFunctionName: {
4130 QualType T = Import(FromName.getCXXNameType());
4131 if (T.isNull())
4132 return DeclarationName();
4133
4134 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4135 ToContext.getCanonicalType(T));
4136 }
4137
4138 case DeclarationName::CXXOperatorName:
4139 return ToContext.DeclarationNames.getCXXOperatorName(
4140 FromName.getCXXOverloadedOperator());
4141
4142 case DeclarationName::CXXLiteralOperatorName:
4143 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4144 Import(FromName.getCXXLiteralIdentifier()));
4145
4146 case DeclarationName::CXXUsingDirective:
4147 // FIXME: STATICS!
4148 return DeclarationName::getUsingDirectiveName();
4149 }
4150
4151 // Silence bogus GCC warning
4152 return DeclarationName();
4153}
4154
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004155IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004156 if (!FromId)
4157 return 0;
4158
4159 return &ToContext.Idents.get(FromId->getName());
4160}
Douglas Gregor089459a2010-02-08 21:09:39 +00004161
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004162Selector ASTImporter::Import(Selector FromSel) {
4163 if (FromSel.isNull())
4164 return Selector();
4165
4166 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4167 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4168 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4169 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4170 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4171}
4172
Douglas Gregor089459a2010-02-08 21:09:39 +00004173DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4174 DeclContext *DC,
4175 unsigned IDNS,
4176 NamedDecl **Decls,
4177 unsigned NumDecls) {
4178 return Name;
4179}
4180
4181DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004182 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004183}
4184
4185DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004186 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004187}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004188
4189Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4190 ImportedDecls[From] = To;
4191 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004192}
Douglas Gregorea35d112010-02-15 23:54:17 +00004193
4194bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4195 llvm::DenseMap<Type *, Type *>::iterator Pos
4196 = ImportedTypes.find(From.getTypePtr());
4197 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4198 return true;
4199
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004200 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004201 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004202}