blob: 60ea7d9081b5dca194ca0d0fa65655f42724acd9 [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());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000304
305 case TemplateArgument::TemplateExpansion:
306 return IsStructurallyEquivalent(Context,
307 Arg1.getAsTemplateOrTemplatePattern(),
308 Arg2.getAsTemplateOrTemplatePattern());
309
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000310 case TemplateArgument::Expression:
311 return IsStructurallyEquivalent(Context,
312 Arg1.getAsExpr(), Arg2.getAsExpr());
313
314 case TemplateArgument::Pack:
315 if (Arg1.pack_size() != Arg2.pack_size())
316 return false;
317
318 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
319 if (!IsStructurallyEquivalent(Context,
320 Arg1.pack_begin()[I],
321 Arg2.pack_begin()[I]))
322 return false;
323
324 return true;
325 }
326
327 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000328 return true;
329}
330
331/// \brief Determine structural equivalence for the common part of array
332/// types.
333static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
334 const ArrayType *Array1,
335 const ArrayType *Array2) {
336 if (!IsStructurallyEquivalent(Context,
337 Array1->getElementType(),
338 Array2->getElementType()))
339 return false;
340 if (Array1->getSizeModifier() != Array2->getSizeModifier())
341 return false;
342 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
343 return false;
344
345 return true;
346}
347
348/// \brief Determine structural equivalence of two types.
349static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
350 QualType T1, QualType T2) {
351 if (T1.isNull() || T2.isNull())
352 return T1.isNull() && T2.isNull();
353
354 if (!Context.StrictTypeSpelling) {
355 // We aren't being strict about token-to-token equivalence of types,
356 // so map down to the canonical type.
357 T1 = Context.C1.getCanonicalType(T1);
358 T2 = Context.C2.getCanonicalType(T2);
359 }
360
361 if (T1.getQualifiers() != T2.getQualifiers())
362 return false;
363
Douglas Gregorea35d112010-02-15 23:54:17 +0000364 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000365
Douglas Gregorea35d112010-02-15 23:54:17 +0000366 if (T1->getTypeClass() != T2->getTypeClass()) {
367 // Compare function types with prototypes vs. without prototypes as if
368 // both did not have prototypes.
369 if (T1->getTypeClass() == Type::FunctionProto &&
370 T2->getTypeClass() == Type::FunctionNoProto)
371 TC = Type::FunctionNoProto;
372 else if (T1->getTypeClass() == Type::FunctionNoProto &&
373 T2->getTypeClass() == Type::FunctionProto)
374 TC = Type::FunctionNoProto;
375 else
376 return false;
377 }
378
379 switch (TC) {
380 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000381 // FIXME: Deal with Char_S/Char_U.
382 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
383 return false;
384 break;
385
386 case Type::Complex:
387 if (!IsStructurallyEquivalent(Context,
388 cast<ComplexType>(T1)->getElementType(),
389 cast<ComplexType>(T2)->getElementType()))
390 return false;
391 break;
392
393 case Type::Pointer:
394 if (!IsStructurallyEquivalent(Context,
395 cast<PointerType>(T1)->getPointeeType(),
396 cast<PointerType>(T2)->getPointeeType()))
397 return false;
398 break;
399
400 case Type::BlockPointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<BlockPointerType>(T1)->getPointeeType(),
403 cast<BlockPointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::LValueReference:
408 case Type::RValueReference: {
409 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
410 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
411 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
412 return false;
413 if (Ref1->isInnerRef() != Ref2->isInnerRef())
414 return false;
415 if (!IsStructurallyEquivalent(Context,
416 Ref1->getPointeeTypeAsWritten(),
417 Ref2->getPointeeTypeAsWritten()))
418 return false;
419 break;
420 }
421
422 case Type::MemberPointer: {
423 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
424 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
425 if (!IsStructurallyEquivalent(Context,
426 MemPtr1->getPointeeType(),
427 MemPtr2->getPointeeType()))
428 return false;
429 if (!IsStructurallyEquivalent(Context,
430 QualType(MemPtr1->getClass(), 0),
431 QualType(MemPtr2->getClass(), 0)))
432 return false;
433 break;
434 }
435
436 case Type::ConstantArray: {
437 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
438 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
439 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
440 return false;
441
442 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
443 return false;
444 break;
445 }
446
447 case Type::IncompleteArray:
448 if (!IsArrayStructurallyEquivalent(Context,
449 cast<ArrayType>(T1),
450 cast<ArrayType>(T2)))
451 return false;
452 break;
453
454 case Type::VariableArray: {
455 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
456 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
457 if (!IsStructurallyEquivalent(Context,
458 Array1->getSizeExpr(), Array2->getSizeExpr()))
459 return false;
460
461 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
462 return false;
463
464 break;
465 }
466
467 case Type::DependentSizedArray: {
468 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
469 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
470 if (!IsStructurallyEquivalent(Context,
471 Array1->getSizeExpr(), Array2->getSizeExpr()))
472 return false;
473
474 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
475 return false;
476
477 break;
478 }
479
480 case Type::DependentSizedExtVector: {
481 const DependentSizedExtVectorType *Vec1
482 = cast<DependentSizedExtVectorType>(T1);
483 const DependentSizedExtVectorType *Vec2
484 = cast<DependentSizedExtVectorType>(T2);
485 if (!IsStructurallyEquivalent(Context,
486 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
487 return false;
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getElementType(),
490 Vec2->getElementType()))
491 return false;
492 break;
493 }
494
495 case Type::Vector:
496 case Type::ExtVector: {
497 const VectorType *Vec1 = cast<VectorType>(T1);
498 const VectorType *Vec2 = cast<VectorType>(T2);
499 if (!IsStructurallyEquivalent(Context,
500 Vec1->getElementType(),
501 Vec2->getElementType()))
502 return false;
503 if (Vec1->getNumElements() != Vec2->getNumElements())
504 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000505 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000506 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000507 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000508 }
509
510 case Type::FunctionProto: {
511 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
512 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
513 if (Proto1->getNumArgs() != Proto2->getNumArgs())
514 return false;
515 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
516 if (!IsStructurallyEquivalent(Context,
517 Proto1->getArgType(I),
518 Proto2->getArgType(I)))
519 return false;
520 }
521 if (Proto1->isVariadic() != Proto2->isVariadic())
522 return false;
523 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
524 return false;
525 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
526 return false;
527 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
528 return false;
529 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
530 if (!IsStructurallyEquivalent(Context,
531 Proto1->getExceptionType(I),
532 Proto2->getExceptionType(I)))
533 return false;
534 }
535 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
536 return false;
537
538 // Fall through to check the bits common with FunctionNoProtoType.
539 }
540
541 case Type::FunctionNoProto: {
542 const FunctionType *Function1 = cast<FunctionType>(T1);
543 const FunctionType *Function2 = cast<FunctionType>(T2);
544 if (!IsStructurallyEquivalent(Context,
545 Function1->getResultType(),
546 Function2->getResultType()))
547 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000548 if (Function1->getExtInfo() != Function2->getExtInfo())
549 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000550 break;
551 }
552
553 case Type::UnresolvedUsing:
554 if (!IsStructurallyEquivalent(Context,
555 cast<UnresolvedUsingType>(T1)->getDecl(),
556 cast<UnresolvedUsingType>(T2)->getDecl()))
557 return false;
558
559 break;
John McCall9d156a72011-01-06 01:58:22 +0000560
561 case Type::Attributed:
562 if (!IsStructurallyEquivalent(Context,
563 cast<AttributedType>(T1)->getModifiedType(),
564 cast<AttributedType>(T2)->getModifiedType()))
565 return false;
566 if (!IsStructurallyEquivalent(Context,
567 cast<AttributedType>(T1)->getEquivalentType(),
568 cast<AttributedType>(T2)->getEquivalentType()))
569 return false;
570 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000571
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000572 case Type::Paren:
573 if (!IsStructurallyEquivalent(Context,
574 cast<ParenType>(T1)->getInnerType(),
575 cast<ParenType>(T2)->getInnerType()))
576 return false;
577 break;
578
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000579 case Type::Typedef:
580 if (!IsStructurallyEquivalent(Context,
581 cast<TypedefType>(T1)->getDecl(),
582 cast<TypedefType>(T2)->getDecl()))
583 return false;
584 break;
585
586 case Type::TypeOfExpr:
587 if (!IsStructurallyEquivalent(Context,
588 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
589 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
590 return false;
591 break;
592
593 case Type::TypeOf:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypeOfType>(T1)->getUnderlyingType(),
596 cast<TypeOfType>(T2)->getUnderlyingType()))
597 return false;
598 break;
599
600 case Type::Decltype:
601 if (!IsStructurallyEquivalent(Context,
602 cast<DecltypeType>(T1)->getUnderlyingExpr(),
603 cast<DecltypeType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::Record:
608 case Type::Enum:
609 if (!IsStructurallyEquivalent(Context,
610 cast<TagType>(T1)->getDecl(),
611 cast<TagType>(T2)->getDecl()))
612 return false;
613 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000614
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000615 case Type::TemplateTypeParm: {
616 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
617 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
618 if (Parm1->getDepth() != Parm2->getDepth())
619 return false;
620 if (Parm1->getIndex() != Parm2->getIndex())
621 return false;
622 if (Parm1->isParameterPack() != Parm2->isParameterPack())
623 return false;
624
625 // Names of template type parameters are never significant.
626 break;
627 }
628
629 case Type::SubstTemplateTypeParm: {
630 const SubstTemplateTypeParmType *Subst1
631 = cast<SubstTemplateTypeParmType>(T1);
632 const SubstTemplateTypeParmType *Subst2
633 = cast<SubstTemplateTypeParmType>(T2);
634 if (!IsStructurallyEquivalent(Context,
635 QualType(Subst1->getReplacedParameter(), 0),
636 QualType(Subst2->getReplacedParameter(), 0)))
637 return false;
638 if (!IsStructurallyEquivalent(Context,
639 Subst1->getReplacementType(),
640 Subst2->getReplacementType()))
641 return false;
642 break;
643 }
644
645 case Type::TemplateSpecialization: {
646 const TemplateSpecializationType *Spec1
647 = cast<TemplateSpecializationType>(T1);
648 const TemplateSpecializationType *Spec2
649 = cast<TemplateSpecializationType>(T2);
650 if (!IsStructurallyEquivalent(Context,
651 Spec1->getTemplateName(),
652 Spec2->getTemplateName()))
653 return false;
654 if (Spec1->getNumArgs() != Spec2->getNumArgs())
655 return false;
656 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
657 if (!IsStructurallyEquivalent(Context,
658 Spec1->getArg(I), Spec2->getArg(I)))
659 return false;
660 }
661 break;
662 }
663
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000664 case Type::Elaborated: {
665 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
666 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
667 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
668 if (Elab1->getKeyword() != Elab2->getKeyword())
669 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000670 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000671 Elab1->getQualifier(),
672 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000673 return false;
674 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000675 Elab1->getNamedType(),
676 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000677 return false;
678 break;
679 }
680
John McCall3cb0ebd2010-03-10 03:28:59 +0000681 case Type::InjectedClassName: {
682 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
683 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
684 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000685 Inj1->getInjectedSpecializationType(),
686 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000687 return false;
688 break;
689 }
690
Douglas Gregor4714c122010-03-31 17:34:00 +0000691 case Type::DependentName: {
692 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
693 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000694 if (!IsStructurallyEquivalent(Context,
695 Typename1->getQualifier(),
696 Typename2->getQualifier()))
697 return false;
698 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
699 Typename2->getIdentifier()))
700 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000701
702 break;
703 }
704
John McCall33500952010-06-11 00:33:02 +0000705 case Type::DependentTemplateSpecialization: {
706 const DependentTemplateSpecializationType *Spec1 =
707 cast<DependentTemplateSpecializationType>(T1);
708 const DependentTemplateSpecializationType *Spec2 =
709 cast<DependentTemplateSpecializationType>(T2);
710 if (!IsStructurallyEquivalent(Context,
711 Spec1->getQualifier(),
712 Spec2->getQualifier()))
713 return false;
714 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
715 Spec2->getIdentifier()))
716 return false;
717 if (Spec1->getNumArgs() != Spec2->getNumArgs())
718 return false;
719 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
720 if (!IsStructurallyEquivalent(Context,
721 Spec1->getArg(I), Spec2->getArg(I)))
722 return false;
723 }
724 break;
725 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000726
727 case Type::PackExpansion:
728 if (!IsStructurallyEquivalent(Context,
729 cast<PackExpansionType>(T1)->getPattern(),
730 cast<PackExpansionType>(T2)->getPattern()))
731 return false;
732 break;
733
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000734 case Type::ObjCInterface: {
735 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
736 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
737 if (!IsStructurallyEquivalent(Context,
738 Iface1->getDecl(), Iface2->getDecl()))
739 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000740 break;
741 }
742
743 case Type::ObjCObject: {
744 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
745 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
746 if (!IsStructurallyEquivalent(Context,
747 Obj1->getBaseType(),
748 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000749 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000750 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
751 return false;
752 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000753 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000754 Obj1->getProtocol(I),
755 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000756 return false;
757 }
758 break;
759 }
760
761 case Type::ObjCObjectPointer: {
762 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
763 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
764 if (!IsStructurallyEquivalent(Context,
765 Ptr1->getPointeeType(),
766 Ptr2->getPointeeType()))
767 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000768 break;
769 }
770
771 } // end switch
772
773 return true;
774}
775
776/// \brief Determine structural equivalence of two records.
777static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
778 RecordDecl *D1, RecordDecl *D2) {
779 if (D1->isUnion() != D2->isUnion()) {
780 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
781 << Context.C2.getTypeDeclType(D2);
782 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
783 << D1->getDeclName() << (unsigned)D1->getTagKind();
784 return false;
785 }
786
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000787 // If both declarations are class template specializations, we know
788 // the ODR applies, so check the template and template arguments.
789 ClassTemplateSpecializationDecl *Spec1
790 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
791 ClassTemplateSpecializationDecl *Spec2
792 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
793 if (Spec1 && Spec2) {
794 // Check that the specialized templates are the same.
795 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
796 Spec2->getSpecializedTemplate()))
797 return false;
798
799 // Check that the template arguments are the same.
800 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
801 return false;
802
803 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
804 if (!IsStructurallyEquivalent(Context,
805 Spec1->getTemplateArgs().get(I),
806 Spec2->getTemplateArgs().get(I)))
807 return false;
808 }
809 // If one is a class template specialization and the other is not, these
810 // structures are diferent.
811 else if (Spec1 || Spec2)
812 return false;
813
Douglas Gregorea35d112010-02-15 23:54:17 +0000814 // Compare the definitions of these two records. If either or both are
815 // incomplete, we assume that they are equivalent.
816 D1 = D1->getDefinition();
817 D2 = D2->getDefinition();
818 if (!D1 || !D2)
819 return true;
820
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000821 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
822 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
823 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
824 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000825 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000826 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000827 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000828 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000829 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000830 return false;
831 }
832
833 // Check the base classes.
834 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
835 BaseEnd1 = D1CXX->bases_end(),
836 Base2 = D2CXX->bases_begin();
837 Base1 != BaseEnd1;
838 ++Base1, ++Base2) {
839 if (!IsStructurallyEquivalent(Context,
840 Base1->getType(), Base2->getType())) {
841 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
842 << Context.C2.getTypeDeclType(D2);
843 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
844 << Base2->getType()
845 << Base2->getSourceRange();
846 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
847 << Base1->getType()
848 << Base1->getSourceRange();
849 return false;
850 }
851
852 // Check virtual vs. non-virtual inheritance mismatch.
853 if (Base1->isVirtual() != Base2->isVirtual()) {
854 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
855 << Context.C2.getTypeDeclType(D2);
856 Context.Diag2(Base2->getSourceRange().getBegin(),
857 diag::note_odr_virtual_base)
858 << Base2->isVirtual() << Base2->getSourceRange();
859 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
860 << Base1->isVirtual()
861 << Base1->getSourceRange();
862 return false;
863 }
864 }
865 } else if (D1CXX->getNumBases() > 0) {
866 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
867 << Context.C2.getTypeDeclType(D2);
868 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
869 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
870 << Base1->getType()
871 << Base1->getSourceRange();
872 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
873 return false;
874 }
875 }
876
877 // Check the fields for consistency.
878 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
879 Field2End = D2->field_end();
880 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
881 Field1End = D1->field_end();
882 Field1 != Field1End;
883 ++Field1, ++Field2) {
884 if (Field2 == Field2End) {
885 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
886 << Context.C2.getTypeDeclType(D2);
887 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
888 << Field1->getDeclName() << Field1->getType();
889 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
890 return false;
891 }
892
893 if (!IsStructurallyEquivalent(Context,
894 Field1->getType(), Field2->getType())) {
895 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
896 << Context.C2.getTypeDeclType(D2);
897 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
898 << Field2->getDeclName() << Field2->getType();
899 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
900 << Field1->getDeclName() << Field1->getType();
901 return false;
902 }
903
904 if (Field1->isBitField() != Field2->isBitField()) {
905 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
906 << Context.C2.getTypeDeclType(D2);
907 if (Field1->isBitField()) {
908 llvm::APSInt Bits;
909 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
910 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
911 << Field1->getDeclName() << Field1->getType()
912 << Bits.toString(10, false);
913 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
914 << Field2->getDeclName();
915 } else {
916 llvm::APSInt Bits;
917 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
918 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
919 << Field2->getDeclName() << Field2->getType()
920 << Bits.toString(10, false);
921 Context.Diag1(Field1->getLocation(),
922 diag::note_odr_not_bit_field)
923 << Field1->getDeclName();
924 }
925 return false;
926 }
927
928 if (Field1->isBitField()) {
929 // Make sure that the bit-fields are the same length.
930 llvm::APSInt Bits1, Bits2;
931 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
932 return false;
933 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
934 return false;
935
936 if (!IsSameValue(Bits1, Bits2)) {
937 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
938 << Context.C2.getTypeDeclType(D2);
939 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
940 << Field2->getDeclName() << Field2->getType()
941 << Bits2.toString(10, false);
942 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
943 << Field1->getDeclName() << Field1->getType()
944 << Bits1.toString(10, false);
945 return false;
946 }
947 }
948 }
949
950 if (Field2 != Field2End) {
951 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
952 << Context.C2.getTypeDeclType(D2);
953 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
954 << Field2->getDeclName() << Field2->getType();
955 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
956 return false;
957 }
958
959 return true;
960}
961
962/// \brief Determine structural equivalence of two enums.
963static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
964 EnumDecl *D1, EnumDecl *D2) {
965 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
966 EC2End = D2->enumerator_end();
967 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
968 EC1End = D1->enumerator_end();
969 EC1 != EC1End; ++EC1, ++EC2) {
970 if (EC2 == EC2End) {
971 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
972 << Context.C2.getTypeDeclType(D2);
973 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
974 << EC1->getDeclName()
975 << EC1->getInitVal().toString(10);
976 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
977 return false;
978 }
979
980 llvm::APSInt Val1 = EC1->getInitVal();
981 llvm::APSInt Val2 = EC2->getInitVal();
982 if (!IsSameValue(Val1, Val2) ||
983 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
984 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
985 << Context.C2.getTypeDeclType(D2);
986 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
987 << EC2->getDeclName()
988 << EC2->getInitVal().toString(10);
989 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
990 << EC1->getDeclName()
991 << EC1->getInitVal().toString(10);
992 return false;
993 }
994 }
995
996 if (EC2 != EC2End) {
997 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
998 << Context.C2.getTypeDeclType(D2);
999 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1000 << EC2->getDeclName()
1001 << EC2->getInitVal().toString(10);
1002 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1003 return false;
1004 }
1005
1006 return true;
1007}
Douglas Gregor040afae2010-11-30 19:14:50 +00001008
1009static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1010 TemplateParameterList *Params1,
1011 TemplateParameterList *Params2) {
1012 if (Params1->size() != Params2->size()) {
1013 Context.Diag2(Params2->getTemplateLoc(),
1014 diag::err_odr_different_num_template_parameters)
1015 << Params1->size() << Params2->size();
1016 Context.Diag1(Params1->getTemplateLoc(),
1017 diag::note_odr_template_parameter_list);
1018 return false;
1019 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001020
Douglas Gregor040afae2010-11-30 19:14:50 +00001021 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1022 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1023 Context.Diag2(Params2->getParam(I)->getLocation(),
1024 diag::err_odr_different_template_parameter_kind);
1025 Context.Diag1(Params1->getParam(I)->getLocation(),
1026 diag::note_odr_template_parameter_here);
1027 return false;
1028 }
1029
1030 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1031 Params2->getParam(I))) {
1032
1033 return false;
1034 }
1035 }
1036
1037 return true;
1038}
1039
1040static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1041 TemplateTypeParmDecl *D1,
1042 TemplateTypeParmDecl *D2) {
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
1051 return true;
1052}
1053
1054static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1055 NonTypeTemplateParmDecl *D1,
1056 NonTypeTemplateParmDecl *D2) {
1057 // FIXME: Enable once we have variadic templates.
1058#if 0
1059 if (D1->isParameterPack() != D2->isParameterPack()) {
1060 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1061 << D2->isParameterPack();
1062 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1063 << D1->isParameterPack();
1064 return false;
1065 }
1066#endif
1067
1068 // Check types.
1069 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1070 Context.Diag2(D2->getLocation(),
1071 diag::err_odr_non_type_parameter_type_inconsistent)
1072 << D2->getType() << D1->getType();
1073 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1074 << D1->getType();
1075 return false;
1076 }
1077
1078 return true;
1079}
1080
1081static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1082 TemplateTemplateParmDecl *D1,
1083 TemplateTemplateParmDecl *D2) {
1084 // FIXME: Enable once we have variadic templates.
1085#if 0
1086 if (D1->isParameterPack() != D2->isParameterPack()) {
1087 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1088 << D2->isParameterPack();
1089 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1090 << D1->isParameterPack();
1091 return false;
1092 }
1093#endif
1094
1095 // Check template parameter lists.
1096 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1097 D2->getTemplateParameters());
1098}
1099
1100static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1101 ClassTemplateDecl *D1,
1102 ClassTemplateDecl *D2) {
1103 // Check template parameters.
1104 if (!IsStructurallyEquivalent(Context,
1105 D1->getTemplateParameters(),
1106 D2->getTemplateParameters()))
1107 return false;
1108
1109 // Check the templated declaration.
1110 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1111 D2->getTemplatedDecl());
1112}
1113
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001114/// \brief Determine structural equivalence of two declarations.
1115static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1116 Decl *D1, Decl *D2) {
1117 // FIXME: Check for known structural equivalences via a callback of some sort.
1118
Douglas Gregorea35d112010-02-15 23:54:17 +00001119 // Check whether we already know that these two declarations are not
1120 // structurally equivalent.
1121 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1122 D2->getCanonicalDecl())))
1123 return false;
1124
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001125 // Determine whether we've already produced a tentative equivalence for D1.
1126 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1127 if (EquivToD1)
1128 return EquivToD1 == D2->getCanonicalDecl();
1129
1130 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1131 EquivToD1 = D2->getCanonicalDecl();
1132 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1133 return true;
1134}
1135
1136bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1137 Decl *D2) {
1138 if (!::IsStructurallyEquivalent(*this, D1, D2))
1139 return false;
1140
1141 return !Finish();
1142}
1143
1144bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1145 QualType T2) {
1146 if (!::IsStructurallyEquivalent(*this, T1, T2))
1147 return false;
1148
1149 return !Finish();
1150}
1151
1152bool StructuralEquivalenceContext::Finish() {
1153 while (!DeclsToCheck.empty()) {
1154 // Check the next declaration.
1155 Decl *D1 = DeclsToCheck.front();
1156 DeclsToCheck.pop_front();
1157
1158 Decl *D2 = TentativeEquivalences[D1];
1159 assert(D2 && "Unrecorded tentative equivalence?");
1160
Douglas Gregorea35d112010-02-15 23:54:17 +00001161 bool Equivalent = true;
1162
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001163 // FIXME: Switch on all declaration kinds. For now, we're just going to
1164 // check the obvious ones.
1165 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1166 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1167 // Check for equivalent structure names.
1168 IdentifierInfo *Name1 = Record1->getIdentifier();
1169 if (!Name1 && Record1->getTypedefForAnonDecl())
1170 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1171 IdentifierInfo *Name2 = Record2->getIdentifier();
1172 if (!Name2 && Record2->getTypedefForAnonDecl())
1173 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001174 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1175 !::IsStructurallyEquivalent(*this, Record1, Record2))
1176 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001177 } else {
1178 // Record/non-record 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 (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001182 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1183 // Check for equivalent enum names.
1184 IdentifierInfo *Name1 = Enum1->getIdentifier();
1185 if (!Name1 && Enum1->getTypedefForAnonDecl())
1186 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1187 IdentifierInfo *Name2 = Enum2->getIdentifier();
1188 if (!Name2 && Enum2->getTypedefForAnonDecl())
1189 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001190 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1191 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1192 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001193 } else {
1194 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001195 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001196 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001197 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001198 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1199 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001200 Typedef2->getIdentifier()) ||
1201 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001202 Typedef1->getUnderlyingType(),
1203 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001204 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001205 } else {
1206 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001207 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001208 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001209 } else if (ClassTemplateDecl *ClassTemplate1
1210 = dyn_cast<ClassTemplateDecl>(D1)) {
1211 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1212 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1213 ClassTemplate2->getIdentifier()) ||
1214 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1215 Equivalent = false;
1216 } else {
1217 // Class template/non-class-template mismatch.
1218 Equivalent = false;
1219 }
1220 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1221 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1222 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1223 Equivalent = false;
1224 } else {
1225 // Kind mismatch.
1226 Equivalent = false;
1227 }
1228 } else if (NonTypeTemplateParmDecl *NTTP1
1229 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1230 if (NonTypeTemplateParmDecl *NTTP2
1231 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1232 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1233 Equivalent = false;
1234 } else {
1235 // Kind mismatch.
1236 Equivalent = false;
1237 }
1238 } else if (TemplateTemplateParmDecl *TTP1
1239 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1240 if (TemplateTemplateParmDecl *TTP2
1241 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1242 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1243 Equivalent = false;
1244 } else {
1245 // Kind mismatch.
1246 Equivalent = false;
1247 }
1248 }
1249
Douglas Gregorea35d112010-02-15 23:54:17 +00001250 if (!Equivalent) {
1251 // Note that these two declarations are not equivalent (and we already
1252 // know about it).
1253 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1254 D2->getCanonicalDecl()));
1255 return true;
1256 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001257 // FIXME: Check other declaration kinds!
1258 }
1259
1260 return false;
1261}
1262
1263//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001264// Import Types
1265//----------------------------------------------------------------------------
1266
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001267QualType ASTNodeImporter::VisitType(Type *T) {
1268 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1269 << T->getTypeClassName();
1270 return QualType();
1271}
1272
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001273QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1274 switch (T->getKind()) {
1275 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1276 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1277
1278 case BuiltinType::Char_U:
1279 // The context we're importing from has an unsigned 'char'. If we're
1280 // importing into a context with a signed 'char', translate to
1281 // 'unsigned char' instead.
1282 if (Importer.getToContext().getLangOptions().CharIsSigned)
1283 return Importer.getToContext().UnsignedCharTy;
1284
1285 return Importer.getToContext().CharTy;
1286
1287 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1288
1289 case BuiltinType::Char16:
1290 // FIXME: Make sure that the "to" context supports C++!
1291 return Importer.getToContext().Char16Ty;
1292
1293 case BuiltinType::Char32:
1294 // FIXME: Make sure that the "to" context supports C++!
1295 return Importer.getToContext().Char32Ty;
1296
1297 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1298 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1299 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1300 case BuiltinType::ULongLong:
1301 return Importer.getToContext().UnsignedLongLongTy;
1302 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1303
1304 case BuiltinType::Char_S:
1305 // The context we're importing from has an unsigned 'char'. If we're
1306 // importing into a context with a signed 'char', translate to
1307 // 'unsigned char' instead.
1308 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1309 return Importer.getToContext().SignedCharTy;
1310
1311 return Importer.getToContext().CharTy;
1312
1313 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001314 case BuiltinType::WChar_S:
1315 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001316 // FIXME: If not in C++, shall we translate to the C equivalent of
1317 // wchar_t?
1318 return Importer.getToContext().WCharTy;
1319
1320 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1321 case BuiltinType::Int : return Importer.getToContext().IntTy;
1322 case BuiltinType::Long : return Importer.getToContext().LongTy;
1323 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1324 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1325 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1326 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1327 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1328
1329 case BuiltinType::NullPtr:
1330 // FIXME: Make sure that the "to" context supports C++0x!
1331 return Importer.getToContext().NullPtrTy;
1332
1333 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1334 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1335 case BuiltinType::UndeducedAuto:
1336 // FIXME: Make sure that the "to" context supports C++0x!
1337 return Importer.getToContext().UndeducedAutoTy;
1338
1339 case BuiltinType::ObjCId:
1340 // FIXME: Make sure that the "to" context supports Objective-C!
1341 return Importer.getToContext().ObjCBuiltinIdTy;
1342
1343 case BuiltinType::ObjCClass:
1344 return Importer.getToContext().ObjCBuiltinClassTy;
1345
1346 case BuiltinType::ObjCSel:
1347 return Importer.getToContext().ObjCBuiltinSelTy;
1348 }
1349
1350 return QualType();
1351}
1352
1353QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1354 QualType ToElementType = Importer.Import(T->getElementType());
1355 if (ToElementType.isNull())
1356 return QualType();
1357
1358 return Importer.getToContext().getComplexType(ToElementType);
1359}
1360
1361QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1362 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1363 if (ToPointeeType.isNull())
1364 return QualType();
1365
1366 return Importer.getToContext().getPointerType(ToPointeeType);
1367}
1368
1369QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1370 // FIXME: Check for blocks support in "to" context.
1371 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1372 if (ToPointeeType.isNull())
1373 return QualType();
1374
1375 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1376}
1377
1378QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1379 // FIXME: Check for C++ support in "to" context.
1380 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1381 if (ToPointeeType.isNull())
1382 return QualType();
1383
1384 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1385}
1386
1387QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1388 // FIXME: Check for C++0x support in "to" context.
1389 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1390 if (ToPointeeType.isNull())
1391 return QualType();
1392
1393 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1394}
1395
1396QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1397 // FIXME: Check for C++ support in "to" context.
1398 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1399 if (ToPointeeType.isNull())
1400 return QualType();
1401
1402 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1403 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1404 ClassType.getTypePtr());
1405}
1406
1407QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1408 QualType ToElementType = Importer.Import(T->getElementType());
1409 if (ToElementType.isNull())
1410 return QualType();
1411
1412 return Importer.getToContext().getConstantArrayType(ToElementType,
1413 T->getSize(),
1414 T->getSizeModifier(),
1415 T->getIndexTypeCVRQualifiers());
1416}
1417
1418QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1419 QualType ToElementType = Importer.Import(T->getElementType());
1420 if (ToElementType.isNull())
1421 return QualType();
1422
1423 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1424 T->getSizeModifier(),
1425 T->getIndexTypeCVRQualifiers());
1426}
1427
1428QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1429 QualType ToElementType = Importer.Import(T->getElementType());
1430 if (ToElementType.isNull())
1431 return QualType();
1432
1433 Expr *Size = Importer.Import(T->getSizeExpr());
1434 if (!Size)
1435 return QualType();
1436
1437 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1438 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1439 T->getSizeModifier(),
1440 T->getIndexTypeCVRQualifiers(),
1441 Brackets);
1442}
1443
1444QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1445 QualType ToElementType = Importer.Import(T->getElementType());
1446 if (ToElementType.isNull())
1447 return QualType();
1448
1449 return Importer.getToContext().getVectorType(ToElementType,
1450 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001451 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001452}
1453
1454QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1455 QualType ToElementType = Importer.Import(T->getElementType());
1456 if (ToElementType.isNull())
1457 return QualType();
1458
1459 return Importer.getToContext().getExtVectorType(ToElementType,
1460 T->getNumElements());
1461}
1462
1463QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1464 // FIXME: What happens if we're importing a function without a prototype
1465 // into C++? Should we make it variadic?
1466 QualType ToResultType = Importer.Import(T->getResultType());
1467 if (ToResultType.isNull())
1468 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001469
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001470 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001471 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001472}
1473
1474QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1475 QualType ToResultType = Importer.Import(T->getResultType());
1476 if (ToResultType.isNull())
1477 return QualType();
1478
1479 // Import argument types
1480 llvm::SmallVector<QualType, 4> ArgTypes;
1481 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1482 AEnd = T->arg_type_end();
1483 A != AEnd; ++A) {
1484 QualType ArgType = Importer.Import(*A);
1485 if (ArgType.isNull())
1486 return QualType();
1487 ArgTypes.push_back(ArgType);
1488 }
1489
1490 // Import exception types
1491 llvm::SmallVector<QualType, 4> ExceptionTypes;
1492 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1493 EEnd = T->exception_end();
1494 E != EEnd; ++E) {
1495 QualType ExceptionType = Importer.Import(*E);
1496 if (ExceptionType.isNull())
1497 return QualType();
1498 ExceptionTypes.push_back(ExceptionType);
1499 }
John McCalle23cf432010-12-14 08:05:40 +00001500
1501 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1502 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001503
1504 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001505 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001506}
1507
1508QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1509 TypedefDecl *ToDecl
1510 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1511 if (!ToDecl)
1512 return QualType();
1513
1514 return Importer.getToContext().getTypeDeclType(ToDecl);
1515}
1516
1517QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1518 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1519 if (!ToExpr)
1520 return QualType();
1521
1522 return Importer.getToContext().getTypeOfExprType(ToExpr);
1523}
1524
1525QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1526 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1527 if (ToUnderlyingType.isNull())
1528 return QualType();
1529
1530 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1531}
1532
1533QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1534 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1535 if (!ToExpr)
1536 return QualType();
1537
1538 return Importer.getToContext().getDecltypeType(ToExpr);
1539}
1540
1541QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1542 RecordDecl *ToDecl
1543 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1544 if (!ToDecl)
1545 return QualType();
1546
1547 return Importer.getToContext().getTagDeclType(ToDecl);
1548}
1549
1550QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1551 EnumDecl *ToDecl
1552 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1553 if (!ToDecl)
1554 return QualType();
1555
1556 return Importer.getToContext().getTagDeclType(ToDecl);
1557}
1558
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001559QualType ASTNodeImporter::VisitTemplateSpecializationType(
1560 TemplateSpecializationType *T) {
1561 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1562 if (ToTemplate.isNull())
1563 return QualType();
1564
1565 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1566 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1567 return QualType();
1568
1569 QualType ToCanonType;
1570 if (!QualType(T, 0).isCanonical()) {
1571 QualType FromCanonType
1572 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1573 ToCanonType =Importer.Import(FromCanonType);
1574 if (ToCanonType.isNull())
1575 return QualType();
1576 }
1577 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1578 ToTemplateArgs.data(),
1579 ToTemplateArgs.size(),
1580 ToCanonType);
1581}
1582
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001583QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001584 NestedNameSpecifier *ToQualifier = 0;
1585 // Note: the qualifier in an ElaboratedType is optional.
1586 if (T->getQualifier()) {
1587 ToQualifier = Importer.Import(T->getQualifier());
1588 if (!ToQualifier)
1589 return QualType();
1590 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001591
1592 QualType ToNamedType = Importer.Import(T->getNamedType());
1593 if (ToNamedType.isNull())
1594 return QualType();
1595
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001596 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1597 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001598}
1599
1600QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1601 ObjCInterfaceDecl *Class
1602 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1603 if (!Class)
1604 return QualType();
1605
John McCallc12c5bb2010-05-15 11:32:37 +00001606 return Importer.getToContext().getObjCInterfaceType(Class);
1607}
1608
1609QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1610 QualType ToBaseType = Importer.Import(T->getBaseType());
1611 if (ToBaseType.isNull())
1612 return QualType();
1613
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001614 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001615 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001616 PEnd = T->qual_end();
1617 P != PEnd; ++P) {
1618 ObjCProtocolDecl *Protocol
1619 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1620 if (!Protocol)
1621 return QualType();
1622 Protocols.push_back(Protocol);
1623 }
1624
John McCallc12c5bb2010-05-15 11:32:37 +00001625 return Importer.getToContext().getObjCObjectType(ToBaseType,
1626 Protocols.data(),
1627 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001628}
1629
1630QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1631 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1632 if (ToPointeeType.isNull())
1633 return QualType();
1634
John McCallc12c5bb2010-05-15 11:32:37 +00001635 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001636}
1637
Douglas Gregor089459a2010-02-08 21:09:39 +00001638//----------------------------------------------------------------------------
1639// Import Declarations
1640//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001641bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1642 DeclContext *&LexicalDC,
1643 DeclarationName &Name,
1644 SourceLocation &Loc) {
1645 // Import the context of this declaration.
1646 DC = Importer.ImportContext(D->getDeclContext());
1647 if (!DC)
1648 return true;
1649
1650 LexicalDC = DC;
1651 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1652 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1653 if (!LexicalDC)
1654 return true;
1655 }
1656
1657 // Import the name of this declaration.
1658 Name = Importer.Import(D->getDeclName());
1659 if (D->getDeclName() && !Name)
1660 return true;
1661
1662 // Import the location of this declaration.
1663 Loc = Importer.Import(D->getLocation());
1664 return false;
1665}
1666
Abramo Bagnara25777432010-08-11 22:01:17 +00001667void
1668ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1669 DeclarationNameInfo& To) {
1670 // NOTE: To.Name and To.Loc are already imported.
1671 // We only have to import To.LocInfo.
1672 switch (To.getName().getNameKind()) {
1673 case DeclarationName::Identifier:
1674 case DeclarationName::ObjCZeroArgSelector:
1675 case DeclarationName::ObjCOneArgSelector:
1676 case DeclarationName::ObjCMultiArgSelector:
1677 case DeclarationName::CXXUsingDirective:
1678 return;
1679
1680 case DeclarationName::CXXOperatorName: {
1681 SourceRange Range = From.getCXXOperatorNameRange();
1682 To.setCXXOperatorNameRange(Importer.Import(Range));
1683 return;
1684 }
1685 case DeclarationName::CXXLiteralOperatorName: {
1686 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1687 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1688 return;
1689 }
1690 case DeclarationName::CXXConstructorName:
1691 case DeclarationName::CXXDestructorName:
1692 case DeclarationName::CXXConversionFunctionName: {
1693 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1694 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1695 return;
1696 }
1697 assert(0 && "Unknown name kind.");
1698 }
1699}
1700
Douglas Gregor083a8212010-02-21 18:24:45 +00001701void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1702 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1703 FromEnd = FromDC->decls_end();
1704 From != FromEnd;
1705 ++From)
1706 Importer.Import(*From);
1707}
1708
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001709bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1710 if (To->getDefinition())
1711 return false;
1712
1713 To->startDefinition();
1714
1715 // Add base classes.
1716 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1717 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1718
1719 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1720 for (CXXRecordDecl::base_class_iterator
1721 Base1 = FromCXX->bases_begin(),
1722 FromBaseEnd = FromCXX->bases_end();
1723 Base1 != FromBaseEnd;
1724 ++Base1) {
1725 QualType T = Importer.Import(Base1->getType());
1726 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001727 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001728
1729 SourceLocation EllipsisLoc;
1730 if (Base1->isPackExpansion())
1731 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001732
1733 Bases.push_back(
1734 new (Importer.getToContext())
1735 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1736 Base1->isVirtual(),
1737 Base1->isBaseOfClass(),
1738 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001739 Importer.Import(Base1->getTypeSourceInfo()),
1740 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001741 }
1742 if (!Bases.empty())
1743 ToCXX->setBases(Bases.data(), Bases.size());
1744 }
1745
1746 ImportDeclContext(From);
1747 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001748 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001749}
1750
Douglas Gregor040afae2010-11-30 19:14:50 +00001751TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1752 TemplateParameterList *Params) {
1753 llvm::SmallVector<NamedDecl *, 4> ToParams;
1754 ToParams.reserve(Params->size());
1755 for (TemplateParameterList::iterator P = Params->begin(),
1756 PEnd = Params->end();
1757 P != PEnd; ++P) {
1758 Decl *To = Importer.Import(*P);
1759 if (!To)
1760 return 0;
1761
1762 ToParams.push_back(cast<NamedDecl>(To));
1763 }
1764
1765 return TemplateParameterList::Create(Importer.getToContext(),
1766 Importer.Import(Params->getTemplateLoc()),
1767 Importer.Import(Params->getLAngleLoc()),
1768 ToParams.data(), ToParams.size(),
1769 Importer.Import(Params->getRAngleLoc()));
1770}
1771
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001772TemplateArgument
1773ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1774 switch (From.getKind()) {
1775 case TemplateArgument::Null:
1776 return TemplateArgument();
1777
1778 case TemplateArgument::Type: {
1779 QualType ToType = Importer.Import(From.getAsType());
1780 if (ToType.isNull())
1781 return TemplateArgument();
1782 return TemplateArgument(ToType);
1783 }
1784
1785 case TemplateArgument::Integral: {
1786 QualType ToType = Importer.Import(From.getIntegralType());
1787 if (ToType.isNull())
1788 return TemplateArgument();
1789 return TemplateArgument(*From.getAsIntegral(), ToType);
1790 }
1791
1792 case TemplateArgument::Declaration:
1793 if (Decl *To = Importer.Import(From.getAsDecl()))
1794 return TemplateArgument(To);
1795 return TemplateArgument();
1796
1797 case TemplateArgument::Template: {
1798 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1799 if (ToTemplate.isNull())
1800 return TemplateArgument();
1801
1802 return TemplateArgument(ToTemplate);
1803 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001804
1805 case TemplateArgument::TemplateExpansion: {
1806 TemplateName ToTemplate
1807 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1808 if (ToTemplate.isNull())
1809 return TemplateArgument();
1810
1811 return TemplateArgument(ToTemplate, true);
1812 }
1813
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001814 case TemplateArgument::Expression:
1815 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1816 return TemplateArgument(ToExpr);
1817 return TemplateArgument();
1818
1819 case TemplateArgument::Pack: {
1820 llvm::SmallVector<TemplateArgument, 2> ToPack;
1821 ToPack.reserve(From.pack_size());
1822 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1823 return TemplateArgument();
1824
1825 TemplateArgument *ToArgs
1826 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1827 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1828 return TemplateArgument(ToArgs, ToPack.size());
1829 }
1830 }
1831
1832 llvm_unreachable("Invalid template argument kind");
1833 return TemplateArgument();
1834}
1835
1836bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1837 unsigned NumFromArgs,
1838 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1839 for (unsigned I = 0; I != NumFromArgs; ++I) {
1840 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1841 if (To.isNull() && !FromArgs[I].isNull())
1842 return true;
1843
1844 ToArgs.push_back(To);
1845 }
1846
1847 return false;
1848}
1849
Douglas Gregor96a01b42010-02-11 00:48:18 +00001850bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001851 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001852 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001853 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001854 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001855 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001856}
1857
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001858bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001859 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001860 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001861 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001862 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001863}
1864
Douglas Gregor040afae2010-11-30 19:14:50 +00001865bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1866 ClassTemplateDecl *To) {
1867 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1868 Importer.getToContext(),
1869 Importer.getNonEquivalentDecls());
1870 return Ctx.IsStructurallyEquivalent(From, To);
1871}
1872
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001873Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001874 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001875 << D->getDeclKindName();
1876 return 0;
1877}
1878
Douglas Gregor788c62d2010-02-21 18:26:36 +00001879Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1880 // Import the major distinguishing characteristics of this namespace.
1881 DeclContext *DC, *LexicalDC;
1882 DeclarationName Name;
1883 SourceLocation Loc;
1884 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1885 return 0;
1886
1887 NamespaceDecl *MergeWithNamespace = 0;
1888 if (!Name) {
1889 // This is an anonymous namespace. Adopt an existing anonymous
1890 // namespace if we can.
1891 // FIXME: Not testable.
1892 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1893 MergeWithNamespace = TU->getAnonymousNamespace();
1894 else
1895 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1896 } else {
1897 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1898 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1899 Lookup.first != Lookup.second;
1900 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001901 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001902 continue;
1903
1904 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1905 MergeWithNamespace = FoundNS;
1906 ConflictingDecls.clear();
1907 break;
1908 }
1909
1910 ConflictingDecls.push_back(*Lookup.first);
1911 }
1912
1913 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001914 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001915 ConflictingDecls.data(),
1916 ConflictingDecls.size());
1917 }
1918 }
1919
1920 // Create the "to" namespace, if needed.
1921 NamespaceDecl *ToNamespace = MergeWithNamespace;
1922 if (!ToNamespace) {
1923 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1924 Name.getAsIdentifierInfo());
1925 ToNamespace->setLexicalDeclContext(LexicalDC);
1926 LexicalDC->addDecl(ToNamespace);
1927
1928 // If this is an anonymous namespace, register it as the anonymous
1929 // namespace within its context.
1930 if (!Name) {
1931 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1932 TU->setAnonymousNamespace(ToNamespace);
1933 else
1934 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1935 }
1936 }
1937 Importer.Imported(D, ToNamespace);
1938
1939 ImportDeclContext(D);
1940
1941 return ToNamespace;
1942}
1943
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001944Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1945 // Import the major distinguishing characteristics of this typedef.
1946 DeclContext *DC, *LexicalDC;
1947 DeclarationName Name;
1948 SourceLocation Loc;
1949 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1950 return 0;
1951
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001952 // If this typedef is not in block scope, determine whether we've
1953 // seen a typedef with the same name (that we can merge with) or any
1954 // other entity by that name (which name lookup could conflict with).
1955 if (!DC->isFunctionOrMethod()) {
1956 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1957 unsigned IDNS = Decl::IDNS_Ordinary;
1958 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1959 Lookup.first != Lookup.second;
1960 ++Lookup.first) {
1961 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1962 continue;
1963 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00001964 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1965 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001966 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001967 }
1968
1969 ConflictingDecls.push_back(*Lookup.first);
1970 }
1971
1972 if (!ConflictingDecls.empty()) {
1973 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1974 ConflictingDecls.data(),
1975 ConflictingDecls.size());
1976 if (!Name)
1977 return 0;
1978 }
1979 }
1980
Douglas Gregorea35d112010-02-15 23:54:17 +00001981 // Import the underlying type of this typedef;
1982 QualType T = Importer.Import(D->getUnderlyingType());
1983 if (T.isNull())
1984 return 0;
1985
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001986 // Create the new typedef node.
1987 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1988 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1989 Loc, Name.getAsIdentifierInfo(),
1990 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00001991 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001992 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00001993 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001994 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00001995
Douglas Gregor9e5d9962010-02-10 21:10:29 +00001996 return ToTypedef;
1997}
1998
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001999Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2000 // Import the major distinguishing characteristics of this enum.
2001 DeclContext *DC, *LexicalDC;
2002 DeclarationName Name;
2003 SourceLocation Loc;
2004 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2005 return 0;
2006
2007 // Figure out what enum name we're looking for.
2008 unsigned IDNS = Decl::IDNS_Tag;
2009 DeclarationName SearchName = Name;
2010 if (!SearchName && D->getTypedefForAnonDecl()) {
2011 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2012 IDNS = Decl::IDNS_Ordinary;
2013 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2014 IDNS |= Decl::IDNS_Ordinary;
2015
2016 // We may already have an enum of the same name; try to find and match it.
2017 if (!DC->isFunctionOrMethod() && SearchName) {
2018 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2019 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2020 Lookup.first != Lookup.second;
2021 ++Lookup.first) {
2022 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2023 continue;
2024
2025 Decl *Found = *Lookup.first;
2026 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2027 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2028 Found = Tag->getDecl();
2029 }
2030
2031 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002032 if (IsStructuralMatch(D, FoundEnum))
2033 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002034 }
2035
2036 ConflictingDecls.push_back(*Lookup.first);
2037 }
2038
2039 if (!ConflictingDecls.empty()) {
2040 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2041 ConflictingDecls.data(),
2042 ConflictingDecls.size());
2043 }
2044 }
2045
2046 // Create the enum declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002047 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002048 Name.getAsIdentifierInfo(),
2049 Importer.Import(D->getTagKeywordLoc()), 0,
2050 D->isScoped(), D->isScopedUsingClassTag(),
2051 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002052 // Import the qualifier, if any.
2053 if (D->getQualifier()) {
2054 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2055 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2056 D2->setQualifierInfo(NNS, NNSRange);
2057 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002058 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002059 D2->setLexicalDeclContext(LexicalDC);
2060 Importer.Imported(D, D2);
2061 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002062
2063 // Import the integer type.
2064 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2065 if (ToIntegerType.isNull())
2066 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002067 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002068
2069 // Import the definition
2070 if (D->isDefinition()) {
2071 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2072 if (T.isNull())
2073 return 0;
2074
2075 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2076 if (ToPromotionType.isNull())
2077 return 0;
2078
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002079 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002080 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002081
2082 // FIXME: we might need to merge the number of positive or negative bits
2083 // if the enumerator lists don't match.
2084 D2->completeDefinition(T, ToPromotionType,
2085 D->getNumPositiveBits(),
2086 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002087 }
2088
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002089 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002090}
2091
Douglas Gregor96a01b42010-02-11 00:48:18 +00002092Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2093 // If this record has a definition in the translation unit we're coming from,
2094 // but this particular declaration is not that definition, import the
2095 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002096 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002097 if (Definition && Definition != D) {
2098 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002099 if (!ImportedDef)
2100 return 0;
2101
2102 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002103 }
2104
2105 // Import the major distinguishing characteristics of this record.
2106 DeclContext *DC, *LexicalDC;
2107 DeclarationName Name;
2108 SourceLocation Loc;
2109 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2110 return 0;
2111
2112 // Figure out what structure name we're looking for.
2113 unsigned IDNS = Decl::IDNS_Tag;
2114 DeclarationName SearchName = Name;
2115 if (!SearchName && D->getTypedefForAnonDecl()) {
2116 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2117 IDNS = Decl::IDNS_Ordinary;
2118 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2119 IDNS |= Decl::IDNS_Ordinary;
2120
2121 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002122 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002123 if (!DC->isFunctionOrMethod() && SearchName) {
2124 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2125 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2126 Lookup.first != Lookup.second;
2127 ++Lookup.first) {
2128 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2129 continue;
2130
2131 Decl *Found = *Lookup.first;
2132 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2133 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2134 Found = Tag->getDecl();
2135 }
2136
2137 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002138 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2139 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2140 // The record types structurally match, or the "from" translation
2141 // unit only had a forward declaration anyway; call it the same
2142 // function.
2143 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002144 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002145 }
2146 } else {
2147 // We have a forward declaration of this type, so adopt that forward
2148 // declaration rather than building a new one.
2149 AdoptDecl = FoundRecord;
2150 continue;
2151 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002152 }
2153
2154 ConflictingDecls.push_back(*Lookup.first);
2155 }
2156
2157 if (!ConflictingDecls.empty()) {
2158 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2159 ConflictingDecls.data(),
2160 ConflictingDecls.size());
2161 }
2162 }
2163
2164 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002165 RecordDecl *D2 = AdoptDecl;
2166 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002167 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002168 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002169 D->getTagKind(),
2170 DC, Loc,
2171 Name.getAsIdentifierInfo(),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002172 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002173 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002174 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002175 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002176 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002177 DC, Loc,
2178 Name.getAsIdentifierInfo(),
2179 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor96a01b42010-02-11 00:48:18 +00002180 }
John McCallb6217662010-03-15 10:12:16 +00002181 // Import the qualifier, if any.
2182 if (D->getQualifier()) {
2183 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2184 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2185 D2->setQualifierInfo(NNS, NNSRange);
2186 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002187 D2->setLexicalDeclContext(LexicalDC);
2188 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002189 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002190
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002191 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002192
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002193 if (D->isDefinition() && ImportDefinition(D, D2))
2194 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002195
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002196 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002197}
2198
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002199Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2200 // Import the major distinguishing characteristics of this enumerator.
2201 DeclContext *DC, *LexicalDC;
2202 DeclarationName Name;
2203 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002204 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002205 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002206
2207 QualType T = Importer.Import(D->getType());
2208 if (T.isNull())
2209 return 0;
2210
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002211 // Determine whether there are any other declarations with the same name and
2212 // in the same context.
2213 if (!LexicalDC->isFunctionOrMethod()) {
2214 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2215 unsigned IDNS = Decl::IDNS_Ordinary;
2216 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2217 Lookup.first != Lookup.second;
2218 ++Lookup.first) {
2219 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2220 continue;
2221
2222 ConflictingDecls.push_back(*Lookup.first);
2223 }
2224
2225 if (!ConflictingDecls.empty()) {
2226 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2227 ConflictingDecls.data(),
2228 ConflictingDecls.size());
2229 if (!Name)
2230 return 0;
2231 }
2232 }
2233
2234 Expr *Init = Importer.Import(D->getInitExpr());
2235 if (D->getInitExpr() && !Init)
2236 return 0;
2237
2238 EnumConstantDecl *ToEnumerator
2239 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2240 Name.getAsIdentifierInfo(), T,
2241 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002242 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002243 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002244 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002245 LexicalDC->addDecl(ToEnumerator);
2246 return ToEnumerator;
2247}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002248
Douglas Gregora404ea62010-02-10 19:54:31 +00002249Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2250 // Import the major distinguishing characteristics of this function.
2251 DeclContext *DC, *LexicalDC;
2252 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002253 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002254 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002255 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002256
Douglas Gregora404ea62010-02-10 19:54:31 +00002257 // Try to find a function in our own ("to") context with the same name, same
2258 // type, and in the same context as the function we're importing.
2259 if (!LexicalDC->isFunctionOrMethod()) {
2260 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2261 unsigned IDNS = Decl::IDNS_Ordinary;
2262 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2263 Lookup.first != Lookup.second;
2264 ++Lookup.first) {
2265 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2266 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002267
Douglas Gregora404ea62010-02-10 19:54:31 +00002268 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2269 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2270 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002271 if (Importer.IsStructurallyEquivalent(D->getType(),
2272 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002273 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002274 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002275 }
2276
2277 // FIXME: Check for overloading more carefully, e.g., by boosting
2278 // Sema::IsOverload out to the AST library.
2279
2280 // Function overloading is okay in C++.
2281 if (Importer.getToContext().getLangOptions().CPlusPlus)
2282 continue;
2283
2284 // Complain about inconsistent function types.
2285 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002286 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002287 Importer.ToDiag(FoundFunction->getLocation(),
2288 diag::note_odr_value_here)
2289 << FoundFunction->getType();
2290 }
2291 }
2292
2293 ConflictingDecls.push_back(*Lookup.first);
2294 }
2295
2296 if (!ConflictingDecls.empty()) {
2297 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2298 ConflictingDecls.data(),
2299 ConflictingDecls.size());
2300 if (!Name)
2301 return 0;
2302 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002303 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002304
Abramo Bagnara25777432010-08-11 22:01:17 +00002305 DeclarationNameInfo NameInfo(Name, Loc);
2306 // Import additional name location/type info.
2307 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2308
Douglas Gregorea35d112010-02-15 23:54:17 +00002309 // Import the type.
2310 QualType T = Importer.Import(D->getType());
2311 if (T.isNull())
2312 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002313
2314 // Import the function parameters.
2315 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2316 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2317 P != PEnd; ++P) {
2318 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2319 if (!ToP)
2320 return 0;
2321
2322 Parameters.push_back(ToP);
2323 }
2324
2325 // Create the imported function.
2326 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002327 FunctionDecl *ToFunction = 0;
2328 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2329 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2330 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002331 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002332 FromConstructor->isExplicit(),
2333 D->isInlineSpecified(),
2334 D->isImplicit());
2335 } else if (isa<CXXDestructorDecl>(D)) {
2336 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2337 cast<CXXRecordDecl>(DC),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002338 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002339 D->isInlineSpecified(),
2340 D->isImplicit());
2341 } else if (CXXConversionDecl *FromConversion
2342 = dyn_cast<CXXConversionDecl>(D)) {
2343 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2344 cast<CXXRecordDecl>(DC),
Abramo Bagnara25777432010-08-11 22:01:17 +00002345 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002346 D->isInlineSpecified(),
2347 FromConversion->isExplicit());
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002348 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2349 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2350 cast<CXXRecordDecl>(DC),
2351 NameInfo, T, TInfo,
2352 Method->isStatic(),
2353 Method->getStorageClassAsWritten(),
2354 Method->isInlineSpecified());
Douglas Gregorc144f352010-02-21 18:29:16 +00002355 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002356 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2357 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002358 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002359 D->isInlineSpecified(),
2360 D->hasWrittenPrototype());
2361 }
John McCallb6217662010-03-15 10:12:16 +00002362
2363 // Import the qualifier, if any.
2364 if (D->getQualifier()) {
2365 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2366 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2367 ToFunction->setQualifierInfo(NNS, NNSRange);
2368 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002369 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002370 ToFunction->setLexicalDeclContext(LexicalDC);
2371 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002372
Douglas Gregora404ea62010-02-10 19:54:31 +00002373 // Set the parameters.
2374 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002375 Parameters[I]->setOwningFunction(ToFunction);
2376 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002377 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002378 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002379
2380 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002381
2382 // Add this function to the lexical context.
2383 LexicalDC->addDecl(ToFunction);
2384
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002385 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002386}
2387
Douglas Gregorc144f352010-02-21 18:29:16 +00002388Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2389 return VisitFunctionDecl(D);
2390}
2391
2392Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2393 return VisitCXXMethodDecl(D);
2394}
2395
2396Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2397 return VisitCXXMethodDecl(D);
2398}
2399
2400Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2401 return VisitCXXMethodDecl(D);
2402}
2403
Douglas Gregor96a01b42010-02-11 00:48:18 +00002404Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2405 // Import the major distinguishing characteristics of a variable.
2406 DeclContext *DC, *LexicalDC;
2407 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002408 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002409 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2410 return 0;
2411
2412 // Import the type.
2413 QualType T = Importer.Import(D->getType());
2414 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002415 return 0;
2416
2417 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2418 Expr *BitWidth = Importer.Import(D->getBitWidth());
2419 if (!BitWidth && D->getBitWidth())
2420 return 0;
2421
2422 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2423 Loc, Name.getAsIdentifierInfo(),
2424 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002425 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002426 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002427 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002428 LexicalDC->addDecl(ToField);
2429 return ToField;
2430}
2431
Francois Pichet87c2e122010-11-21 06:08:52 +00002432Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2433 // Import the major distinguishing characteristics of a variable.
2434 DeclContext *DC, *LexicalDC;
2435 DeclarationName Name;
2436 SourceLocation Loc;
2437 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2438 return 0;
2439
2440 // Import the type.
2441 QualType T = Importer.Import(D->getType());
2442 if (T.isNull())
2443 return 0;
2444
2445 NamedDecl **NamedChain =
2446 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2447
2448 unsigned i = 0;
2449 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2450 PE = D->chain_end(); PI != PE; ++PI) {
2451 Decl* D = Importer.Import(*PI);
2452 if (!D)
2453 return 0;
2454 NamedChain[i++] = cast<NamedDecl>(D);
2455 }
2456
2457 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2458 Importer.getToContext(), DC,
2459 Loc, Name.getAsIdentifierInfo(), T,
2460 NamedChain, D->getChainingSize());
2461 ToIndirectField->setAccess(D->getAccess());
2462 ToIndirectField->setLexicalDeclContext(LexicalDC);
2463 Importer.Imported(D, ToIndirectField);
2464 LexicalDC->addDecl(ToIndirectField);
2465 return ToIndirectField;
2466}
2467
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002468Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2469 // Import the major distinguishing characteristics of an ivar.
2470 DeclContext *DC, *LexicalDC;
2471 DeclarationName Name;
2472 SourceLocation Loc;
2473 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2474 return 0;
2475
2476 // Determine whether we've already imported this ivar
2477 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2478 Lookup.first != Lookup.second;
2479 ++Lookup.first) {
2480 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2481 if (Importer.IsStructurallyEquivalent(D->getType(),
2482 FoundIvar->getType())) {
2483 Importer.Imported(D, FoundIvar);
2484 return FoundIvar;
2485 }
2486
2487 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2488 << Name << D->getType() << FoundIvar->getType();
2489 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2490 << FoundIvar->getType();
2491 return 0;
2492 }
2493 }
2494
2495 // Import the type.
2496 QualType T = Importer.Import(D->getType());
2497 if (T.isNull())
2498 return 0;
2499
2500 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2501 Expr *BitWidth = Importer.Import(D->getBitWidth());
2502 if (!BitWidth && D->getBitWidth())
2503 return 0;
2504
Daniel Dunbara0654922010-04-02 20:10:03 +00002505 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2506 cast<ObjCContainerDecl>(DC),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002507 Loc, Name.getAsIdentifierInfo(),
2508 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002509 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002510 ToIvar->setLexicalDeclContext(LexicalDC);
2511 Importer.Imported(D, ToIvar);
2512 LexicalDC->addDecl(ToIvar);
2513 return ToIvar;
2514
2515}
2516
Douglas Gregora404ea62010-02-10 19:54:31 +00002517Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2518 // Import the major distinguishing characteristics of a variable.
2519 DeclContext *DC, *LexicalDC;
2520 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002521 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002522 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002523 return 0;
2524
Douglas Gregor089459a2010-02-08 21:09:39 +00002525 // Try to find a variable in our own ("to") context with the same name and
2526 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002527 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002528 VarDecl *MergeWithVar = 0;
2529 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2530 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002531 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002532 Lookup.first != Lookup.second;
2533 ++Lookup.first) {
2534 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2535 continue;
2536
2537 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2538 // We have found a variable that we may need to merge with. Check it.
2539 if (isExternalLinkage(FoundVar->getLinkage()) &&
2540 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002541 if (Importer.IsStructurallyEquivalent(D->getType(),
2542 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002543 MergeWithVar = FoundVar;
2544 break;
2545 }
2546
Douglas Gregord0145422010-02-12 17:23:39 +00002547 const ArrayType *FoundArray
2548 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2549 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002550 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002551 if (FoundArray && TArray) {
2552 if (isa<IncompleteArrayType>(FoundArray) &&
2553 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002554 // Import the type.
2555 QualType T = Importer.Import(D->getType());
2556 if (T.isNull())
2557 return 0;
2558
Douglas Gregord0145422010-02-12 17:23:39 +00002559 FoundVar->setType(T);
2560 MergeWithVar = FoundVar;
2561 break;
2562 } else if (isa<IncompleteArrayType>(TArray) &&
2563 isa<ConstantArrayType>(FoundArray)) {
2564 MergeWithVar = FoundVar;
2565 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002566 }
2567 }
2568
Douglas Gregor089459a2010-02-08 21:09:39 +00002569 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002570 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002571 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2572 << FoundVar->getType();
2573 }
2574 }
2575
2576 ConflictingDecls.push_back(*Lookup.first);
2577 }
2578
2579 if (MergeWithVar) {
2580 // An equivalent variable with external linkage has been found. Link
2581 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002582 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002583
2584 if (VarDecl *DDef = D->getDefinition()) {
2585 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2586 Importer.ToDiag(ExistingDef->getLocation(),
2587 diag::err_odr_variable_multiple_def)
2588 << Name;
2589 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2590 } else {
2591 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002592 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002593 }
2594 }
2595
2596 return MergeWithVar;
2597 }
2598
2599 if (!ConflictingDecls.empty()) {
2600 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2601 ConflictingDecls.data(),
2602 ConflictingDecls.size());
2603 if (!Name)
2604 return 0;
2605 }
2606 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002607
Douglas Gregorea35d112010-02-15 23:54:17 +00002608 // Import the type.
2609 QualType T = Importer.Import(D->getType());
2610 if (T.isNull())
2611 return 0;
2612
Douglas Gregor089459a2010-02-08 21:09:39 +00002613 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002614 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor089459a2010-02-08 21:09:39 +00002615 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2616 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002617 D->getStorageClass(),
2618 D->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002619 // Import the qualifier, if any.
2620 if (D->getQualifier()) {
2621 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2622 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2623 ToVar->setQualifierInfo(NNS, NNSRange);
2624 }
Douglas Gregor325bf172010-02-22 17:42:47 +00002625 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002626 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002627 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002628 LexicalDC->addDecl(ToVar);
2629
Douglas Gregor089459a2010-02-08 21:09:39 +00002630 // Merge the initializer.
2631 // FIXME: Can we really import any initializer? Alternatively, we could force
2632 // ourselves to import every declaration of a variable and then only use
2633 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002634 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002635
2636 // FIXME: Other bits to merge?
2637
2638 return ToVar;
2639}
2640
Douglas Gregor2cd00932010-02-17 21:22:52 +00002641Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2642 // Parameters are created in the translation unit's context, then moved
2643 // into the function declaration's context afterward.
2644 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2645
2646 // Import the name of this declaration.
2647 DeclarationName Name = Importer.Import(D->getDeclName());
2648 if (D->getDeclName() && !Name)
2649 return 0;
2650
2651 // Import the location of this declaration.
2652 SourceLocation Loc = Importer.Import(D->getLocation());
2653
2654 // Import the parameter's type.
2655 QualType T = Importer.Import(D->getType());
2656 if (T.isNull())
2657 return 0;
2658
2659 // Create the imported parameter.
2660 ImplicitParamDecl *ToParm
2661 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2662 Loc, Name.getAsIdentifierInfo(),
2663 T);
2664 return Importer.Imported(D, ToParm);
2665}
2666
Douglas Gregora404ea62010-02-10 19:54:31 +00002667Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2668 // Parameters are created in the translation unit's context, then moved
2669 // into the function declaration's context afterward.
2670 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2671
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002672 // Import the name of this declaration.
2673 DeclarationName Name = Importer.Import(D->getDeclName());
2674 if (D->getDeclName() && !Name)
2675 return 0;
2676
Douglas Gregora404ea62010-02-10 19:54:31 +00002677 // Import the location of this declaration.
2678 SourceLocation Loc = Importer.Import(D->getLocation());
2679
2680 // Import the parameter's type.
2681 QualType T = Importer.Import(D->getType());
2682 if (T.isNull())
2683 return 0;
2684
2685 // Create the imported parameter.
2686 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2687 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2688 Loc, Name.getAsIdentifierInfo(),
2689 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002690 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002691 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002692 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002693 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002694}
2695
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002696Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2697 // Import the major distinguishing characteristics of a method.
2698 DeclContext *DC, *LexicalDC;
2699 DeclarationName Name;
2700 SourceLocation Loc;
2701 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2702 return 0;
2703
2704 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2705 Lookup.first != Lookup.second;
2706 ++Lookup.first) {
2707 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2708 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2709 continue;
2710
2711 // Check return types.
2712 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2713 FoundMethod->getResultType())) {
2714 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2715 << D->isInstanceMethod() << Name
2716 << D->getResultType() << FoundMethod->getResultType();
2717 Importer.ToDiag(FoundMethod->getLocation(),
2718 diag::note_odr_objc_method_here)
2719 << D->isInstanceMethod() << Name;
2720 return 0;
2721 }
2722
2723 // Check the number of parameters.
2724 if (D->param_size() != FoundMethod->param_size()) {
2725 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2726 << D->isInstanceMethod() << Name
2727 << D->param_size() << FoundMethod->param_size();
2728 Importer.ToDiag(FoundMethod->getLocation(),
2729 diag::note_odr_objc_method_here)
2730 << D->isInstanceMethod() << Name;
2731 return 0;
2732 }
2733
2734 // Check parameter types.
2735 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2736 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2737 P != PEnd; ++P, ++FoundP) {
2738 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2739 (*FoundP)->getType())) {
2740 Importer.FromDiag((*P)->getLocation(),
2741 diag::err_odr_objc_method_param_type_inconsistent)
2742 << D->isInstanceMethod() << Name
2743 << (*P)->getType() << (*FoundP)->getType();
2744 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2745 << (*FoundP)->getType();
2746 return 0;
2747 }
2748 }
2749
2750 // Check variadic/non-variadic.
2751 // Check the number of parameters.
2752 if (D->isVariadic() != FoundMethod->isVariadic()) {
2753 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2754 << D->isInstanceMethod() << Name;
2755 Importer.ToDiag(FoundMethod->getLocation(),
2756 diag::note_odr_objc_method_here)
2757 << D->isInstanceMethod() << Name;
2758 return 0;
2759 }
2760
2761 // FIXME: Any other bits we need to merge?
2762 return Importer.Imported(D, FoundMethod);
2763 }
2764 }
2765
2766 // Import the result type.
2767 QualType ResultTy = Importer.Import(D->getResultType());
2768 if (ResultTy.isNull())
2769 return 0;
2770
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002771 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2772
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002773 ObjCMethodDecl *ToMethod
2774 = ObjCMethodDecl::Create(Importer.getToContext(),
2775 Loc,
2776 Importer.Import(D->getLocEnd()),
2777 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002778 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002779 D->isInstanceMethod(),
2780 D->isVariadic(),
2781 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002782 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002783 D->getImplementationControl());
2784
2785 // FIXME: When we decide to merge method definitions, we'll need to
2786 // deal with implicit parameters.
2787
2788 // Import the parameters
2789 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2790 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2791 FromPEnd = D->param_end();
2792 FromP != FromPEnd;
2793 ++FromP) {
2794 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2795 if (!ToP)
2796 return 0;
2797
2798 ToParams.push_back(ToP);
2799 }
2800
2801 // Set the parameters.
2802 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2803 ToParams[I]->setOwningFunction(ToMethod);
2804 ToMethod->addDecl(ToParams[I]);
2805 }
2806 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002807 ToParams.data(), ToParams.size(),
2808 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002809
2810 ToMethod->setLexicalDeclContext(LexicalDC);
2811 Importer.Imported(D, ToMethod);
2812 LexicalDC->addDecl(ToMethod);
2813 return ToMethod;
2814}
2815
Douglas Gregorb4677b62010-02-18 01:47:50 +00002816Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2817 // Import the major distinguishing characteristics of a category.
2818 DeclContext *DC, *LexicalDC;
2819 DeclarationName Name;
2820 SourceLocation Loc;
2821 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2822 return 0;
2823
2824 ObjCInterfaceDecl *ToInterface
2825 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2826 if (!ToInterface)
2827 return 0;
2828
2829 // Determine if we've already encountered this category.
2830 ObjCCategoryDecl *MergeWithCategory
2831 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2832 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2833 if (!ToCategory) {
2834 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2835 Importer.Import(D->getAtLoc()),
2836 Loc,
2837 Importer.Import(D->getCategoryNameLoc()),
2838 Name.getAsIdentifierInfo());
2839 ToCategory->setLexicalDeclContext(LexicalDC);
2840 LexicalDC->addDecl(ToCategory);
2841 Importer.Imported(D, ToCategory);
2842
2843 // Link this category into its class's category list.
2844 ToCategory->setClassInterface(ToInterface);
2845 ToCategory->insertNextClassCategory();
2846
2847 // Import protocols
2848 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2849 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2850 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2851 = D->protocol_loc_begin();
2852 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2853 FromProtoEnd = D->protocol_end();
2854 FromProto != FromProtoEnd;
2855 ++FromProto, ++FromProtoLoc) {
2856 ObjCProtocolDecl *ToProto
2857 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2858 if (!ToProto)
2859 return 0;
2860 Protocols.push_back(ToProto);
2861 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2862 }
2863
2864 // FIXME: If we're merging, make sure that the protocol list is the same.
2865 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2866 ProtocolLocs.data(), Importer.getToContext());
2867
2868 } else {
2869 Importer.Imported(D, ToCategory);
2870 }
2871
2872 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002873 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002874
2875 // If we have an implementation, import it as well.
2876 if (D->getImplementation()) {
2877 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002878 = cast_or_null<ObjCCategoryImplDecl>(
2879 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002880 if (!Impl)
2881 return 0;
2882
2883 ToCategory->setImplementation(Impl);
2884 }
2885
2886 return ToCategory;
2887}
2888
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002889Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002890 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002891 DeclContext *DC, *LexicalDC;
2892 DeclarationName Name;
2893 SourceLocation Loc;
2894 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2895 return 0;
2896
2897 ObjCProtocolDecl *MergeWithProtocol = 0;
2898 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2899 Lookup.first != Lookup.second;
2900 ++Lookup.first) {
2901 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2902 continue;
2903
2904 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2905 break;
2906 }
2907
2908 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2909 if (!ToProto || ToProto->isForwardDecl()) {
2910 if (!ToProto) {
2911 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2912 Name.getAsIdentifierInfo());
2913 ToProto->setForwardDecl(D->isForwardDecl());
2914 ToProto->setLexicalDeclContext(LexicalDC);
2915 LexicalDC->addDecl(ToProto);
2916 }
2917 Importer.Imported(D, ToProto);
2918
2919 // Import protocols
2920 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2921 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2922 ObjCProtocolDecl::protocol_loc_iterator
2923 FromProtoLoc = D->protocol_loc_begin();
2924 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2925 FromProtoEnd = D->protocol_end();
2926 FromProto != FromProtoEnd;
2927 ++FromProto, ++FromProtoLoc) {
2928 ObjCProtocolDecl *ToProto
2929 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2930 if (!ToProto)
2931 return 0;
2932 Protocols.push_back(ToProto);
2933 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2934 }
2935
2936 // FIXME: If we're merging, make sure that the protocol list is the same.
2937 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2938 ProtocolLocs.data(), Importer.getToContext());
2939 } else {
2940 Importer.Imported(D, ToProto);
2941 }
2942
Douglas Gregorb4677b62010-02-18 01:47:50 +00002943 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00002944 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002945
2946 return ToProto;
2947}
2948
Douglas Gregora12d2942010-02-16 01:20:57 +00002949Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2950 // Import the major distinguishing characteristics of an @interface.
2951 DeclContext *DC, *LexicalDC;
2952 DeclarationName Name;
2953 SourceLocation Loc;
2954 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2955 return 0;
2956
2957 ObjCInterfaceDecl *MergeWithIface = 0;
2958 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2959 Lookup.first != Lookup.second;
2960 ++Lookup.first) {
2961 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2962 continue;
2963
2964 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2965 break;
2966 }
2967
2968 ObjCInterfaceDecl *ToIface = MergeWithIface;
2969 if (!ToIface || ToIface->isForwardDecl()) {
2970 if (!ToIface) {
2971 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2972 DC, Loc,
2973 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00002974 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00002975 D->isForwardDecl(),
2976 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002977 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00002978 ToIface->setLexicalDeclContext(LexicalDC);
2979 LexicalDC->addDecl(ToIface);
2980 }
2981 Importer.Imported(D, ToIface);
2982
Douglas Gregora12d2942010-02-16 01:20:57 +00002983 if (D->getSuperClass()) {
2984 ObjCInterfaceDecl *Super
2985 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2986 if (!Super)
2987 return 0;
2988
2989 ToIface->setSuperClass(Super);
2990 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2991 }
2992
2993 // Import protocols
2994 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2995 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2996 ObjCInterfaceDecl::protocol_loc_iterator
2997 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00002998
2999 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003000 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3001 FromProtoEnd = D->protocol_end();
3002 FromProto != FromProtoEnd;
3003 ++FromProto, ++FromProtoLoc) {
3004 ObjCProtocolDecl *ToProto
3005 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3006 if (!ToProto)
3007 return 0;
3008 Protocols.push_back(ToProto);
3009 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3010 }
3011
3012 // FIXME: If we're merging, make sure that the protocol list is the same.
3013 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3014 ProtocolLocs.data(), Importer.getToContext());
3015
Douglas Gregora12d2942010-02-16 01:20:57 +00003016 // Import @end range
3017 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3018 } else {
3019 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003020
3021 // Check for consistency of superclasses.
3022 DeclarationName FromSuperName, ToSuperName;
3023 if (D->getSuperClass())
3024 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3025 if (ToIface->getSuperClass())
3026 ToSuperName = ToIface->getSuperClass()->getDeclName();
3027 if (FromSuperName != ToSuperName) {
3028 Importer.ToDiag(ToIface->getLocation(),
3029 diag::err_odr_objc_superclass_inconsistent)
3030 << ToIface->getDeclName();
3031 if (ToIface->getSuperClass())
3032 Importer.ToDiag(ToIface->getSuperClassLoc(),
3033 diag::note_odr_objc_superclass)
3034 << ToIface->getSuperClass()->getDeclName();
3035 else
3036 Importer.ToDiag(ToIface->getLocation(),
3037 diag::note_odr_objc_missing_superclass);
3038 if (D->getSuperClass())
3039 Importer.FromDiag(D->getSuperClassLoc(),
3040 diag::note_odr_objc_superclass)
3041 << D->getSuperClass()->getDeclName();
3042 else
3043 Importer.FromDiag(D->getLocation(),
3044 diag::note_odr_objc_missing_superclass);
3045 return 0;
3046 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003047 }
3048
Douglas Gregorb4677b62010-02-18 01:47:50 +00003049 // Import categories. When the categories themselves are imported, they'll
3050 // hook themselves into this interface.
3051 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3052 FromCat = FromCat->getNextClassCategory())
3053 Importer.Import(FromCat);
3054
Douglas Gregora12d2942010-02-16 01:20:57 +00003055 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003056 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003057
3058 // If we have an @implementation, import it as well.
3059 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003060 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3061 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003062 if (!Impl)
3063 return 0;
3064
3065 ToIface->setImplementation(Impl);
3066 }
3067
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003068 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003069}
3070
Douglas Gregor3daef292010-12-07 15:32:12 +00003071Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3072 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3073 Importer.Import(D->getCategoryDecl()));
3074 if (!Category)
3075 return 0;
3076
3077 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3078 if (!ToImpl) {
3079 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3080 if (!DC)
3081 return 0;
3082
3083 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3084 Importer.Import(D->getLocation()),
3085 Importer.Import(D->getIdentifier()),
3086 Category->getClassInterface());
3087
3088 DeclContext *LexicalDC = DC;
3089 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3090 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3091 if (!LexicalDC)
3092 return 0;
3093
3094 ToImpl->setLexicalDeclContext(LexicalDC);
3095 }
3096
3097 LexicalDC->addDecl(ToImpl);
3098 Category->setImplementation(ToImpl);
3099 }
3100
3101 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003102 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003103 return ToImpl;
3104}
3105
Douglas Gregordd182ff2010-12-07 01:26:03 +00003106Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3107 // Find the corresponding interface.
3108 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3109 Importer.Import(D->getClassInterface()));
3110 if (!Iface)
3111 return 0;
3112
3113 // Import the superclass, if any.
3114 ObjCInterfaceDecl *Super = 0;
3115 if (D->getSuperClass()) {
3116 Super = cast_or_null<ObjCInterfaceDecl>(
3117 Importer.Import(D->getSuperClass()));
3118 if (!Super)
3119 return 0;
3120 }
3121
3122 ObjCImplementationDecl *Impl = Iface->getImplementation();
3123 if (!Impl) {
3124 // We haven't imported an implementation yet. Create a new @implementation
3125 // now.
3126 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3127 Importer.ImportContext(D->getDeclContext()),
3128 Importer.Import(D->getLocation()),
3129 Iface, Super);
3130
3131 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3132 DeclContext *LexicalDC
3133 = Importer.ImportContext(D->getLexicalDeclContext());
3134 if (!LexicalDC)
3135 return 0;
3136 Impl->setLexicalDeclContext(LexicalDC);
3137 }
3138
3139 // Associate the implementation with the class it implements.
3140 Iface->setImplementation(Impl);
3141 Importer.Imported(D, Iface->getImplementation());
3142 } else {
3143 Importer.Imported(D, Iface->getImplementation());
3144
3145 // Verify that the existing @implementation has the same superclass.
3146 if ((Super && !Impl->getSuperClass()) ||
3147 (!Super && Impl->getSuperClass()) ||
3148 (Super && Impl->getSuperClass() &&
3149 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3150 Importer.ToDiag(Impl->getLocation(),
3151 diag::err_odr_objc_superclass_inconsistent)
3152 << Iface->getDeclName();
3153 // FIXME: It would be nice to have the location of the superclass
3154 // below.
3155 if (Impl->getSuperClass())
3156 Importer.ToDiag(Impl->getLocation(),
3157 diag::note_odr_objc_superclass)
3158 << Impl->getSuperClass()->getDeclName();
3159 else
3160 Importer.ToDiag(Impl->getLocation(),
3161 diag::note_odr_objc_missing_superclass);
3162 if (D->getSuperClass())
3163 Importer.FromDiag(D->getLocation(),
3164 diag::note_odr_objc_superclass)
3165 << D->getSuperClass()->getDeclName();
3166 else
3167 Importer.FromDiag(D->getLocation(),
3168 diag::note_odr_objc_missing_superclass);
3169 return 0;
3170 }
3171 }
3172
3173 // Import all of the members of this @implementation.
3174 ImportDeclContext(D);
3175
3176 return Impl;
3177}
3178
Douglas Gregore3261622010-02-17 18:02:10 +00003179Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3180 // Import the major distinguishing characteristics of an @property.
3181 DeclContext *DC, *LexicalDC;
3182 DeclarationName Name;
3183 SourceLocation Loc;
3184 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3185 return 0;
3186
3187 // Check whether we have already imported this property.
3188 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3189 Lookup.first != Lookup.second;
3190 ++Lookup.first) {
3191 if (ObjCPropertyDecl *FoundProp
3192 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3193 // Check property types.
3194 if (!Importer.IsStructurallyEquivalent(D->getType(),
3195 FoundProp->getType())) {
3196 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3197 << Name << D->getType() << FoundProp->getType();
3198 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3199 << FoundProp->getType();
3200 return 0;
3201 }
3202
3203 // FIXME: Check property attributes, getters, setters, etc.?
3204
3205 // Consider these properties to be equivalent.
3206 Importer.Imported(D, FoundProp);
3207 return FoundProp;
3208 }
3209 }
3210
3211 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003212 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3213 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003214 return 0;
3215
3216 // Create the new property.
3217 ObjCPropertyDecl *ToProperty
3218 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3219 Name.getAsIdentifierInfo(),
3220 Importer.Import(D->getAtLoc()),
3221 T,
3222 D->getPropertyImplementation());
3223 Importer.Imported(D, ToProperty);
3224 ToProperty->setLexicalDeclContext(LexicalDC);
3225 LexicalDC->addDecl(ToProperty);
3226
3227 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003228 ToProperty->setPropertyAttributesAsWritten(
3229 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003230 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3231 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3232 ToProperty->setGetterMethodDecl(
3233 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3234 ToProperty->setSetterMethodDecl(
3235 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3236 ToProperty->setPropertyIvarDecl(
3237 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3238 return ToProperty;
3239}
3240
Douglas Gregor954e0c72010-12-07 18:32:03 +00003241Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3242 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3243 Importer.Import(D->getPropertyDecl()));
3244 if (!Property)
3245 return 0;
3246
3247 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3248 if (!DC)
3249 return 0;
3250
3251 // Import the lexical declaration context.
3252 DeclContext *LexicalDC = DC;
3253 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3254 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3255 if (!LexicalDC)
3256 return 0;
3257 }
3258
3259 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3260 if (!InImpl)
3261 return 0;
3262
3263 // Import the ivar (for an @synthesize).
3264 ObjCIvarDecl *Ivar = 0;
3265 if (D->getPropertyIvarDecl()) {
3266 Ivar = cast_or_null<ObjCIvarDecl>(
3267 Importer.Import(D->getPropertyIvarDecl()));
3268 if (!Ivar)
3269 return 0;
3270 }
3271
3272 ObjCPropertyImplDecl *ToImpl
3273 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3274 if (!ToImpl) {
3275 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3276 Importer.Import(D->getLocStart()),
3277 Importer.Import(D->getLocation()),
3278 Property,
3279 D->getPropertyImplementation(),
3280 Ivar,
3281 Importer.Import(D->getPropertyIvarDeclLoc()));
3282 ToImpl->setLexicalDeclContext(LexicalDC);
3283 Importer.Imported(D, ToImpl);
3284 LexicalDC->addDecl(ToImpl);
3285 } else {
3286 // Check that we have the same kind of property implementation (@synthesize
3287 // vs. @dynamic).
3288 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3289 Importer.ToDiag(ToImpl->getLocation(),
3290 diag::err_odr_objc_property_impl_kind_inconsistent)
3291 << Property->getDeclName()
3292 << (ToImpl->getPropertyImplementation()
3293 == ObjCPropertyImplDecl::Dynamic);
3294 Importer.FromDiag(D->getLocation(),
3295 diag::note_odr_objc_property_impl_kind)
3296 << D->getPropertyDecl()->getDeclName()
3297 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3298 return 0;
3299 }
3300
3301 // For @synthesize, check that we have the same
3302 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3303 Ivar != ToImpl->getPropertyIvarDecl()) {
3304 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3305 diag::err_odr_objc_synthesize_ivar_inconsistent)
3306 << Property->getDeclName()
3307 << ToImpl->getPropertyIvarDecl()->getDeclName()
3308 << Ivar->getDeclName();
3309 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3310 diag::note_odr_objc_synthesize_ivar_here)
3311 << D->getPropertyIvarDecl()->getDeclName();
3312 return 0;
3313 }
3314
3315 // Merge the existing implementation with the new implementation.
3316 Importer.Imported(D, ToImpl);
3317 }
3318
3319 return ToImpl;
3320}
3321
Douglas Gregor2b785022010-02-18 02:12:22 +00003322Decl *
3323ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3324 // Import the context of this declaration.
3325 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3326 if (!DC)
3327 return 0;
3328
3329 DeclContext *LexicalDC = DC;
3330 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3331 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3332 if (!LexicalDC)
3333 return 0;
3334 }
3335
3336 // Import the location of this declaration.
3337 SourceLocation Loc = Importer.Import(D->getLocation());
3338
3339 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3340 llvm::SmallVector<SourceLocation, 4> Locations;
3341 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3342 = D->protocol_loc_begin();
3343 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3344 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3345 FromProto != FromProtoEnd;
3346 ++FromProto, ++FromProtoLoc) {
3347 ObjCProtocolDecl *ToProto
3348 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3349 if (!ToProto)
3350 continue;
3351
3352 Protocols.push_back(ToProto);
3353 Locations.push_back(Importer.Import(*FromProtoLoc));
3354 }
3355
3356 ObjCForwardProtocolDecl *ToForward
3357 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3358 Protocols.data(), Protocols.size(),
3359 Locations.data());
3360 ToForward->setLexicalDeclContext(LexicalDC);
3361 LexicalDC->addDecl(ToForward);
3362 Importer.Imported(D, ToForward);
3363 return ToForward;
3364}
3365
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003366Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3367 // Import the context of this declaration.
3368 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3369 if (!DC)
3370 return 0;
3371
3372 DeclContext *LexicalDC = DC;
3373 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3374 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3375 if (!LexicalDC)
3376 return 0;
3377 }
3378
3379 // Import the location of this declaration.
3380 SourceLocation Loc = Importer.Import(D->getLocation());
3381
3382 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3383 llvm::SmallVector<SourceLocation, 4> Locations;
3384 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3385 From != FromEnd; ++From) {
3386 ObjCInterfaceDecl *ToIface
3387 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3388 if (!ToIface)
3389 continue;
3390
3391 Interfaces.push_back(ToIface);
3392 Locations.push_back(Importer.Import(From->getLocation()));
3393 }
3394
3395 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3396 Loc,
3397 Interfaces.data(),
3398 Locations.data(),
3399 Interfaces.size());
3400 ToClass->setLexicalDeclContext(LexicalDC);
3401 LexicalDC->addDecl(ToClass);
3402 Importer.Imported(D, ToClass);
3403 return ToClass;
3404}
3405
Douglas Gregor040afae2010-11-30 19:14:50 +00003406Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3407 // For template arguments, we adopt the translation unit as our declaration
3408 // context. This context will be fixed when the actual template declaration
3409 // is created.
3410
3411 // FIXME: Import default argument.
3412 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3413 Importer.getToContext().getTranslationUnitDecl(),
3414 Importer.Import(D->getLocation()),
3415 D->getDepth(),
3416 D->getIndex(),
3417 Importer.Import(D->getIdentifier()),
3418 D->wasDeclaredWithTypename(),
3419 D->isParameterPack());
3420}
3421
3422Decl *
3423ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3424 // Import the name of this declaration.
3425 DeclarationName Name = Importer.Import(D->getDeclName());
3426 if (D->getDeclName() && !Name)
3427 return 0;
3428
3429 // Import the location of this declaration.
3430 SourceLocation Loc = Importer.Import(D->getLocation());
3431
3432 // Import the type of this declaration.
3433 QualType T = Importer.Import(D->getType());
3434 if (T.isNull())
3435 return 0;
3436
3437 // Import type-source information.
3438 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3439 if (D->getTypeSourceInfo() && !TInfo)
3440 return 0;
3441
3442 // FIXME: Import default argument.
3443
3444 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3445 Importer.getToContext().getTranslationUnitDecl(),
3446 Loc, D->getDepth(), D->getPosition(),
3447 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003448 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003449}
3450
3451Decl *
3452ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3453 // Import the name of this declaration.
3454 DeclarationName Name = Importer.Import(D->getDeclName());
3455 if (D->getDeclName() && !Name)
3456 return 0;
3457
3458 // Import the location of this declaration.
3459 SourceLocation Loc = Importer.Import(D->getLocation());
3460
3461 // Import template parameters.
3462 TemplateParameterList *TemplateParams
3463 = ImportTemplateParameterList(D->getTemplateParameters());
3464 if (!TemplateParams)
3465 return 0;
3466
3467 // FIXME: Import default argument.
3468
3469 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3470 Importer.getToContext().getTranslationUnitDecl(),
3471 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003472 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003473 Name.getAsIdentifierInfo(),
3474 TemplateParams);
3475}
3476
3477Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3478 // If this record has a definition in the translation unit we're coming from,
3479 // but this particular declaration is not that definition, import the
3480 // definition and map to that.
3481 CXXRecordDecl *Definition
3482 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3483 if (Definition && Definition != D->getTemplatedDecl()) {
3484 Decl *ImportedDef
3485 = Importer.Import(Definition->getDescribedClassTemplate());
3486 if (!ImportedDef)
3487 return 0;
3488
3489 return Importer.Imported(D, ImportedDef);
3490 }
3491
3492 // Import the major distinguishing characteristics of this class template.
3493 DeclContext *DC, *LexicalDC;
3494 DeclarationName Name;
3495 SourceLocation Loc;
3496 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3497 return 0;
3498
3499 // We may already have a template of the same name; try to find and match it.
3500 if (!DC->isFunctionOrMethod()) {
3501 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3502 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3503 Lookup.first != Lookup.second;
3504 ++Lookup.first) {
3505 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3506 continue;
3507
3508 Decl *Found = *Lookup.first;
3509 if (ClassTemplateDecl *FoundTemplate
3510 = dyn_cast<ClassTemplateDecl>(Found)) {
3511 if (IsStructuralMatch(D, FoundTemplate)) {
3512 // The class templates structurally match; call it the same template.
3513 // FIXME: We may be filling in a forward declaration here. Handle
3514 // this case!
3515 Importer.Imported(D->getTemplatedDecl(),
3516 FoundTemplate->getTemplatedDecl());
3517 return Importer.Imported(D, FoundTemplate);
3518 }
3519 }
3520
3521 ConflictingDecls.push_back(*Lookup.first);
3522 }
3523
3524 if (!ConflictingDecls.empty()) {
3525 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3526 ConflictingDecls.data(),
3527 ConflictingDecls.size());
3528 }
3529
3530 if (!Name)
3531 return 0;
3532 }
3533
3534 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3535
3536 // Create the declaration that is being templated.
3537 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3538 DTemplated->getTagKind(),
3539 DC,
3540 Importer.Import(DTemplated->getLocation()),
3541 Name.getAsIdentifierInfo(),
3542 Importer.Import(DTemplated->getTagKeywordLoc()));
3543 D2Templated->setAccess(DTemplated->getAccess());
3544
3545
3546 // Import the qualifier, if any.
3547 if (DTemplated->getQualifier()) {
3548 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3549 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3550 D2Templated->setQualifierInfo(NNS, NNSRange);
3551 }
3552 D2Templated->setLexicalDeclContext(LexicalDC);
3553
3554 // Create the class template declaration itself.
3555 TemplateParameterList *TemplateParams
3556 = ImportTemplateParameterList(D->getTemplateParameters());
3557 if (!TemplateParams)
3558 return 0;
3559
3560 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3561 Loc, Name, TemplateParams,
3562 D2Templated,
3563 /*PrevDecl=*/0);
3564 D2Templated->setDescribedClassTemplate(D2);
3565
3566 D2->setAccess(D->getAccess());
3567 D2->setLexicalDeclContext(LexicalDC);
3568 LexicalDC->addDecl(D2);
3569
3570 // Note the relationship between the class templates.
3571 Importer.Imported(D, D2);
3572 Importer.Imported(DTemplated, D2Templated);
3573
3574 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3575 // FIXME: Import definition!
3576 }
3577
3578 return D2;
3579}
3580
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003581Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3582 ClassTemplateSpecializationDecl *D) {
3583 // If this record has a definition in the translation unit we're coming from,
3584 // but this particular declaration is not that definition, import the
3585 // definition and map to that.
3586 TagDecl *Definition = D->getDefinition();
3587 if (Definition && Definition != D) {
3588 Decl *ImportedDef = Importer.Import(Definition);
3589 if (!ImportedDef)
3590 return 0;
3591
3592 return Importer.Imported(D, ImportedDef);
3593 }
3594
3595 ClassTemplateDecl *ClassTemplate
3596 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3597 D->getSpecializedTemplate()));
3598 if (!ClassTemplate)
3599 return 0;
3600
3601 // Import the context of this declaration.
3602 DeclContext *DC = ClassTemplate->getDeclContext();
3603 if (!DC)
3604 return 0;
3605
3606 DeclContext *LexicalDC = DC;
3607 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3608 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3609 if (!LexicalDC)
3610 return 0;
3611 }
3612
3613 // Import the location of this declaration.
3614 SourceLocation Loc = Importer.Import(D->getLocation());
3615
3616 // Import template arguments.
3617 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3618 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3619 D->getTemplateArgs().size(),
3620 TemplateArgs))
3621 return 0;
3622
3623 // Try to find an existing specialization with these template arguments.
3624 void *InsertPos = 0;
3625 ClassTemplateSpecializationDecl *D2
3626 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3627 TemplateArgs.size(), InsertPos);
3628 if (D2) {
3629 // We already have a class template specialization with these template
3630 // arguments.
3631
3632 // FIXME: Check for specialization vs. instantiation errors.
3633
3634 if (RecordDecl *FoundDef = D2->getDefinition()) {
3635 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3636 // The record types structurally match, or the "from" translation
3637 // unit only had a forward declaration anyway; call it the same
3638 // function.
3639 return Importer.Imported(D, FoundDef);
3640 }
3641 }
3642 } else {
3643 // Create a new specialization.
3644 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3645 D->getTagKind(), DC,
3646 Loc, ClassTemplate,
3647 TemplateArgs.data(),
3648 TemplateArgs.size(),
3649 /*PrevDecl=*/0);
3650 D2->setSpecializationKind(D->getSpecializationKind());
3651
3652 // Add this specialization to the class template.
3653 ClassTemplate->AddSpecialization(D2, InsertPos);
3654
3655 // Import the qualifier, if any.
3656 if (D->getQualifier()) {
3657 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3658 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3659 D2->setQualifierInfo(NNS, NNSRange);
3660 }
3661
3662
3663 // Add the specialization to this context.
3664 D2->setLexicalDeclContext(LexicalDC);
3665 LexicalDC->addDecl(D2);
3666 }
3667 Importer.Imported(D, D2);
3668
3669 if (D->isDefinition() && ImportDefinition(D, D2))
3670 return 0;
3671
3672 return D2;
3673}
3674
Douglas Gregor4800d952010-02-11 19:21:55 +00003675//----------------------------------------------------------------------------
3676// Import Statements
3677//----------------------------------------------------------------------------
3678
3679Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3680 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3681 << S->getStmtClassName();
3682 return 0;
3683}
3684
3685//----------------------------------------------------------------------------
3686// Import Expressions
3687//----------------------------------------------------------------------------
3688Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3689 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3690 << E->getStmtClassName();
3691 return 0;
3692}
3693
Douglas Gregor44080632010-02-19 01:17:02 +00003694Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3695 NestedNameSpecifier *Qualifier = 0;
3696 if (E->getQualifier()) {
3697 Qualifier = Importer.Import(E->getQualifier());
3698 if (!E->getQualifier())
3699 return 0;
3700 }
3701
3702 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3703 if (!ToD)
3704 return 0;
3705
3706 QualType T = Importer.Import(E->getType());
3707 if (T.isNull())
3708 return 0;
3709
3710 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3711 Importer.Import(E->getQualifierRange()),
3712 ToD,
3713 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003714 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003715 /*FIXME:TemplateArgs=*/0);
3716}
3717
Douglas Gregor4800d952010-02-11 19:21:55 +00003718Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3719 QualType T = Importer.Import(E->getType());
3720 if (T.isNull())
3721 return 0;
3722
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003723 return IntegerLiteral::Create(Importer.getToContext(),
3724 E->getValue(), T,
3725 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003726}
3727
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003728Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3729 QualType T = Importer.Import(E->getType());
3730 if (T.isNull())
3731 return 0;
3732
3733 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3734 E->isWide(), T,
3735 Importer.Import(E->getLocation()));
3736}
3737
Douglas Gregorf638f952010-02-19 01:07:06 +00003738Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3739 Expr *SubExpr = Importer.Import(E->getSubExpr());
3740 if (!SubExpr)
3741 return 0;
3742
3743 return new (Importer.getToContext())
3744 ParenExpr(Importer.Import(E->getLParen()),
3745 Importer.Import(E->getRParen()),
3746 SubExpr);
3747}
3748
3749Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3750 QualType T = Importer.Import(E->getType());
3751 if (T.isNull())
3752 return 0;
3753
3754 Expr *SubExpr = Importer.Import(E->getSubExpr());
3755 if (!SubExpr)
3756 return 0;
3757
3758 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003759 T, E->getValueKind(),
3760 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003761 Importer.Import(E->getOperatorLoc()));
3762}
3763
Douglas Gregorbd249a52010-02-19 01:24:23 +00003764Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3765 QualType ResultType = Importer.Import(E->getType());
3766
3767 if (E->isArgumentType()) {
3768 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3769 if (!TInfo)
3770 return 0;
3771
3772 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3773 TInfo, ResultType,
3774 Importer.Import(E->getOperatorLoc()),
3775 Importer.Import(E->getRParenLoc()));
3776 }
3777
3778 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3779 if (!SubExpr)
3780 return 0;
3781
3782 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3783 SubExpr, ResultType,
3784 Importer.Import(E->getOperatorLoc()),
3785 Importer.Import(E->getRParenLoc()));
3786}
3787
Douglas Gregorf638f952010-02-19 01:07:06 +00003788Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3789 QualType T = Importer.Import(E->getType());
3790 if (T.isNull())
3791 return 0;
3792
3793 Expr *LHS = Importer.Import(E->getLHS());
3794 if (!LHS)
3795 return 0;
3796
3797 Expr *RHS = Importer.Import(E->getRHS());
3798 if (!RHS)
3799 return 0;
3800
3801 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003802 T, E->getValueKind(),
3803 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003804 Importer.Import(E->getOperatorLoc()));
3805}
3806
3807Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3808 QualType T = Importer.Import(E->getType());
3809 if (T.isNull())
3810 return 0;
3811
3812 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3813 if (CompLHSType.isNull())
3814 return 0;
3815
3816 QualType CompResultType = Importer.Import(E->getComputationResultType());
3817 if (CompResultType.isNull())
3818 return 0;
3819
3820 Expr *LHS = Importer.Import(E->getLHS());
3821 if (!LHS)
3822 return 0;
3823
3824 Expr *RHS = Importer.Import(E->getRHS());
3825 if (!RHS)
3826 return 0;
3827
3828 return new (Importer.getToContext())
3829 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003830 T, E->getValueKind(),
3831 E->getObjectKind(),
3832 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003833 Importer.Import(E->getOperatorLoc()));
3834}
3835
John McCallf871d0c2010-08-07 06:22:56 +00003836bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3837 if (E->path_empty()) return false;
3838
3839 // TODO: import cast paths
3840 return true;
3841}
3842
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003843Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3844 QualType T = Importer.Import(E->getType());
3845 if (T.isNull())
3846 return 0;
3847
3848 Expr *SubExpr = Importer.Import(E->getSubExpr());
3849 if (!SubExpr)
3850 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003851
3852 CXXCastPath BasePath;
3853 if (ImportCastPath(E, BasePath))
3854 return 0;
3855
3856 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003857 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003858}
3859
Douglas Gregor008847a2010-02-19 01:32:14 +00003860Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3861 QualType T = Importer.Import(E->getType());
3862 if (T.isNull())
3863 return 0;
3864
3865 Expr *SubExpr = Importer.Import(E->getSubExpr());
3866 if (!SubExpr)
3867 return 0;
3868
3869 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3870 if (!TInfo && E->getTypeInfoAsWritten())
3871 return 0;
3872
John McCallf871d0c2010-08-07 06:22:56 +00003873 CXXCastPath BasePath;
3874 if (ImportCastPath(E, BasePath))
3875 return 0;
3876
John McCallf89e55a2010-11-18 06:31:45 +00003877 return CStyleCastExpr::Create(Importer.getToContext(), T,
3878 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003879 SubExpr, &BasePath, TInfo,
3880 Importer.Import(E->getLParenLoc()),
3881 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003882}
3883
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003884ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner39b49bc2010-11-23 08:35:12 +00003885 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003886 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner39b49bc2010-11-23 08:35:12 +00003887 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor9bed8792010-02-09 19:21:46 +00003888 ImportedDecls[FromContext.getTranslationUnitDecl()]
3889 = ToContext.getTranslationUnitDecl();
3890}
3891
3892ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003893
3894QualType ASTImporter::Import(QualType FromT) {
3895 if (FromT.isNull())
3896 return QualType();
3897
Douglas Gregor169fba52010-02-08 15:18:58 +00003898 // Check whether we've already imported this type.
3899 llvm::DenseMap<Type *, Type *>::iterator Pos
3900 = ImportedTypes.find(FromT.getTypePtr());
3901 if (Pos != ImportedTypes.end())
3902 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003903
Douglas Gregor169fba52010-02-08 15:18:58 +00003904 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003905 ASTNodeImporter Importer(*this);
3906 QualType ToT = Importer.Visit(FromT.getTypePtr());
3907 if (ToT.isNull())
3908 return ToT;
3909
Douglas Gregor169fba52010-02-08 15:18:58 +00003910 // Record the imported type.
3911 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3912
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003913 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3914}
3915
Douglas Gregor9bed8792010-02-09 19:21:46 +00003916TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003917 if (!FromTSI)
3918 return FromTSI;
3919
3920 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003921 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003922 QualType T = Import(FromTSI->getType());
3923 if (T.isNull())
3924 return 0;
3925
3926 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003927 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003928}
3929
3930Decl *ASTImporter::Import(Decl *FromD) {
3931 if (!FromD)
3932 return 0;
3933
3934 // Check whether we've already imported this declaration.
3935 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3936 if (Pos != ImportedDecls.end())
3937 return Pos->second;
3938
3939 // Import the type
3940 ASTNodeImporter Importer(*this);
3941 Decl *ToD = Importer.Visit(FromD);
3942 if (!ToD)
3943 return 0;
3944
3945 // Record the imported declaration.
3946 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00003947
3948 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3949 // Keep track of anonymous tags that have an associated typedef.
3950 if (FromTag->getTypedefForAnonDecl())
3951 AnonTagsWithPendingTypedefs.push_back(FromTag);
3952 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3953 // When we've finished transforming a typedef, see whether it was the
3954 // typedef for an anonymous tag.
3955 for (llvm::SmallVector<TagDecl *, 4>::iterator
3956 FromTag = AnonTagsWithPendingTypedefs.begin(),
3957 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3958 FromTag != FromTagEnd; ++FromTag) {
3959 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3960 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3961 // We found the typedef for an anonymous tag; link them.
3962 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3963 AnonTagsWithPendingTypedefs.erase(FromTag);
3964 break;
3965 }
3966 }
3967 }
3968 }
3969
Douglas Gregor9bed8792010-02-09 19:21:46 +00003970 return ToD;
3971}
3972
3973DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3974 if (!FromDC)
3975 return FromDC;
3976
3977 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3978}
3979
3980Expr *ASTImporter::Import(Expr *FromE) {
3981 if (!FromE)
3982 return 0;
3983
3984 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3985}
3986
3987Stmt *ASTImporter::Import(Stmt *FromS) {
3988 if (!FromS)
3989 return 0;
3990
Douglas Gregor4800d952010-02-11 19:21:55 +00003991 // Check whether we've already imported this declaration.
3992 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3993 if (Pos != ImportedStmts.end())
3994 return Pos->second;
3995
3996 // Import the type
3997 ASTNodeImporter Importer(*this);
3998 Stmt *ToS = Importer.Visit(FromS);
3999 if (!ToS)
4000 return 0;
4001
4002 // Record the imported declaration.
4003 ImportedStmts[FromS] = ToS;
4004 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004005}
4006
4007NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4008 if (!FromNNS)
4009 return 0;
4010
4011 // FIXME: Implement!
4012 return 0;
4013}
4014
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004015TemplateName ASTImporter::Import(TemplateName From) {
4016 switch (From.getKind()) {
4017 case TemplateName::Template:
4018 if (TemplateDecl *ToTemplate
4019 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4020 return TemplateName(ToTemplate);
4021
4022 return TemplateName();
4023
4024 case TemplateName::OverloadedTemplate: {
4025 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4026 UnresolvedSet<2> ToTemplates;
4027 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4028 E = FromStorage->end();
4029 I != E; ++I) {
4030 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4031 ToTemplates.addDecl(To);
4032 else
4033 return TemplateName();
4034 }
4035 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4036 ToTemplates.end());
4037 }
4038
4039 case TemplateName::QualifiedTemplate: {
4040 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4041 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4042 if (!Qualifier)
4043 return TemplateName();
4044
4045 if (TemplateDecl *ToTemplate
4046 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4047 return ToContext.getQualifiedTemplateName(Qualifier,
4048 QTN->hasTemplateKeyword(),
4049 ToTemplate);
4050
4051 return TemplateName();
4052 }
4053
4054 case TemplateName::DependentTemplate: {
4055 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4056 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4057 if (!Qualifier)
4058 return TemplateName();
4059
4060 if (DTN->isIdentifier()) {
4061 return ToContext.getDependentTemplateName(Qualifier,
4062 Import(DTN->getIdentifier()));
4063 }
4064
4065 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4066 }
4067 }
4068
4069 llvm_unreachable("Invalid template name kind");
4070 return TemplateName();
4071}
4072
Douglas Gregor9bed8792010-02-09 19:21:46 +00004073SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4074 if (FromLoc.isInvalid())
4075 return SourceLocation();
4076
Douglas Gregor88523732010-02-10 00:15:17 +00004077 SourceManager &FromSM = FromContext.getSourceManager();
4078
4079 // For now, map everything down to its spelling location, so that we
4080 // don't have to import macro instantiations.
4081 // FIXME: Import macro instantiations!
4082 FromLoc = FromSM.getSpellingLoc(FromLoc);
4083 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4084 SourceManager &ToSM = ToContext.getSourceManager();
4085 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4086 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004087}
4088
4089SourceRange ASTImporter::Import(SourceRange FromRange) {
4090 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4091}
4092
Douglas Gregor88523732010-02-10 00:15:17 +00004093FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004094 llvm::DenseMap<FileID, FileID>::iterator Pos
4095 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004096 if (Pos != ImportedFileIDs.end())
4097 return Pos->second;
4098
4099 SourceManager &FromSM = FromContext.getSourceManager();
4100 SourceManager &ToSM = ToContext.getSourceManager();
4101 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4102 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4103
4104 // Include location of this file.
4105 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4106
4107 // Map the FileID for to the "to" source manager.
4108 FileID ToID;
4109 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
4110 if (Cache->Entry) {
4111 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4112 // disk again
4113 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4114 // than mmap the files several times.
Chris Lattner39b49bc2010-11-23 08:35:12 +00004115 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004116 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4117 FromSLoc.getFile().getFileCharacteristic());
4118 } else {
4119 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004120 const llvm::MemoryBuffer *
4121 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004122 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004123 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004124 FromBuf->getBufferIdentifier());
4125 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4126 }
4127
4128
Sebastian Redl535a3e22010-09-30 01:03:06 +00004129 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004130 return ToID;
4131}
4132
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004133DeclarationName ASTImporter::Import(DeclarationName FromName) {
4134 if (!FromName)
4135 return DeclarationName();
4136
4137 switch (FromName.getNameKind()) {
4138 case DeclarationName::Identifier:
4139 return Import(FromName.getAsIdentifierInfo());
4140
4141 case DeclarationName::ObjCZeroArgSelector:
4142 case DeclarationName::ObjCOneArgSelector:
4143 case DeclarationName::ObjCMultiArgSelector:
4144 return Import(FromName.getObjCSelector());
4145
4146 case DeclarationName::CXXConstructorName: {
4147 QualType T = Import(FromName.getCXXNameType());
4148 if (T.isNull())
4149 return DeclarationName();
4150
4151 return ToContext.DeclarationNames.getCXXConstructorName(
4152 ToContext.getCanonicalType(T));
4153 }
4154
4155 case DeclarationName::CXXDestructorName: {
4156 QualType T = Import(FromName.getCXXNameType());
4157 if (T.isNull())
4158 return DeclarationName();
4159
4160 return ToContext.DeclarationNames.getCXXDestructorName(
4161 ToContext.getCanonicalType(T));
4162 }
4163
4164 case DeclarationName::CXXConversionFunctionName: {
4165 QualType T = Import(FromName.getCXXNameType());
4166 if (T.isNull())
4167 return DeclarationName();
4168
4169 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4170 ToContext.getCanonicalType(T));
4171 }
4172
4173 case DeclarationName::CXXOperatorName:
4174 return ToContext.DeclarationNames.getCXXOperatorName(
4175 FromName.getCXXOverloadedOperator());
4176
4177 case DeclarationName::CXXLiteralOperatorName:
4178 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4179 Import(FromName.getCXXLiteralIdentifier()));
4180
4181 case DeclarationName::CXXUsingDirective:
4182 // FIXME: STATICS!
4183 return DeclarationName::getUsingDirectiveName();
4184 }
4185
4186 // Silence bogus GCC warning
4187 return DeclarationName();
4188}
4189
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004190IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004191 if (!FromId)
4192 return 0;
4193
4194 return &ToContext.Idents.get(FromId->getName());
4195}
Douglas Gregor089459a2010-02-08 21:09:39 +00004196
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004197Selector ASTImporter::Import(Selector FromSel) {
4198 if (FromSel.isNull())
4199 return Selector();
4200
4201 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4202 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4203 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4204 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4205 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4206}
4207
Douglas Gregor089459a2010-02-08 21:09:39 +00004208DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4209 DeclContext *DC,
4210 unsigned IDNS,
4211 NamedDecl **Decls,
4212 unsigned NumDecls) {
4213 return Name;
4214}
4215
4216DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004217 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004218}
4219
4220DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004221 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004222}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004223
4224Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4225 ImportedDecls[From] = To;
4226 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004227}
Douglas Gregorea35d112010-02-15 23:54:17 +00004228
4229bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4230 llvm::DenseMap<Type *, Type *>::iterator Pos
4231 = ImportedTypes.find(From.getTypePtr());
4232 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4233 return true;
4234
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004235 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004236 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004237}