blob: c62225ef407dea18c5b16895afcd827db04bbc83 [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 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000710
711 case Type::PackExpansion:
712 if (!IsStructurallyEquivalent(Context,
713 cast<PackExpansionType>(T1)->getPattern(),
714 cast<PackExpansionType>(T2)->getPattern()))
715 return false;
716 break;
717
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000718 case Type::ObjCInterface: {
719 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
720 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
721 if (!IsStructurallyEquivalent(Context,
722 Iface1->getDecl(), Iface2->getDecl()))
723 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000724 break;
725 }
726
727 case Type::ObjCObject: {
728 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
729 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
730 if (!IsStructurallyEquivalent(Context,
731 Obj1->getBaseType(),
732 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000733 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000734 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
735 return false;
736 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000737 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000738 Obj1->getProtocol(I),
739 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000740 return false;
741 }
742 break;
743 }
744
745 case Type::ObjCObjectPointer: {
746 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
747 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
748 if (!IsStructurallyEquivalent(Context,
749 Ptr1->getPointeeType(),
750 Ptr2->getPointeeType()))
751 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000752 break;
753 }
754
755 } // end switch
756
757 return true;
758}
759
760/// \brief Determine structural equivalence of two records.
761static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
762 RecordDecl *D1, RecordDecl *D2) {
763 if (D1->isUnion() != D2->isUnion()) {
764 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
765 << Context.C2.getTypeDeclType(D2);
766 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
767 << D1->getDeclName() << (unsigned)D1->getTagKind();
768 return false;
769 }
770
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000771 // If both declarations are class template specializations, we know
772 // the ODR applies, so check the template and template arguments.
773 ClassTemplateSpecializationDecl *Spec1
774 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
775 ClassTemplateSpecializationDecl *Spec2
776 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
777 if (Spec1 && Spec2) {
778 // Check that the specialized templates are the same.
779 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
780 Spec2->getSpecializedTemplate()))
781 return false;
782
783 // Check that the template arguments are the same.
784 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
785 return false;
786
787 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
788 if (!IsStructurallyEquivalent(Context,
789 Spec1->getTemplateArgs().get(I),
790 Spec2->getTemplateArgs().get(I)))
791 return false;
792 }
793 // If one is a class template specialization and the other is not, these
794 // structures are diferent.
795 else if (Spec1 || Spec2)
796 return false;
797
Douglas Gregorea35d112010-02-15 23:54:17 +0000798 // Compare the definitions of these two records. If either or both are
799 // incomplete, we assume that they are equivalent.
800 D1 = D1->getDefinition();
801 D2 = D2->getDefinition();
802 if (!D1 || !D2)
803 return true;
804
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000805 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
806 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
807 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
808 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000809 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000810 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000811 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000812 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000813 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000814 return false;
815 }
816
817 // Check the base classes.
818 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
819 BaseEnd1 = D1CXX->bases_end(),
820 Base2 = D2CXX->bases_begin();
821 Base1 != BaseEnd1;
822 ++Base1, ++Base2) {
823 if (!IsStructurallyEquivalent(Context,
824 Base1->getType(), Base2->getType())) {
825 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
826 << Context.C2.getTypeDeclType(D2);
827 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
828 << Base2->getType()
829 << Base2->getSourceRange();
830 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
831 << Base1->getType()
832 << Base1->getSourceRange();
833 return false;
834 }
835
836 // Check virtual vs. non-virtual inheritance mismatch.
837 if (Base1->isVirtual() != Base2->isVirtual()) {
838 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
839 << Context.C2.getTypeDeclType(D2);
840 Context.Diag2(Base2->getSourceRange().getBegin(),
841 diag::note_odr_virtual_base)
842 << Base2->isVirtual() << Base2->getSourceRange();
843 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
844 << Base1->isVirtual()
845 << Base1->getSourceRange();
846 return false;
847 }
848 }
849 } else if (D1CXX->getNumBases() > 0) {
850 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
851 << Context.C2.getTypeDeclType(D2);
852 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
853 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
854 << Base1->getType()
855 << Base1->getSourceRange();
856 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
857 return false;
858 }
859 }
860
861 // Check the fields for consistency.
862 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
863 Field2End = D2->field_end();
864 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
865 Field1End = D1->field_end();
866 Field1 != Field1End;
867 ++Field1, ++Field2) {
868 if (Field2 == Field2End) {
869 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
870 << Context.C2.getTypeDeclType(D2);
871 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
872 << Field1->getDeclName() << Field1->getType();
873 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
874 return false;
875 }
876
877 if (!IsStructurallyEquivalent(Context,
878 Field1->getType(), Field2->getType())) {
879 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
880 << Context.C2.getTypeDeclType(D2);
881 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
882 << Field2->getDeclName() << Field2->getType();
883 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
884 << Field1->getDeclName() << Field1->getType();
885 return false;
886 }
887
888 if (Field1->isBitField() != Field2->isBitField()) {
889 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
890 << Context.C2.getTypeDeclType(D2);
891 if (Field1->isBitField()) {
892 llvm::APSInt Bits;
893 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
894 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
895 << Field1->getDeclName() << Field1->getType()
896 << Bits.toString(10, false);
897 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
898 << Field2->getDeclName();
899 } else {
900 llvm::APSInt Bits;
901 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
902 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
903 << Field2->getDeclName() << Field2->getType()
904 << Bits.toString(10, false);
905 Context.Diag1(Field1->getLocation(),
906 diag::note_odr_not_bit_field)
907 << Field1->getDeclName();
908 }
909 return false;
910 }
911
912 if (Field1->isBitField()) {
913 // Make sure that the bit-fields are the same length.
914 llvm::APSInt Bits1, Bits2;
915 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
916 return false;
917 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
918 return false;
919
920 if (!IsSameValue(Bits1, Bits2)) {
921 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
922 << Context.C2.getTypeDeclType(D2);
923 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
924 << Field2->getDeclName() << Field2->getType()
925 << Bits2.toString(10, false);
926 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
927 << Field1->getDeclName() << Field1->getType()
928 << Bits1.toString(10, false);
929 return false;
930 }
931 }
932 }
933
934 if (Field2 != Field2End) {
935 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
936 << Context.C2.getTypeDeclType(D2);
937 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
938 << Field2->getDeclName() << Field2->getType();
939 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
940 return false;
941 }
942
943 return true;
944}
945
946/// \brief Determine structural equivalence of two enums.
947static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
948 EnumDecl *D1, EnumDecl *D2) {
949 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
950 EC2End = D2->enumerator_end();
951 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
952 EC1End = D1->enumerator_end();
953 EC1 != EC1End; ++EC1, ++EC2) {
954 if (EC2 == EC2End) {
955 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
956 << Context.C2.getTypeDeclType(D2);
957 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
958 << EC1->getDeclName()
959 << EC1->getInitVal().toString(10);
960 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
961 return false;
962 }
963
964 llvm::APSInt Val1 = EC1->getInitVal();
965 llvm::APSInt Val2 = EC2->getInitVal();
966 if (!IsSameValue(Val1, Val2) ||
967 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
968 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
969 << Context.C2.getTypeDeclType(D2);
970 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
971 << EC2->getDeclName()
972 << EC2->getInitVal().toString(10);
973 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
974 << EC1->getDeclName()
975 << EC1->getInitVal().toString(10);
976 return false;
977 }
978 }
979
980 if (EC2 != EC2End) {
981 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
982 << Context.C2.getTypeDeclType(D2);
983 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
984 << EC2->getDeclName()
985 << EC2->getInitVal().toString(10);
986 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
987 return false;
988 }
989
990 return true;
991}
Douglas Gregor040afae2010-11-30 19:14:50 +0000992
993static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
994 TemplateParameterList *Params1,
995 TemplateParameterList *Params2) {
996 if (Params1->size() != Params2->size()) {
997 Context.Diag2(Params2->getTemplateLoc(),
998 diag::err_odr_different_num_template_parameters)
999 << Params1->size() << Params2->size();
1000 Context.Diag1(Params1->getTemplateLoc(),
1001 diag::note_odr_template_parameter_list);
1002 return false;
1003 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001004
Douglas Gregor040afae2010-11-30 19:14:50 +00001005 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1006 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1007 Context.Diag2(Params2->getParam(I)->getLocation(),
1008 diag::err_odr_different_template_parameter_kind);
1009 Context.Diag1(Params1->getParam(I)->getLocation(),
1010 diag::note_odr_template_parameter_here);
1011 return false;
1012 }
1013
1014 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1015 Params2->getParam(I))) {
1016
1017 return false;
1018 }
1019 }
1020
1021 return true;
1022}
1023
1024static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1025 TemplateTypeParmDecl *D1,
1026 TemplateTypeParmDecl *D2) {
1027 if (D1->isParameterPack() != D2->isParameterPack()) {
1028 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1029 << D2->isParameterPack();
1030 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1031 << D1->isParameterPack();
1032 return false;
1033 }
1034
1035 return true;
1036}
1037
1038static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1039 NonTypeTemplateParmDecl *D1,
1040 NonTypeTemplateParmDecl *D2) {
1041 // FIXME: Enable once we have variadic templates.
1042#if 0
1043 if (D1->isParameterPack() != D2->isParameterPack()) {
1044 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1045 << D2->isParameterPack();
1046 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1047 << D1->isParameterPack();
1048 return false;
1049 }
1050#endif
1051
1052 // Check types.
1053 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1054 Context.Diag2(D2->getLocation(),
1055 diag::err_odr_non_type_parameter_type_inconsistent)
1056 << D2->getType() << D1->getType();
1057 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1058 << D1->getType();
1059 return false;
1060 }
1061
1062 return true;
1063}
1064
1065static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1066 TemplateTemplateParmDecl *D1,
1067 TemplateTemplateParmDecl *D2) {
1068 // FIXME: Enable once we have variadic templates.
1069#if 0
1070 if (D1->isParameterPack() != D2->isParameterPack()) {
1071 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1072 << D2->isParameterPack();
1073 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1074 << D1->isParameterPack();
1075 return false;
1076 }
1077#endif
1078
1079 // Check template parameter lists.
1080 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1081 D2->getTemplateParameters());
1082}
1083
1084static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1085 ClassTemplateDecl *D1,
1086 ClassTemplateDecl *D2) {
1087 // Check template parameters.
1088 if (!IsStructurallyEquivalent(Context,
1089 D1->getTemplateParameters(),
1090 D2->getTemplateParameters()))
1091 return false;
1092
1093 // Check the templated declaration.
1094 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1095 D2->getTemplatedDecl());
1096}
1097
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001098/// \brief Determine structural equivalence of two declarations.
1099static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1100 Decl *D1, Decl *D2) {
1101 // FIXME: Check for known structural equivalences via a callback of some sort.
1102
Douglas Gregorea35d112010-02-15 23:54:17 +00001103 // Check whether we already know that these two declarations are not
1104 // structurally equivalent.
1105 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1106 D2->getCanonicalDecl())))
1107 return false;
1108
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001109 // Determine whether we've already produced a tentative equivalence for D1.
1110 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1111 if (EquivToD1)
1112 return EquivToD1 == D2->getCanonicalDecl();
1113
1114 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1115 EquivToD1 = D2->getCanonicalDecl();
1116 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1117 return true;
1118}
1119
1120bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1121 Decl *D2) {
1122 if (!::IsStructurallyEquivalent(*this, D1, D2))
1123 return false;
1124
1125 return !Finish();
1126}
1127
1128bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1129 QualType T2) {
1130 if (!::IsStructurallyEquivalent(*this, T1, T2))
1131 return false;
1132
1133 return !Finish();
1134}
1135
1136bool StructuralEquivalenceContext::Finish() {
1137 while (!DeclsToCheck.empty()) {
1138 // Check the next declaration.
1139 Decl *D1 = DeclsToCheck.front();
1140 DeclsToCheck.pop_front();
1141
1142 Decl *D2 = TentativeEquivalences[D1];
1143 assert(D2 && "Unrecorded tentative equivalence?");
1144
Douglas Gregorea35d112010-02-15 23:54:17 +00001145 bool Equivalent = true;
1146
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001147 // FIXME: Switch on all declaration kinds. For now, we're just going to
1148 // check the obvious ones.
1149 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1150 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1151 // Check for equivalent structure names.
1152 IdentifierInfo *Name1 = Record1->getIdentifier();
1153 if (!Name1 && Record1->getTypedefForAnonDecl())
1154 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1155 IdentifierInfo *Name2 = Record2->getIdentifier();
1156 if (!Name2 && Record2->getTypedefForAnonDecl())
1157 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001158 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1159 !::IsStructurallyEquivalent(*this, Record1, Record2))
1160 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001161 } else {
1162 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001163 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001164 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001165 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001166 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1167 // Check for equivalent enum names.
1168 IdentifierInfo *Name1 = Enum1->getIdentifier();
1169 if (!Name1 && Enum1->getTypedefForAnonDecl())
1170 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1171 IdentifierInfo *Name2 = Enum2->getIdentifier();
1172 if (!Name2 && Enum2->getTypedefForAnonDecl())
1173 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001174 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1175 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1176 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001177 } else {
1178 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001179 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001180 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001181 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001182 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1183 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001184 Typedef2->getIdentifier()) ||
1185 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001186 Typedef1->getUnderlyingType(),
1187 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001188 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001189 } else {
1190 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001191 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001192 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001193 } else if (ClassTemplateDecl *ClassTemplate1
1194 = dyn_cast<ClassTemplateDecl>(D1)) {
1195 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1196 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1197 ClassTemplate2->getIdentifier()) ||
1198 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1199 Equivalent = false;
1200 } else {
1201 // Class template/non-class-template mismatch.
1202 Equivalent = false;
1203 }
1204 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1205 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1206 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1207 Equivalent = false;
1208 } else {
1209 // Kind mismatch.
1210 Equivalent = false;
1211 }
1212 } else if (NonTypeTemplateParmDecl *NTTP1
1213 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1214 if (NonTypeTemplateParmDecl *NTTP2
1215 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1216 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1217 Equivalent = false;
1218 } else {
1219 // Kind mismatch.
1220 Equivalent = false;
1221 }
1222 } else if (TemplateTemplateParmDecl *TTP1
1223 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1224 if (TemplateTemplateParmDecl *TTP2
1225 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1226 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1227 Equivalent = false;
1228 } else {
1229 // Kind mismatch.
1230 Equivalent = false;
1231 }
1232 }
1233
Douglas Gregorea35d112010-02-15 23:54:17 +00001234 if (!Equivalent) {
1235 // Note that these two declarations are not equivalent (and we already
1236 // know about it).
1237 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1238 D2->getCanonicalDecl()));
1239 return true;
1240 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001241 // FIXME: Check other declaration kinds!
1242 }
1243
1244 return false;
1245}
1246
1247//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001248// Import Types
1249//----------------------------------------------------------------------------
1250
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001251QualType ASTNodeImporter::VisitType(Type *T) {
1252 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1253 << T->getTypeClassName();
1254 return QualType();
1255}
1256
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001257QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1258 switch (T->getKind()) {
1259 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1260 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1261
1262 case BuiltinType::Char_U:
1263 // The context we're importing from has an unsigned 'char'. If we're
1264 // importing into a context with a signed 'char', translate to
1265 // 'unsigned char' instead.
1266 if (Importer.getToContext().getLangOptions().CharIsSigned)
1267 return Importer.getToContext().UnsignedCharTy;
1268
1269 return Importer.getToContext().CharTy;
1270
1271 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1272
1273 case BuiltinType::Char16:
1274 // FIXME: Make sure that the "to" context supports C++!
1275 return Importer.getToContext().Char16Ty;
1276
1277 case BuiltinType::Char32:
1278 // FIXME: Make sure that the "to" context supports C++!
1279 return Importer.getToContext().Char32Ty;
1280
1281 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1282 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1283 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1284 case BuiltinType::ULongLong:
1285 return Importer.getToContext().UnsignedLongLongTy;
1286 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1287
1288 case BuiltinType::Char_S:
1289 // The context we're importing from has an unsigned 'char'. If we're
1290 // importing into a context with a signed 'char', translate to
1291 // 'unsigned char' instead.
1292 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1293 return Importer.getToContext().SignedCharTy;
1294
1295 return Importer.getToContext().CharTy;
1296
1297 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001298 case BuiltinType::WChar_S:
1299 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001300 // FIXME: If not in C++, shall we translate to the C equivalent of
1301 // wchar_t?
1302 return Importer.getToContext().WCharTy;
1303
1304 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1305 case BuiltinType::Int : return Importer.getToContext().IntTy;
1306 case BuiltinType::Long : return Importer.getToContext().LongTy;
1307 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1308 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1309 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1310 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1311 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1312
1313 case BuiltinType::NullPtr:
1314 // FIXME: Make sure that the "to" context supports C++0x!
1315 return Importer.getToContext().NullPtrTy;
1316
1317 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1318 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1319 case BuiltinType::UndeducedAuto:
1320 // FIXME: Make sure that the "to" context supports C++0x!
1321 return Importer.getToContext().UndeducedAutoTy;
1322
1323 case BuiltinType::ObjCId:
1324 // FIXME: Make sure that the "to" context supports Objective-C!
1325 return Importer.getToContext().ObjCBuiltinIdTy;
1326
1327 case BuiltinType::ObjCClass:
1328 return Importer.getToContext().ObjCBuiltinClassTy;
1329
1330 case BuiltinType::ObjCSel:
1331 return Importer.getToContext().ObjCBuiltinSelTy;
1332 }
1333
1334 return QualType();
1335}
1336
1337QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1338 QualType ToElementType = Importer.Import(T->getElementType());
1339 if (ToElementType.isNull())
1340 return QualType();
1341
1342 return Importer.getToContext().getComplexType(ToElementType);
1343}
1344
1345QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1346 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1347 if (ToPointeeType.isNull())
1348 return QualType();
1349
1350 return Importer.getToContext().getPointerType(ToPointeeType);
1351}
1352
1353QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1354 // FIXME: Check for blocks support in "to" context.
1355 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1356 if (ToPointeeType.isNull())
1357 return QualType();
1358
1359 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1360}
1361
1362QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1363 // FIXME: Check for C++ support in "to" context.
1364 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1365 if (ToPointeeType.isNull())
1366 return QualType();
1367
1368 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1369}
1370
1371QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1372 // FIXME: Check for C++0x support in "to" context.
1373 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1374 if (ToPointeeType.isNull())
1375 return QualType();
1376
1377 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1378}
1379
1380QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1381 // FIXME: Check for C++ support in "to" context.
1382 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1383 if (ToPointeeType.isNull())
1384 return QualType();
1385
1386 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1387 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1388 ClassType.getTypePtr());
1389}
1390
1391QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1392 QualType ToElementType = Importer.Import(T->getElementType());
1393 if (ToElementType.isNull())
1394 return QualType();
1395
1396 return Importer.getToContext().getConstantArrayType(ToElementType,
1397 T->getSize(),
1398 T->getSizeModifier(),
1399 T->getIndexTypeCVRQualifiers());
1400}
1401
1402QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1403 QualType ToElementType = Importer.Import(T->getElementType());
1404 if (ToElementType.isNull())
1405 return QualType();
1406
1407 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1408 T->getSizeModifier(),
1409 T->getIndexTypeCVRQualifiers());
1410}
1411
1412QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1413 QualType ToElementType = Importer.Import(T->getElementType());
1414 if (ToElementType.isNull())
1415 return QualType();
1416
1417 Expr *Size = Importer.Import(T->getSizeExpr());
1418 if (!Size)
1419 return QualType();
1420
1421 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1422 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1423 T->getSizeModifier(),
1424 T->getIndexTypeCVRQualifiers(),
1425 Brackets);
1426}
1427
1428QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1429 QualType ToElementType = Importer.Import(T->getElementType());
1430 if (ToElementType.isNull())
1431 return QualType();
1432
1433 return Importer.getToContext().getVectorType(ToElementType,
1434 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001435 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001436}
1437
1438QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1439 QualType ToElementType = Importer.Import(T->getElementType());
1440 if (ToElementType.isNull())
1441 return QualType();
1442
1443 return Importer.getToContext().getExtVectorType(ToElementType,
1444 T->getNumElements());
1445}
1446
1447QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1448 // FIXME: What happens if we're importing a function without a prototype
1449 // into C++? Should we make it variadic?
1450 QualType ToResultType = Importer.Import(T->getResultType());
1451 if (ToResultType.isNull())
1452 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001453
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001454 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001455 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001456}
1457
1458QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1459 QualType ToResultType = Importer.Import(T->getResultType());
1460 if (ToResultType.isNull())
1461 return QualType();
1462
1463 // Import argument types
1464 llvm::SmallVector<QualType, 4> ArgTypes;
1465 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1466 AEnd = T->arg_type_end();
1467 A != AEnd; ++A) {
1468 QualType ArgType = Importer.Import(*A);
1469 if (ArgType.isNull())
1470 return QualType();
1471 ArgTypes.push_back(ArgType);
1472 }
1473
1474 // Import exception types
1475 llvm::SmallVector<QualType, 4> ExceptionTypes;
1476 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1477 EEnd = T->exception_end();
1478 E != EEnd; ++E) {
1479 QualType ExceptionType = Importer.Import(*E);
1480 if (ExceptionType.isNull())
1481 return QualType();
1482 ExceptionTypes.push_back(ExceptionType);
1483 }
John McCalle23cf432010-12-14 08:05:40 +00001484
1485 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1486 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001487
1488 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001489 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490}
1491
1492QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1493 TypedefDecl *ToDecl
1494 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1495 if (!ToDecl)
1496 return QualType();
1497
1498 return Importer.getToContext().getTypeDeclType(ToDecl);
1499}
1500
1501QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1502 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1503 if (!ToExpr)
1504 return QualType();
1505
1506 return Importer.getToContext().getTypeOfExprType(ToExpr);
1507}
1508
1509QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1510 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1511 if (ToUnderlyingType.isNull())
1512 return QualType();
1513
1514 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1515}
1516
1517QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1518 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1519 if (!ToExpr)
1520 return QualType();
1521
1522 return Importer.getToContext().getDecltypeType(ToExpr);
1523}
1524
1525QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1526 RecordDecl *ToDecl
1527 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1528 if (!ToDecl)
1529 return QualType();
1530
1531 return Importer.getToContext().getTagDeclType(ToDecl);
1532}
1533
1534QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1535 EnumDecl *ToDecl
1536 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1537 if (!ToDecl)
1538 return QualType();
1539
1540 return Importer.getToContext().getTagDeclType(ToDecl);
1541}
1542
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001543QualType ASTNodeImporter::VisitTemplateSpecializationType(
1544 TemplateSpecializationType *T) {
1545 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1546 if (ToTemplate.isNull())
1547 return QualType();
1548
1549 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1550 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1551 return QualType();
1552
1553 QualType ToCanonType;
1554 if (!QualType(T, 0).isCanonical()) {
1555 QualType FromCanonType
1556 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1557 ToCanonType =Importer.Import(FromCanonType);
1558 if (ToCanonType.isNull())
1559 return QualType();
1560 }
1561 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1562 ToTemplateArgs.data(),
1563 ToTemplateArgs.size(),
1564 ToCanonType);
1565}
1566
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001567QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001568 NestedNameSpecifier *ToQualifier = 0;
1569 // Note: the qualifier in an ElaboratedType is optional.
1570 if (T->getQualifier()) {
1571 ToQualifier = Importer.Import(T->getQualifier());
1572 if (!ToQualifier)
1573 return QualType();
1574 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001575
1576 QualType ToNamedType = Importer.Import(T->getNamedType());
1577 if (ToNamedType.isNull())
1578 return QualType();
1579
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001580 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1581 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001582}
1583
1584QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1585 ObjCInterfaceDecl *Class
1586 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1587 if (!Class)
1588 return QualType();
1589
John McCallc12c5bb2010-05-15 11:32:37 +00001590 return Importer.getToContext().getObjCInterfaceType(Class);
1591}
1592
1593QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1594 QualType ToBaseType = Importer.Import(T->getBaseType());
1595 if (ToBaseType.isNull())
1596 return QualType();
1597
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001598 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001599 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001600 PEnd = T->qual_end();
1601 P != PEnd; ++P) {
1602 ObjCProtocolDecl *Protocol
1603 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1604 if (!Protocol)
1605 return QualType();
1606 Protocols.push_back(Protocol);
1607 }
1608
John McCallc12c5bb2010-05-15 11:32:37 +00001609 return Importer.getToContext().getObjCObjectType(ToBaseType,
1610 Protocols.data(),
1611 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001612}
1613
1614QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1615 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1616 if (ToPointeeType.isNull())
1617 return QualType();
1618
John McCallc12c5bb2010-05-15 11:32:37 +00001619 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001620}
1621
Douglas Gregor089459a2010-02-08 21:09:39 +00001622//----------------------------------------------------------------------------
1623// Import Declarations
1624//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001625bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1626 DeclContext *&LexicalDC,
1627 DeclarationName &Name,
1628 SourceLocation &Loc) {
1629 // Import the context of this declaration.
1630 DC = Importer.ImportContext(D->getDeclContext());
1631 if (!DC)
1632 return true;
1633
1634 LexicalDC = DC;
1635 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1636 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1637 if (!LexicalDC)
1638 return true;
1639 }
1640
1641 // Import the name of this declaration.
1642 Name = Importer.Import(D->getDeclName());
1643 if (D->getDeclName() && !Name)
1644 return true;
1645
1646 // Import the location of this declaration.
1647 Loc = Importer.Import(D->getLocation());
1648 return false;
1649}
1650
Abramo Bagnara25777432010-08-11 22:01:17 +00001651void
1652ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1653 DeclarationNameInfo& To) {
1654 // NOTE: To.Name and To.Loc are already imported.
1655 // We only have to import To.LocInfo.
1656 switch (To.getName().getNameKind()) {
1657 case DeclarationName::Identifier:
1658 case DeclarationName::ObjCZeroArgSelector:
1659 case DeclarationName::ObjCOneArgSelector:
1660 case DeclarationName::ObjCMultiArgSelector:
1661 case DeclarationName::CXXUsingDirective:
1662 return;
1663
1664 case DeclarationName::CXXOperatorName: {
1665 SourceRange Range = From.getCXXOperatorNameRange();
1666 To.setCXXOperatorNameRange(Importer.Import(Range));
1667 return;
1668 }
1669 case DeclarationName::CXXLiteralOperatorName: {
1670 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1671 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1672 return;
1673 }
1674 case DeclarationName::CXXConstructorName:
1675 case DeclarationName::CXXDestructorName:
1676 case DeclarationName::CXXConversionFunctionName: {
1677 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1678 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1679 return;
1680 }
1681 assert(0 && "Unknown name kind.");
1682 }
1683}
1684
Douglas Gregor083a8212010-02-21 18:24:45 +00001685void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1686 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1687 FromEnd = FromDC->decls_end();
1688 From != FromEnd;
1689 ++From)
1690 Importer.Import(*From);
1691}
1692
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001693bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1694 if (To->getDefinition())
1695 return false;
1696
1697 To->startDefinition();
1698
1699 // Add base classes.
1700 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1701 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1702
1703 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1704 for (CXXRecordDecl::base_class_iterator
1705 Base1 = FromCXX->bases_begin(),
1706 FromBaseEnd = FromCXX->bases_end();
1707 Base1 != FromBaseEnd;
1708 ++Base1) {
1709 QualType T = Importer.Import(Base1->getType());
1710 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001711 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001712
1713 SourceLocation EllipsisLoc;
1714 if (Base1->isPackExpansion())
1715 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001716
1717 Bases.push_back(
1718 new (Importer.getToContext())
1719 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1720 Base1->isVirtual(),
1721 Base1->isBaseOfClass(),
1722 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001723 Importer.Import(Base1->getTypeSourceInfo()),
1724 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001725 }
1726 if (!Bases.empty())
1727 ToCXX->setBases(Bases.data(), Bases.size());
1728 }
1729
1730 ImportDeclContext(From);
1731 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001732 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001733}
1734
Douglas Gregor040afae2010-11-30 19:14:50 +00001735TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1736 TemplateParameterList *Params) {
1737 llvm::SmallVector<NamedDecl *, 4> ToParams;
1738 ToParams.reserve(Params->size());
1739 for (TemplateParameterList::iterator P = Params->begin(),
1740 PEnd = Params->end();
1741 P != PEnd; ++P) {
1742 Decl *To = Importer.Import(*P);
1743 if (!To)
1744 return 0;
1745
1746 ToParams.push_back(cast<NamedDecl>(To));
1747 }
1748
1749 return TemplateParameterList::Create(Importer.getToContext(),
1750 Importer.Import(Params->getTemplateLoc()),
1751 Importer.Import(Params->getLAngleLoc()),
1752 ToParams.data(), ToParams.size(),
1753 Importer.Import(Params->getRAngleLoc()));
1754}
1755
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001756TemplateArgument
1757ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1758 switch (From.getKind()) {
1759 case TemplateArgument::Null:
1760 return TemplateArgument();
1761
1762 case TemplateArgument::Type: {
1763 QualType ToType = Importer.Import(From.getAsType());
1764 if (ToType.isNull())
1765 return TemplateArgument();
1766 return TemplateArgument(ToType);
1767 }
1768
1769 case TemplateArgument::Integral: {
1770 QualType ToType = Importer.Import(From.getIntegralType());
1771 if (ToType.isNull())
1772 return TemplateArgument();
1773 return TemplateArgument(*From.getAsIntegral(), ToType);
1774 }
1775
1776 case TemplateArgument::Declaration:
1777 if (Decl *To = Importer.Import(From.getAsDecl()))
1778 return TemplateArgument(To);
1779 return TemplateArgument();
1780
1781 case TemplateArgument::Template: {
1782 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1783 if (ToTemplate.isNull())
1784 return TemplateArgument();
1785
1786 return TemplateArgument(ToTemplate);
1787 }
1788
1789 case TemplateArgument::Expression:
1790 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1791 return TemplateArgument(ToExpr);
1792 return TemplateArgument();
1793
1794 case TemplateArgument::Pack: {
1795 llvm::SmallVector<TemplateArgument, 2> ToPack;
1796 ToPack.reserve(From.pack_size());
1797 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1798 return TemplateArgument();
1799
1800 TemplateArgument *ToArgs
1801 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1802 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1803 return TemplateArgument(ToArgs, ToPack.size());
1804 }
1805 }
1806
1807 llvm_unreachable("Invalid template argument kind");
1808 return TemplateArgument();
1809}
1810
1811bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1812 unsigned NumFromArgs,
1813 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1814 for (unsigned I = 0; I != NumFromArgs; ++I) {
1815 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1816 if (To.isNull() && !FromArgs[I].isNull())
1817 return true;
1818
1819 ToArgs.push_back(To);
1820 }
1821
1822 return false;
1823}
1824
Douglas Gregor96a01b42010-02-11 00:48:18 +00001825bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001826 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001827 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001828 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001829 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001830 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001831}
1832
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001833bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001834 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001835 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001836 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001837 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001838}
1839
Douglas Gregor040afae2010-11-30 19:14:50 +00001840bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1841 ClassTemplateDecl *To) {
1842 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1843 Importer.getToContext(),
1844 Importer.getNonEquivalentDecls());
1845 return Ctx.IsStructurallyEquivalent(From, To);
1846}
1847
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001848Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001849 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001850 << D->getDeclKindName();
1851 return 0;
1852}
1853
Douglas Gregor788c62d2010-02-21 18:26:36 +00001854Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1855 // Import the major distinguishing characteristics of this namespace.
1856 DeclContext *DC, *LexicalDC;
1857 DeclarationName Name;
1858 SourceLocation Loc;
1859 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1860 return 0;
1861
1862 NamespaceDecl *MergeWithNamespace = 0;
1863 if (!Name) {
1864 // This is an anonymous namespace. Adopt an existing anonymous
1865 // namespace if we can.
1866 // FIXME: Not testable.
1867 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1868 MergeWithNamespace = TU->getAnonymousNamespace();
1869 else
1870 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1871 } else {
1872 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1873 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1874 Lookup.first != Lookup.second;
1875 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001876 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001877 continue;
1878
1879 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1880 MergeWithNamespace = FoundNS;
1881 ConflictingDecls.clear();
1882 break;
1883 }
1884
1885 ConflictingDecls.push_back(*Lookup.first);
1886 }
1887
1888 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001889 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001890 ConflictingDecls.data(),
1891 ConflictingDecls.size());
1892 }
1893 }
1894
1895 // Create the "to" namespace, if needed.
1896 NamespaceDecl *ToNamespace = MergeWithNamespace;
1897 if (!ToNamespace) {
1898 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1899 Name.getAsIdentifierInfo());
1900 ToNamespace->setLexicalDeclContext(LexicalDC);
1901 LexicalDC->addDecl(ToNamespace);
1902
1903 // If this is an anonymous namespace, register it as the anonymous
1904 // namespace within its context.
1905 if (!Name) {
1906 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1907 TU->setAnonymousNamespace(ToNamespace);
1908 else
1909 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1910 }
1911 }
1912 Importer.Imported(D, ToNamespace);
1913
1914 ImportDeclContext(D);
1915
1916 return ToNamespace;
1917}
1918
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001919Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1920 // Import the major distinguishing characteristics of this typedef.
1921 DeclContext *DC, *LexicalDC;
1922 DeclarationName Name;
1923 SourceLocation Loc;
1924 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1925 return 0;
1926
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001927 // If this typedef is not in block scope, determine whether we've
1928 // seen a typedef with the same name (that we can merge with) or any
1929 // other entity by that name (which name lookup could conflict with).
1930 if (!DC->isFunctionOrMethod()) {
1931 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1932 unsigned IDNS = Decl::IDNS_Ordinary;
1933 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1934 Lookup.first != Lookup.second;
1935 ++Lookup.first) {
1936 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1937 continue;
1938 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001939 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1940 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001941 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001942 }
1943
1944 ConflictingDecls.push_back(*Lookup.first);
1945 }
1946
1947 if (!ConflictingDecls.empty()) {
1948 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1949 ConflictingDecls.data(),
1950 ConflictingDecls.size());
1951 if (!Name)
1952 return 0;
1953 }
1954 }
1955
Douglas Gregorea35d112010-02-15 23:54:17 +00001956 // Import the underlying type of this typedef;
1957 QualType T = Importer.Import(D->getUnderlyingType());
1958 if (T.isNull())
1959 return 0;
1960
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001961 // Create the new typedef node.
1962 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1963 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1964 Loc, Name.getAsIdentifierInfo(),
1965 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001966 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001967 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001968 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001969 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001970
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001971 return ToTypedef;
1972}
1973
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001974Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1975 // Import the major distinguishing characteristics of this enum.
1976 DeclContext *DC, *LexicalDC;
1977 DeclarationName Name;
1978 SourceLocation Loc;
1979 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1980 return 0;
1981
1982 // Figure out what enum name we're looking for.
1983 unsigned IDNS = Decl::IDNS_Tag;
1984 DeclarationName SearchName = Name;
1985 if (!SearchName && D->getTypedefForAnonDecl()) {
1986 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1987 IDNS = Decl::IDNS_Ordinary;
1988 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1989 IDNS |= Decl::IDNS_Ordinary;
1990
1991 // We may already have an enum of the same name; try to find and match it.
1992 if (!DC->isFunctionOrMethod() && SearchName) {
1993 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1994 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1995 Lookup.first != Lookup.second;
1996 ++Lookup.first) {
1997 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1998 continue;
1999
2000 Decl *Found = *Lookup.first;
2001 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2002 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2003 Found = Tag->getDecl();
2004 }
2005
2006 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002007 if (IsStructuralMatch(D, FoundEnum))
2008 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002009 }
2010
2011 ConflictingDecls.push_back(*Lookup.first);
2012 }
2013
2014 if (!ConflictingDecls.empty()) {
2015 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2016 ConflictingDecls.data(),
2017 ConflictingDecls.size());
2018 }
2019 }
2020
2021 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002022 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002023 Name.getAsIdentifierInfo(),
2024 Importer.Import(D->getTagKeywordLoc()), 0,
2025 D->isScoped(), D->isScopedUsingClassTag(),
2026 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002027 // Import the qualifier, if any.
2028 if (D->getQualifier()) {
2029 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2030 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2031 D2->setQualifierInfo(NNS, NNSRange);
2032 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002033 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002034 D2->setLexicalDeclContext(LexicalDC);
2035 Importer.Imported(D, D2);
2036 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002037
2038 // Import the integer type.
2039 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2040 if (ToIntegerType.isNull())
2041 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002042 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002043
2044 // Import the definition
2045 if (D->isDefinition()) {
2046 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2047 if (T.isNull())
2048 return 0;
2049
2050 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2051 if (ToPromotionType.isNull())
2052 return 0;
2053
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002054 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002055 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002056
2057 // FIXME: we might need to merge the number of positive or negative bits
2058 // if the enumerator lists don't match.
2059 D2->completeDefinition(T, ToPromotionType,
2060 D->getNumPositiveBits(),
2061 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002062 }
2063
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002064 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002065}
2066
Douglas Gregor96a01b42010-02-11 00:48:18 +00002067Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2068 // If this record has a definition in the translation unit we're coming from,
2069 // but this particular declaration is not that definition, import the
2070 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002071 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002072 if (Definition && Definition != D) {
2073 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002074 if (!ImportedDef)
2075 return 0;
2076
2077 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002078 }
2079
2080 // Import the major distinguishing characteristics of this record.
2081 DeclContext *DC, *LexicalDC;
2082 DeclarationName Name;
2083 SourceLocation Loc;
2084 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2085 return 0;
2086
2087 // Figure out what structure name we're looking for.
2088 unsigned IDNS = Decl::IDNS_Tag;
2089 DeclarationName SearchName = Name;
2090 if (!SearchName && D->getTypedefForAnonDecl()) {
2091 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2092 IDNS = Decl::IDNS_Ordinary;
2093 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2094 IDNS |= Decl::IDNS_Ordinary;
2095
2096 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002097 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002098 if (!DC->isFunctionOrMethod() && SearchName) {
2099 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2100 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2101 Lookup.first != Lookup.second;
2102 ++Lookup.first) {
2103 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2104 continue;
2105
2106 Decl *Found = *Lookup.first;
2107 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2108 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2109 Found = Tag->getDecl();
2110 }
2111
2112 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002113 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2114 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2115 // The record types structurally match, or the "from" translation
2116 // unit only had a forward declaration anyway; call it the same
2117 // function.
2118 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002119 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002120 }
2121 } else {
2122 // We have a forward declaration of this type, so adopt that forward
2123 // declaration rather than building a new one.
2124 AdoptDecl = FoundRecord;
2125 continue;
2126 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002127 }
2128
2129 ConflictingDecls.push_back(*Lookup.first);
2130 }
2131
2132 if (!ConflictingDecls.empty()) {
2133 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2134 ConflictingDecls.data(),
2135 ConflictingDecls.size());
2136 }
2137 }
2138
2139 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002140 RecordDecl *D2 = AdoptDecl;
2141 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002142 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002143 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002144 D->getTagKind(),
2145 DC, Loc,
2146 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002147 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002148 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002149 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002150 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002151 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002152 DC, Loc,
2153 Name.getAsIdentifierInfo(),
2154 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002155 }
John McCallb6217662010-03-15 10:12:16 +00002156 // Import the qualifier, if any.
2157 if (D->getQualifier()) {
2158 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2159 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2160 D2->setQualifierInfo(NNS, NNSRange);
2161 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002162 D2->setLexicalDeclContext(LexicalDC);
2163 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002164 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002165
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002166 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002167
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002168 if (D->isDefinition() && ImportDefinition(D, D2))
2169 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002170
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002171 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002172}
2173
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002174Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2175 // Import the major distinguishing characteristics of this enumerator.
2176 DeclContext *DC, *LexicalDC;
2177 DeclarationName Name;
2178 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002179 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002180 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002181
2182 QualType T = Importer.Import(D->getType());
2183 if (T.isNull())
2184 return 0;
2185
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002186 // Determine whether there are any other declarations with the same name and
2187 // in the same context.
2188 if (!LexicalDC->isFunctionOrMethod()) {
2189 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2190 unsigned IDNS = Decl::IDNS_Ordinary;
2191 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2192 Lookup.first != Lookup.second;
2193 ++Lookup.first) {
2194 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2195 continue;
2196
2197 ConflictingDecls.push_back(*Lookup.first);
2198 }
2199
2200 if (!ConflictingDecls.empty()) {
2201 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2202 ConflictingDecls.data(),
2203 ConflictingDecls.size());
2204 if (!Name)
2205 return 0;
2206 }
2207 }
2208
2209 Expr *Init = Importer.Import(D->getInitExpr());
2210 if (D->getInitExpr() && !Init)
2211 return 0;
2212
2213 EnumConstantDecl *ToEnumerator
2214 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2215 Name.getAsIdentifierInfo(), T,
2216 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002217 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002218 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002219 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002220 LexicalDC->addDecl(ToEnumerator);
2221 return ToEnumerator;
2222}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002223
Douglas Gregora404ea62010-02-10 19:54:31 +00002224Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2225 // Import the major distinguishing characteristics of this function.
2226 DeclContext *DC, *LexicalDC;
2227 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002228 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002229 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002230 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002231
Douglas Gregora404ea62010-02-10 19:54:31 +00002232 // Try to find a function in our own ("to") context with the same name, same
2233 // type, and in the same context as the function we're importing.
2234 if (!LexicalDC->isFunctionOrMethod()) {
2235 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2236 unsigned IDNS = Decl::IDNS_Ordinary;
2237 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2238 Lookup.first != Lookup.second;
2239 ++Lookup.first) {
2240 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2241 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002242
Douglas Gregora404ea62010-02-10 19:54:31 +00002243 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2244 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2245 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002246 if (Importer.IsStructurallyEquivalent(D->getType(),
2247 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002248 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002249 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002250 }
2251
2252 // FIXME: Check for overloading more carefully, e.g., by boosting
2253 // Sema::IsOverload out to the AST library.
2254
2255 // Function overloading is okay in C++.
2256 if (Importer.getToContext().getLangOptions().CPlusPlus)
2257 continue;
2258
2259 // Complain about inconsistent function types.
2260 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002261 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002262 Importer.ToDiag(FoundFunction->getLocation(),
2263 diag::note_odr_value_here)
2264 << FoundFunction->getType();
2265 }
2266 }
2267
2268 ConflictingDecls.push_back(*Lookup.first);
2269 }
2270
2271 if (!ConflictingDecls.empty()) {
2272 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2273 ConflictingDecls.data(),
2274 ConflictingDecls.size());
2275 if (!Name)
2276 return 0;
2277 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002278 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002279
Abramo Bagnara25777432010-08-11 22:01:17 +00002280 DeclarationNameInfo NameInfo(Name, Loc);
2281 // Import additional name location/type info.
2282 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2283
Douglas Gregorea35d112010-02-15 23:54:17 +00002284 // Import the type.
2285 QualType T = Importer.Import(D->getType());
2286 if (T.isNull())
2287 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002288
2289 // Import the function parameters.
2290 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2291 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2292 P != PEnd; ++P) {
2293 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2294 if (!ToP)
2295 return 0;
2296
2297 Parameters.push_back(ToP);
2298 }
2299
2300 // Create the imported function.
2301 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002302 FunctionDecl *ToFunction = 0;
2303 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2304 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2305 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002306 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002307 FromConstructor->isExplicit(),
2308 D->isInlineSpecified(),
2309 D->isImplicit());
2310 } else if (isa<CXXDestructorDecl>(D)) {
2311 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2312 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002313 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002314 D->isInlineSpecified(),
2315 D->isImplicit());
2316 } else if (CXXConversionDecl *FromConversion
2317 = dyn_cast<CXXConversionDecl>(D)) {
2318 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2319 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002320 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002321 D->isInlineSpecified(),
2322 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002323 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2324 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2325 cast<CXXRecordDecl>(DC),
2326 NameInfo, T, TInfo,
2327 Method->isStatic(),
2328 Method->getStorageClassAsWritten(),
2329 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002330 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002331 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2332 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002333 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002334 D->isInlineSpecified(),
2335 D->hasWrittenPrototype());
2336 }
John McCallb6217662010-03-15 10:12:16 +00002337
2338 // Import the qualifier, if any.
2339 if (D->getQualifier()) {
2340 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2341 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2342 ToFunction->setQualifierInfo(NNS, NNSRange);
2343 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002344 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002345 ToFunction->setLexicalDeclContext(LexicalDC);
2346 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002347
Douglas Gregora404ea62010-02-10 19:54:31 +00002348 // Set the parameters.
2349 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002350 Parameters[I]->setOwningFunction(ToFunction);
2351 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002352 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002353 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002354
2355 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002356
2357 // Add this function to the lexical context.
2358 LexicalDC->addDecl(ToFunction);
2359
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002360 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002361}
2362
Douglas Gregorc144f352010-02-21 18:29:16 +00002363Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2364 return VisitFunctionDecl(D);
2365}
2366
2367Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2368 return VisitCXXMethodDecl(D);
2369}
2370
2371Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2372 return VisitCXXMethodDecl(D);
2373}
2374
2375Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2376 return VisitCXXMethodDecl(D);
2377}
2378
Douglas Gregor96a01b42010-02-11 00:48:18 +00002379Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2380 // Import the major distinguishing characteristics of a variable.
2381 DeclContext *DC, *LexicalDC;
2382 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002383 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002384 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2385 return 0;
2386
2387 // Import the type.
2388 QualType T = Importer.Import(D->getType());
2389 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002390 return 0;
2391
2392 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2393 Expr *BitWidth = Importer.Import(D->getBitWidth());
2394 if (!BitWidth && D->getBitWidth())
2395 return 0;
2396
2397 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2398 Loc, Name.getAsIdentifierInfo(),
2399 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002400 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002401 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002402 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002403 LexicalDC->addDecl(ToField);
2404 return ToField;
2405}
2406
Francois Pichet87c2e122010-11-21 06:08:52 +00002407Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2408 // Import the major distinguishing characteristics of a variable.
2409 DeclContext *DC, *LexicalDC;
2410 DeclarationName Name;
2411 SourceLocation Loc;
2412 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2413 return 0;
2414
2415 // Import the type.
2416 QualType T = Importer.Import(D->getType());
2417 if (T.isNull())
2418 return 0;
2419
2420 NamedDecl **NamedChain =
2421 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2422
2423 unsigned i = 0;
2424 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2425 PE = D->chain_end(); PI != PE; ++PI) {
2426 Decl* D = Importer.Import(*PI);
2427 if (!D)
2428 return 0;
2429 NamedChain[i++] = cast<NamedDecl>(D);
2430 }
2431
2432 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2433 Importer.getToContext(), DC,
2434 Loc, Name.getAsIdentifierInfo(), T,
2435 NamedChain, D->getChainingSize());
2436 ToIndirectField->setAccess(D->getAccess());
2437 ToIndirectField->setLexicalDeclContext(LexicalDC);
2438 Importer.Imported(D, ToIndirectField);
2439 LexicalDC->addDecl(ToIndirectField);
2440 return ToIndirectField;
2441}
2442
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002443Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2444 // Import the major distinguishing characteristics of an ivar.
2445 DeclContext *DC, *LexicalDC;
2446 DeclarationName Name;
2447 SourceLocation Loc;
2448 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2449 return 0;
2450
2451 // Determine whether we've already imported this ivar
2452 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2453 Lookup.first != Lookup.second;
2454 ++Lookup.first) {
2455 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2456 if (Importer.IsStructurallyEquivalent(D->getType(),
2457 FoundIvar->getType())) {
2458 Importer.Imported(D, FoundIvar);
2459 return FoundIvar;
2460 }
2461
2462 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2463 << Name << D->getType() << FoundIvar->getType();
2464 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2465 << FoundIvar->getType();
2466 return 0;
2467 }
2468 }
2469
2470 // Import the type.
2471 QualType T = Importer.Import(D->getType());
2472 if (T.isNull())
2473 return 0;
2474
2475 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2476 Expr *BitWidth = Importer.Import(D->getBitWidth());
2477 if (!BitWidth && D->getBitWidth())
2478 return 0;
2479
Daniel Dunbara0654922010-04-02 20:10:03 +00002480 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2481 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002482 Loc, Name.getAsIdentifierInfo(),
2483 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002484 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002485 ToIvar->setLexicalDeclContext(LexicalDC);
2486 Importer.Imported(D, ToIvar);
2487 LexicalDC->addDecl(ToIvar);
2488 return ToIvar;
2489
2490}
2491
Douglas Gregora404ea62010-02-10 19:54:31 +00002492Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2493 // Import the major distinguishing characteristics of a variable.
2494 DeclContext *DC, *LexicalDC;
2495 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002496 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002497 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002498 return 0;
2499
Douglas Gregor089459a2010-02-08 21:09:39 +00002500 // Try to find a variable in our own ("to") context with the same name and
2501 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002502 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002503 VarDecl *MergeWithVar = 0;
2504 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2505 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002506 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002507 Lookup.first != Lookup.second;
2508 ++Lookup.first) {
2509 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2510 continue;
2511
2512 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2513 // We have found a variable that we may need to merge with. Check it.
2514 if (isExternalLinkage(FoundVar->getLinkage()) &&
2515 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002516 if (Importer.IsStructurallyEquivalent(D->getType(),
2517 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002518 MergeWithVar = FoundVar;
2519 break;
2520 }
2521
Douglas Gregord0145422010-02-12 17:23:39 +00002522 const ArrayType *FoundArray
2523 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2524 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002525 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002526 if (FoundArray && TArray) {
2527 if (isa<IncompleteArrayType>(FoundArray) &&
2528 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002529 // Import the type.
2530 QualType T = Importer.Import(D->getType());
2531 if (T.isNull())
2532 return 0;
2533
Douglas Gregord0145422010-02-12 17:23:39 +00002534 FoundVar->setType(T);
2535 MergeWithVar = FoundVar;
2536 break;
2537 } else if (isa<IncompleteArrayType>(TArray) &&
2538 isa<ConstantArrayType>(FoundArray)) {
2539 MergeWithVar = FoundVar;
2540 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002541 }
2542 }
2543
Douglas Gregor089459a2010-02-08 21:09:39 +00002544 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002545 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002546 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2547 << FoundVar->getType();
2548 }
2549 }
2550
2551 ConflictingDecls.push_back(*Lookup.first);
2552 }
2553
2554 if (MergeWithVar) {
2555 // An equivalent variable with external linkage has been found. Link
2556 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002557 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002558
2559 if (VarDecl *DDef = D->getDefinition()) {
2560 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2561 Importer.ToDiag(ExistingDef->getLocation(),
2562 diag::err_odr_variable_multiple_def)
2563 << Name;
2564 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2565 } else {
2566 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002567 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002568 }
2569 }
2570
2571 return MergeWithVar;
2572 }
2573
2574 if (!ConflictingDecls.empty()) {
2575 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2576 ConflictingDecls.data(),
2577 ConflictingDecls.size());
2578 if (!Name)
2579 return 0;
2580 }
2581 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002582
Douglas Gregorea35d112010-02-15 23:54:17 +00002583 // Import the type.
2584 QualType T = Importer.Import(D->getType());
2585 if (T.isNull())
2586 return 0;
2587
Douglas Gregor089459a2010-02-08 21:09:39 +00002588 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002589 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002590 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2591 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002592 D->getStorageClass(),
2593 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002594 // Import the qualifier, if any.
2595 if (D->getQualifier()) {
2596 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2597 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2598 ToVar->setQualifierInfo(NNS, NNSRange);
2599 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002600 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002601 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002602 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002603 LexicalDC->addDecl(ToVar);
2604
Douglas Gregor089459a2010-02-08 21:09:39 +00002605 // Merge the initializer.
2606 // FIXME: Can we really import any initializer? Alternatively, we could force
2607 // ourselves to import every declaration of a variable and then only use
2608 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002609 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002610
2611 // FIXME: Other bits to merge?
2612
2613 return ToVar;
2614}
2615
Douglas Gregor2cd00932010-02-17 21:22:52 +00002616Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2617 // Parameters are created in the translation unit's context, then moved
2618 // into the function declaration's context afterward.
2619 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2620
2621 // Import the name of this declaration.
2622 DeclarationName Name = Importer.Import(D->getDeclName());
2623 if (D->getDeclName() && !Name)
2624 return 0;
2625
2626 // Import the location of this declaration.
2627 SourceLocation Loc = Importer.Import(D->getLocation());
2628
2629 // Import the parameter's type.
2630 QualType T = Importer.Import(D->getType());
2631 if (T.isNull())
2632 return 0;
2633
2634 // Create the imported parameter.
2635 ImplicitParamDecl *ToParm
2636 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2637 Loc, Name.getAsIdentifierInfo(),
2638 T);
2639 return Importer.Imported(D, ToParm);
2640}
2641
Douglas Gregora404ea62010-02-10 19:54:31 +00002642Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2643 // Parameters are created in the translation unit's context, then moved
2644 // into the function declaration's context afterward.
2645 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2646
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002647 // Import the name of this declaration.
2648 DeclarationName Name = Importer.Import(D->getDeclName());
2649 if (D->getDeclName() && !Name)
2650 return 0;
2651
Douglas Gregora404ea62010-02-10 19:54:31 +00002652 // Import the location of this declaration.
2653 SourceLocation Loc = Importer.Import(D->getLocation());
2654
2655 // Import the parameter's type.
2656 QualType T = Importer.Import(D->getType());
2657 if (T.isNull())
2658 return 0;
2659
2660 // Create the imported parameter.
2661 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2662 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2663 Loc, Name.getAsIdentifierInfo(),
2664 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002665 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002666 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002667 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002668 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002669}
2670
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002671Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2672 // Import the major distinguishing characteristics of a method.
2673 DeclContext *DC, *LexicalDC;
2674 DeclarationName Name;
2675 SourceLocation Loc;
2676 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2677 return 0;
2678
2679 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2680 Lookup.first != Lookup.second;
2681 ++Lookup.first) {
2682 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2683 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2684 continue;
2685
2686 // Check return types.
2687 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2688 FoundMethod->getResultType())) {
2689 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2690 << D->isInstanceMethod() << Name
2691 << D->getResultType() << FoundMethod->getResultType();
2692 Importer.ToDiag(FoundMethod->getLocation(),
2693 diag::note_odr_objc_method_here)
2694 << D->isInstanceMethod() << Name;
2695 return 0;
2696 }
2697
2698 // Check the number of parameters.
2699 if (D->param_size() != FoundMethod->param_size()) {
2700 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2701 << D->isInstanceMethod() << Name
2702 << D->param_size() << FoundMethod->param_size();
2703 Importer.ToDiag(FoundMethod->getLocation(),
2704 diag::note_odr_objc_method_here)
2705 << D->isInstanceMethod() << Name;
2706 return 0;
2707 }
2708
2709 // Check parameter types.
2710 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2711 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2712 P != PEnd; ++P, ++FoundP) {
2713 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2714 (*FoundP)->getType())) {
2715 Importer.FromDiag((*P)->getLocation(),
2716 diag::err_odr_objc_method_param_type_inconsistent)
2717 << D->isInstanceMethod() << Name
2718 << (*P)->getType() << (*FoundP)->getType();
2719 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2720 << (*FoundP)->getType();
2721 return 0;
2722 }
2723 }
2724
2725 // Check variadic/non-variadic.
2726 // Check the number of parameters.
2727 if (D->isVariadic() != FoundMethod->isVariadic()) {
2728 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2729 << D->isInstanceMethod() << Name;
2730 Importer.ToDiag(FoundMethod->getLocation(),
2731 diag::note_odr_objc_method_here)
2732 << D->isInstanceMethod() << Name;
2733 return 0;
2734 }
2735
2736 // FIXME: Any other bits we need to merge?
2737 return Importer.Imported(D, FoundMethod);
2738 }
2739 }
2740
2741 // Import the result type.
2742 QualType ResultTy = Importer.Import(D->getResultType());
2743 if (ResultTy.isNull())
2744 return 0;
2745
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002746 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2747
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002748 ObjCMethodDecl *ToMethod
2749 = ObjCMethodDecl::Create(Importer.getToContext(),
2750 Loc,
2751 Importer.Import(D->getLocEnd()),
2752 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002753 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002754 D->isInstanceMethod(),
2755 D->isVariadic(),
2756 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002757 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002758 D->getImplementationControl());
2759
2760 // FIXME: When we decide to merge method definitions, we'll need to
2761 // deal with implicit parameters.
2762
2763 // Import the parameters
2764 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2765 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2766 FromPEnd = D->param_end();
2767 FromP != FromPEnd;
2768 ++FromP) {
2769 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2770 if (!ToP)
2771 return 0;
2772
2773 ToParams.push_back(ToP);
2774 }
2775
2776 // Set the parameters.
2777 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2778 ToParams[I]->setOwningFunction(ToMethod);
2779 ToMethod->addDecl(ToParams[I]);
2780 }
2781 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002782 ToParams.data(), ToParams.size(),
2783 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002784
2785 ToMethod->setLexicalDeclContext(LexicalDC);
2786 Importer.Imported(D, ToMethod);
2787 LexicalDC->addDecl(ToMethod);
2788 return ToMethod;
2789}
2790
Douglas Gregorb4677b62010-02-18 01:47:50 +00002791Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2792 // Import the major distinguishing characteristics of a category.
2793 DeclContext *DC, *LexicalDC;
2794 DeclarationName Name;
2795 SourceLocation Loc;
2796 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2797 return 0;
2798
2799 ObjCInterfaceDecl *ToInterface
2800 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2801 if (!ToInterface)
2802 return 0;
2803
2804 // Determine if we've already encountered this category.
2805 ObjCCategoryDecl *MergeWithCategory
2806 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2807 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2808 if (!ToCategory) {
2809 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2810 Importer.Import(D->getAtLoc()),
2811 Loc,
2812 Importer.Import(D->getCategoryNameLoc()),
2813 Name.getAsIdentifierInfo());
2814 ToCategory->setLexicalDeclContext(LexicalDC);
2815 LexicalDC->addDecl(ToCategory);
2816 Importer.Imported(D, ToCategory);
2817
2818 // Link this category into its class's category list.
2819 ToCategory->setClassInterface(ToInterface);
2820 ToCategory->insertNextClassCategory();
2821
2822 // Import protocols
2823 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2824 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2825 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2826 = D->protocol_loc_begin();
2827 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2828 FromProtoEnd = D->protocol_end();
2829 FromProto != FromProtoEnd;
2830 ++FromProto, ++FromProtoLoc) {
2831 ObjCProtocolDecl *ToProto
2832 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2833 if (!ToProto)
2834 return 0;
2835 Protocols.push_back(ToProto);
2836 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2837 }
2838
2839 // FIXME: If we're merging, make sure that the protocol list is the same.
2840 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2841 ProtocolLocs.data(), Importer.getToContext());
2842
2843 } else {
2844 Importer.Imported(D, ToCategory);
2845 }
2846
2847 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002848 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002849
2850 // If we have an implementation, import it as well.
2851 if (D->getImplementation()) {
2852 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002853 = cast_or_null<ObjCCategoryImplDecl>(
2854 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002855 if (!Impl)
2856 return 0;
2857
2858 ToCategory->setImplementation(Impl);
2859 }
2860
2861 return ToCategory;
2862}
2863
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002864Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002865 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002866 DeclContext *DC, *LexicalDC;
2867 DeclarationName Name;
2868 SourceLocation Loc;
2869 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2870 return 0;
2871
2872 ObjCProtocolDecl *MergeWithProtocol = 0;
2873 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2874 Lookup.first != Lookup.second;
2875 ++Lookup.first) {
2876 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2877 continue;
2878
2879 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2880 break;
2881 }
2882
2883 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2884 if (!ToProto || ToProto->isForwardDecl()) {
2885 if (!ToProto) {
2886 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2887 Name.getAsIdentifierInfo());
2888 ToProto->setForwardDecl(D->isForwardDecl());
2889 ToProto->setLexicalDeclContext(LexicalDC);
2890 LexicalDC->addDecl(ToProto);
2891 }
2892 Importer.Imported(D, ToProto);
2893
2894 // Import protocols
2895 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2896 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2897 ObjCProtocolDecl::protocol_loc_iterator
2898 FromProtoLoc = D->protocol_loc_begin();
2899 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2900 FromProtoEnd = D->protocol_end();
2901 FromProto != FromProtoEnd;
2902 ++FromProto, ++FromProtoLoc) {
2903 ObjCProtocolDecl *ToProto
2904 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2905 if (!ToProto)
2906 return 0;
2907 Protocols.push_back(ToProto);
2908 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2909 }
2910
2911 // FIXME: If we're merging, make sure that the protocol list is the same.
2912 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2913 ProtocolLocs.data(), Importer.getToContext());
2914 } else {
2915 Importer.Imported(D, ToProto);
2916 }
2917
Douglas Gregorb4677b62010-02-18 01:47:50 +00002918 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002919 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002920
2921 return ToProto;
2922}
2923
Douglas Gregora12d2942010-02-16 01:20:57 +00002924Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2925 // Import the major distinguishing characteristics of an @interface.
2926 DeclContext *DC, *LexicalDC;
2927 DeclarationName Name;
2928 SourceLocation Loc;
2929 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2930 return 0;
2931
2932 ObjCInterfaceDecl *MergeWithIface = 0;
2933 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2934 Lookup.first != Lookup.second;
2935 ++Lookup.first) {
2936 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2937 continue;
2938
2939 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2940 break;
2941 }
2942
2943 ObjCInterfaceDecl *ToIface = MergeWithIface;
2944 if (!ToIface || ToIface->isForwardDecl()) {
2945 if (!ToIface) {
2946 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2947 DC, Loc,
2948 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002949 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002950 D->isForwardDecl(),
2951 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002952 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002953 ToIface->setLexicalDeclContext(LexicalDC);
2954 LexicalDC->addDecl(ToIface);
2955 }
2956 Importer.Imported(D, ToIface);
2957
Douglas Gregora12d2942010-02-16 01:20:57 +00002958 if (D->getSuperClass()) {
2959 ObjCInterfaceDecl *Super
2960 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2961 if (!Super)
2962 return 0;
2963
2964 ToIface->setSuperClass(Super);
2965 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2966 }
2967
2968 // Import protocols
2969 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2970 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2971 ObjCInterfaceDecl::protocol_loc_iterator
2972 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002973
2974 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00002975 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2976 FromProtoEnd = D->protocol_end();
2977 FromProto != FromProtoEnd;
2978 ++FromProto, ++FromProtoLoc) {
2979 ObjCProtocolDecl *ToProto
2980 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2981 if (!ToProto)
2982 return 0;
2983 Protocols.push_back(ToProto);
2984 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2985 }
2986
2987 // FIXME: If we're merging, make sure that the protocol list is the same.
2988 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2989 ProtocolLocs.data(), Importer.getToContext());
2990
Douglas Gregora12d2942010-02-16 01:20:57 +00002991 // Import @end range
2992 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2993 } else {
2994 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002995
2996 // Check for consistency of superclasses.
2997 DeclarationName FromSuperName, ToSuperName;
2998 if (D->getSuperClass())
2999 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3000 if (ToIface->getSuperClass())
3001 ToSuperName = ToIface->getSuperClass()->getDeclName();
3002 if (FromSuperName != ToSuperName) {
3003 Importer.ToDiag(ToIface->getLocation(),
3004 diag::err_odr_objc_superclass_inconsistent)
3005 << ToIface->getDeclName();
3006 if (ToIface->getSuperClass())
3007 Importer.ToDiag(ToIface->getSuperClassLoc(),
3008 diag::note_odr_objc_superclass)
3009 << ToIface->getSuperClass()->getDeclName();
3010 else
3011 Importer.ToDiag(ToIface->getLocation(),
3012 diag::note_odr_objc_missing_superclass);
3013 if (D->getSuperClass())
3014 Importer.FromDiag(D->getSuperClassLoc(),
3015 diag::note_odr_objc_superclass)
3016 << D->getSuperClass()->getDeclName();
3017 else
3018 Importer.FromDiag(D->getLocation(),
3019 diag::note_odr_objc_missing_superclass);
3020 return 0;
3021 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003022 }
3023
Douglas Gregorb4677b62010-02-18 01:47:50 +00003024 // Import categories. When the categories themselves are imported, they'll
3025 // hook themselves into this interface.
3026 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3027 FromCat = FromCat->getNextClassCategory())
3028 Importer.Import(FromCat);
3029
Douglas Gregora12d2942010-02-16 01:20:57 +00003030 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003031 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003032
3033 // If we have an @implementation, import it as well.
3034 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003035 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3036 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003037 if (!Impl)
3038 return 0;
3039
3040 ToIface->setImplementation(Impl);
3041 }
3042
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003043 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003044}
3045
Douglas Gregor3daef292010-12-07 15:32:12 +00003046Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3047 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3048 Importer.Import(D->getCategoryDecl()));
3049 if (!Category)
3050 return 0;
3051
3052 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3053 if (!ToImpl) {
3054 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3055 if (!DC)
3056 return 0;
3057
3058 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3059 Importer.Import(D->getLocation()),
3060 Importer.Import(D->getIdentifier()),
3061 Category->getClassInterface());
3062
3063 DeclContext *LexicalDC = DC;
3064 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3065 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3066 if (!LexicalDC)
3067 return 0;
3068
3069 ToImpl->setLexicalDeclContext(LexicalDC);
3070 }
3071
3072 LexicalDC->addDecl(ToImpl);
3073 Category->setImplementation(ToImpl);
3074 }
3075
3076 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003077 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003078 return ToImpl;
3079}
3080
Douglas Gregordd182ff2010-12-07 01:26:03 +00003081Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3082 // Find the corresponding interface.
3083 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3084 Importer.Import(D->getClassInterface()));
3085 if (!Iface)
3086 return 0;
3087
3088 // Import the superclass, if any.
3089 ObjCInterfaceDecl *Super = 0;
3090 if (D->getSuperClass()) {
3091 Super = cast_or_null<ObjCInterfaceDecl>(
3092 Importer.Import(D->getSuperClass()));
3093 if (!Super)
3094 return 0;
3095 }
3096
3097 ObjCImplementationDecl *Impl = Iface->getImplementation();
3098 if (!Impl) {
3099 // We haven't imported an implementation yet. Create a new @implementation
3100 // now.
3101 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3102 Importer.ImportContext(D->getDeclContext()),
3103 Importer.Import(D->getLocation()),
3104 Iface, Super);
3105
3106 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3107 DeclContext *LexicalDC
3108 = Importer.ImportContext(D->getLexicalDeclContext());
3109 if (!LexicalDC)
3110 return 0;
3111 Impl->setLexicalDeclContext(LexicalDC);
3112 }
3113
3114 // Associate the implementation with the class it implements.
3115 Iface->setImplementation(Impl);
3116 Importer.Imported(D, Iface->getImplementation());
3117 } else {
3118 Importer.Imported(D, Iface->getImplementation());
3119
3120 // Verify that the existing @implementation has the same superclass.
3121 if ((Super && !Impl->getSuperClass()) ||
3122 (!Super && Impl->getSuperClass()) ||
3123 (Super && Impl->getSuperClass() &&
3124 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3125 Importer.ToDiag(Impl->getLocation(),
3126 diag::err_odr_objc_superclass_inconsistent)
3127 << Iface->getDeclName();
3128 // FIXME: It would be nice to have the location of the superclass
3129 // below.
3130 if (Impl->getSuperClass())
3131 Importer.ToDiag(Impl->getLocation(),
3132 diag::note_odr_objc_superclass)
3133 << Impl->getSuperClass()->getDeclName();
3134 else
3135 Importer.ToDiag(Impl->getLocation(),
3136 diag::note_odr_objc_missing_superclass);
3137 if (D->getSuperClass())
3138 Importer.FromDiag(D->getLocation(),
3139 diag::note_odr_objc_superclass)
3140 << D->getSuperClass()->getDeclName();
3141 else
3142 Importer.FromDiag(D->getLocation(),
3143 diag::note_odr_objc_missing_superclass);
3144 return 0;
3145 }
3146 }
3147
3148 // Import all of the members of this @implementation.
3149 ImportDeclContext(D);
3150
3151 return Impl;
3152}
3153
Douglas Gregore3261622010-02-17 18:02:10 +00003154Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3155 // Import the major distinguishing characteristics of an @property.
3156 DeclContext *DC, *LexicalDC;
3157 DeclarationName Name;
3158 SourceLocation Loc;
3159 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3160 return 0;
3161
3162 // Check whether we have already imported this property.
3163 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3164 Lookup.first != Lookup.second;
3165 ++Lookup.first) {
3166 if (ObjCPropertyDecl *FoundProp
3167 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3168 // Check property types.
3169 if (!Importer.IsStructurallyEquivalent(D->getType(),
3170 FoundProp->getType())) {
3171 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3172 << Name << D->getType() << FoundProp->getType();
3173 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3174 << FoundProp->getType();
3175 return 0;
3176 }
3177
3178 // FIXME: Check property attributes, getters, setters, etc.?
3179
3180 // Consider these properties to be equivalent.
3181 Importer.Imported(D, FoundProp);
3182 return FoundProp;
3183 }
3184 }
3185
3186 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003187 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3188 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003189 return 0;
3190
3191 // Create the new property.
3192 ObjCPropertyDecl *ToProperty
3193 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3194 Name.getAsIdentifierInfo(),
3195 Importer.Import(D->getAtLoc()),
3196 T,
3197 D->getPropertyImplementation());
3198 Importer.Imported(D, ToProperty);
3199 ToProperty->setLexicalDeclContext(LexicalDC);
3200 LexicalDC->addDecl(ToProperty);
3201
3202 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003203 ToProperty->setPropertyAttributesAsWritten(
3204 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003205 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3206 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3207 ToProperty->setGetterMethodDecl(
3208 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3209 ToProperty->setSetterMethodDecl(
3210 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3211 ToProperty->setPropertyIvarDecl(
3212 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3213 return ToProperty;
3214}
3215
Douglas Gregor954e0c72010-12-07 18:32:03 +00003216Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3217 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3218 Importer.Import(D->getPropertyDecl()));
3219 if (!Property)
3220 return 0;
3221
3222 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3223 if (!DC)
3224 return 0;
3225
3226 // Import the lexical declaration context.
3227 DeclContext *LexicalDC = DC;
3228 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3229 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3230 if (!LexicalDC)
3231 return 0;
3232 }
3233
3234 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3235 if (!InImpl)
3236 return 0;
3237
3238 // Import the ivar (for an @synthesize).
3239 ObjCIvarDecl *Ivar = 0;
3240 if (D->getPropertyIvarDecl()) {
3241 Ivar = cast_or_null<ObjCIvarDecl>(
3242 Importer.Import(D->getPropertyIvarDecl()));
3243 if (!Ivar)
3244 return 0;
3245 }
3246
3247 ObjCPropertyImplDecl *ToImpl
3248 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3249 if (!ToImpl) {
3250 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3251 Importer.Import(D->getLocStart()),
3252 Importer.Import(D->getLocation()),
3253 Property,
3254 D->getPropertyImplementation(),
3255 Ivar,
3256 Importer.Import(D->getPropertyIvarDeclLoc()));
3257 ToImpl->setLexicalDeclContext(LexicalDC);
3258 Importer.Imported(D, ToImpl);
3259 LexicalDC->addDecl(ToImpl);
3260 } else {
3261 // Check that we have the same kind of property implementation (@synthesize
3262 // vs. @dynamic).
3263 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3264 Importer.ToDiag(ToImpl->getLocation(),
3265 diag::err_odr_objc_property_impl_kind_inconsistent)
3266 << Property->getDeclName()
3267 << (ToImpl->getPropertyImplementation()
3268 == ObjCPropertyImplDecl::Dynamic);
3269 Importer.FromDiag(D->getLocation(),
3270 diag::note_odr_objc_property_impl_kind)
3271 << D->getPropertyDecl()->getDeclName()
3272 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3273 return 0;
3274 }
3275
3276 // For @synthesize, check that we have the same
3277 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3278 Ivar != ToImpl->getPropertyIvarDecl()) {
3279 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3280 diag::err_odr_objc_synthesize_ivar_inconsistent)
3281 << Property->getDeclName()
3282 << ToImpl->getPropertyIvarDecl()->getDeclName()
3283 << Ivar->getDeclName();
3284 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3285 diag::note_odr_objc_synthesize_ivar_here)
3286 << D->getPropertyIvarDecl()->getDeclName();
3287 return 0;
3288 }
3289
3290 // Merge the existing implementation with the new implementation.
3291 Importer.Imported(D, ToImpl);
3292 }
3293
3294 return ToImpl;
3295}
3296
Douglas Gregor2b785022010-02-18 02:12:22 +00003297Decl *
3298ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3299 // Import the context of this declaration.
3300 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3301 if (!DC)
3302 return 0;
3303
3304 DeclContext *LexicalDC = DC;
3305 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3306 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3307 if (!LexicalDC)
3308 return 0;
3309 }
3310
3311 // Import the location of this declaration.
3312 SourceLocation Loc = Importer.Import(D->getLocation());
3313
3314 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3315 llvm::SmallVector<SourceLocation, 4> Locations;
3316 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3317 = D->protocol_loc_begin();
3318 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3319 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3320 FromProto != FromProtoEnd;
3321 ++FromProto, ++FromProtoLoc) {
3322 ObjCProtocolDecl *ToProto
3323 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3324 if (!ToProto)
3325 continue;
3326
3327 Protocols.push_back(ToProto);
3328 Locations.push_back(Importer.Import(*FromProtoLoc));
3329 }
3330
3331 ObjCForwardProtocolDecl *ToForward
3332 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3333 Protocols.data(), Protocols.size(),
3334 Locations.data());
3335 ToForward->setLexicalDeclContext(LexicalDC);
3336 LexicalDC->addDecl(ToForward);
3337 Importer.Imported(D, ToForward);
3338 return ToForward;
3339}
3340
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003341Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3342 // Import the context of this declaration.
3343 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3344 if (!DC)
3345 return 0;
3346
3347 DeclContext *LexicalDC = DC;
3348 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3349 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3350 if (!LexicalDC)
3351 return 0;
3352 }
3353
3354 // Import the location of this declaration.
3355 SourceLocation Loc = Importer.Import(D->getLocation());
3356
3357 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3358 llvm::SmallVector<SourceLocation, 4> Locations;
3359 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3360 From != FromEnd; ++From) {
3361 ObjCInterfaceDecl *ToIface
3362 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3363 if (!ToIface)
3364 continue;
3365
3366 Interfaces.push_back(ToIface);
3367 Locations.push_back(Importer.Import(From->getLocation()));
3368 }
3369
3370 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3371 Loc,
3372 Interfaces.data(),
3373 Locations.data(),
3374 Interfaces.size());
3375 ToClass->setLexicalDeclContext(LexicalDC);
3376 LexicalDC->addDecl(ToClass);
3377 Importer.Imported(D, ToClass);
3378 return ToClass;
3379}
3380
Douglas Gregor040afae2010-11-30 19:14:50 +00003381Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3382 // For template arguments, we adopt the translation unit as our declaration
3383 // context. This context will be fixed when the actual template declaration
3384 // is created.
3385
3386 // FIXME: Import default argument.
3387 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3388 Importer.getToContext().getTranslationUnitDecl(),
3389 Importer.Import(D->getLocation()),
3390 D->getDepth(),
3391 D->getIndex(),
3392 Importer.Import(D->getIdentifier()),
3393 D->wasDeclaredWithTypename(),
3394 D->isParameterPack());
3395}
3396
3397Decl *
3398ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3399 // Import the name of this declaration.
3400 DeclarationName Name = Importer.Import(D->getDeclName());
3401 if (D->getDeclName() && !Name)
3402 return 0;
3403
3404 // Import the location of this declaration.
3405 SourceLocation Loc = Importer.Import(D->getLocation());
3406
3407 // Import the type of this declaration.
3408 QualType T = Importer.Import(D->getType());
3409 if (T.isNull())
3410 return 0;
3411
3412 // Import type-source information.
3413 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3414 if (D->getTypeSourceInfo() && !TInfo)
3415 return 0;
3416
3417 // FIXME: Import default argument.
3418
3419 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3420 Importer.getToContext().getTranslationUnitDecl(),
3421 Loc, D->getDepth(), D->getPosition(),
3422 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003423 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003424}
3425
3426Decl *
3427ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3428 // Import the name of this declaration.
3429 DeclarationName Name = Importer.Import(D->getDeclName());
3430 if (D->getDeclName() && !Name)
3431 return 0;
3432
3433 // Import the location of this declaration.
3434 SourceLocation Loc = Importer.Import(D->getLocation());
3435
3436 // Import template parameters.
3437 TemplateParameterList *TemplateParams
3438 = ImportTemplateParameterList(D->getTemplateParameters());
3439 if (!TemplateParams)
3440 return 0;
3441
3442 // FIXME: Import default argument.
3443
3444 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3445 Importer.getToContext().getTranslationUnitDecl(),
3446 Loc, D->getDepth(), D->getPosition(),
3447 Name.getAsIdentifierInfo(),
3448 TemplateParams);
3449}
3450
3451Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3452 // If this record has a definition in the translation unit we're coming from,
3453 // but this particular declaration is not that definition, import the
3454 // definition and map to that.
3455 CXXRecordDecl *Definition
3456 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3457 if (Definition && Definition != D->getTemplatedDecl()) {
3458 Decl *ImportedDef
3459 = Importer.Import(Definition->getDescribedClassTemplate());
3460 if (!ImportedDef)
3461 return 0;
3462
3463 return Importer.Imported(D, ImportedDef);
3464 }
3465
3466 // Import the major distinguishing characteristics of this class template.
3467 DeclContext *DC, *LexicalDC;
3468 DeclarationName Name;
3469 SourceLocation Loc;
3470 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3471 return 0;
3472
3473 // We may already have a template of the same name; try to find and match it.
3474 if (!DC->isFunctionOrMethod()) {
3475 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3476 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3477 Lookup.first != Lookup.second;
3478 ++Lookup.first) {
3479 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3480 continue;
3481
3482 Decl *Found = *Lookup.first;
3483 if (ClassTemplateDecl *FoundTemplate
3484 = dyn_cast<ClassTemplateDecl>(Found)) {
3485 if (IsStructuralMatch(D, FoundTemplate)) {
3486 // The class templates structurally match; call it the same template.
3487 // FIXME: We may be filling in a forward declaration here. Handle
3488 // this case!
3489 Importer.Imported(D->getTemplatedDecl(),
3490 FoundTemplate->getTemplatedDecl());
3491 return Importer.Imported(D, FoundTemplate);
3492 }
3493 }
3494
3495 ConflictingDecls.push_back(*Lookup.first);
3496 }
3497
3498 if (!ConflictingDecls.empty()) {
3499 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3500 ConflictingDecls.data(),
3501 ConflictingDecls.size());
3502 }
3503
3504 if (!Name)
3505 return 0;
3506 }
3507
3508 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3509
3510 // Create the declaration that is being templated.
3511 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3512 DTemplated->getTagKind(),
3513 DC,
3514 Importer.Import(DTemplated->getLocation()),
3515 Name.getAsIdentifierInfo(),
3516 Importer.Import(DTemplated->getTagKeywordLoc()));
3517 D2Templated->setAccess(DTemplated->getAccess());
3518
3519
3520 // Import the qualifier, if any.
3521 if (DTemplated->getQualifier()) {
3522 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3523 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3524 D2Templated->setQualifierInfo(NNS, NNSRange);
3525 }
3526 D2Templated->setLexicalDeclContext(LexicalDC);
3527
3528 // Create the class template declaration itself.
3529 TemplateParameterList *TemplateParams
3530 = ImportTemplateParameterList(D->getTemplateParameters());
3531 if (!TemplateParams)
3532 return 0;
3533
3534 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3535 Loc, Name, TemplateParams,
3536 D2Templated,
3537 /*PrevDecl=*/0);
3538 D2Templated->setDescribedClassTemplate(D2);
3539
3540 D2->setAccess(D->getAccess());
3541 D2->setLexicalDeclContext(LexicalDC);
3542 LexicalDC->addDecl(D2);
3543
3544 // Note the relationship between the class templates.
3545 Importer.Imported(D, D2);
3546 Importer.Imported(DTemplated, D2Templated);
3547
3548 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3549 // FIXME: Import definition!
3550 }
3551
3552 return D2;
3553}
3554
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003555Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3556 ClassTemplateSpecializationDecl *D) {
3557 // If this record has a definition in the translation unit we're coming from,
3558 // but this particular declaration is not that definition, import the
3559 // definition and map to that.
3560 TagDecl *Definition = D->getDefinition();
3561 if (Definition && Definition != D) {
3562 Decl *ImportedDef = Importer.Import(Definition);
3563 if (!ImportedDef)
3564 return 0;
3565
3566 return Importer.Imported(D, ImportedDef);
3567 }
3568
3569 ClassTemplateDecl *ClassTemplate
3570 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3571 D->getSpecializedTemplate()));
3572 if (!ClassTemplate)
3573 return 0;
3574
3575 // Import the context of this declaration.
3576 DeclContext *DC = ClassTemplate->getDeclContext();
3577 if (!DC)
3578 return 0;
3579
3580 DeclContext *LexicalDC = DC;
3581 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3582 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3583 if (!LexicalDC)
3584 return 0;
3585 }
3586
3587 // Import the location of this declaration.
3588 SourceLocation Loc = Importer.Import(D->getLocation());
3589
3590 // Import template arguments.
3591 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3592 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3593 D->getTemplateArgs().size(),
3594 TemplateArgs))
3595 return 0;
3596
3597 // Try to find an existing specialization with these template arguments.
3598 void *InsertPos = 0;
3599 ClassTemplateSpecializationDecl *D2
3600 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3601 TemplateArgs.size(), InsertPos);
3602 if (D2) {
3603 // We already have a class template specialization with these template
3604 // arguments.
3605
3606 // FIXME: Check for specialization vs. instantiation errors.
3607
3608 if (RecordDecl *FoundDef = D2->getDefinition()) {
3609 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3610 // The record types structurally match, or the "from" translation
3611 // unit only had a forward declaration anyway; call it the same
3612 // function.
3613 return Importer.Imported(D, FoundDef);
3614 }
3615 }
3616 } else {
3617 // Create a new specialization.
3618 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3619 D->getTagKind(), DC,
3620 Loc, ClassTemplate,
3621 TemplateArgs.data(),
3622 TemplateArgs.size(),
3623 /*PrevDecl=*/0);
3624 D2->setSpecializationKind(D->getSpecializationKind());
3625
3626 // Add this specialization to the class template.
3627 ClassTemplate->AddSpecialization(D2, InsertPos);
3628
3629 // Import the qualifier, if any.
3630 if (D->getQualifier()) {
3631 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3632 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3633 D2->setQualifierInfo(NNS, NNSRange);
3634 }
3635
3636
3637 // Add the specialization to this context.
3638 D2->setLexicalDeclContext(LexicalDC);
3639 LexicalDC->addDecl(D2);
3640 }
3641 Importer.Imported(D, D2);
3642
3643 if (D->isDefinition() && ImportDefinition(D, D2))
3644 return 0;
3645
3646 return D2;
3647}
3648
Douglas Gregor4800d952010-02-11 19:21:55 +00003649//----------------------------------------------------------------------------
3650// Import Statements
3651//----------------------------------------------------------------------------
3652
3653Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3654 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3655 << S->getStmtClassName();
3656 return 0;
3657}
3658
3659//----------------------------------------------------------------------------
3660// Import Expressions
3661//----------------------------------------------------------------------------
3662Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3663 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3664 << E->getStmtClassName();
3665 return 0;
3666}
3667
Douglas Gregor44080632010-02-19 01:17:02 +00003668Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3669 NestedNameSpecifier *Qualifier = 0;
3670 if (E->getQualifier()) {
3671 Qualifier = Importer.Import(E->getQualifier());
3672 if (!E->getQualifier())
3673 return 0;
3674 }
3675
3676 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3677 if (!ToD)
3678 return 0;
3679
3680 QualType T = Importer.Import(E->getType());
3681 if (T.isNull())
3682 return 0;
3683
3684 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3685 Importer.Import(E->getQualifierRange()),
3686 ToD,
3687 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003688 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003689 /*FIXME:TemplateArgs=*/0);
3690}
3691
Douglas Gregor4800d952010-02-11 19:21:55 +00003692Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3693 QualType T = Importer.Import(E->getType());
3694 if (T.isNull())
3695 return 0;
3696
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003697 return IntegerLiteral::Create(Importer.getToContext(),
3698 E->getValue(), T,
3699 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003700}
3701
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003702Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3703 QualType T = Importer.Import(E->getType());
3704 if (T.isNull())
3705 return 0;
3706
3707 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3708 E->isWide(), T,
3709 Importer.Import(E->getLocation()));
3710}
3711
Douglas Gregorf638f952010-02-19 01:07:06 +00003712Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3713 Expr *SubExpr = Importer.Import(E->getSubExpr());
3714 if (!SubExpr)
3715 return 0;
3716
3717 return new (Importer.getToContext())
3718 ParenExpr(Importer.Import(E->getLParen()),
3719 Importer.Import(E->getRParen()),
3720 SubExpr);
3721}
3722
3723Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3724 QualType T = Importer.Import(E->getType());
3725 if (T.isNull())
3726 return 0;
3727
3728 Expr *SubExpr = Importer.Import(E->getSubExpr());
3729 if (!SubExpr)
3730 return 0;
3731
3732 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003733 T, E->getValueKind(),
3734 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003735 Importer.Import(E->getOperatorLoc()));
3736}
3737
Douglas Gregorbd249a52010-02-19 01:24:23 +00003738Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3739 QualType ResultType = Importer.Import(E->getType());
3740
3741 if (E->isArgumentType()) {
3742 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3743 if (!TInfo)
3744 return 0;
3745
3746 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3747 TInfo, ResultType,
3748 Importer.Import(E->getOperatorLoc()),
3749 Importer.Import(E->getRParenLoc()));
3750 }
3751
3752 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3753 if (!SubExpr)
3754 return 0;
3755
3756 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3757 SubExpr, ResultType,
3758 Importer.Import(E->getOperatorLoc()),
3759 Importer.Import(E->getRParenLoc()));
3760}
3761
Douglas Gregorf638f952010-02-19 01:07:06 +00003762Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3763 QualType T = Importer.Import(E->getType());
3764 if (T.isNull())
3765 return 0;
3766
3767 Expr *LHS = Importer.Import(E->getLHS());
3768 if (!LHS)
3769 return 0;
3770
3771 Expr *RHS = Importer.Import(E->getRHS());
3772 if (!RHS)
3773 return 0;
3774
3775 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003776 T, E->getValueKind(),
3777 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003778 Importer.Import(E->getOperatorLoc()));
3779}
3780
3781Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3782 QualType T = Importer.Import(E->getType());
3783 if (T.isNull())
3784 return 0;
3785
3786 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3787 if (CompLHSType.isNull())
3788 return 0;
3789
3790 QualType CompResultType = Importer.Import(E->getComputationResultType());
3791 if (CompResultType.isNull())
3792 return 0;
3793
3794 Expr *LHS = Importer.Import(E->getLHS());
3795 if (!LHS)
3796 return 0;
3797
3798 Expr *RHS = Importer.Import(E->getRHS());
3799 if (!RHS)
3800 return 0;
3801
3802 return new (Importer.getToContext())
3803 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003804 T, E->getValueKind(),
3805 E->getObjectKind(),
3806 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003807 Importer.Import(E->getOperatorLoc()));
3808}
3809
John McCallf871d0c2010-08-07 06:22:56 +00003810bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3811 if (E->path_empty()) return false;
3812
3813 // TODO: import cast paths
3814 return true;
3815}
3816
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003817Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3818 QualType T = Importer.Import(E->getType());
3819 if (T.isNull())
3820 return 0;
3821
3822 Expr *SubExpr = Importer.Import(E->getSubExpr());
3823 if (!SubExpr)
3824 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003825
3826 CXXCastPath BasePath;
3827 if (ImportCastPath(E, BasePath))
3828 return 0;
3829
3830 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003831 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003832}
3833
Douglas Gregor008847a2010-02-19 01:32:14 +00003834Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3835 QualType T = Importer.Import(E->getType());
3836 if (T.isNull())
3837 return 0;
3838
3839 Expr *SubExpr = Importer.Import(E->getSubExpr());
3840 if (!SubExpr)
3841 return 0;
3842
3843 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3844 if (!TInfo && E->getTypeInfoAsWritten())
3845 return 0;
3846
John McCallf871d0c2010-08-07 06:22:56 +00003847 CXXCastPath BasePath;
3848 if (ImportCastPath(E, BasePath))
3849 return 0;
3850
John McCallf89e55a2010-11-18 06:31:45 +00003851 return CStyleCastExpr::Create(Importer.getToContext(), T,
3852 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003853 SubExpr, &BasePath, TInfo,
3854 Importer.Import(E->getLParenLoc()),
3855 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003856}
3857
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003858ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003859 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003860 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003861 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003862 ImportedDecls[FromContext.getTranslationUnitDecl()]
3863 = ToContext.getTranslationUnitDecl();
3864}
3865
3866ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003867
3868QualType ASTImporter::Import(QualType FromT) {
3869 if (FromT.isNull())
3870 return QualType();
3871
Douglas Gregor169fba52010-02-08 15:18:58 +00003872 // Check whether we've already imported this type.
3873 llvm::DenseMap<Type *, Type *>::iterator Pos
3874 = ImportedTypes.find(FromT.getTypePtr());
3875 if (Pos != ImportedTypes.end())
3876 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003877
Douglas Gregor169fba52010-02-08 15:18:58 +00003878 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003879 ASTNodeImporter Importer(*this);
3880 QualType ToT = Importer.Visit(FromT.getTypePtr());
3881 if (ToT.isNull())
3882 return ToT;
3883
Douglas Gregor169fba52010-02-08 15:18:58 +00003884 // Record the imported type.
3885 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3886
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003887 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3888}
3889
Douglas Gregor9bed8792010-02-09 19:21:46 +00003890TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003891 if (!FromTSI)
3892 return FromTSI;
3893
3894 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003895 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003896 QualType T = Import(FromTSI->getType());
3897 if (T.isNull())
3898 return 0;
3899
3900 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003901 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003902}
3903
3904Decl *ASTImporter::Import(Decl *FromD) {
3905 if (!FromD)
3906 return 0;
3907
3908 // Check whether we've already imported this declaration.
3909 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3910 if (Pos != ImportedDecls.end())
3911 return Pos->second;
3912
3913 // Import the type
3914 ASTNodeImporter Importer(*this);
3915 Decl *ToD = Importer.Visit(FromD);
3916 if (!ToD)
3917 return 0;
3918
3919 // Record the imported declaration.
3920 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003921
3922 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3923 // Keep track of anonymous tags that have an associated typedef.
3924 if (FromTag->getTypedefForAnonDecl())
3925 AnonTagsWithPendingTypedefs.push_back(FromTag);
3926 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3927 // When we've finished transforming a typedef, see whether it was the
3928 // typedef for an anonymous tag.
3929 for (llvm::SmallVector<TagDecl *, 4>::iterator
3930 FromTag = AnonTagsWithPendingTypedefs.begin(),
3931 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3932 FromTag != FromTagEnd; ++FromTag) {
3933 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3934 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3935 // We found the typedef for an anonymous tag; link them.
3936 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3937 AnonTagsWithPendingTypedefs.erase(FromTag);
3938 break;
3939 }
3940 }
3941 }
3942 }
3943
Douglas Gregor9bed8792010-02-09 19:21:46 +00003944 return ToD;
3945}
3946
3947DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3948 if (!FromDC)
3949 return FromDC;
3950
3951 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3952}
3953
3954Expr *ASTImporter::Import(Expr *FromE) {
3955 if (!FromE)
3956 return 0;
3957
3958 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3959}
3960
3961Stmt *ASTImporter::Import(Stmt *FromS) {
3962 if (!FromS)
3963 return 0;
3964
Douglas Gregor4800d952010-02-11 19:21:55 +00003965 // Check whether we've already imported this declaration.
3966 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3967 if (Pos != ImportedStmts.end())
3968 return Pos->second;
3969
3970 // Import the type
3971 ASTNodeImporter Importer(*this);
3972 Stmt *ToS = Importer.Visit(FromS);
3973 if (!ToS)
3974 return 0;
3975
3976 // Record the imported declaration.
3977 ImportedStmts[FromS] = ToS;
3978 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00003979}
3980
3981NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3982 if (!FromNNS)
3983 return 0;
3984
3985 // FIXME: Implement!
3986 return 0;
3987}
3988
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003989TemplateName ASTImporter::Import(TemplateName From) {
3990 switch (From.getKind()) {
3991 case TemplateName::Template:
3992 if (TemplateDecl *ToTemplate
3993 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3994 return TemplateName(ToTemplate);
3995
3996 return TemplateName();
3997
3998 case TemplateName::OverloadedTemplate: {
3999 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4000 UnresolvedSet<2> ToTemplates;
4001 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4002 E = FromStorage->end();
4003 I != E; ++I) {
4004 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4005 ToTemplates.addDecl(To);
4006 else
4007 return TemplateName();
4008 }
4009 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4010 ToTemplates.end());
4011 }
4012
4013 case TemplateName::QualifiedTemplate: {
4014 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4015 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4016 if (!Qualifier)
4017 return TemplateName();
4018
4019 if (TemplateDecl *ToTemplate
4020 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4021 return ToContext.getQualifiedTemplateName(Qualifier,
4022 QTN->hasTemplateKeyword(),
4023 ToTemplate);
4024
4025 return TemplateName();
4026 }
4027
4028 case TemplateName::DependentTemplate: {
4029 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4030 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4031 if (!Qualifier)
4032 return TemplateName();
4033
4034 if (DTN->isIdentifier()) {
4035 return ToContext.getDependentTemplateName(Qualifier,
4036 Import(DTN->getIdentifier()));
4037 }
4038
4039 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4040 }
4041 }
4042
4043 llvm_unreachable("Invalid template name kind");
4044 return TemplateName();
4045}
4046
Douglas Gregor9bed8792010-02-09 19:21:46 +00004047SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4048 if (FromLoc.isInvalid())
4049 return SourceLocation();
4050
Douglas Gregor88523732010-02-10 00:15:17 +00004051 SourceManager &FromSM = FromContext.getSourceManager();
4052
4053 // For now, map everything down to its spelling location, so that we
4054 // don't have to import macro instantiations.
4055 // FIXME: Import macro instantiations!
4056 FromLoc = FromSM.getSpellingLoc(FromLoc);
4057 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4058 SourceManager &ToSM = ToContext.getSourceManager();
4059 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4060 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004061}
4062
4063SourceRange ASTImporter::Import(SourceRange FromRange) {
4064 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4065}
4066
Douglas Gregor88523732010-02-10 00:15:17 +00004067FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004068 llvm::DenseMap<FileID, FileID>::iterator Pos
4069 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004070 if (Pos != ImportedFileIDs.end())
4071 return Pos->second;
4072
4073 SourceManager &FromSM = FromContext.getSourceManager();
4074 SourceManager &ToSM = ToContext.getSourceManager();
4075 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4076 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4077
4078 // Include location of this file.
4079 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4080
4081 // Map the FileID for to the "to" source manager.
4082 FileID ToID;
4083 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4084 if (Cache->Entry) {
4085 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4086 // disk again
4087 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4088 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004089 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004090 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4091 FromSLoc.getFile().getFileCharacteristic());
4092 } else {
4093 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004094 const llvm::MemoryBuffer *
4095 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004096 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004097 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004098 FromBuf->getBufferIdentifier());
4099 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4100 }
4101
4102
Sebastian Redl535a3e22010-09-30 01:03:06 +00004103 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004104 return ToID;
4105}
4106
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004107DeclarationName ASTImporter::Import(DeclarationName FromName) {
4108 if (!FromName)
4109 return DeclarationName();
4110
4111 switch (FromName.getNameKind()) {
4112 case DeclarationName::Identifier:
4113 return Import(FromName.getAsIdentifierInfo());
4114
4115 case DeclarationName::ObjCZeroArgSelector:
4116 case DeclarationName::ObjCOneArgSelector:
4117 case DeclarationName::ObjCMultiArgSelector:
4118 return Import(FromName.getObjCSelector());
4119
4120 case DeclarationName::CXXConstructorName: {
4121 QualType T = Import(FromName.getCXXNameType());
4122 if (T.isNull())
4123 return DeclarationName();
4124
4125 return ToContext.DeclarationNames.getCXXConstructorName(
4126 ToContext.getCanonicalType(T));
4127 }
4128
4129 case DeclarationName::CXXDestructorName: {
4130 QualType T = Import(FromName.getCXXNameType());
4131 if (T.isNull())
4132 return DeclarationName();
4133
4134 return ToContext.DeclarationNames.getCXXDestructorName(
4135 ToContext.getCanonicalType(T));
4136 }
4137
4138 case DeclarationName::CXXConversionFunctionName: {
4139 QualType T = Import(FromName.getCXXNameType());
4140 if (T.isNull())
4141 return DeclarationName();
4142
4143 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4144 ToContext.getCanonicalType(T));
4145 }
4146
4147 case DeclarationName::CXXOperatorName:
4148 return ToContext.DeclarationNames.getCXXOperatorName(
4149 FromName.getCXXOverloadedOperator());
4150
4151 case DeclarationName::CXXLiteralOperatorName:
4152 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4153 Import(FromName.getCXXLiteralIdentifier()));
4154
4155 case DeclarationName::CXXUsingDirective:
4156 // FIXME: STATICS!
4157 return DeclarationName::getUsingDirectiveName();
4158 }
4159
4160 // Silence bogus GCC warning
4161 return DeclarationName();
4162}
4163
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004164IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004165 if (!FromId)
4166 return 0;
4167
4168 return &ToContext.Idents.get(FromId->getName());
4169}
Douglas Gregor089459a2010-02-08 21:09:39 +00004170
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004171Selector ASTImporter::Import(Selector FromSel) {
4172 if (FromSel.isNull())
4173 return Selector();
4174
4175 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4176 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4177 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4178 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4179 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4180}
4181
Douglas Gregor089459a2010-02-08 21:09:39 +00004182DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4183 DeclContext *DC,
4184 unsigned IDNS,
4185 NamedDecl **Decls,
4186 unsigned NumDecls) {
4187 return Name;
4188}
4189
4190DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004191 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004192}
4193
4194DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004195 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004196}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004197
4198Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4199 ImportedDecls[From] = To;
4200 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004201}
Douglas Gregorea35d112010-02-15 23:54:17 +00004202
4203bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4204 llvm::DenseMap<Type *, Type *>::iterator Pos
4205 = ImportedTypes.find(From.getTypePtr());
4206 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4207 return true;
4208
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004209 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004210 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004211}