blob: cc485c47d9f05a2cf40239486191f4206b985128 [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 }
John McCalle23cf432010-12-14 08:05:40 +00001476
1477 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1478 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001479
1480 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001481 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001482}
1483
1484QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1485 TypedefDecl *ToDecl
1486 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1487 if (!ToDecl)
1488 return QualType();
1489
1490 return Importer.getToContext().getTypeDeclType(ToDecl);
1491}
1492
1493QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1494 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1495 if (!ToExpr)
1496 return QualType();
1497
1498 return Importer.getToContext().getTypeOfExprType(ToExpr);
1499}
1500
1501QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1502 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1503 if (ToUnderlyingType.isNull())
1504 return QualType();
1505
1506 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1507}
1508
1509QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1510 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1511 if (!ToExpr)
1512 return QualType();
1513
1514 return Importer.getToContext().getDecltypeType(ToExpr);
1515}
1516
1517QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1518 RecordDecl *ToDecl
1519 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1520 if (!ToDecl)
1521 return QualType();
1522
1523 return Importer.getToContext().getTagDeclType(ToDecl);
1524}
1525
1526QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1527 EnumDecl *ToDecl
1528 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1529 if (!ToDecl)
1530 return QualType();
1531
1532 return Importer.getToContext().getTagDeclType(ToDecl);
1533}
1534
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001535QualType ASTNodeImporter::VisitTemplateSpecializationType(
1536 TemplateSpecializationType *T) {
1537 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1538 if (ToTemplate.isNull())
1539 return QualType();
1540
1541 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1542 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1543 return QualType();
1544
1545 QualType ToCanonType;
1546 if (!QualType(T, 0).isCanonical()) {
1547 QualType FromCanonType
1548 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1549 ToCanonType =Importer.Import(FromCanonType);
1550 if (ToCanonType.isNull())
1551 return QualType();
1552 }
1553 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1554 ToTemplateArgs.data(),
1555 ToTemplateArgs.size(),
1556 ToCanonType);
1557}
1558
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001559QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001560 NestedNameSpecifier *ToQualifier = 0;
1561 // Note: the qualifier in an ElaboratedType is optional.
1562 if (T->getQualifier()) {
1563 ToQualifier = Importer.Import(T->getQualifier());
1564 if (!ToQualifier)
1565 return QualType();
1566 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001567
1568 QualType ToNamedType = Importer.Import(T->getNamedType());
1569 if (ToNamedType.isNull())
1570 return QualType();
1571
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001572 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1573 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001574}
1575
1576QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1577 ObjCInterfaceDecl *Class
1578 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1579 if (!Class)
1580 return QualType();
1581
John McCallc12c5bb2010-05-15 11:32:37 +00001582 return Importer.getToContext().getObjCInterfaceType(Class);
1583}
1584
1585QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1586 QualType ToBaseType = Importer.Import(T->getBaseType());
1587 if (ToBaseType.isNull())
1588 return QualType();
1589
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001590 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001591 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001592 PEnd = T->qual_end();
1593 P != PEnd; ++P) {
1594 ObjCProtocolDecl *Protocol
1595 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1596 if (!Protocol)
1597 return QualType();
1598 Protocols.push_back(Protocol);
1599 }
1600
John McCallc12c5bb2010-05-15 11:32:37 +00001601 return Importer.getToContext().getObjCObjectType(ToBaseType,
1602 Protocols.data(),
1603 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001604}
1605
1606QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1607 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1608 if (ToPointeeType.isNull())
1609 return QualType();
1610
John McCallc12c5bb2010-05-15 11:32:37 +00001611 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001612}
1613
Douglas Gregor089459a2010-02-08 21:09:39 +00001614//----------------------------------------------------------------------------
1615// Import Declarations
1616//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001617bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1618 DeclContext *&LexicalDC,
1619 DeclarationName &Name,
1620 SourceLocation &Loc) {
1621 // Import the context of this declaration.
1622 DC = Importer.ImportContext(D->getDeclContext());
1623 if (!DC)
1624 return true;
1625
1626 LexicalDC = DC;
1627 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1628 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1629 if (!LexicalDC)
1630 return true;
1631 }
1632
1633 // Import the name of this declaration.
1634 Name = Importer.Import(D->getDeclName());
1635 if (D->getDeclName() && !Name)
1636 return true;
1637
1638 // Import the location of this declaration.
1639 Loc = Importer.Import(D->getLocation());
1640 return false;
1641}
1642
Abramo Bagnara25777432010-08-11 22:01:17 +00001643void
1644ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1645 DeclarationNameInfo& To) {
1646 // NOTE: To.Name and To.Loc are already imported.
1647 // We only have to import To.LocInfo.
1648 switch (To.getName().getNameKind()) {
1649 case DeclarationName::Identifier:
1650 case DeclarationName::ObjCZeroArgSelector:
1651 case DeclarationName::ObjCOneArgSelector:
1652 case DeclarationName::ObjCMultiArgSelector:
1653 case DeclarationName::CXXUsingDirective:
1654 return;
1655
1656 case DeclarationName::CXXOperatorName: {
1657 SourceRange Range = From.getCXXOperatorNameRange();
1658 To.setCXXOperatorNameRange(Importer.Import(Range));
1659 return;
1660 }
1661 case DeclarationName::CXXLiteralOperatorName: {
1662 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1663 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1664 return;
1665 }
1666 case DeclarationName::CXXConstructorName:
1667 case DeclarationName::CXXDestructorName:
1668 case DeclarationName::CXXConversionFunctionName: {
1669 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1670 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1671 return;
1672 }
1673 assert(0 && "Unknown name kind.");
1674 }
1675}
1676
Douglas Gregor083a8212010-02-21 18:24:45 +00001677void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1678 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1679 FromEnd = FromDC->decls_end();
1680 From != FromEnd;
1681 ++From)
1682 Importer.Import(*From);
1683}
1684
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001685bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1686 if (To->getDefinition())
1687 return false;
1688
1689 To->startDefinition();
1690
1691 // Add base classes.
1692 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1693 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1694
1695 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1696 for (CXXRecordDecl::base_class_iterator
1697 Base1 = FromCXX->bases_begin(),
1698 FromBaseEnd = FromCXX->bases_end();
1699 Base1 != FromBaseEnd;
1700 ++Base1) {
1701 QualType T = Importer.Import(Base1->getType());
1702 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001703 return true;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001704
1705 Bases.push_back(
1706 new (Importer.getToContext())
1707 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1708 Base1->isVirtual(),
1709 Base1->isBaseOfClass(),
1710 Base1->getAccessSpecifierAsWritten(),
1711 Importer.Import(Base1->getTypeSourceInfo())));
1712 }
1713 if (!Bases.empty())
1714 ToCXX->setBases(Bases.data(), Bases.size());
1715 }
1716
1717 ImportDeclContext(From);
1718 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001719 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001720}
1721
Douglas Gregor040afae2010-11-30 19:14:50 +00001722TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1723 TemplateParameterList *Params) {
1724 llvm::SmallVector<NamedDecl *, 4> ToParams;
1725 ToParams.reserve(Params->size());
1726 for (TemplateParameterList::iterator P = Params->begin(),
1727 PEnd = Params->end();
1728 P != PEnd; ++P) {
1729 Decl *To = Importer.Import(*P);
1730 if (!To)
1731 return 0;
1732
1733 ToParams.push_back(cast<NamedDecl>(To));
1734 }
1735
1736 return TemplateParameterList::Create(Importer.getToContext(),
1737 Importer.Import(Params->getTemplateLoc()),
1738 Importer.Import(Params->getLAngleLoc()),
1739 ToParams.data(), ToParams.size(),
1740 Importer.Import(Params->getRAngleLoc()));
1741}
1742
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001743TemplateArgument
1744ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1745 switch (From.getKind()) {
1746 case TemplateArgument::Null:
1747 return TemplateArgument();
1748
1749 case TemplateArgument::Type: {
1750 QualType ToType = Importer.Import(From.getAsType());
1751 if (ToType.isNull())
1752 return TemplateArgument();
1753 return TemplateArgument(ToType);
1754 }
1755
1756 case TemplateArgument::Integral: {
1757 QualType ToType = Importer.Import(From.getIntegralType());
1758 if (ToType.isNull())
1759 return TemplateArgument();
1760 return TemplateArgument(*From.getAsIntegral(), ToType);
1761 }
1762
1763 case TemplateArgument::Declaration:
1764 if (Decl *To = Importer.Import(From.getAsDecl()))
1765 return TemplateArgument(To);
1766 return TemplateArgument();
1767
1768 case TemplateArgument::Template: {
1769 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1770 if (ToTemplate.isNull())
1771 return TemplateArgument();
1772
1773 return TemplateArgument(ToTemplate);
1774 }
1775
1776 case TemplateArgument::Expression:
1777 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1778 return TemplateArgument(ToExpr);
1779 return TemplateArgument();
1780
1781 case TemplateArgument::Pack: {
1782 llvm::SmallVector<TemplateArgument, 2> ToPack;
1783 ToPack.reserve(From.pack_size());
1784 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1785 return TemplateArgument();
1786
1787 TemplateArgument *ToArgs
1788 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1789 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1790 return TemplateArgument(ToArgs, ToPack.size());
1791 }
1792 }
1793
1794 llvm_unreachable("Invalid template argument kind");
1795 return TemplateArgument();
1796}
1797
1798bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1799 unsigned NumFromArgs,
1800 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1801 for (unsigned I = 0; I != NumFromArgs; ++I) {
1802 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1803 if (To.isNull() && !FromArgs[I].isNull())
1804 return true;
1805
1806 ToArgs.push_back(To);
1807 }
1808
1809 return false;
1810}
1811
Douglas Gregor96a01b42010-02-11 00:48:18 +00001812bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001813 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001814 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001815 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001816 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001817 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001818}
1819
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001820bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001821 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001822 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001823 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001824 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001825}
1826
Douglas Gregor040afae2010-11-30 19:14:50 +00001827bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1828 ClassTemplateDecl *To) {
1829 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1830 Importer.getToContext(),
1831 Importer.getNonEquivalentDecls());
1832 return Ctx.IsStructurallyEquivalent(From, To);
1833}
1834
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001835Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001836 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001837 << D->getDeclKindName();
1838 return 0;
1839}
1840
Douglas Gregor788c62d2010-02-21 18:26:36 +00001841Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1842 // Import the major distinguishing characteristics of this namespace.
1843 DeclContext *DC, *LexicalDC;
1844 DeclarationName Name;
1845 SourceLocation Loc;
1846 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1847 return 0;
1848
1849 NamespaceDecl *MergeWithNamespace = 0;
1850 if (!Name) {
1851 // This is an anonymous namespace. Adopt an existing anonymous
1852 // namespace if we can.
1853 // FIXME: Not testable.
1854 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1855 MergeWithNamespace = TU->getAnonymousNamespace();
1856 else
1857 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1858 } else {
1859 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1860 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1861 Lookup.first != Lookup.second;
1862 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001863 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001864 continue;
1865
1866 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1867 MergeWithNamespace = FoundNS;
1868 ConflictingDecls.clear();
1869 break;
1870 }
1871
1872 ConflictingDecls.push_back(*Lookup.first);
1873 }
1874
1875 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001876 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001877 ConflictingDecls.data(),
1878 ConflictingDecls.size());
1879 }
1880 }
1881
1882 // Create the "to" namespace, if needed.
1883 NamespaceDecl *ToNamespace = MergeWithNamespace;
1884 if (!ToNamespace) {
1885 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1886 Name.getAsIdentifierInfo());
1887 ToNamespace->setLexicalDeclContext(LexicalDC);
1888 LexicalDC->addDecl(ToNamespace);
1889
1890 // If this is an anonymous namespace, register it as the anonymous
1891 // namespace within its context.
1892 if (!Name) {
1893 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1894 TU->setAnonymousNamespace(ToNamespace);
1895 else
1896 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1897 }
1898 }
1899 Importer.Imported(D, ToNamespace);
1900
1901 ImportDeclContext(D);
1902
1903 return ToNamespace;
1904}
1905
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001906Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1907 // Import the major distinguishing characteristics of this typedef.
1908 DeclContext *DC, *LexicalDC;
1909 DeclarationName Name;
1910 SourceLocation Loc;
1911 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1912 return 0;
1913
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001914 // If this typedef is not in block scope, determine whether we've
1915 // seen a typedef with the same name (that we can merge with) or any
1916 // other entity by that name (which name lookup could conflict with).
1917 if (!DC->isFunctionOrMethod()) {
1918 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1919 unsigned IDNS = Decl::IDNS_Ordinary;
1920 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1921 Lookup.first != Lookup.second;
1922 ++Lookup.first) {
1923 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1924 continue;
1925 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001926 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1927 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001928 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001929 }
1930
1931 ConflictingDecls.push_back(*Lookup.first);
1932 }
1933
1934 if (!ConflictingDecls.empty()) {
1935 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1936 ConflictingDecls.data(),
1937 ConflictingDecls.size());
1938 if (!Name)
1939 return 0;
1940 }
1941 }
1942
Douglas Gregorea35d112010-02-15 23:54:17 +00001943 // Import the underlying type of this typedef;
1944 QualType T = Importer.Import(D->getUnderlyingType());
1945 if (T.isNull())
1946 return 0;
1947
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001948 // Create the new typedef node.
1949 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1950 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1951 Loc, Name.getAsIdentifierInfo(),
1952 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001953 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001954 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001955 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001956 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001957
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001958 return ToTypedef;
1959}
1960
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001961Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1962 // Import the major distinguishing characteristics of this enum.
1963 DeclContext *DC, *LexicalDC;
1964 DeclarationName Name;
1965 SourceLocation Loc;
1966 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1967 return 0;
1968
1969 // Figure out what enum name we're looking for.
1970 unsigned IDNS = Decl::IDNS_Tag;
1971 DeclarationName SearchName = Name;
1972 if (!SearchName && D->getTypedefForAnonDecl()) {
1973 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1974 IDNS = Decl::IDNS_Ordinary;
1975 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1976 IDNS |= Decl::IDNS_Ordinary;
1977
1978 // We may already have an enum of the same name; try to find and match it.
1979 if (!DC->isFunctionOrMethod() && SearchName) {
1980 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1981 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1982 Lookup.first != Lookup.second;
1983 ++Lookup.first) {
1984 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1985 continue;
1986
1987 Decl *Found = *Lookup.first;
1988 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1989 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1990 Found = Tag->getDecl();
1991 }
1992
1993 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001994 if (IsStructuralMatch(D, FoundEnum))
1995 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001996 }
1997
1998 ConflictingDecls.push_back(*Lookup.first);
1999 }
2000
2001 if (!ConflictingDecls.empty()) {
2002 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2003 ConflictingDecls.data(),
2004 ConflictingDecls.size());
2005 }
2006 }
2007
2008 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002009 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002010 Name.getAsIdentifierInfo(),
2011 Importer.Import(D->getTagKeywordLoc()), 0,
2012 D->isScoped(), D->isScopedUsingClassTag(),
2013 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002014 // Import the qualifier, if any.
2015 if (D->getQualifier()) {
2016 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2017 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2018 D2->setQualifierInfo(NNS, NNSRange);
2019 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002020 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002021 D2->setLexicalDeclContext(LexicalDC);
2022 Importer.Imported(D, D2);
2023 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002024
2025 // Import the integer type.
2026 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2027 if (ToIntegerType.isNull())
2028 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002029 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002030
2031 // Import the definition
2032 if (D->isDefinition()) {
2033 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2034 if (T.isNull())
2035 return 0;
2036
2037 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2038 if (ToPromotionType.isNull())
2039 return 0;
2040
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002041 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002042 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002043
2044 // FIXME: we might need to merge the number of positive or negative bits
2045 // if the enumerator lists don't match.
2046 D2->completeDefinition(T, ToPromotionType,
2047 D->getNumPositiveBits(),
2048 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002049 }
2050
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002051 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002052}
2053
Douglas Gregor96a01b42010-02-11 00:48:18 +00002054Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2055 // If this record has a definition in the translation unit we're coming from,
2056 // but this particular declaration is not that definition, import the
2057 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002058 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002059 if (Definition && Definition != D) {
2060 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002061 if (!ImportedDef)
2062 return 0;
2063
2064 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002065 }
2066
2067 // Import the major distinguishing characteristics of this record.
2068 DeclContext *DC, *LexicalDC;
2069 DeclarationName Name;
2070 SourceLocation Loc;
2071 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2072 return 0;
2073
2074 // Figure out what structure name we're looking for.
2075 unsigned IDNS = Decl::IDNS_Tag;
2076 DeclarationName SearchName = Name;
2077 if (!SearchName && D->getTypedefForAnonDecl()) {
2078 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2079 IDNS = Decl::IDNS_Ordinary;
2080 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2081 IDNS |= Decl::IDNS_Ordinary;
2082
2083 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002084 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002085 if (!DC->isFunctionOrMethod() && SearchName) {
2086 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2087 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2088 Lookup.first != Lookup.second;
2089 ++Lookup.first) {
2090 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2091 continue;
2092
2093 Decl *Found = *Lookup.first;
2094 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2095 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2096 Found = Tag->getDecl();
2097 }
2098
2099 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002100 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2101 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2102 // The record types structurally match, or the "from" translation
2103 // unit only had a forward declaration anyway; call it the same
2104 // function.
2105 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002106 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002107 }
2108 } else {
2109 // We have a forward declaration of this type, so adopt that forward
2110 // declaration rather than building a new one.
2111 AdoptDecl = FoundRecord;
2112 continue;
2113 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002114 }
2115
2116 ConflictingDecls.push_back(*Lookup.first);
2117 }
2118
2119 if (!ConflictingDecls.empty()) {
2120 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2121 ConflictingDecls.data(),
2122 ConflictingDecls.size());
2123 }
2124 }
2125
2126 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002127 RecordDecl *D2 = AdoptDecl;
2128 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002129 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002130 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002131 D->getTagKind(),
2132 DC, Loc,
2133 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002134 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002135 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002136 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002137 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002138 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002139 DC, Loc,
2140 Name.getAsIdentifierInfo(),
2141 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002142 }
John McCallb6217662010-03-15 10:12:16 +00002143 // Import the qualifier, if any.
2144 if (D->getQualifier()) {
2145 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2146 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2147 D2->setQualifierInfo(NNS, NNSRange);
2148 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002149 D2->setLexicalDeclContext(LexicalDC);
2150 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002151 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002152
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002153 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002154
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002155 if (D->isDefinition() && ImportDefinition(D, D2))
2156 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002157
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002158 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002159}
2160
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002161Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2162 // Import the major distinguishing characteristics of this enumerator.
2163 DeclContext *DC, *LexicalDC;
2164 DeclarationName Name;
2165 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002166 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002167 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002168
2169 QualType T = Importer.Import(D->getType());
2170 if (T.isNull())
2171 return 0;
2172
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002173 // Determine whether there are any other declarations with the same name and
2174 // in the same context.
2175 if (!LexicalDC->isFunctionOrMethod()) {
2176 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2177 unsigned IDNS = Decl::IDNS_Ordinary;
2178 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2179 Lookup.first != Lookup.second;
2180 ++Lookup.first) {
2181 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2182 continue;
2183
2184 ConflictingDecls.push_back(*Lookup.first);
2185 }
2186
2187 if (!ConflictingDecls.empty()) {
2188 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2189 ConflictingDecls.data(),
2190 ConflictingDecls.size());
2191 if (!Name)
2192 return 0;
2193 }
2194 }
2195
2196 Expr *Init = Importer.Import(D->getInitExpr());
2197 if (D->getInitExpr() && !Init)
2198 return 0;
2199
2200 EnumConstantDecl *ToEnumerator
2201 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2202 Name.getAsIdentifierInfo(), T,
2203 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002204 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002205 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002206 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002207 LexicalDC->addDecl(ToEnumerator);
2208 return ToEnumerator;
2209}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002210
Douglas Gregora404ea62010-02-10 19:54:31 +00002211Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2212 // Import the major distinguishing characteristics of this function.
2213 DeclContext *DC, *LexicalDC;
2214 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002215 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002216 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002217 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002218
Douglas Gregora404ea62010-02-10 19:54:31 +00002219 // Try to find a function in our own ("to") context with the same name, same
2220 // type, and in the same context as the function we're importing.
2221 if (!LexicalDC->isFunctionOrMethod()) {
2222 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2223 unsigned IDNS = Decl::IDNS_Ordinary;
2224 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2225 Lookup.first != Lookup.second;
2226 ++Lookup.first) {
2227 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2228 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002229
Douglas Gregora404ea62010-02-10 19:54:31 +00002230 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2231 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2232 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002233 if (Importer.IsStructurallyEquivalent(D->getType(),
2234 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002235 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002236 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002237 }
2238
2239 // FIXME: Check for overloading more carefully, e.g., by boosting
2240 // Sema::IsOverload out to the AST library.
2241
2242 // Function overloading is okay in C++.
2243 if (Importer.getToContext().getLangOptions().CPlusPlus)
2244 continue;
2245
2246 // Complain about inconsistent function types.
2247 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002248 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002249 Importer.ToDiag(FoundFunction->getLocation(),
2250 diag::note_odr_value_here)
2251 << FoundFunction->getType();
2252 }
2253 }
2254
2255 ConflictingDecls.push_back(*Lookup.first);
2256 }
2257
2258 if (!ConflictingDecls.empty()) {
2259 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2260 ConflictingDecls.data(),
2261 ConflictingDecls.size());
2262 if (!Name)
2263 return 0;
2264 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002265 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002266
Abramo Bagnara25777432010-08-11 22:01:17 +00002267 DeclarationNameInfo NameInfo(Name, Loc);
2268 // Import additional name location/type info.
2269 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2270
Douglas Gregorea35d112010-02-15 23:54:17 +00002271 // Import the type.
2272 QualType T = Importer.Import(D->getType());
2273 if (T.isNull())
2274 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002275
2276 // Import the function parameters.
2277 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2278 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2279 P != PEnd; ++P) {
2280 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2281 if (!ToP)
2282 return 0;
2283
2284 Parameters.push_back(ToP);
2285 }
2286
2287 // Create the imported function.
2288 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002289 FunctionDecl *ToFunction = 0;
2290 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2291 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2292 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002293 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002294 FromConstructor->isExplicit(),
2295 D->isInlineSpecified(),
2296 D->isImplicit());
2297 } else if (isa<CXXDestructorDecl>(D)) {
2298 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2299 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002300 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002301 D->isInlineSpecified(),
2302 D->isImplicit());
2303 } else if (CXXConversionDecl *FromConversion
2304 = dyn_cast<CXXConversionDecl>(D)) {
2305 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2306 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002307 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002308 D->isInlineSpecified(),
2309 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002310 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2311 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2312 cast<CXXRecordDecl>(DC),
2313 NameInfo, T, TInfo,
2314 Method->isStatic(),
2315 Method->getStorageClassAsWritten(),
2316 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002317 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002318 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2319 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002320 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002321 D->isInlineSpecified(),
2322 D->hasWrittenPrototype());
2323 }
John McCallb6217662010-03-15 10:12:16 +00002324
2325 // Import the qualifier, if any.
2326 if (D->getQualifier()) {
2327 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2328 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2329 ToFunction->setQualifierInfo(NNS, NNSRange);
2330 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002331 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002332 ToFunction->setLexicalDeclContext(LexicalDC);
2333 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002334
Douglas Gregora404ea62010-02-10 19:54:31 +00002335 // Set the parameters.
2336 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002337 Parameters[I]->setOwningFunction(ToFunction);
2338 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002339 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002340 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002341
2342 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002343
2344 // Add this function to the lexical context.
2345 LexicalDC->addDecl(ToFunction);
2346
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002347 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002348}
2349
Douglas Gregorc144f352010-02-21 18:29:16 +00002350Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2351 return VisitFunctionDecl(D);
2352}
2353
2354Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2355 return VisitCXXMethodDecl(D);
2356}
2357
2358Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2359 return VisitCXXMethodDecl(D);
2360}
2361
2362Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2363 return VisitCXXMethodDecl(D);
2364}
2365
Douglas Gregor96a01b42010-02-11 00:48:18 +00002366Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2367 // Import the major distinguishing characteristics of a variable.
2368 DeclContext *DC, *LexicalDC;
2369 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002370 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002371 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2372 return 0;
2373
2374 // Import the type.
2375 QualType T = Importer.Import(D->getType());
2376 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002377 return 0;
2378
2379 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2380 Expr *BitWidth = Importer.Import(D->getBitWidth());
2381 if (!BitWidth && D->getBitWidth())
2382 return 0;
2383
2384 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2385 Loc, Name.getAsIdentifierInfo(),
2386 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002387 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002388 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002389 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002390 LexicalDC->addDecl(ToField);
2391 return ToField;
2392}
2393
Francois Pichet87c2e122010-11-21 06:08:52 +00002394Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2395 // Import the major distinguishing characteristics of a variable.
2396 DeclContext *DC, *LexicalDC;
2397 DeclarationName Name;
2398 SourceLocation Loc;
2399 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2400 return 0;
2401
2402 // Import the type.
2403 QualType T = Importer.Import(D->getType());
2404 if (T.isNull())
2405 return 0;
2406
2407 NamedDecl **NamedChain =
2408 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2409
2410 unsigned i = 0;
2411 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2412 PE = D->chain_end(); PI != PE; ++PI) {
2413 Decl* D = Importer.Import(*PI);
2414 if (!D)
2415 return 0;
2416 NamedChain[i++] = cast<NamedDecl>(D);
2417 }
2418
2419 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2420 Importer.getToContext(), DC,
2421 Loc, Name.getAsIdentifierInfo(), T,
2422 NamedChain, D->getChainingSize());
2423 ToIndirectField->setAccess(D->getAccess());
2424 ToIndirectField->setLexicalDeclContext(LexicalDC);
2425 Importer.Imported(D, ToIndirectField);
2426 LexicalDC->addDecl(ToIndirectField);
2427 return ToIndirectField;
2428}
2429
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002430Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2431 // Import the major distinguishing characteristics of an ivar.
2432 DeclContext *DC, *LexicalDC;
2433 DeclarationName Name;
2434 SourceLocation Loc;
2435 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2436 return 0;
2437
2438 // Determine whether we've already imported this ivar
2439 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2440 Lookup.first != Lookup.second;
2441 ++Lookup.first) {
2442 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2443 if (Importer.IsStructurallyEquivalent(D->getType(),
2444 FoundIvar->getType())) {
2445 Importer.Imported(D, FoundIvar);
2446 return FoundIvar;
2447 }
2448
2449 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2450 << Name << D->getType() << FoundIvar->getType();
2451 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2452 << FoundIvar->getType();
2453 return 0;
2454 }
2455 }
2456
2457 // Import the type.
2458 QualType T = Importer.Import(D->getType());
2459 if (T.isNull())
2460 return 0;
2461
2462 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2463 Expr *BitWidth = Importer.Import(D->getBitWidth());
2464 if (!BitWidth && D->getBitWidth())
2465 return 0;
2466
Daniel Dunbara0654922010-04-02 20:10:03 +00002467 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2468 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002469 Loc, Name.getAsIdentifierInfo(),
2470 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002471 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002472 ToIvar->setLexicalDeclContext(LexicalDC);
2473 Importer.Imported(D, ToIvar);
2474 LexicalDC->addDecl(ToIvar);
2475 return ToIvar;
2476
2477}
2478
Douglas Gregora404ea62010-02-10 19:54:31 +00002479Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2480 // Import the major distinguishing characteristics of a variable.
2481 DeclContext *DC, *LexicalDC;
2482 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002483 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002484 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002485 return 0;
2486
Douglas Gregor089459a2010-02-08 21:09:39 +00002487 // Try to find a variable in our own ("to") context with the same name and
2488 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002489 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002490 VarDecl *MergeWithVar = 0;
2491 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2492 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002493 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002494 Lookup.first != Lookup.second;
2495 ++Lookup.first) {
2496 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2497 continue;
2498
2499 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2500 // We have found a variable that we may need to merge with. Check it.
2501 if (isExternalLinkage(FoundVar->getLinkage()) &&
2502 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002503 if (Importer.IsStructurallyEquivalent(D->getType(),
2504 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002505 MergeWithVar = FoundVar;
2506 break;
2507 }
2508
Douglas Gregord0145422010-02-12 17:23:39 +00002509 const ArrayType *FoundArray
2510 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2511 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002512 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002513 if (FoundArray && TArray) {
2514 if (isa<IncompleteArrayType>(FoundArray) &&
2515 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002516 // Import the type.
2517 QualType T = Importer.Import(D->getType());
2518 if (T.isNull())
2519 return 0;
2520
Douglas Gregord0145422010-02-12 17:23:39 +00002521 FoundVar->setType(T);
2522 MergeWithVar = FoundVar;
2523 break;
2524 } else if (isa<IncompleteArrayType>(TArray) &&
2525 isa<ConstantArrayType>(FoundArray)) {
2526 MergeWithVar = FoundVar;
2527 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002528 }
2529 }
2530
Douglas Gregor089459a2010-02-08 21:09:39 +00002531 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002532 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002533 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2534 << FoundVar->getType();
2535 }
2536 }
2537
2538 ConflictingDecls.push_back(*Lookup.first);
2539 }
2540
2541 if (MergeWithVar) {
2542 // An equivalent variable with external linkage has been found. Link
2543 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002544 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002545
2546 if (VarDecl *DDef = D->getDefinition()) {
2547 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2548 Importer.ToDiag(ExistingDef->getLocation(),
2549 diag::err_odr_variable_multiple_def)
2550 << Name;
2551 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2552 } else {
2553 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002554 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002555 }
2556 }
2557
2558 return MergeWithVar;
2559 }
2560
2561 if (!ConflictingDecls.empty()) {
2562 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2563 ConflictingDecls.data(),
2564 ConflictingDecls.size());
2565 if (!Name)
2566 return 0;
2567 }
2568 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002569
Douglas Gregorea35d112010-02-15 23:54:17 +00002570 // Import the type.
2571 QualType T = Importer.Import(D->getType());
2572 if (T.isNull())
2573 return 0;
2574
Douglas Gregor089459a2010-02-08 21:09:39 +00002575 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002576 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002577 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2578 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002579 D->getStorageClass(),
2580 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002581 // Import the qualifier, if any.
2582 if (D->getQualifier()) {
2583 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2584 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2585 ToVar->setQualifierInfo(NNS, NNSRange);
2586 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002587 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002588 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002589 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002590 LexicalDC->addDecl(ToVar);
2591
Douglas Gregor089459a2010-02-08 21:09:39 +00002592 // Merge the initializer.
2593 // FIXME: Can we really import any initializer? Alternatively, we could force
2594 // ourselves to import every declaration of a variable and then only use
2595 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002596 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002597
2598 // FIXME: Other bits to merge?
2599
2600 return ToVar;
2601}
2602
Douglas Gregor2cd00932010-02-17 21:22:52 +00002603Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2604 // Parameters are created in the translation unit's context, then moved
2605 // into the function declaration's context afterward.
2606 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2607
2608 // Import the name of this declaration.
2609 DeclarationName Name = Importer.Import(D->getDeclName());
2610 if (D->getDeclName() && !Name)
2611 return 0;
2612
2613 // Import the location of this declaration.
2614 SourceLocation Loc = Importer.Import(D->getLocation());
2615
2616 // Import the parameter's type.
2617 QualType T = Importer.Import(D->getType());
2618 if (T.isNull())
2619 return 0;
2620
2621 // Create the imported parameter.
2622 ImplicitParamDecl *ToParm
2623 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2624 Loc, Name.getAsIdentifierInfo(),
2625 T);
2626 return Importer.Imported(D, ToParm);
2627}
2628
Douglas Gregora404ea62010-02-10 19:54:31 +00002629Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2630 // Parameters are created in the translation unit's context, then moved
2631 // into the function declaration's context afterward.
2632 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2633
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002634 // Import the name of this declaration.
2635 DeclarationName Name = Importer.Import(D->getDeclName());
2636 if (D->getDeclName() && !Name)
2637 return 0;
2638
Douglas Gregora404ea62010-02-10 19:54:31 +00002639 // Import the location of this declaration.
2640 SourceLocation Loc = Importer.Import(D->getLocation());
2641
2642 // Import the parameter's type.
2643 QualType T = Importer.Import(D->getType());
2644 if (T.isNull())
2645 return 0;
2646
2647 // Create the imported parameter.
2648 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2649 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2650 Loc, Name.getAsIdentifierInfo(),
2651 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002652 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002653 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002654 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002655 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002656}
2657
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002658Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2659 // Import the major distinguishing characteristics of a method.
2660 DeclContext *DC, *LexicalDC;
2661 DeclarationName Name;
2662 SourceLocation Loc;
2663 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2664 return 0;
2665
2666 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2667 Lookup.first != Lookup.second;
2668 ++Lookup.first) {
2669 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2670 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2671 continue;
2672
2673 // Check return types.
2674 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2675 FoundMethod->getResultType())) {
2676 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2677 << D->isInstanceMethod() << Name
2678 << D->getResultType() << FoundMethod->getResultType();
2679 Importer.ToDiag(FoundMethod->getLocation(),
2680 diag::note_odr_objc_method_here)
2681 << D->isInstanceMethod() << Name;
2682 return 0;
2683 }
2684
2685 // Check the number of parameters.
2686 if (D->param_size() != FoundMethod->param_size()) {
2687 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2688 << D->isInstanceMethod() << Name
2689 << D->param_size() << FoundMethod->param_size();
2690 Importer.ToDiag(FoundMethod->getLocation(),
2691 diag::note_odr_objc_method_here)
2692 << D->isInstanceMethod() << Name;
2693 return 0;
2694 }
2695
2696 // Check parameter types.
2697 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2698 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2699 P != PEnd; ++P, ++FoundP) {
2700 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2701 (*FoundP)->getType())) {
2702 Importer.FromDiag((*P)->getLocation(),
2703 diag::err_odr_objc_method_param_type_inconsistent)
2704 << D->isInstanceMethod() << Name
2705 << (*P)->getType() << (*FoundP)->getType();
2706 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2707 << (*FoundP)->getType();
2708 return 0;
2709 }
2710 }
2711
2712 // Check variadic/non-variadic.
2713 // Check the number of parameters.
2714 if (D->isVariadic() != FoundMethod->isVariadic()) {
2715 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2716 << D->isInstanceMethod() << Name;
2717 Importer.ToDiag(FoundMethod->getLocation(),
2718 diag::note_odr_objc_method_here)
2719 << D->isInstanceMethod() << Name;
2720 return 0;
2721 }
2722
2723 // FIXME: Any other bits we need to merge?
2724 return Importer.Imported(D, FoundMethod);
2725 }
2726 }
2727
2728 // Import the result type.
2729 QualType ResultTy = Importer.Import(D->getResultType());
2730 if (ResultTy.isNull())
2731 return 0;
2732
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002733 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2734
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002735 ObjCMethodDecl *ToMethod
2736 = ObjCMethodDecl::Create(Importer.getToContext(),
2737 Loc,
2738 Importer.Import(D->getLocEnd()),
2739 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002740 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002741 D->isInstanceMethod(),
2742 D->isVariadic(),
2743 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002744 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002745 D->getImplementationControl());
2746
2747 // FIXME: When we decide to merge method definitions, we'll need to
2748 // deal with implicit parameters.
2749
2750 // Import the parameters
2751 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2752 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2753 FromPEnd = D->param_end();
2754 FromP != FromPEnd;
2755 ++FromP) {
2756 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2757 if (!ToP)
2758 return 0;
2759
2760 ToParams.push_back(ToP);
2761 }
2762
2763 // Set the parameters.
2764 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2765 ToParams[I]->setOwningFunction(ToMethod);
2766 ToMethod->addDecl(ToParams[I]);
2767 }
2768 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002769 ToParams.data(), ToParams.size(),
2770 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002771
2772 ToMethod->setLexicalDeclContext(LexicalDC);
2773 Importer.Imported(D, ToMethod);
2774 LexicalDC->addDecl(ToMethod);
2775 return ToMethod;
2776}
2777
Douglas Gregorb4677b62010-02-18 01:47:50 +00002778Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2779 // Import the major distinguishing characteristics of a category.
2780 DeclContext *DC, *LexicalDC;
2781 DeclarationName Name;
2782 SourceLocation Loc;
2783 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2784 return 0;
2785
2786 ObjCInterfaceDecl *ToInterface
2787 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2788 if (!ToInterface)
2789 return 0;
2790
2791 // Determine if we've already encountered this category.
2792 ObjCCategoryDecl *MergeWithCategory
2793 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2794 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2795 if (!ToCategory) {
2796 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2797 Importer.Import(D->getAtLoc()),
2798 Loc,
2799 Importer.Import(D->getCategoryNameLoc()),
2800 Name.getAsIdentifierInfo());
2801 ToCategory->setLexicalDeclContext(LexicalDC);
2802 LexicalDC->addDecl(ToCategory);
2803 Importer.Imported(D, ToCategory);
2804
2805 // Link this category into its class's category list.
2806 ToCategory->setClassInterface(ToInterface);
2807 ToCategory->insertNextClassCategory();
2808
2809 // Import protocols
2810 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2811 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2812 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2813 = D->protocol_loc_begin();
2814 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2815 FromProtoEnd = D->protocol_end();
2816 FromProto != FromProtoEnd;
2817 ++FromProto, ++FromProtoLoc) {
2818 ObjCProtocolDecl *ToProto
2819 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2820 if (!ToProto)
2821 return 0;
2822 Protocols.push_back(ToProto);
2823 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2824 }
2825
2826 // FIXME: If we're merging, make sure that the protocol list is the same.
2827 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2828 ProtocolLocs.data(), Importer.getToContext());
2829
2830 } else {
2831 Importer.Imported(D, ToCategory);
2832 }
2833
2834 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002835 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002836
2837 // If we have an implementation, import it as well.
2838 if (D->getImplementation()) {
2839 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002840 = cast_or_null<ObjCCategoryImplDecl>(
2841 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002842 if (!Impl)
2843 return 0;
2844
2845 ToCategory->setImplementation(Impl);
2846 }
2847
2848 return ToCategory;
2849}
2850
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002851Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002852 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002853 DeclContext *DC, *LexicalDC;
2854 DeclarationName Name;
2855 SourceLocation Loc;
2856 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2857 return 0;
2858
2859 ObjCProtocolDecl *MergeWithProtocol = 0;
2860 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2861 Lookup.first != Lookup.second;
2862 ++Lookup.first) {
2863 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2864 continue;
2865
2866 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2867 break;
2868 }
2869
2870 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2871 if (!ToProto || ToProto->isForwardDecl()) {
2872 if (!ToProto) {
2873 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2874 Name.getAsIdentifierInfo());
2875 ToProto->setForwardDecl(D->isForwardDecl());
2876 ToProto->setLexicalDeclContext(LexicalDC);
2877 LexicalDC->addDecl(ToProto);
2878 }
2879 Importer.Imported(D, ToProto);
2880
2881 // Import protocols
2882 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2883 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2884 ObjCProtocolDecl::protocol_loc_iterator
2885 FromProtoLoc = D->protocol_loc_begin();
2886 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2887 FromProtoEnd = D->protocol_end();
2888 FromProto != FromProtoEnd;
2889 ++FromProto, ++FromProtoLoc) {
2890 ObjCProtocolDecl *ToProto
2891 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2892 if (!ToProto)
2893 return 0;
2894 Protocols.push_back(ToProto);
2895 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2896 }
2897
2898 // FIXME: If we're merging, make sure that the protocol list is the same.
2899 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2900 ProtocolLocs.data(), Importer.getToContext());
2901 } else {
2902 Importer.Imported(D, ToProto);
2903 }
2904
Douglas Gregorb4677b62010-02-18 01:47:50 +00002905 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002906 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002907
2908 return ToProto;
2909}
2910
Douglas Gregora12d2942010-02-16 01:20:57 +00002911Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2912 // Import the major distinguishing characteristics of an @interface.
2913 DeclContext *DC, *LexicalDC;
2914 DeclarationName Name;
2915 SourceLocation Loc;
2916 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2917 return 0;
2918
2919 ObjCInterfaceDecl *MergeWithIface = 0;
2920 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2921 Lookup.first != Lookup.second;
2922 ++Lookup.first) {
2923 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2924 continue;
2925
2926 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2927 break;
2928 }
2929
2930 ObjCInterfaceDecl *ToIface = MergeWithIface;
2931 if (!ToIface || ToIface->isForwardDecl()) {
2932 if (!ToIface) {
2933 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2934 DC, Loc,
2935 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002936 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002937 D->isForwardDecl(),
2938 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002939 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002940 ToIface->setLexicalDeclContext(LexicalDC);
2941 LexicalDC->addDecl(ToIface);
2942 }
2943 Importer.Imported(D, ToIface);
2944
Douglas Gregora12d2942010-02-16 01:20:57 +00002945 if (D->getSuperClass()) {
2946 ObjCInterfaceDecl *Super
2947 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2948 if (!Super)
2949 return 0;
2950
2951 ToIface->setSuperClass(Super);
2952 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2953 }
2954
2955 // Import protocols
2956 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2957 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2958 ObjCInterfaceDecl::protocol_loc_iterator
2959 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002960
2961 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00002962 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2963 FromProtoEnd = D->protocol_end();
2964 FromProto != FromProtoEnd;
2965 ++FromProto, ++FromProtoLoc) {
2966 ObjCProtocolDecl *ToProto
2967 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2968 if (!ToProto)
2969 return 0;
2970 Protocols.push_back(ToProto);
2971 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2972 }
2973
2974 // FIXME: If we're merging, make sure that the protocol list is the same.
2975 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2976 ProtocolLocs.data(), Importer.getToContext());
2977
Douglas Gregora12d2942010-02-16 01:20:57 +00002978 // Import @end range
2979 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2980 } else {
2981 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002982
2983 // Check for consistency of superclasses.
2984 DeclarationName FromSuperName, ToSuperName;
2985 if (D->getSuperClass())
2986 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2987 if (ToIface->getSuperClass())
2988 ToSuperName = ToIface->getSuperClass()->getDeclName();
2989 if (FromSuperName != ToSuperName) {
2990 Importer.ToDiag(ToIface->getLocation(),
2991 diag::err_odr_objc_superclass_inconsistent)
2992 << ToIface->getDeclName();
2993 if (ToIface->getSuperClass())
2994 Importer.ToDiag(ToIface->getSuperClassLoc(),
2995 diag::note_odr_objc_superclass)
2996 << ToIface->getSuperClass()->getDeclName();
2997 else
2998 Importer.ToDiag(ToIface->getLocation(),
2999 diag::note_odr_objc_missing_superclass);
3000 if (D->getSuperClass())
3001 Importer.FromDiag(D->getSuperClassLoc(),
3002 diag::note_odr_objc_superclass)
3003 << D->getSuperClass()->getDeclName();
3004 else
3005 Importer.FromDiag(D->getLocation(),
3006 diag::note_odr_objc_missing_superclass);
3007 return 0;
3008 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003009 }
3010
Douglas Gregorb4677b62010-02-18 01:47:50 +00003011 // Import categories. When the categories themselves are imported, they'll
3012 // hook themselves into this interface.
3013 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3014 FromCat = FromCat->getNextClassCategory())
3015 Importer.Import(FromCat);
3016
Douglas Gregora12d2942010-02-16 01:20:57 +00003017 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003018 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003019
3020 // If we have an @implementation, import it as well.
3021 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003022 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3023 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003024 if (!Impl)
3025 return 0;
3026
3027 ToIface->setImplementation(Impl);
3028 }
3029
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003030 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003031}
3032
Douglas Gregor3daef292010-12-07 15:32:12 +00003033Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3034 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3035 Importer.Import(D->getCategoryDecl()));
3036 if (!Category)
3037 return 0;
3038
3039 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3040 if (!ToImpl) {
3041 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3042 if (!DC)
3043 return 0;
3044
3045 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3046 Importer.Import(D->getLocation()),
3047 Importer.Import(D->getIdentifier()),
3048 Category->getClassInterface());
3049
3050 DeclContext *LexicalDC = DC;
3051 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3052 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3053 if (!LexicalDC)
3054 return 0;
3055
3056 ToImpl->setLexicalDeclContext(LexicalDC);
3057 }
3058
3059 LexicalDC->addDecl(ToImpl);
3060 Category->setImplementation(ToImpl);
3061 }
3062
3063 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003064 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003065 return ToImpl;
3066}
3067
Douglas Gregordd182ff2010-12-07 01:26:03 +00003068Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3069 // Find the corresponding interface.
3070 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3071 Importer.Import(D->getClassInterface()));
3072 if (!Iface)
3073 return 0;
3074
3075 // Import the superclass, if any.
3076 ObjCInterfaceDecl *Super = 0;
3077 if (D->getSuperClass()) {
3078 Super = cast_or_null<ObjCInterfaceDecl>(
3079 Importer.Import(D->getSuperClass()));
3080 if (!Super)
3081 return 0;
3082 }
3083
3084 ObjCImplementationDecl *Impl = Iface->getImplementation();
3085 if (!Impl) {
3086 // We haven't imported an implementation yet. Create a new @implementation
3087 // now.
3088 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3089 Importer.ImportContext(D->getDeclContext()),
3090 Importer.Import(D->getLocation()),
3091 Iface, Super);
3092
3093 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3094 DeclContext *LexicalDC
3095 = Importer.ImportContext(D->getLexicalDeclContext());
3096 if (!LexicalDC)
3097 return 0;
3098 Impl->setLexicalDeclContext(LexicalDC);
3099 }
3100
3101 // Associate the implementation with the class it implements.
3102 Iface->setImplementation(Impl);
3103 Importer.Imported(D, Iface->getImplementation());
3104 } else {
3105 Importer.Imported(D, Iface->getImplementation());
3106
3107 // Verify that the existing @implementation has the same superclass.
3108 if ((Super && !Impl->getSuperClass()) ||
3109 (!Super && Impl->getSuperClass()) ||
3110 (Super && Impl->getSuperClass() &&
3111 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3112 Importer.ToDiag(Impl->getLocation(),
3113 diag::err_odr_objc_superclass_inconsistent)
3114 << Iface->getDeclName();
3115 // FIXME: It would be nice to have the location of the superclass
3116 // below.
3117 if (Impl->getSuperClass())
3118 Importer.ToDiag(Impl->getLocation(),
3119 diag::note_odr_objc_superclass)
3120 << Impl->getSuperClass()->getDeclName();
3121 else
3122 Importer.ToDiag(Impl->getLocation(),
3123 diag::note_odr_objc_missing_superclass);
3124 if (D->getSuperClass())
3125 Importer.FromDiag(D->getLocation(),
3126 diag::note_odr_objc_superclass)
3127 << D->getSuperClass()->getDeclName();
3128 else
3129 Importer.FromDiag(D->getLocation(),
3130 diag::note_odr_objc_missing_superclass);
3131 return 0;
3132 }
3133 }
3134
3135 // Import all of the members of this @implementation.
3136 ImportDeclContext(D);
3137
3138 return Impl;
3139}
3140
Douglas Gregore3261622010-02-17 18:02:10 +00003141Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3142 // Import the major distinguishing characteristics of an @property.
3143 DeclContext *DC, *LexicalDC;
3144 DeclarationName Name;
3145 SourceLocation Loc;
3146 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3147 return 0;
3148
3149 // Check whether we have already imported this property.
3150 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3151 Lookup.first != Lookup.second;
3152 ++Lookup.first) {
3153 if (ObjCPropertyDecl *FoundProp
3154 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3155 // Check property types.
3156 if (!Importer.IsStructurallyEquivalent(D->getType(),
3157 FoundProp->getType())) {
3158 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3159 << Name << D->getType() << FoundProp->getType();
3160 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3161 << FoundProp->getType();
3162 return 0;
3163 }
3164
3165 // FIXME: Check property attributes, getters, setters, etc.?
3166
3167 // Consider these properties to be equivalent.
3168 Importer.Imported(D, FoundProp);
3169 return FoundProp;
3170 }
3171 }
3172
3173 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003174 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3175 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003176 return 0;
3177
3178 // Create the new property.
3179 ObjCPropertyDecl *ToProperty
3180 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3181 Name.getAsIdentifierInfo(),
3182 Importer.Import(D->getAtLoc()),
3183 T,
3184 D->getPropertyImplementation());
3185 Importer.Imported(D, ToProperty);
3186 ToProperty->setLexicalDeclContext(LexicalDC);
3187 LexicalDC->addDecl(ToProperty);
3188
3189 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003190 ToProperty->setPropertyAttributesAsWritten(
3191 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003192 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3193 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3194 ToProperty->setGetterMethodDecl(
3195 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3196 ToProperty->setSetterMethodDecl(
3197 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3198 ToProperty->setPropertyIvarDecl(
3199 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3200 return ToProperty;
3201}
3202
Douglas Gregor954e0c72010-12-07 18:32:03 +00003203Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3204 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3205 Importer.Import(D->getPropertyDecl()));
3206 if (!Property)
3207 return 0;
3208
3209 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3210 if (!DC)
3211 return 0;
3212
3213 // Import the lexical declaration context.
3214 DeclContext *LexicalDC = DC;
3215 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3216 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3217 if (!LexicalDC)
3218 return 0;
3219 }
3220
3221 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3222 if (!InImpl)
3223 return 0;
3224
3225 // Import the ivar (for an @synthesize).
3226 ObjCIvarDecl *Ivar = 0;
3227 if (D->getPropertyIvarDecl()) {
3228 Ivar = cast_or_null<ObjCIvarDecl>(
3229 Importer.Import(D->getPropertyIvarDecl()));
3230 if (!Ivar)
3231 return 0;
3232 }
3233
3234 ObjCPropertyImplDecl *ToImpl
3235 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3236 if (!ToImpl) {
3237 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3238 Importer.Import(D->getLocStart()),
3239 Importer.Import(D->getLocation()),
3240 Property,
3241 D->getPropertyImplementation(),
3242 Ivar,
3243 Importer.Import(D->getPropertyIvarDeclLoc()));
3244 ToImpl->setLexicalDeclContext(LexicalDC);
3245 Importer.Imported(D, ToImpl);
3246 LexicalDC->addDecl(ToImpl);
3247 } else {
3248 // Check that we have the same kind of property implementation (@synthesize
3249 // vs. @dynamic).
3250 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3251 Importer.ToDiag(ToImpl->getLocation(),
3252 diag::err_odr_objc_property_impl_kind_inconsistent)
3253 << Property->getDeclName()
3254 << (ToImpl->getPropertyImplementation()
3255 == ObjCPropertyImplDecl::Dynamic);
3256 Importer.FromDiag(D->getLocation(),
3257 diag::note_odr_objc_property_impl_kind)
3258 << D->getPropertyDecl()->getDeclName()
3259 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3260 return 0;
3261 }
3262
3263 // For @synthesize, check that we have the same
3264 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3265 Ivar != ToImpl->getPropertyIvarDecl()) {
3266 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3267 diag::err_odr_objc_synthesize_ivar_inconsistent)
3268 << Property->getDeclName()
3269 << ToImpl->getPropertyIvarDecl()->getDeclName()
3270 << Ivar->getDeclName();
3271 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3272 diag::note_odr_objc_synthesize_ivar_here)
3273 << D->getPropertyIvarDecl()->getDeclName();
3274 return 0;
3275 }
3276
3277 // Merge the existing implementation with the new implementation.
3278 Importer.Imported(D, ToImpl);
3279 }
3280
3281 return ToImpl;
3282}
3283
Douglas Gregor2b785022010-02-18 02:12:22 +00003284Decl *
3285ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3286 // Import the context of this declaration.
3287 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3288 if (!DC)
3289 return 0;
3290
3291 DeclContext *LexicalDC = DC;
3292 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3293 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3294 if (!LexicalDC)
3295 return 0;
3296 }
3297
3298 // Import the location of this declaration.
3299 SourceLocation Loc = Importer.Import(D->getLocation());
3300
3301 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3302 llvm::SmallVector<SourceLocation, 4> Locations;
3303 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3304 = D->protocol_loc_begin();
3305 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3306 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3307 FromProto != FromProtoEnd;
3308 ++FromProto, ++FromProtoLoc) {
3309 ObjCProtocolDecl *ToProto
3310 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3311 if (!ToProto)
3312 continue;
3313
3314 Protocols.push_back(ToProto);
3315 Locations.push_back(Importer.Import(*FromProtoLoc));
3316 }
3317
3318 ObjCForwardProtocolDecl *ToForward
3319 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3320 Protocols.data(), Protocols.size(),
3321 Locations.data());
3322 ToForward->setLexicalDeclContext(LexicalDC);
3323 LexicalDC->addDecl(ToForward);
3324 Importer.Imported(D, ToForward);
3325 return ToForward;
3326}
3327
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003328Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3329 // Import the context of this declaration.
3330 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3331 if (!DC)
3332 return 0;
3333
3334 DeclContext *LexicalDC = DC;
3335 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3336 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3337 if (!LexicalDC)
3338 return 0;
3339 }
3340
3341 // Import the location of this declaration.
3342 SourceLocation Loc = Importer.Import(D->getLocation());
3343
3344 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3345 llvm::SmallVector<SourceLocation, 4> Locations;
3346 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3347 From != FromEnd; ++From) {
3348 ObjCInterfaceDecl *ToIface
3349 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3350 if (!ToIface)
3351 continue;
3352
3353 Interfaces.push_back(ToIface);
3354 Locations.push_back(Importer.Import(From->getLocation()));
3355 }
3356
3357 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3358 Loc,
3359 Interfaces.data(),
3360 Locations.data(),
3361 Interfaces.size());
3362 ToClass->setLexicalDeclContext(LexicalDC);
3363 LexicalDC->addDecl(ToClass);
3364 Importer.Imported(D, ToClass);
3365 return ToClass;
3366}
3367
Douglas Gregor040afae2010-11-30 19:14:50 +00003368Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3369 // For template arguments, we adopt the translation unit as our declaration
3370 // context. This context will be fixed when the actual template declaration
3371 // is created.
3372
3373 // FIXME: Import default argument.
3374 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3375 Importer.getToContext().getTranslationUnitDecl(),
3376 Importer.Import(D->getLocation()),
3377 D->getDepth(),
3378 D->getIndex(),
3379 Importer.Import(D->getIdentifier()),
3380 D->wasDeclaredWithTypename(),
3381 D->isParameterPack());
3382}
3383
3384Decl *
3385ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3386 // Import the name of this declaration.
3387 DeclarationName Name = Importer.Import(D->getDeclName());
3388 if (D->getDeclName() && !Name)
3389 return 0;
3390
3391 // Import the location of this declaration.
3392 SourceLocation Loc = Importer.Import(D->getLocation());
3393
3394 // Import the type of this declaration.
3395 QualType T = Importer.Import(D->getType());
3396 if (T.isNull())
3397 return 0;
3398
3399 // Import type-source information.
3400 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3401 if (D->getTypeSourceInfo() && !TInfo)
3402 return 0;
3403
3404 // FIXME: Import default argument.
3405
3406 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3407 Importer.getToContext().getTranslationUnitDecl(),
3408 Loc, D->getDepth(), D->getPosition(),
3409 Name.getAsIdentifierInfo(),
3410 T, TInfo);
3411}
3412
3413Decl *
3414ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3415 // Import the name of this declaration.
3416 DeclarationName Name = Importer.Import(D->getDeclName());
3417 if (D->getDeclName() && !Name)
3418 return 0;
3419
3420 // Import the location of this declaration.
3421 SourceLocation Loc = Importer.Import(D->getLocation());
3422
3423 // Import template parameters.
3424 TemplateParameterList *TemplateParams
3425 = ImportTemplateParameterList(D->getTemplateParameters());
3426 if (!TemplateParams)
3427 return 0;
3428
3429 // FIXME: Import default argument.
3430
3431 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3432 Importer.getToContext().getTranslationUnitDecl(),
3433 Loc, D->getDepth(), D->getPosition(),
3434 Name.getAsIdentifierInfo(),
3435 TemplateParams);
3436}
3437
3438Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3439 // If this record has a definition in the translation unit we're coming from,
3440 // but this particular declaration is not that definition, import the
3441 // definition and map to that.
3442 CXXRecordDecl *Definition
3443 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3444 if (Definition && Definition != D->getTemplatedDecl()) {
3445 Decl *ImportedDef
3446 = Importer.Import(Definition->getDescribedClassTemplate());
3447 if (!ImportedDef)
3448 return 0;
3449
3450 return Importer.Imported(D, ImportedDef);
3451 }
3452
3453 // Import the major distinguishing characteristics of this class template.
3454 DeclContext *DC, *LexicalDC;
3455 DeclarationName Name;
3456 SourceLocation Loc;
3457 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3458 return 0;
3459
3460 // We may already have a template of the same name; try to find and match it.
3461 if (!DC->isFunctionOrMethod()) {
3462 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3463 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3464 Lookup.first != Lookup.second;
3465 ++Lookup.first) {
3466 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3467 continue;
3468
3469 Decl *Found = *Lookup.first;
3470 if (ClassTemplateDecl *FoundTemplate
3471 = dyn_cast<ClassTemplateDecl>(Found)) {
3472 if (IsStructuralMatch(D, FoundTemplate)) {
3473 // The class templates structurally match; call it the same template.
3474 // FIXME: We may be filling in a forward declaration here. Handle
3475 // this case!
3476 Importer.Imported(D->getTemplatedDecl(),
3477 FoundTemplate->getTemplatedDecl());
3478 return Importer.Imported(D, FoundTemplate);
3479 }
3480 }
3481
3482 ConflictingDecls.push_back(*Lookup.first);
3483 }
3484
3485 if (!ConflictingDecls.empty()) {
3486 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3487 ConflictingDecls.data(),
3488 ConflictingDecls.size());
3489 }
3490
3491 if (!Name)
3492 return 0;
3493 }
3494
3495 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3496
3497 // Create the declaration that is being templated.
3498 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3499 DTemplated->getTagKind(),
3500 DC,
3501 Importer.Import(DTemplated->getLocation()),
3502 Name.getAsIdentifierInfo(),
3503 Importer.Import(DTemplated->getTagKeywordLoc()));
3504 D2Templated->setAccess(DTemplated->getAccess());
3505
3506
3507 // Import the qualifier, if any.
3508 if (DTemplated->getQualifier()) {
3509 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3510 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3511 D2Templated->setQualifierInfo(NNS, NNSRange);
3512 }
3513 D2Templated->setLexicalDeclContext(LexicalDC);
3514
3515 // Create the class template declaration itself.
3516 TemplateParameterList *TemplateParams
3517 = ImportTemplateParameterList(D->getTemplateParameters());
3518 if (!TemplateParams)
3519 return 0;
3520
3521 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3522 Loc, Name, TemplateParams,
3523 D2Templated,
3524 /*PrevDecl=*/0);
3525 D2Templated->setDescribedClassTemplate(D2);
3526
3527 D2->setAccess(D->getAccess());
3528 D2->setLexicalDeclContext(LexicalDC);
3529 LexicalDC->addDecl(D2);
3530
3531 // Note the relationship between the class templates.
3532 Importer.Imported(D, D2);
3533 Importer.Imported(DTemplated, D2Templated);
3534
3535 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3536 // FIXME: Import definition!
3537 }
3538
3539 return D2;
3540}
3541
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003542Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3543 ClassTemplateSpecializationDecl *D) {
3544 // If this record has a definition in the translation unit we're coming from,
3545 // but this particular declaration is not that definition, import the
3546 // definition and map to that.
3547 TagDecl *Definition = D->getDefinition();
3548 if (Definition && Definition != D) {
3549 Decl *ImportedDef = Importer.Import(Definition);
3550 if (!ImportedDef)
3551 return 0;
3552
3553 return Importer.Imported(D, ImportedDef);
3554 }
3555
3556 ClassTemplateDecl *ClassTemplate
3557 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3558 D->getSpecializedTemplate()));
3559 if (!ClassTemplate)
3560 return 0;
3561
3562 // Import the context of this declaration.
3563 DeclContext *DC = ClassTemplate->getDeclContext();
3564 if (!DC)
3565 return 0;
3566
3567 DeclContext *LexicalDC = DC;
3568 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3569 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3570 if (!LexicalDC)
3571 return 0;
3572 }
3573
3574 // Import the location of this declaration.
3575 SourceLocation Loc = Importer.Import(D->getLocation());
3576
3577 // Import template arguments.
3578 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3579 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3580 D->getTemplateArgs().size(),
3581 TemplateArgs))
3582 return 0;
3583
3584 // Try to find an existing specialization with these template arguments.
3585 void *InsertPos = 0;
3586 ClassTemplateSpecializationDecl *D2
3587 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3588 TemplateArgs.size(), InsertPos);
3589 if (D2) {
3590 // We already have a class template specialization with these template
3591 // arguments.
3592
3593 // FIXME: Check for specialization vs. instantiation errors.
3594
3595 if (RecordDecl *FoundDef = D2->getDefinition()) {
3596 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3597 // The record types structurally match, or the "from" translation
3598 // unit only had a forward declaration anyway; call it the same
3599 // function.
3600 return Importer.Imported(D, FoundDef);
3601 }
3602 }
3603 } else {
3604 // Create a new specialization.
3605 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3606 D->getTagKind(), DC,
3607 Loc, ClassTemplate,
3608 TemplateArgs.data(),
3609 TemplateArgs.size(),
3610 /*PrevDecl=*/0);
3611 D2->setSpecializationKind(D->getSpecializationKind());
3612
3613 // Add this specialization to the class template.
3614 ClassTemplate->AddSpecialization(D2, InsertPos);
3615
3616 // Import the qualifier, if any.
3617 if (D->getQualifier()) {
3618 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3619 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3620 D2->setQualifierInfo(NNS, NNSRange);
3621 }
3622
3623
3624 // Add the specialization to this context.
3625 D2->setLexicalDeclContext(LexicalDC);
3626 LexicalDC->addDecl(D2);
3627 }
3628 Importer.Imported(D, D2);
3629
3630 if (D->isDefinition() && ImportDefinition(D, D2))
3631 return 0;
3632
3633 return D2;
3634}
3635
Douglas Gregor4800d952010-02-11 19:21:55 +00003636//----------------------------------------------------------------------------
3637// Import Statements
3638//----------------------------------------------------------------------------
3639
3640Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3641 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3642 << S->getStmtClassName();
3643 return 0;
3644}
3645
3646//----------------------------------------------------------------------------
3647// Import Expressions
3648//----------------------------------------------------------------------------
3649Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3650 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3651 << E->getStmtClassName();
3652 return 0;
3653}
3654
Douglas Gregor44080632010-02-19 01:17:02 +00003655Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3656 NestedNameSpecifier *Qualifier = 0;
3657 if (E->getQualifier()) {
3658 Qualifier = Importer.Import(E->getQualifier());
3659 if (!E->getQualifier())
3660 return 0;
3661 }
3662
3663 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3664 if (!ToD)
3665 return 0;
3666
3667 QualType T = Importer.Import(E->getType());
3668 if (T.isNull())
3669 return 0;
3670
3671 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3672 Importer.Import(E->getQualifierRange()),
3673 ToD,
3674 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003675 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003676 /*FIXME:TemplateArgs=*/0);
3677}
3678
Douglas Gregor4800d952010-02-11 19:21:55 +00003679Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3680 QualType T = Importer.Import(E->getType());
3681 if (T.isNull())
3682 return 0;
3683
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003684 return IntegerLiteral::Create(Importer.getToContext(),
3685 E->getValue(), T,
3686 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003687}
3688
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003689Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3690 QualType T = Importer.Import(E->getType());
3691 if (T.isNull())
3692 return 0;
3693
3694 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3695 E->isWide(), T,
3696 Importer.Import(E->getLocation()));
3697}
3698
Douglas Gregorf638f952010-02-19 01:07:06 +00003699Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3700 Expr *SubExpr = Importer.Import(E->getSubExpr());
3701 if (!SubExpr)
3702 return 0;
3703
3704 return new (Importer.getToContext())
3705 ParenExpr(Importer.Import(E->getLParen()),
3706 Importer.Import(E->getRParen()),
3707 SubExpr);
3708}
3709
3710Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3711 QualType T = Importer.Import(E->getType());
3712 if (T.isNull())
3713 return 0;
3714
3715 Expr *SubExpr = Importer.Import(E->getSubExpr());
3716 if (!SubExpr)
3717 return 0;
3718
3719 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003720 T, E->getValueKind(),
3721 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003722 Importer.Import(E->getOperatorLoc()));
3723}
3724
Douglas Gregorbd249a52010-02-19 01:24:23 +00003725Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3726 QualType ResultType = Importer.Import(E->getType());
3727
3728 if (E->isArgumentType()) {
3729 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3730 if (!TInfo)
3731 return 0;
3732
3733 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3734 TInfo, ResultType,
3735 Importer.Import(E->getOperatorLoc()),
3736 Importer.Import(E->getRParenLoc()));
3737 }
3738
3739 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3740 if (!SubExpr)
3741 return 0;
3742
3743 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3744 SubExpr, ResultType,
3745 Importer.Import(E->getOperatorLoc()),
3746 Importer.Import(E->getRParenLoc()));
3747}
3748
Douglas Gregorf638f952010-02-19 01:07:06 +00003749Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3750 QualType T = Importer.Import(E->getType());
3751 if (T.isNull())
3752 return 0;
3753
3754 Expr *LHS = Importer.Import(E->getLHS());
3755 if (!LHS)
3756 return 0;
3757
3758 Expr *RHS = Importer.Import(E->getRHS());
3759 if (!RHS)
3760 return 0;
3761
3762 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003763 T, E->getValueKind(),
3764 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003765 Importer.Import(E->getOperatorLoc()));
3766}
3767
3768Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3769 QualType T = Importer.Import(E->getType());
3770 if (T.isNull())
3771 return 0;
3772
3773 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3774 if (CompLHSType.isNull())
3775 return 0;
3776
3777 QualType CompResultType = Importer.Import(E->getComputationResultType());
3778 if (CompResultType.isNull())
3779 return 0;
3780
3781 Expr *LHS = Importer.Import(E->getLHS());
3782 if (!LHS)
3783 return 0;
3784
3785 Expr *RHS = Importer.Import(E->getRHS());
3786 if (!RHS)
3787 return 0;
3788
3789 return new (Importer.getToContext())
3790 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003791 T, E->getValueKind(),
3792 E->getObjectKind(),
3793 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003794 Importer.Import(E->getOperatorLoc()));
3795}
3796
John McCallf871d0c2010-08-07 06:22:56 +00003797bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3798 if (E->path_empty()) return false;
3799
3800 // TODO: import cast paths
3801 return true;
3802}
3803
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003804Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3805 QualType T = Importer.Import(E->getType());
3806 if (T.isNull())
3807 return 0;
3808
3809 Expr *SubExpr = Importer.Import(E->getSubExpr());
3810 if (!SubExpr)
3811 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003812
3813 CXXCastPath BasePath;
3814 if (ImportCastPath(E, BasePath))
3815 return 0;
3816
3817 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003818 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003819}
3820
Douglas Gregor008847a2010-02-19 01:32:14 +00003821Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3822 QualType T = Importer.Import(E->getType());
3823 if (T.isNull())
3824 return 0;
3825
3826 Expr *SubExpr = Importer.Import(E->getSubExpr());
3827 if (!SubExpr)
3828 return 0;
3829
3830 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3831 if (!TInfo && E->getTypeInfoAsWritten())
3832 return 0;
3833
John McCallf871d0c2010-08-07 06:22:56 +00003834 CXXCastPath BasePath;
3835 if (ImportCastPath(E, BasePath))
3836 return 0;
3837
John McCallf89e55a2010-11-18 06:31:45 +00003838 return CStyleCastExpr::Create(Importer.getToContext(), T,
3839 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003840 SubExpr, &BasePath, TInfo,
3841 Importer.Import(E->getLParenLoc()),
3842 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003843}
3844
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003845ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003846 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003847 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003848 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003849 ImportedDecls[FromContext.getTranslationUnitDecl()]
3850 = ToContext.getTranslationUnitDecl();
3851}
3852
3853ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003854
3855QualType ASTImporter::Import(QualType FromT) {
3856 if (FromT.isNull())
3857 return QualType();
3858
Douglas Gregor169fba52010-02-08 15:18:58 +00003859 // Check whether we've already imported this type.
3860 llvm::DenseMap<Type *, Type *>::iterator Pos
3861 = ImportedTypes.find(FromT.getTypePtr());
3862 if (Pos != ImportedTypes.end())
3863 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003864
Douglas Gregor169fba52010-02-08 15:18:58 +00003865 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003866 ASTNodeImporter Importer(*this);
3867 QualType ToT = Importer.Visit(FromT.getTypePtr());
3868 if (ToT.isNull())
3869 return ToT;
3870
Douglas Gregor169fba52010-02-08 15:18:58 +00003871 // Record the imported type.
3872 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3873
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003874 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3875}
3876
Douglas Gregor9bed8792010-02-09 19:21:46 +00003877TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003878 if (!FromTSI)
3879 return FromTSI;
3880
3881 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003882 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003883 QualType T = Import(FromTSI->getType());
3884 if (T.isNull())
3885 return 0;
3886
3887 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003888 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003889}
3890
3891Decl *ASTImporter::Import(Decl *FromD) {
3892 if (!FromD)
3893 return 0;
3894
3895 // Check whether we've already imported this declaration.
3896 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3897 if (Pos != ImportedDecls.end())
3898 return Pos->second;
3899
3900 // Import the type
3901 ASTNodeImporter Importer(*this);
3902 Decl *ToD = Importer.Visit(FromD);
3903 if (!ToD)
3904 return 0;
3905
3906 // Record the imported declaration.
3907 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003908
3909 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3910 // Keep track of anonymous tags that have an associated typedef.
3911 if (FromTag->getTypedefForAnonDecl())
3912 AnonTagsWithPendingTypedefs.push_back(FromTag);
3913 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3914 // When we've finished transforming a typedef, see whether it was the
3915 // typedef for an anonymous tag.
3916 for (llvm::SmallVector<TagDecl *, 4>::iterator
3917 FromTag = AnonTagsWithPendingTypedefs.begin(),
3918 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3919 FromTag != FromTagEnd; ++FromTag) {
3920 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3921 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3922 // We found the typedef for an anonymous tag; link them.
3923 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3924 AnonTagsWithPendingTypedefs.erase(FromTag);
3925 break;
3926 }
3927 }
3928 }
3929 }
3930
Douglas Gregor9bed8792010-02-09 19:21:46 +00003931 return ToD;
3932}
3933
3934DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3935 if (!FromDC)
3936 return FromDC;
3937
3938 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3939}
3940
3941Expr *ASTImporter::Import(Expr *FromE) {
3942 if (!FromE)
3943 return 0;
3944
3945 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3946}
3947
3948Stmt *ASTImporter::Import(Stmt *FromS) {
3949 if (!FromS)
3950 return 0;
3951
Douglas Gregor4800d952010-02-11 19:21:55 +00003952 // Check whether we've already imported this declaration.
3953 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3954 if (Pos != ImportedStmts.end())
3955 return Pos->second;
3956
3957 // Import the type
3958 ASTNodeImporter Importer(*this);
3959 Stmt *ToS = Importer.Visit(FromS);
3960 if (!ToS)
3961 return 0;
3962
3963 // Record the imported declaration.
3964 ImportedStmts[FromS] = ToS;
3965 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003966}
3967
3968NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3969 if (!FromNNS)
3970 return 0;
3971
3972 // FIXME: Implement!
3973 return 0;
3974}
3975
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003976TemplateName ASTImporter::Import(TemplateName From) {
3977 switch (From.getKind()) {
3978 case TemplateName::Template:
3979 if (TemplateDecl *ToTemplate
3980 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3981 return TemplateName(ToTemplate);
3982
3983 return TemplateName();
3984
3985 case TemplateName::OverloadedTemplate: {
3986 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3987 UnresolvedSet<2> ToTemplates;
3988 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3989 E = FromStorage->end();
3990 I != E; ++I) {
3991 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3992 ToTemplates.addDecl(To);
3993 else
3994 return TemplateName();
3995 }
3996 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3997 ToTemplates.end());
3998 }
3999
4000 case TemplateName::QualifiedTemplate: {
4001 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4002 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4003 if (!Qualifier)
4004 return TemplateName();
4005
4006 if (TemplateDecl *ToTemplate
4007 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4008 return ToContext.getQualifiedTemplateName(Qualifier,
4009 QTN->hasTemplateKeyword(),
4010 ToTemplate);
4011
4012 return TemplateName();
4013 }
4014
4015 case TemplateName::DependentTemplate: {
4016 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4017 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4018 if (!Qualifier)
4019 return TemplateName();
4020
4021 if (DTN->isIdentifier()) {
4022 return ToContext.getDependentTemplateName(Qualifier,
4023 Import(DTN->getIdentifier()));
4024 }
4025
4026 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4027 }
4028 }
4029
4030 llvm_unreachable("Invalid template name kind");
4031 return TemplateName();
4032}
4033
Douglas Gregor9bed8792010-02-09 19:21:46 +00004034SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4035 if (FromLoc.isInvalid())
4036 return SourceLocation();
4037
Douglas Gregor88523732010-02-10 00:15:17 +00004038 SourceManager &FromSM = FromContext.getSourceManager();
4039
4040 // For now, map everything down to its spelling location, so that we
4041 // don't have to import macro instantiations.
4042 // FIXME: Import macro instantiations!
4043 FromLoc = FromSM.getSpellingLoc(FromLoc);
4044 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4045 SourceManager &ToSM = ToContext.getSourceManager();
4046 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4047 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004048}
4049
4050SourceRange ASTImporter::Import(SourceRange FromRange) {
4051 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4052}
4053
Douglas Gregor88523732010-02-10 00:15:17 +00004054FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004055 llvm::DenseMap<FileID, FileID>::iterator Pos
4056 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004057 if (Pos != ImportedFileIDs.end())
4058 return Pos->second;
4059
4060 SourceManager &FromSM = FromContext.getSourceManager();
4061 SourceManager &ToSM = ToContext.getSourceManager();
4062 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4063 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4064
4065 // Include location of this file.
4066 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4067
4068 // Map the FileID for to the "to" source manager.
4069 FileID ToID;
4070 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4071 if (Cache->Entry) {
4072 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4073 // disk again
4074 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4075 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004076 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004077 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4078 FromSLoc.getFile().getFileCharacteristic());
4079 } else {
4080 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004081 const llvm::MemoryBuffer *
4082 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004083 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004084 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004085 FromBuf->getBufferIdentifier());
4086 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4087 }
4088
4089
Sebastian Redl535a3e22010-09-30 01:03:06 +00004090 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004091 return ToID;
4092}
4093
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004094DeclarationName ASTImporter::Import(DeclarationName FromName) {
4095 if (!FromName)
4096 return DeclarationName();
4097
4098 switch (FromName.getNameKind()) {
4099 case DeclarationName::Identifier:
4100 return Import(FromName.getAsIdentifierInfo());
4101
4102 case DeclarationName::ObjCZeroArgSelector:
4103 case DeclarationName::ObjCOneArgSelector:
4104 case DeclarationName::ObjCMultiArgSelector:
4105 return Import(FromName.getObjCSelector());
4106
4107 case DeclarationName::CXXConstructorName: {
4108 QualType T = Import(FromName.getCXXNameType());
4109 if (T.isNull())
4110 return DeclarationName();
4111
4112 return ToContext.DeclarationNames.getCXXConstructorName(
4113 ToContext.getCanonicalType(T));
4114 }
4115
4116 case DeclarationName::CXXDestructorName: {
4117 QualType T = Import(FromName.getCXXNameType());
4118 if (T.isNull())
4119 return DeclarationName();
4120
4121 return ToContext.DeclarationNames.getCXXDestructorName(
4122 ToContext.getCanonicalType(T));
4123 }
4124
4125 case DeclarationName::CXXConversionFunctionName: {
4126 QualType T = Import(FromName.getCXXNameType());
4127 if (T.isNull())
4128 return DeclarationName();
4129
4130 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4131 ToContext.getCanonicalType(T));
4132 }
4133
4134 case DeclarationName::CXXOperatorName:
4135 return ToContext.DeclarationNames.getCXXOperatorName(
4136 FromName.getCXXOverloadedOperator());
4137
4138 case DeclarationName::CXXLiteralOperatorName:
4139 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4140 Import(FromName.getCXXLiteralIdentifier()));
4141
4142 case DeclarationName::CXXUsingDirective:
4143 // FIXME: STATICS!
4144 return DeclarationName::getUsingDirectiveName();
4145 }
4146
4147 // Silence bogus GCC warning
4148 return DeclarationName();
4149}
4150
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004151IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004152 if (!FromId)
4153 return 0;
4154
4155 return &ToContext.Idents.get(FromId->getName());
4156}
Douglas Gregor089459a2010-02-08 21:09:39 +00004157
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004158Selector ASTImporter::Import(Selector FromSel) {
4159 if (FromSel.isNull())
4160 return Selector();
4161
4162 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4163 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4164 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4165 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4166 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4167}
4168
Douglas Gregor089459a2010-02-08 21:09:39 +00004169DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4170 DeclContext *DC,
4171 unsigned IDNS,
4172 NamedDecl **Decls,
4173 unsigned NumDecls) {
4174 return Name;
4175}
4176
4177DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004178 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004179}
4180
4181DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004182 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004183}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004184
4185Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4186 ImportedDecls[From] = To;
4187 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004188}
Douglas Gregorea35d112010-02-15 23:54:17 +00004189
4190bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4191 llvm::DenseMap<Type *, Type *>::iterator Pos
4192 = ImportedTypes.find(From.getTypePtr());
4193 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4194 return true;
4195
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004196 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004197 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004198}