blob: 2f7497db66c85db41b9d0cc8fa1ff458c2af997f [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
John McCallf4c73712011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000073 // FIXME: TemplateTypeParmType
74 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000075 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000077 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000078 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000079 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80 QualType VisitObjCObjectType(const ObjCObjectType *T);
81 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000082
83 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000084 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000086 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000087 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000088 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
89 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000090 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000091 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
92 bool ForceImport = false);
93 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
94 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000095 TemplateParameterList *ImportTemplateParameterList(
96 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000097 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
98 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
99 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000102 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000103 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000104 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000105 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000106 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000107 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000108 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000109 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000110 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000111 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000112 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000113 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
114 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
115 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
116 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000117 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000118 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000119 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000120 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000121 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000122 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000123 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000124 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000125 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000126 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000127 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000128 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000129 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000130 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000131 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000132 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000133 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
134 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
135 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
136 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000137 Decl *VisitClassTemplateSpecializationDecl(
138 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000139
Douglas Gregor4800d952010-02-11 19:21:55 +0000140 // Importing statements
141 Stmt *VisitStmt(Stmt *S);
142
143 // Importing expressions
144 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000145 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000146 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000147 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitParenExpr(ParenExpr *E);
149 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000150 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000151 Expr *VisitBinaryOperator(BinaryOperator *E);
152 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000153 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000154 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155 };
156}
157
158//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000159// Structural Equivalence
160//----------------------------------------------------------------------------
161
162namespace {
163 struct StructuralEquivalenceContext {
164 /// \brief AST contexts for which we are checking structural equivalence.
165 ASTContext &C1, &C2;
166
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000167 /// \brief The set of "tentative" equivalences between two canonical
168 /// declarations, mapping from a declaration in the first context to the
169 /// declaration in the second context that we believe to be equivalent.
170 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
171
172 /// \brief Queue of declarations in the first context whose equivalence
173 /// with a declaration in the second context still needs to be verified.
174 std::deque<Decl *> DeclsToCheck;
175
Douglas Gregorea35d112010-02-15 23:54:17 +0000176 /// \brief Declaration (from, to) pairs that are known not to be equivalent
177 /// (which we have already complained about).
178 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
179
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180 /// \brief Whether we're being strict about the spelling of types when
181 /// unifying two types.
182 bool StrictTypeSpelling;
183
184 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000185 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000186 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000187 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000188 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000189
190 /// \brief Determine whether the two declarations are structurally
191 /// equivalent.
192 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
193
194 /// \brief Determine whether the two types are structurally equivalent.
195 bool IsStructurallyEquivalent(QualType T1, QualType T2);
196
197 private:
198 /// \brief Finish checking all of the structural equivalences.
199 ///
200 /// \returns true if an error occurred, false otherwise.
201 bool Finish();
202
203 public:
204 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000205 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000206 }
207
208 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000209 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000210 }
211 };
212}
213
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 QualType T1, QualType T2);
216static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
217 Decl *D1, Decl *D2);
218
219/// \brief Determine if two APInts have the same value, after zero-extending
220/// one of them (if needed!) to ensure that the bit-widths match.
221static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
222 if (I1.getBitWidth() == I2.getBitWidth())
223 return I1 == I2;
224
225 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227
Jay Foad9f71a8f2010-12-07 08:25:34 +0000228 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000229}
230
231/// \brief Determine if two APSInts have the same value, zero- or sign-extending
232/// as needed.
233static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
234 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
235 return I1 == I2;
236
237 // Check for a bit-width mismatch.
238 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000241 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000242
243 // We have a signedness mismatch. Turn the signed value into an unsigned
244 // value.
245 if (I1.isSigned()) {
246 if (I1.isNegative())
247 return false;
248
249 return llvm::APSInt(I1, true) == I2;
250 }
251
252 if (I2.isNegative())
253 return false;
254
255 return I1 == llvm::APSInt(I2, true);
256}
257
258/// \brief Determine structural equivalence of two expressions.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 Expr *E1, Expr *E2) {
261 if (!E1 || !E2)
262 return E1 == E2;
263
264 // FIXME: Actually perform a structural comparison!
265 return true;
266}
267
268/// \brief Determine whether two identifiers are equivalent.
269static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
270 const IdentifierInfo *Name2) {
271 if (!Name1 || !Name2)
272 return Name1 == Name2;
273
274 return Name1->getName() == Name2->getName();
275}
276
277/// \brief Determine whether two nested-name-specifiers are equivalent.
278static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
279 NestedNameSpecifier *NNS1,
280 NestedNameSpecifier *NNS2) {
281 // FIXME: Implement!
282 return true;
283}
284
285/// \brief Determine whether two template arguments are equivalent.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 const TemplateArgument &Arg1,
288 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000289 if (Arg1.getKind() != Arg2.getKind())
290 return false;
291
292 switch (Arg1.getKind()) {
293 case TemplateArgument::Null:
294 return true;
295
296 case TemplateArgument::Type:
297 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
298
299 case TemplateArgument::Integral:
300 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
301 Arg2.getIntegralType()))
302 return false;
303
304 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
305
306 case TemplateArgument::Declaration:
307 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
308
309 case TemplateArgument::Template:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplate(),
312 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000313
314 case TemplateArgument::TemplateExpansion:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsTemplateOrTemplatePattern(),
317 Arg2.getAsTemplateOrTemplatePattern());
318
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000319 case TemplateArgument::Expression:
320 return IsStructurallyEquivalent(Context,
321 Arg1.getAsExpr(), Arg2.getAsExpr());
322
323 case TemplateArgument::Pack:
324 if (Arg1.pack_size() != Arg2.pack_size())
325 return false;
326
327 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
328 if (!IsStructurallyEquivalent(Context,
329 Arg1.pack_begin()[I],
330 Arg2.pack_begin()[I]))
331 return false;
332
333 return true;
334 }
335
336 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000337 return true;
338}
339
340/// \brief Determine structural equivalence for the common part of array
341/// types.
342static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
343 const ArrayType *Array1,
344 const ArrayType *Array2) {
345 if (!IsStructurallyEquivalent(Context,
346 Array1->getElementType(),
347 Array2->getElementType()))
348 return false;
349 if (Array1->getSizeModifier() != Array2->getSizeModifier())
350 return false;
351 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
352 return false;
353
354 return true;
355}
356
357/// \brief Determine structural equivalence of two types.
358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
359 QualType T1, QualType T2) {
360 if (T1.isNull() || T2.isNull())
361 return T1.isNull() && T2.isNull();
362
363 if (!Context.StrictTypeSpelling) {
364 // We aren't being strict about token-to-token equivalence of types,
365 // so map down to the canonical type.
366 T1 = Context.C1.getCanonicalType(T1);
367 T2 = Context.C2.getCanonicalType(T2);
368 }
369
370 if (T1.getQualifiers() != T2.getQualifiers())
371 return false;
372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000374
Douglas Gregorea35d112010-02-15 23:54:17 +0000375 if (T1->getTypeClass() != T2->getTypeClass()) {
376 // Compare function types with prototypes vs. without prototypes as if
377 // both did not have prototypes.
378 if (T1->getTypeClass() == Type::FunctionProto &&
379 T2->getTypeClass() == Type::FunctionNoProto)
380 TC = Type::FunctionNoProto;
381 else if (T1->getTypeClass() == Type::FunctionNoProto &&
382 T2->getTypeClass() == Type::FunctionProto)
383 TC = Type::FunctionNoProto;
384 else
385 return false;
386 }
387
388 switch (TC) {
389 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000390 // FIXME: Deal with Char_S/Char_U.
391 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
392 return false;
393 break;
394
395 case Type::Complex:
396 if (!IsStructurallyEquivalent(Context,
397 cast<ComplexType>(T1)->getElementType(),
398 cast<ComplexType>(T2)->getElementType()))
399 return false;
400 break;
401
402 case Type::Pointer:
403 if (!IsStructurallyEquivalent(Context,
404 cast<PointerType>(T1)->getPointeeType(),
405 cast<PointerType>(T2)->getPointeeType()))
406 return false;
407 break;
408
409 case Type::BlockPointer:
410 if (!IsStructurallyEquivalent(Context,
411 cast<BlockPointerType>(T1)->getPointeeType(),
412 cast<BlockPointerType>(T2)->getPointeeType()))
413 return false;
414 break;
415
416 case Type::LValueReference:
417 case Type::RValueReference: {
418 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
419 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
420 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
421 return false;
422 if (Ref1->isInnerRef() != Ref2->isInnerRef())
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 Ref1->getPointeeTypeAsWritten(),
426 Ref2->getPointeeTypeAsWritten()))
427 return false;
428 break;
429 }
430
431 case Type::MemberPointer: {
432 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
433 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
434 if (!IsStructurallyEquivalent(Context,
435 MemPtr1->getPointeeType(),
436 MemPtr2->getPointeeType()))
437 return false;
438 if (!IsStructurallyEquivalent(Context,
439 QualType(MemPtr1->getClass(), 0),
440 QualType(MemPtr2->getClass(), 0)))
441 return false;
442 break;
443 }
444
445 case Type::ConstantArray: {
446 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
447 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
448 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
449 return false;
450
451 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
452 return false;
453 break;
454 }
455
456 case Type::IncompleteArray:
457 if (!IsArrayStructurallyEquivalent(Context,
458 cast<ArrayType>(T1),
459 cast<ArrayType>(T2)))
460 return false;
461 break;
462
463 case Type::VariableArray: {
464 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
465 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
466 if (!IsStructurallyEquivalent(Context,
467 Array1->getSizeExpr(), Array2->getSizeExpr()))
468 return false;
469
470 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
471 return false;
472
473 break;
474 }
475
476 case Type::DependentSizedArray: {
477 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
478 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Array1->getSizeExpr(), Array2->getSizeExpr()))
481 return false;
482
483 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
484 return false;
485
486 break;
487 }
488
489 case Type::DependentSizedExtVector: {
490 const DependentSizedExtVectorType *Vec1
491 = cast<DependentSizedExtVectorType>(T1);
492 const DependentSizedExtVectorType *Vec2
493 = cast<DependentSizedExtVectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
496 return false;
497 if (!IsStructurallyEquivalent(Context,
498 Vec1->getElementType(),
499 Vec2->getElementType()))
500 return false;
501 break;
502 }
503
504 case Type::Vector:
505 case Type::ExtVector: {
506 const VectorType *Vec1 = cast<VectorType>(T1);
507 const VectorType *Vec2 = cast<VectorType>(T2);
508 if (!IsStructurallyEquivalent(Context,
509 Vec1->getElementType(),
510 Vec2->getElementType()))
511 return false;
512 if (Vec1->getNumElements() != Vec2->getNumElements())
513 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000514 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000516 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000517 }
518
519 case Type::FunctionProto: {
520 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
521 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
522 if (Proto1->getNumArgs() != Proto2->getNumArgs())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getArgType(I),
527 Proto2->getArgType(I)))
528 return false;
529 }
530 if (Proto1->isVariadic() != Proto2->isVariadic())
531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000533 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000534 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
535 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
536 return false;
537 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
538 if (!IsStructurallyEquivalent(Context,
539 Proto1->getExceptionType(I),
540 Proto2->getExceptionType(I)))
541 return false;
542 }
543 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000544 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000545 Proto1->getNoexceptExpr(),
546 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000547 return false;
548 }
549 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
550 return false;
551
552 // Fall through to check the bits common with FunctionNoProtoType.
553 }
554
555 case Type::FunctionNoProto: {
556 const FunctionType *Function1 = cast<FunctionType>(T1);
557 const FunctionType *Function2 = cast<FunctionType>(T2);
558 if (!IsStructurallyEquivalent(Context,
559 Function1->getResultType(),
560 Function2->getResultType()))
561 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000562 if (Function1->getExtInfo() != Function2->getExtInfo())
563 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 break;
565 }
566
567 case Type::UnresolvedUsing:
568 if (!IsStructurallyEquivalent(Context,
569 cast<UnresolvedUsingType>(T1)->getDecl(),
570 cast<UnresolvedUsingType>(T2)->getDecl()))
571 return false;
572
573 break;
John McCall9d156a72011-01-06 01:58:22 +0000574
575 case Type::Attributed:
576 if (!IsStructurallyEquivalent(Context,
577 cast<AttributedType>(T1)->getModifiedType(),
578 cast<AttributedType>(T2)->getModifiedType()))
579 return false;
580 if (!IsStructurallyEquivalent(Context,
581 cast<AttributedType>(T1)->getEquivalentType(),
582 cast<AttributedType>(T2)->getEquivalentType()))
583 return false;
584 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000585
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000586 case Type::Paren:
587 if (!IsStructurallyEquivalent(Context,
588 cast<ParenType>(T1)->getInnerType(),
589 cast<ParenType>(T2)->getInnerType()))
590 return false;
591 break;
592
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000593 case Type::Typedef:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypedefType>(T1)->getDecl(),
596 cast<TypedefType>(T2)->getDecl()))
597 return false;
598 break;
599
600 case Type::TypeOfExpr:
601 if (!IsStructurallyEquivalent(Context,
602 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
603 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::TypeOf:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypeOfType>(T1)->getUnderlyingType(),
610 cast<TypeOfType>(T2)->getUnderlyingType()))
611 return false;
612 break;
Sean Huntca63c202011-05-24 22:41:36 +0000613
614 case Type::UnaryTransform:
615 if (!IsStructurallyEquivalent(Context,
616 cast<UnaryTransformType>(T1)->getUnderlyingType(),
617 cast<UnaryTransformType>(T1)->getUnderlyingType()))
618 return false;
619 break;
620
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000621 case Type::Decltype:
622 if (!IsStructurallyEquivalent(Context,
623 cast<DecltypeType>(T1)->getUnderlyingExpr(),
624 cast<DecltypeType>(T2)->getUnderlyingExpr()))
625 return false;
626 break;
627
Richard Smith34b41d92011-02-20 03:19:35 +0000628 case Type::Auto:
629 if (!IsStructurallyEquivalent(Context,
630 cast<AutoType>(T1)->getDeducedType(),
631 cast<AutoType>(T2)->getDeducedType()))
632 return false;
633 break;
634
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000635 case Type::Record:
636 case Type::Enum:
637 if (!IsStructurallyEquivalent(Context,
638 cast<TagType>(T1)->getDecl(),
639 cast<TagType>(T2)->getDecl()))
640 return false;
641 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000642
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000643 case Type::TemplateTypeParm: {
644 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
645 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
646 if (Parm1->getDepth() != Parm2->getDepth())
647 return false;
648 if (Parm1->getIndex() != Parm2->getIndex())
649 return false;
650 if (Parm1->isParameterPack() != Parm2->isParameterPack())
651 return false;
652
653 // Names of template type parameters are never significant.
654 break;
655 }
656
657 case Type::SubstTemplateTypeParm: {
658 const SubstTemplateTypeParmType *Subst1
659 = cast<SubstTemplateTypeParmType>(T1);
660 const SubstTemplateTypeParmType *Subst2
661 = cast<SubstTemplateTypeParmType>(T2);
662 if (!IsStructurallyEquivalent(Context,
663 QualType(Subst1->getReplacedParameter(), 0),
664 QualType(Subst2->getReplacedParameter(), 0)))
665 return false;
666 if (!IsStructurallyEquivalent(Context,
667 Subst1->getReplacementType(),
668 Subst2->getReplacementType()))
669 return false;
670 break;
671 }
672
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000673 case Type::SubstTemplateTypeParmPack: {
674 const SubstTemplateTypeParmPackType *Subst1
675 = cast<SubstTemplateTypeParmPackType>(T1);
676 const SubstTemplateTypeParmPackType *Subst2
677 = cast<SubstTemplateTypeParmPackType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 QualType(Subst1->getReplacedParameter(), 0),
680 QualType(Subst2->getReplacedParameter(), 0)))
681 return false;
682 if (!IsStructurallyEquivalent(Context,
683 Subst1->getArgumentPack(),
684 Subst2->getArgumentPack()))
685 return false;
686 break;
687 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000688 case Type::TemplateSpecialization: {
689 const TemplateSpecializationType *Spec1
690 = cast<TemplateSpecializationType>(T1);
691 const TemplateSpecializationType *Spec2
692 = cast<TemplateSpecializationType>(T2);
693 if (!IsStructurallyEquivalent(Context,
694 Spec1->getTemplateName(),
695 Spec2->getTemplateName()))
696 return false;
697 if (Spec1->getNumArgs() != Spec2->getNumArgs())
698 return false;
699 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
700 if (!IsStructurallyEquivalent(Context,
701 Spec1->getArg(I), Spec2->getArg(I)))
702 return false;
703 }
704 break;
705 }
706
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000707 case Type::Elaborated: {
708 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
709 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
710 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
711 if (Elab1->getKeyword() != Elab2->getKeyword())
712 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000713 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000714 Elab1->getQualifier(),
715 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000716 return false;
717 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000718 Elab1->getNamedType(),
719 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000720 return false;
721 break;
722 }
723
John McCall3cb0ebd2010-03-10 03:28:59 +0000724 case Type::InjectedClassName: {
725 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
726 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
727 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000728 Inj1->getInjectedSpecializationType(),
729 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000730 return false;
731 break;
732 }
733
Douglas Gregor4714c122010-03-31 17:34:00 +0000734 case Type::DependentName: {
735 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
736 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000737 if (!IsStructurallyEquivalent(Context,
738 Typename1->getQualifier(),
739 Typename2->getQualifier()))
740 return false;
741 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
742 Typename2->getIdentifier()))
743 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000744
745 break;
746 }
747
John McCall33500952010-06-11 00:33:02 +0000748 case Type::DependentTemplateSpecialization: {
749 const DependentTemplateSpecializationType *Spec1 =
750 cast<DependentTemplateSpecializationType>(T1);
751 const DependentTemplateSpecializationType *Spec2 =
752 cast<DependentTemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getQualifier(),
755 Spec2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
758 Spec2->getIdentifier()))
759 return false;
760 if (Spec1->getNumArgs() != Spec2->getNumArgs())
761 return false;
762 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
763 if (!IsStructurallyEquivalent(Context,
764 Spec1->getArg(I), Spec2->getArg(I)))
765 return false;
766 }
767 break;
768 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000769
770 case Type::PackExpansion:
771 if (!IsStructurallyEquivalent(Context,
772 cast<PackExpansionType>(T1)->getPattern(),
773 cast<PackExpansionType>(T2)->getPattern()))
774 return false;
775 break;
776
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000777 case Type::ObjCInterface: {
778 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
779 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Iface1->getDecl(), Iface2->getDecl()))
782 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000783 break;
784 }
785
786 case Type::ObjCObject: {
787 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
788 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
789 if (!IsStructurallyEquivalent(Context,
790 Obj1->getBaseType(),
791 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000792 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000793 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
794 return false;
795 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000797 Obj1->getProtocol(I),
798 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000799 return false;
800 }
801 break;
802 }
803
804 case Type::ObjCObjectPointer: {
805 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
806 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Ptr1->getPointeeType(),
809 Ptr2->getPointeeType()))
810 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000811 break;
812 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000813
814 case Type::Atomic: {
815 if (!IsStructurallyEquivalent(Context,
816 cast<AtomicType>(T1)->getValueType(),
817 cast<AtomicType>(T2)->getValueType()))
818 return false;
819 break;
820 }
821
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000822 } // end switch
823
824 return true;
825}
826
827/// \brief Determine structural equivalence of two records.
828static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
829 RecordDecl *D1, RecordDecl *D2) {
830 if (D1->isUnion() != D2->isUnion()) {
831 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
832 << Context.C2.getTypeDeclType(D2);
833 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
834 << D1->getDeclName() << (unsigned)D1->getTagKind();
835 return false;
836 }
837
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000838 // If both declarations are class template specializations, we know
839 // the ODR applies, so check the template and template arguments.
840 ClassTemplateSpecializationDecl *Spec1
841 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
842 ClassTemplateSpecializationDecl *Spec2
843 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
844 if (Spec1 && Spec2) {
845 // Check that the specialized templates are the same.
846 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
847 Spec2->getSpecializedTemplate()))
848 return false;
849
850 // Check that the template arguments are the same.
851 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
852 return false;
853
854 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
855 if (!IsStructurallyEquivalent(Context,
856 Spec1->getTemplateArgs().get(I),
857 Spec2->getTemplateArgs().get(I)))
858 return false;
859 }
860 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000861 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000862 else if (Spec1 || Spec2)
863 return false;
864
Douglas Gregorea35d112010-02-15 23:54:17 +0000865 // Compare the definitions of these two records. If either or both are
866 // incomplete, we assume that they are equivalent.
867 D1 = D1->getDefinition();
868 D2 = D2->getDefinition();
869 if (!D1 || !D2)
870 return true;
871
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000872 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
873 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
874 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000876 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000877 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000878 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000879 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000880 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000881 return false;
882 }
883
884 // Check the base classes.
885 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
886 BaseEnd1 = D1CXX->bases_end(),
887 Base2 = D2CXX->bases_begin();
888 Base1 != BaseEnd1;
889 ++Base1, ++Base2) {
890 if (!IsStructurallyEquivalent(Context,
891 Base1->getType(), Base2->getType())) {
892 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
893 << Context.C2.getTypeDeclType(D2);
894 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
895 << Base2->getType()
896 << Base2->getSourceRange();
897 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
898 << Base1->getType()
899 << Base1->getSourceRange();
900 return false;
901 }
902
903 // Check virtual vs. non-virtual inheritance mismatch.
904 if (Base1->isVirtual() != Base2->isVirtual()) {
905 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
906 << Context.C2.getTypeDeclType(D2);
907 Context.Diag2(Base2->getSourceRange().getBegin(),
908 diag::note_odr_virtual_base)
909 << Base2->isVirtual() << Base2->getSourceRange();
910 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
911 << Base1->isVirtual()
912 << Base1->getSourceRange();
913 return false;
914 }
915 }
916 } else if (D1CXX->getNumBases() > 0) {
917 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
918 << Context.C2.getTypeDeclType(D2);
919 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
920 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
921 << Base1->getType()
922 << Base1->getSourceRange();
923 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
924 return false;
925 }
926 }
927
928 // Check the fields for consistency.
929 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
930 Field2End = D2->field_end();
931 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
932 Field1End = D1->field_end();
933 Field1 != Field1End;
934 ++Field1, ++Field2) {
935 if (Field2 == Field2End) {
936 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
937 << Context.C2.getTypeDeclType(D2);
938 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
939 << Field1->getDeclName() << Field1->getType();
940 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
941 return false;
942 }
943
944 if (!IsStructurallyEquivalent(Context,
945 Field1->getType(), Field2->getType())) {
946 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
947 << Context.C2.getTypeDeclType(D2);
948 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
949 << Field2->getDeclName() << Field2->getType();
950 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
951 << Field1->getDeclName() << Field1->getType();
952 return false;
953 }
954
955 if (Field1->isBitField() != Field2->isBitField()) {
956 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
957 << Context.C2.getTypeDeclType(D2);
958 if (Field1->isBitField()) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000959 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
960 << Field1->getDeclName() << Field1->getType()
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000961 << Field1->getBitWidthValue(Context.C1);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000962 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
963 << Field2->getDeclName();
964 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000965 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
966 << Field2->getDeclName() << Field2->getType()
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000967 << Field2->getBitWidthValue(Context.C2);
968 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
969 << Field1->getDeclName();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000970 }
971 return false;
972 }
973
974 if (Field1->isBitField()) {
975 // Make sure that the bit-fields are the same length.
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000976 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
977 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000978
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000979 if (Bits1 != Bits2) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000980 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
981 << Context.C2.getTypeDeclType(D2);
982 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000983 << Field2->getDeclName() << Field2->getType() << Bits2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000984 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000985 << Field1->getDeclName() << Field1->getType() << Bits1;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000986 return false;
987 }
988 }
989 }
990
991 if (Field2 != Field2End) {
992 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
993 << Context.C2.getTypeDeclType(D2);
994 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
995 << Field2->getDeclName() << Field2->getType();
996 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
997 return false;
998 }
999
1000 return true;
1001}
1002
1003/// \brief Determine structural equivalence of two enums.
1004static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1005 EnumDecl *D1, EnumDecl *D2) {
1006 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1007 EC2End = D2->enumerator_end();
1008 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1009 EC1End = D1->enumerator_end();
1010 EC1 != EC1End; ++EC1, ++EC2) {
1011 if (EC2 == EC2End) {
1012 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1013 << Context.C2.getTypeDeclType(D2);
1014 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1015 << EC1->getDeclName()
1016 << EC1->getInitVal().toString(10);
1017 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1018 return false;
1019 }
1020
1021 llvm::APSInt Val1 = EC1->getInitVal();
1022 llvm::APSInt Val2 = EC2->getInitVal();
1023 if (!IsSameValue(Val1, Val2) ||
1024 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1025 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1026 << Context.C2.getTypeDeclType(D2);
1027 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1028 << EC2->getDeclName()
1029 << EC2->getInitVal().toString(10);
1030 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1031 << EC1->getDeclName()
1032 << EC1->getInitVal().toString(10);
1033 return false;
1034 }
1035 }
1036
1037 if (EC2 != EC2End) {
1038 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1039 << Context.C2.getTypeDeclType(D2);
1040 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1041 << EC2->getDeclName()
1042 << EC2->getInitVal().toString(10);
1043 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1044 return false;
1045 }
1046
1047 return true;
1048}
Douglas Gregor040afae2010-11-30 19:14:50 +00001049
1050static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1051 TemplateParameterList *Params1,
1052 TemplateParameterList *Params2) {
1053 if (Params1->size() != Params2->size()) {
1054 Context.Diag2(Params2->getTemplateLoc(),
1055 diag::err_odr_different_num_template_parameters)
1056 << Params1->size() << Params2->size();
1057 Context.Diag1(Params1->getTemplateLoc(),
1058 diag::note_odr_template_parameter_list);
1059 return false;
1060 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001061
Douglas Gregor040afae2010-11-30 19:14:50 +00001062 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1063 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1064 Context.Diag2(Params2->getParam(I)->getLocation(),
1065 diag::err_odr_different_template_parameter_kind);
1066 Context.Diag1(Params1->getParam(I)->getLocation(),
1067 diag::note_odr_template_parameter_here);
1068 return false;
1069 }
1070
1071 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1072 Params2->getParam(I))) {
1073
1074 return false;
1075 }
1076 }
1077
1078 return true;
1079}
1080
1081static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1082 TemplateTypeParmDecl *D1,
1083 TemplateTypeParmDecl *D2) {
1084 if (D1->isParameterPack() != D2->isParameterPack()) {
1085 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1086 << D2->isParameterPack();
1087 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1088 << D1->isParameterPack();
1089 return false;
1090 }
1091
1092 return true;
1093}
1094
1095static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1096 NonTypeTemplateParmDecl *D1,
1097 NonTypeTemplateParmDecl *D2) {
1098 // FIXME: Enable once we have variadic templates.
1099#if 0
1100 if (D1->isParameterPack() != D2->isParameterPack()) {
1101 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1102 << D2->isParameterPack();
1103 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1104 << D1->isParameterPack();
1105 return false;
1106 }
1107#endif
1108
1109 // Check types.
1110 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1111 Context.Diag2(D2->getLocation(),
1112 diag::err_odr_non_type_parameter_type_inconsistent)
1113 << D2->getType() << D1->getType();
1114 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1115 << D1->getType();
1116 return false;
1117 }
1118
1119 return true;
1120}
1121
1122static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1123 TemplateTemplateParmDecl *D1,
1124 TemplateTemplateParmDecl *D2) {
1125 // FIXME: Enable once we have variadic templates.
1126#if 0
1127 if (D1->isParameterPack() != D2->isParameterPack()) {
1128 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1129 << D2->isParameterPack();
1130 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1131 << D1->isParameterPack();
1132 return false;
1133 }
1134#endif
1135
1136 // Check template parameter lists.
1137 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1138 D2->getTemplateParameters());
1139}
1140
1141static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1142 ClassTemplateDecl *D1,
1143 ClassTemplateDecl *D2) {
1144 // Check template parameters.
1145 if (!IsStructurallyEquivalent(Context,
1146 D1->getTemplateParameters(),
1147 D2->getTemplateParameters()))
1148 return false;
1149
1150 // Check the templated declaration.
1151 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1152 D2->getTemplatedDecl());
1153}
1154
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001155/// \brief Determine structural equivalence of two declarations.
1156static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1157 Decl *D1, Decl *D2) {
1158 // FIXME: Check for known structural equivalences via a callback of some sort.
1159
Douglas Gregorea35d112010-02-15 23:54:17 +00001160 // Check whether we already know that these two declarations are not
1161 // structurally equivalent.
1162 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1163 D2->getCanonicalDecl())))
1164 return false;
1165
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001166 // Determine whether we've already produced a tentative equivalence for D1.
1167 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1168 if (EquivToD1)
1169 return EquivToD1 == D2->getCanonicalDecl();
1170
1171 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1172 EquivToD1 = D2->getCanonicalDecl();
1173 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1174 return true;
1175}
1176
1177bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1178 Decl *D2) {
1179 if (!::IsStructurallyEquivalent(*this, D1, D2))
1180 return false;
1181
1182 return !Finish();
1183}
1184
1185bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1186 QualType T2) {
1187 if (!::IsStructurallyEquivalent(*this, T1, T2))
1188 return false;
1189
1190 return !Finish();
1191}
1192
1193bool StructuralEquivalenceContext::Finish() {
1194 while (!DeclsToCheck.empty()) {
1195 // Check the next declaration.
1196 Decl *D1 = DeclsToCheck.front();
1197 DeclsToCheck.pop_front();
1198
1199 Decl *D2 = TentativeEquivalences[D1];
1200 assert(D2 && "Unrecorded tentative equivalence?");
1201
Douglas Gregorea35d112010-02-15 23:54:17 +00001202 bool Equivalent = true;
1203
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001204 // FIXME: Switch on all declaration kinds. For now, we're just going to
1205 // check the obvious ones.
1206 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1207 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1208 // Check for equivalent structure names.
1209 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001210 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1211 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001212 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001213 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1214 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001215 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1216 !::IsStructurallyEquivalent(*this, Record1, Record2))
1217 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001218 } else {
1219 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001220 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001221 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001222 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001223 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1224 // Check for equivalent enum names.
1225 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001226 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1227 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001228 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001229 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1230 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001231 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1232 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1233 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001234 } else {
1235 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001236 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001237 }
Richard Smith162e1c12011-04-15 14:24:37 +00001238 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1239 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001240 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001241 Typedef2->getIdentifier()) ||
1242 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001243 Typedef1->getUnderlyingType(),
1244 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001245 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001246 } else {
1247 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001248 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001249 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001250 } else if (ClassTemplateDecl *ClassTemplate1
1251 = dyn_cast<ClassTemplateDecl>(D1)) {
1252 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1253 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1254 ClassTemplate2->getIdentifier()) ||
1255 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1256 Equivalent = false;
1257 } else {
1258 // Class template/non-class-template mismatch.
1259 Equivalent = false;
1260 }
1261 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1262 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1263 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1264 Equivalent = false;
1265 } else {
1266 // Kind mismatch.
1267 Equivalent = false;
1268 }
1269 } else if (NonTypeTemplateParmDecl *NTTP1
1270 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1271 if (NonTypeTemplateParmDecl *NTTP2
1272 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1273 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1274 Equivalent = false;
1275 } else {
1276 // Kind mismatch.
1277 Equivalent = false;
1278 }
1279 } else if (TemplateTemplateParmDecl *TTP1
1280 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1281 if (TemplateTemplateParmDecl *TTP2
1282 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1283 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1284 Equivalent = false;
1285 } else {
1286 // Kind mismatch.
1287 Equivalent = false;
1288 }
1289 }
1290
Douglas Gregorea35d112010-02-15 23:54:17 +00001291 if (!Equivalent) {
1292 // Note that these two declarations are not equivalent (and we already
1293 // know about it).
1294 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1295 D2->getCanonicalDecl()));
1296 return true;
1297 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001298 // FIXME: Check other declaration kinds!
1299 }
1300
1301 return false;
1302}
1303
1304//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001305// Import Types
1306//----------------------------------------------------------------------------
1307
John McCallf4c73712011-01-19 06:33:43 +00001308QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001309 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1310 << T->getTypeClassName();
1311 return QualType();
1312}
1313
John McCallf4c73712011-01-19 06:33:43 +00001314QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001315 switch (T->getKind()) {
1316 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1317 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1318
1319 case BuiltinType::Char_U:
1320 // The context we're importing from has an unsigned 'char'. If we're
1321 // importing into a context with a signed 'char', translate to
1322 // 'unsigned char' instead.
1323 if (Importer.getToContext().getLangOptions().CharIsSigned)
1324 return Importer.getToContext().UnsignedCharTy;
1325
1326 return Importer.getToContext().CharTy;
1327
1328 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1329
1330 case BuiltinType::Char16:
1331 // FIXME: Make sure that the "to" context supports C++!
1332 return Importer.getToContext().Char16Ty;
1333
1334 case BuiltinType::Char32:
1335 // FIXME: Make sure that the "to" context supports C++!
1336 return Importer.getToContext().Char32Ty;
1337
1338 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1339 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1340 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1341 case BuiltinType::ULongLong:
1342 return Importer.getToContext().UnsignedLongLongTy;
1343 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1344
1345 case BuiltinType::Char_S:
1346 // The context we're importing from has an unsigned 'char'. If we're
1347 // importing into a context with a signed 'char', translate to
1348 // 'unsigned char' instead.
1349 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1350 return Importer.getToContext().SignedCharTy;
1351
1352 return Importer.getToContext().CharTy;
1353
1354 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001355 case BuiltinType::WChar_S:
1356 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001357 // FIXME: If not in C++, shall we translate to the C equivalent of
1358 // wchar_t?
1359 return Importer.getToContext().WCharTy;
1360
1361 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1362 case BuiltinType::Int : return Importer.getToContext().IntTy;
1363 case BuiltinType::Long : return Importer.getToContext().LongTy;
1364 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1365 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1366 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1367 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1368 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1369
1370 case BuiltinType::NullPtr:
1371 // FIXME: Make sure that the "to" context supports C++0x!
1372 return Importer.getToContext().NullPtrTy;
1373
1374 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1375 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001376 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001377 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001378
1379 case BuiltinType::ObjCId:
1380 // FIXME: Make sure that the "to" context supports Objective-C!
1381 return Importer.getToContext().ObjCBuiltinIdTy;
1382
1383 case BuiltinType::ObjCClass:
1384 return Importer.getToContext().ObjCBuiltinClassTy;
1385
1386 case BuiltinType::ObjCSel:
1387 return Importer.getToContext().ObjCBuiltinSelTy;
1388 }
1389
1390 return QualType();
1391}
1392
John McCallf4c73712011-01-19 06:33:43 +00001393QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001394 QualType ToElementType = Importer.Import(T->getElementType());
1395 if (ToElementType.isNull())
1396 return QualType();
1397
1398 return Importer.getToContext().getComplexType(ToElementType);
1399}
1400
John McCallf4c73712011-01-19 06:33:43 +00001401QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001402 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1403 if (ToPointeeType.isNull())
1404 return QualType();
1405
1406 return Importer.getToContext().getPointerType(ToPointeeType);
1407}
1408
John McCallf4c73712011-01-19 06:33:43 +00001409QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001410 // FIXME: Check for blocks support in "to" context.
1411 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1412 if (ToPointeeType.isNull())
1413 return QualType();
1414
1415 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1416}
1417
John McCallf4c73712011-01-19 06:33:43 +00001418QualType
1419ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001420 // FIXME: Check for C++ support in "to" context.
1421 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1422 if (ToPointeeType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1426}
1427
John McCallf4c73712011-01-19 06:33:43 +00001428QualType
1429ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001430 // FIXME: Check for C++0x support in "to" context.
1431 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1432 if (ToPointeeType.isNull())
1433 return QualType();
1434
1435 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1436}
1437
John McCallf4c73712011-01-19 06:33:43 +00001438QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001439 // FIXME: Check for C++ support in "to" context.
1440 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1441 if (ToPointeeType.isNull())
1442 return QualType();
1443
1444 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1445 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1446 ClassType.getTypePtr());
1447}
1448
John McCallf4c73712011-01-19 06:33:43 +00001449QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001450 QualType ToElementType = Importer.Import(T->getElementType());
1451 if (ToElementType.isNull())
1452 return QualType();
1453
1454 return Importer.getToContext().getConstantArrayType(ToElementType,
1455 T->getSize(),
1456 T->getSizeModifier(),
1457 T->getIndexTypeCVRQualifiers());
1458}
1459
John McCallf4c73712011-01-19 06:33:43 +00001460QualType
1461ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001462 QualType ToElementType = Importer.Import(T->getElementType());
1463 if (ToElementType.isNull())
1464 return QualType();
1465
1466 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1467 T->getSizeModifier(),
1468 T->getIndexTypeCVRQualifiers());
1469}
1470
John McCallf4c73712011-01-19 06:33:43 +00001471QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001472 QualType ToElementType = Importer.Import(T->getElementType());
1473 if (ToElementType.isNull())
1474 return QualType();
1475
1476 Expr *Size = Importer.Import(T->getSizeExpr());
1477 if (!Size)
1478 return QualType();
1479
1480 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1481 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1482 T->getSizeModifier(),
1483 T->getIndexTypeCVRQualifiers(),
1484 Brackets);
1485}
1486
John McCallf4c73712011-01-19 06:33:43 +00001487QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001488 QualType ToElementType = Importer.Import(T->getElementType());
1489 if (ToElementType.isNull())
1490 return QualType();
1491
1492 return Importer.getToContext().getVectorType(ToElementType,
1493 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001494 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001495}
1496
John McCallf4c73712011-01-19 06:33:43 +00001497QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001498 QualType ToElementType = Importer.Import(T->getElementType());
1499 if (ToElementType.isNull())
1500 return QualType();
1501
1502 return Importer.getToContext().getExtVectorType(ToElementType,
1503 T->getNumElements());
1504}
1505
John McCallf4c73712011-01-19 06:33:43 +00001506QualType
1507ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001508 // FIXME: What happens if we're importing a function without a prototype
1509 // into C++? Should we make it variadic?
1510 QualType ToResultType = Importer.Import(T->getResultType());
1511 if (ToResultType.isNull())
1512 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001513
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001514 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001515 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001516}
1517
John McCallf4c73712011-01-19 06:33:43 +00001518QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001519 QualType ToResultType = Importer.Import(T->getResultType());
1520 if (ToResultType.isNull())
1521 return QualType();
1522
1523 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001524 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001525 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1526 AEnd = T->arg_type_end();
1527 A != AEnd; ++A) {
1528 QualType ArgType = Importer.Import(*A);
1529 if (ArgType.isNull())
1530 return QualType();
1531 ArgTypes.push_back(ArgType);
1532 }
1533
1534 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001535 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001536 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1537 EEnd = T->exception_end();
1538 E != EEnd; ++E) {
1539 QualType ExceptionType = Importer.Import(*E);
1540 if (ExceptionType.isNull())
1541 return QualType();
1542 ExceptionTypes.push_back(ExceptionType);
1543 }
John McCalle23cf432010-12-14 08:05:40 +00001544
1545 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1546 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001547
1548 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001549 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001550}
1551
Sean Callanan0aeb2892011-08-11 16:56:07 +00001552QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1553 QualType ToInnerType = Importer.Import(T->getInnerType());
1554 if (ToInnerType.isNull())
1555 return QualType();
1556
1557 return Importer.getToContext().getParenType(ToInnerType);
1558}
1559
John McCallf4c73712011-01-19 06:33:43 +00001560QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001561 TypedefNameDecl *ToDecl
1562 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001563 if (!ToDecl)
1564 return QualType();
1565
1566 return Importer.getToContext().getTypeDeclType(ToDecl);
1567}
1568
John McCallf4c73712011-01-19 06:33:43 +00001569QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001570 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1571 if (!ToExpr)
1572 return QualType();
1573
1574 return Importer.getToContext().getTypeOfExprType(ToExpr);
1575}
1576
John McCallf4c73712011-01-19 06:33:43 +00001577QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001578 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1579 if (ToUnderlyingType.isNull())
1580 return QualType();
1581
1582 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1583}
1584
John McCallf4c73712011-01-19 06:33:43 +00001585QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001586 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001587 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1588 if (!ToExpr)
1589 return QualType();
1590
1591 return Importer.getToContext().getDecltypeType(ToExpr);
1592}
1593
Sean Huntca63c202011-05-24 22:41:36 +00001594QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1595 QualType ToBaseType = Importer.Import(T->getBaseType());
1596 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1597 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1598 return QualType();
1599
1600 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1601 ToUnderlyingType,
1602 T->getUTTKind());
1603}
1604
Richard Smith34b41d92011-02-20 03:19:35 +00001605QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1606 // FIXME: Make sure that the "to" context supports C++0x!
1607 QualType FromDeduced = T->getDeducedType();
1608 QualType ToDeduced;
1609 if (!FromDeduced.isNull()) {
1610 ToDeduced = Importer.Import(FromDeduced);
1611 if (ToDeduced.isNull())
1612 return QualType();
1613 }
1614
1615 return Importer.getToContext().getAutoType(ToDeduced);
1616}
1617
John McCallf4c73712011-01-19 06:33:43 +00001618QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001619 RecordDecl *ToDecl
1620 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1621 if (!ToDecl)
1622 return QualType();
1623
1624 return Importer.getToContext().getTagDeclType(ToDecl);
1625}
1626
John McCallf4c73712011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001628 EnumDecl *ToDecl
1629 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1630 if (!ToDecl)
1631 return QualType();
1632
1633 return Importer.getToContext().getTagDeclType(ToDecl);
1634}
1635
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001636QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001637 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001638 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1639 if (ToTemplate.isNull())
1640 return QualType();
1641
Chris Lattner5f9e2722011-07-23 10:55:15 +00001642 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001643 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1644 return QualType();
1645
1646 QualType ToCanonType;
1647 if (!QualType(T, 0).isCanonical()) {
1648 QualType FromCanonType
1649 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1650 ToCanonType =Importer.Import(FromCanonType);
1651 if (ToCanonType.isNull())
1652 return QualType();
1653 }
1654 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1655 ToTemplateArgs.data(),
1656 ToTemplateArgs.size(),
1657 ToCanonType);
1658}
1659
John McCallf4c73712011-01-19 06:33:43 +00001660QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001661 NestedNameSpecifier *ToQualifier = 0;
1662 // Note: the qualifier in an ElaboratedType is optional.
1663 if (T->getQualifier()) {
1664 ToQualifier = Importer.Import(T->getQualifier());
1665 if (!ToQualifier)
1666 return QualType();
1667 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001668
1669 QualType ToNamedType = Importer.Import(T->getNamedType());
1670 if (ToNamedType.isNull())
1671 return QualType();
1672
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001673 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1674 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001675}
1676
John McCallf4c73712011-01-19 06:33:43 +00001677QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001678 ObjCInterfaceDecl *Class
1679 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1680 if (!Class)
1681 return QualType();
1682
John McCallc12c5bb2010-05-15 11:32:37 +00001683 return Importer.getToContext().getObjCInterfaceType(Class);
1684}
1685
John McCallf4c73712011-01-19 06:33:43 +00001686QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001687 QualType ToBaseType = Importer.Import(T->getBaseType());
1688 if (ToBaseType.isNull())
1689 return QualType();
1690
Chris Lattner5f9e2722011-07-23 10:55:15 +00001691 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001692 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001693 PEnd = T->qual_end();
1694 P != PEnd; ++P) {
1695 ObjCProtocolDecl *Protocol
1696 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1697 if (!Protocol)
1698 return QualType();
1699 Protocols.push_back(Protocol);
1700 }
1701
John McCallc12c5bb2010-05-15 11:32:37 +00001702 return Importer.getToContext().getObjCObjectType(ToBaseType,
1703 Protocols.data(),
1704 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001705}
1706
John McCallf4c73712011-01-19 06:33:43 +00001707QualType
1708ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001709 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1710 if (ToPointeeType.isNull())
1711 return QualType();
1712
John McCallc12c5bb2010-05-15 11:32:37 +00001713 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001714}
1715
Douglas Gregor089459a2010-02-08 21:09:39 +00001716//----------------------------------------------------------------------------
1717// Import Declarations
1718//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001719bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1720 DeclContext *&LexicalDC,
1721 DeclarationName &Name,
1722 SourceLocation &Loc) {
1723 // Import the context of this declaration.
1724 DC = Importer.ImportContext(D->getDeclContext());
1725 if (!DC)
1726 return true;
1727
1728 LexicalDC = DC;
1729 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1730 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1731 if (!LexicalDC)
1732 return true;
1733 }
1734
1735 // Import the name of this declaration.
1736 Name = Importer.Import(D->getDeclName());
1737 if (D->getDeclName() && !Name)
1738 return true;
1739
1740 // Import the location of this declaration.
1741 Loc = Importer.Import(D->getLocation());
1742 return false;
1743}
1744
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001745void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1746 if (!FromD)
1747 return;
1748
1749 if (!ToD) {
1750 ToD = Importer.Import(FromD);
1751 if (!ToD)
1752 return;
1753 }
1754
1755 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1756 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1757 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1758 ImportDefinition(FromRecord, ToRecord);
1759 }
1760 }
1761 return;
1762 }
1763
1764 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1765 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1766 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1767 ImportDefinition(FromEnum, ToEnum);
1768 }
1769 }
1770 return;
1771 }
1772}
1773
Abramo Bagnara25777432010-08-11 22:01:17 +00001774void
1775ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1776 DeclarationNameInfo& To) {
1777 // NOTE: To.Name and To.Loc are already imported.
1778 // We only have to import To.LocInfo.
1779 switch (To.getName().getNameKind()) {
1780 case DeclarationName::Identifier:
1781 case DeclarationName::ObjCZeroArgSelector:
1782 case DeclarationName::ObjCOneArgSelector:
1783 case DeclarationName::ObjCMultiArgSelector:
1784 case DeclarationName::CXXUsingDirective:
1785 return;
1786
1787 case DeclarationName::CXXOperatorName: {
1788 SourceRange Range = From.getCXXOperatorNameRange();
1789 To.setCXXOperatorNameRange(Importer.Import(Range));
1790 return;
1791 }
1792 case DeclarationName::CXXLiteralOperatorName: {
1793 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1794 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1795 return;
1796 }
1797 case DeclarationName::CXXConstructorName:
1798 case DeclarationName::CXXDestructorName:
1799 case DeclarationName::CXXConversionFunctionName: {
1800 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1801 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1802 return;
1803 }
David Blaikieb219cfc2011-09-23 05:06:16 +00001804 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001805 }
1806}
1807
Douglas Gregord8868a62011-01-18 03:11:38 +00001808void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1809 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001810 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001811 return;
1812 }
1813
Douglas Gregor083a8212010-02-21 18:24:45 +00001814 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1815 FromEnd = FromDC->decls_end();
1816 From != FromEnd;
1817 ++From)
1818 Importer.Import(*From);
1819}
1820
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001821bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1822 bool ForceImport) {
1823 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001824 return false;
1825
1826 To->startDefinition();
1827
1828 // Add base classes.
1829 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1830 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1831
Chris Lattner5f9e2722011-07-23 10:55:15 +00001832 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001833 for (CXXRecordDecl::base_class_iterator
1834 Base1 = FromCXX->bases_begin(),
1835 FromBaseEnd = FromCXX->bases_end();
1836 Base1 != FromBaseEnd;
1837 ++Base1) {
1838 QualType T = Importer.Import(Base1->getType());
1839 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001840 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001841
1842 SourceLocation EllipsisLoc;
1843 if (Base1->isPackExpansion())
1844 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001845
1846 // Ensure that we have a definition for the base.
1847 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1848
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001849 Bases.push_back(
1850 new (Importer.getToContext())
1851 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1852 Base1->isVirtual(),
1853 Base1->isBaseOfClass(),
1854 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001855 Importer.Import(Base1->getTypeSourceInfo()),
1856 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001857 }
1858 if (!Bases.empty())
1859 ToCXX->setBases(Bases.data(), Bases.size());
1860 }
1861
Sean Callanan673e7752011-07-19 22:38:25 +00001862 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001863 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001864 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001865}
1866
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001867bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1868 bool ForceImport) {
1869 if (To->getDefinition() || To->isBeingDefined())
1870 return false;
1871
1872 To->startDefinition();
1873
1874 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1875 if (T.isNull())
1876 return true;
1877
1878 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1879 if (ToPromotionType.isNull())
1880 return true;
1881
1882 ImportDeclContext(From, ForceImport);
1883
1884 // FIXME: we might need to merge the number of positive or negative bits
1885 // if the enumerator lists don't match.
1886 To->completeDefinition(T, ToPromotionType,
1887 From->getNumPositiveBits(),
1888 From->getNumNegativeBits());
1889 return false;
1890}
1891
Douglas Gregor040afae2010-11-30 19:14:50 +00001892TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1893 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001894 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001895 ToParams.reserve(Params->size());
1896 for (TemplateParameterList::iterator P = Params->begin(),
1897 PEnd = Params->end();
1898 P != PEnd; ++P) {
1899 Decl *To = Importer.Import(*P);
1900 if (!To)
1901 return 0;
1902
1903 ToParams.push_back(cast<NamedDecl>(To));
1904 }
1905
1906 return TemplateParameterList::Create(Importer.getToContext(),
1907 Importer.Import(Params->getTemplateLoc()),
1908 Importer.Import(Params->getLAngleLoc()),
1909 ToParams.data(), ToParams.size(),
1910 Importer.Import(Params->getRAngleLoc()));
1911}
1912
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001913TemplateArgument
1914ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1915 switch (From.getKind()) {
1916 case TemplateArgument::Null:
1917 return TemplateArgument();
1918
1919 case TemplateArgument::Type: {
1920 QualType ToType = Importer.Import(From.getAsType());
1921 if (ToType.isNull())
1922 return TemplateArgument();
1923 return TemplateArgument(ToType);
1924 }
1925
1926 case TemplateArgument::Integral: {
1927 QualType ToType = Importer.Import(From.getIntegralType());
1928 if (ToType.isNull())
1929 return TemplateArgument();
1930 return TemplateArgument(*From.getAsIntegral(), ToType);
1931 }
1932
1933 case TemplateArgument::Declaration:
1934 if (Decl *To = Importer.Import(From.getAsDecl()))
1935 return TemplateArgument(To);
1936 return TemplateArgument();
1937
1938 case TemplateArgument::Template: {
1939 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1940 if (ToTemplate.isNull())
1941 return TemplateArgument();
1942
1943 return TemplateArgument(ToTemplate);
1944 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001945
1946 case TemplateArgument::TemplateExpansion: {
1947 TemplateName ToTemplate
1948 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1949 if (ToTemplate.isNull())
1950 return TemplateArgument();
1951
Douglas Gregor2be29f42011-01-14 23:41:42 +00001952 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001953 }
1954
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001955 case TemplateArgument::Expression:
1956 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1957 return TemplateArgument(ToExpr);
1958 return TemplateArgument();
1959
1960 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001961 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001962 ToPack.reserve(From.pack_size());
1963 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1964 return TemplateArgument();
1965
1966 TemplateArgument *ToArgs
1967 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1968 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1969 return TemplateArgument(ToArgs, ToPack.size());
1970 }
1971 }
1972
1973 llvm_unreachable("Invalid template argument kind");
1974 return TemplateArgument();
1975}
1976
1977bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1978 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001979 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001980 for (unsigned I = 0; I != NumFromArgs; ++I) {
1981 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1982 if (To.isNull() && !FromArgs[I].isNull())
1983 return true;
1984
1985 ToArgs.push_back(To);
1986 }
1987
1988 return false;
1989}
1990
Douglas Gregor96a01b42010-02-11 00:48:18 +00001991bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001992 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001993 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001994 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001995 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001996 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001997}
1998
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001999bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002000 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002001 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002002 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002003 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002004}
2005
Douglas Gregor040afae2010-11-30 19:14:50 +00002006bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2007 ClassTemplateDecl *To) {
2008 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2009 Importer.getToContext(),
2010 Importer.getNonEquivalentDecls());
2011 return Ctx.IsStructurallyEquivalent(From, To);
2012}
2013
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002014Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002015 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002016 << D->getDeclKindName();
2017 return 0;
2018}
2019
Douglas Gregor788c62d2010-02-21 18:26:36 +00002020Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2021 // Import the major distinguishing characteristics of this namespace.
2022 DeclContext *DC, *LexicalDC;
2023 DeclarationName Name;
2024 SourceLocation Loc;
2025 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2026 return 0;
2027
2028 NamespaceDecl *MergeWithNamespace = 0;
2029 if (!Name) {
2030 // This is an anonymous namespace. Adopt an existing anonymous
2031 // namespace if we can.
2032 // FIXME: Not testable.
2033 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2034 MergeWithNamespace = TU->getAnonymousNamespace();
2035 else
2036 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2037 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002038 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002039 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2040 Lookup.first != Lookup.second;
2041 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00002042 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002043 continue;
2044
2045 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2046 MergeWithNamespace = FoundNS;
2047 ConflictingDecls.clear();
2048 break;
2049 }
2050
2051 ConflictingDecls.push_back(*Lookup.first);
2052 }
2053
2054 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002055 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002056 ConflictingDecls.data(),
2057 ConflictingDecls.size());
2058 }
2059 }
2060
2061 // Create the "to" namespace, if needed.
2062 NamespaceDecl *ToNamespace = MergeWithNamespace;
2063 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002064 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2065 Importer.Import(D->getLocStart()),
2066 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002067 ToNamespace->setLexicalDeclContext(LexicalDC);
2068 LexicalDC->addDecl(ToNamespace);
2069
2070 // If this is an anonymous namespace, register it as the anonymous
2071 // namespace within its context.
2072 if (!Name) {
2073 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2074 TU->setAnonymousNamespace(ToNamespace);
2075 else
2076 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2077 }
2078 }
2079 Importer.Imported(D, ToNamespace);
2080
2081 ImportDeclContext(D);
2082
2083 return ToNamespace;
2084}
2085
Richard Smith162e1c12011-04-15 14:24:37 +00002086Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002087 // Import the major distinguishing characteristics of this typedef.
2088 DeclContext *DC, *LexicalDC;
2089 DeclarationName Name;
2090 SourceLocation Loc;
2091 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2092 return 0;
2093
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002094 // If this typedef is not in block scope, determine whether we've
2095 // seen a typedef with the same name (that we can merge with) or any
2096 // other entity by that name (which name lookup could conflict with).
2097 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002098 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002099 unsigned IDNS = Decl::IDNS_Ordinary;
2100 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2101 Lookup.first != Lookup.second;
2102 ++Lookup.first) {
2103 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2104 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002105 if (TypedefNameDecl *FoundTypedef =
2106 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002107 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2108 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002109 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002110 }
2111
2112 ConflictingDecls.push_back(*Lookup.first);
2113 }
2114
2115 if (!ConflictingDecls.empty()) {
2116 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2117 ConflictingDecls.data(),
2118 ConflictingDecls.size());
2119 if (!Name)
2120 return 0;
2121 }
2122 }
2123
Douglas Gregorea35d112010-02-15 23:54:17 +00002124 // Import the underlying type of this typedef;
2125 QualType T = Importer.Import(D->getUnderlyingType());
2126 if (T.isNull())
2127 return 0;
2128
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002129 // Create the new typedef node.
2130 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002131 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002132 TypedefNameDecl *ToTypedef;
2133 if (IsAlias)
2134 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2135 StartL, Loc,
2136 Name.getAsIdentifierInfo(),
2137 TInfo);
2138 else
2139 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2140 StartL, Loc,
2141 Name.getAsIdentifierInfo(),
2142 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002143 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002144 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002145 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002146 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002147
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002148 return ToTypedef;
2149}
2150
Richard Smith162e1c12011-04-15 14:24:37 +00002151Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2152 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2153}
2154
2155Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2156 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2157}
2158
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002159Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2160 // Import the major distinguishing characteristics of this enum.
2161 DeclContext *DC, *LexicalDC;
2162 DeclarationName Name;
2163 SourceLocation Loc;
2164 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2165 return 0;
2166
2167 // Figure out what enum name we're looking for.
2168 unsigned IDNS = Decl::IDNS_Tag;
2169 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002170 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2171 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002172 IDNS = Decl::IDNS_Ordinary;
2173 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2174 IDNS |= Decl::IDNS_Ordinary;
2175
2176 // We may already have an enum of the same name; try to find and match it.
2177 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002178 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002179 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002180 Lookup.first != Lookup.second;
2181 ++Lookup.first) {
2182 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2183 continue;
2184
2185 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002186 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002187 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2188 Found = Tag->getDecl();
2189 }
2190
2191 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002192 if (IsStructuralMatch(D, FoundEnum))
2193 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002194 }
2195
2196 ConflictingDecls.push_back(*Lookup.first);
2197 }
2198
2199 if (!ConflictingDecls.empty()) {
2200 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2201 ConflictingDecls.data(),
2202 ConflictingDecls.size());
2203 }
2204 }
2205
2206 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002207 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2208 Importer.Import(D->getLocStart()),
2209 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002210 D->isScoped(), D->isScopedUsingClassTag(),
2211 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002212 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002213 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002214 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002215 D2->setLexicalDeclContext(LexicalDC);
2216 Importer.Imported(D, D2);
2217 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002218
2219 // Import the integer type.
2220 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2221 if (ToIntegerType.isNull())
2222 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002223 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002224
2225 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002226 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002227 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002228
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002229 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002230}
2231
Douglas Gregor96a01b42010-02-11 00:48:18 +00002232Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2233 // If this record has a definition in the translation unit we're coming from,
2234 // but this particular declaration is not that definition, import the
2235 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002236 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002237 if (Definition && Definition != D) {
2238 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002239 if (!ImportedDef)
2240 return 0;
2241
2242 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002243 }
2244
2245 // Import the major distinguishing characteristics of this record.
2246 DeclContext *DC, *LexicalDC;
2247 DeclarationName Name;
2248 SourceLocation Loc;
2249 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2250 return 0;
2251
2252 // Figure out what structure name we're looking for.
2253 unsigned IDNS = Decl::IDNS_Tag;
2254 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002255 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2256 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002257 IDNS = Decl::IDNS_Ordinary;
2258 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2259 IDNS |= Decl::IDNS_Ordinary;
2260
2261 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002262 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002263 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002264 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002265 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002266 Lookup.first != Lookup.second;
2267 ++Lookup.first) {
2268 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2269 continue;
2270
2271 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002272 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002273 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2274 Found = Tag->getDecl();
2275 }
2276
2277 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002278 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002279 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002280 // The record types structurally match, or the "from" translation
2281 // unit only had a forward declaration anyway; call it the same
2282 // function.
2283 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002284 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002285 }
2286 } else {
2287 // We have a forward declaration of this type, so adopt that forward
2288 // declaration rather than building a new one.
2289 AdoptDecl = FoundRecord;
2290 continue;
2291 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002292 }
2293
2294 ConflictingDecls.push_back(*Lookup.first);
2295 }
2296
2297 if (!ConflictingDecls.empty()) {
2298 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2299 ConflictingDecls.data(),
2300 ConflictingDecls.size());
2301 }
2302 }
2303
2304 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002305 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002306 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002307 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002308 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002309 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002310 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002311 DC, StartLoc, Loc,
2312 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002313 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002314 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002315 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002316 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002317 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002318 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002319
2320 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002321 D2->setLexicalDeclContext(LexicalDC);
2322 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002323 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002324
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002325 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002326
John McCall5e1cdac2011-10-07 06:10:15 +00002327 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002328 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002329
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002330 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002331}
2332
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002333Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2334 // Import the major distinguishing characteristics of this enumerator.
2335 DeclContext *DC, *LexicalDC;
2336 DeclarationName Name;
2337 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002338 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002339 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002340
2341 QualType T = Importer.Import(D->getType());
2342 if (T.isNull())
2343 return 0;
2344
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002345 // Determine whether there are any other declarations with the same name and
2346 // in the same context.
2347 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002348 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002349 unsigned IDNS = Decl::IDNS_Ordinary;
2350 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2351 Lookup.first != Lookup.second;
2352 ++Lookup.first) {
2353 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2354 continue;
2355
2356 ConflictingDecls.push_back(*Lookup.first);
2357 }
2358
2359 if (!ConflictingDecls.empty()) {
2360 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2361 ConflictingDecls.data(),
2362 ConflictingDecls.size());
2363 if (!Name)
2364 return 0;
2365 }
2366 }
2367
2368 Expr *Init = Importer.Import(D->getInitExpr());
2369 if (D->getInitExpr() && !Init)
2370 return 0;
2371
2372 EnumConstantDecl *ToEnumerator
2373 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2374 Name.getAsIdentifierInfo(), T,
2375 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002376 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002377 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002378 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002379 LexicalDC->addDecl(ToEnumerator);
2380 return ToEnumerator;
2381}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002382
Douglas Gregora404ea62010-02-10 19:54:31 +00002383Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2384 // Import the major distinguishing characteristics of this function.
2385 DeclContext *DC, *LexicalDC;
2386 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002387 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002388 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002389 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002390
Douglas Gregora404ea62010-02-10 19:54:31 +00002391 // Try to find a function in our own ("to") context with the same name, same
2392 // type, and in the same context as the function we're importing.
2393 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002394 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002395 unsigned IDNS = Decl::IDNS_Ordinary;
2396 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2397 Lookup.first != Lookup.second;
2398 ++Lookup.first) {
2399 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2400 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002401
Douglas Gregora404ea62010-02-10 19:54:31 +00002402 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2403 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2404 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002405 if (Importer.IsStructurallyEquivalent(D->getType(),
2406 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002407 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002408 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002409 }
2410
2411 // FIXME: Check for overloading more carefully, e.g., by boosting
2412 // Sema::IsOverload out to the AST library.
2413
2414 // Function overloading is okay in C++.
2415 if (Importer.getToContext().getLangOptions().CPlusPlus)
2416 continue;
2417
2418 // Complain about inconsistent function types.
2419 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002420 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002421 Importer.ToDiag(FoundFunction->getLocation(),
2422 diag::note_odr_value_here)
2423 << FoundFunction->getType();
2424 }
2425 }
2426
2427 ConflictingDecls.push_back(*Lookup.first);
2428 }
2429
2430 if (!ConflictingDecls.empty()) {
2431 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2432 ConflictingDecls.data(),
2433 ConflictingDecls.size());
2434 if (!Name)
2435 return 0;
2436 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002437 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002438
Abramo Bagnara25777432010-08-11 22:01:17 +00002439 DeclarationNameInfo NameInfo(Name, Loc);
2440 // Import additional name location/type info.
2441 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2442
Douglas Gregorea35d112010-02-15 23:54:17 +00002443 // Import the type.
2444 QualType T = Importer.Import(D->getType());
2445 if (T.isNull())
2446 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002447
2448 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002449 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002450 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2451 P != PEnd; ++P) {
2452 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2453 if (!ToP)
2454 return 0;
2455
2456 Parameters.push_back(ToP);
2457 }
2458
2459 // Create the imported function.
2460 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002461 FunctionDecl *ToFunction = 0;
2462 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2463 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2464 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002465 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002466 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002467 FromConstructor->isExplicit(),
2468 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002469 D->isImplicit(),
2470 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002471 } else if (isa<CXXDestructorDecl>(D)) {
2472 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2473 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002474 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002475 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002476 D->isInlineSpecified(),
2477 D->isImplicit());
2478 } else if (CXXConversionDecl *FromConversion
2479 = dyn_cast<CXXConversionDecl>(D)) {
2480 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2481 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002482 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002483 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002484 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002485 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002486 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002487 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002488 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2489 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2490 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002491 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002492 NameInfo, T, TInfo,
2493 Method->isStatic(),
2494 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002495 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002496 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002497 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002498 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002499 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002500 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002501 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002502 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002503 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002504 D->hasWrittenPrototype(),
2505 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002506 }
John McCallb6217662010-03-15 10:12:16 +00002507
2508 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002509 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002510 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002511 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002512 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2513 ToFunction->setTrivial(D->isTrivial());
2514 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002515 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002516
Douglas Gregora404ea62010-02-10 19:54:31 +00002517 // Set the parameters.
2518 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002519 Parameters[I]->setOwningFunction(ToFunction);
2520 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002521 }
David Blaikie4278c652011-09-21 18:16:56 +00002522 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002523
2524 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002525
2526 // Add this function to the lexical context.
2527 LexicalDC->addDecl(ToFunction);
2528
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002529 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002530}
2531
Douglas Gregorc144f352010-02-21 18:29:16 +00002532Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2533 return VisitFunctionDecl(D);
2534}
2535
2536Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2537 return VisitCXXMethodDecl(D);
2538}
2539
2540Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2541 return VisitCXXMethodDecl(D);
2542}
2543
2544Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2545 return VisitCXXMethodDecl(D);
2546}
2547
Douglas Gregor96a01b42010-02-11 00:48:18 +00002548Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2549 // Import the major distinguishing characteristics of a variable.
2550 DeclContext *DC, *LexicalDC;
2551 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002552 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002553 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2554 return 0;
2555
2556 // Import the type.
2557 QualType T = Importer.Import(D->getType());
2558 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002559 return 0;
2560
2561 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2562 Expr *BitWidth = Importer.Import(D->getBitWidth());
2563 if (!BitWidth && D->getBitWidth())
2564 return 0;
2565
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002566 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2567 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002568 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002569 T, TInfo, BitWidth, D->isMutable(),
2570 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002571 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002572 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002573 if (ToField->hasInClassInitializer())
2574 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002575 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002576 LexicalDC->addDecl(ToField);
2577 return ToField;
2578}
2579
Francois Pichet87c2e122010-11-21 06:08:52 +00002580Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2581 // Import the major distinguishing characteristics of a variable.
2582 DeclContext *DC, *LexicalDC;
2583 DeclarationName Name;
2584 SourceLocation Loc;
2585 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2586 return 0;
2587
2588 // Import the type.
2589 QualType T = Importer.Import(D->getType());
2590 if (T.isNull())
2591 return 0;
2592
2593 NamedDecl **NamedChain =
2594 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2595
2596 unsigned i = 0;
2597 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2598 PE = D->chain_end(); PI != PE; ++PI) {
2599 Decl* D = Importer.Import(*PI);
2600 if (!D)
2601 return 0;
2602 NamedChain[i++] = cast<NamedDecl>(D);
2603 }
2604
2605 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2606 Importer.getToContext(), DC,
2607 Loc, Name.getAsIdentifierInfo(), T,
2608 NamedChain, D->getChainingSize());
2609 ToIndirectField->setAccess(D->getAccess());
2610 ToIndirectField->setLexicalDeclContext(LexicalDC);
2611 Importer.Imported(D, ToIndirectField);
2612 LexicalDC->addDecl(ToIndirectField);
2613 return ToIndirectField;
2614}
2615
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002616Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2617 // Import the major distinguishing characteristics of an ivar.
2618 DeclContext *DC, *LexicalDC;
2619 DeclarationName Name;
2620 SourceLocation Loc;
2621 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2622 return 0;
2623
2624 // Determine whether we've already imported this ivar
2625 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2626 Lookup.first != Lookup.second;
2627 ++Lookup.first) {
2628 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2629 if (Importer.IsStructurallyEquivalent(D->getType(),
2630 FoundIvar->getType())) {
2631 Importer.Imported(D, FoundIvar);
2632 return FoundIvar;
2633 }
2634
2635 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2636 << Name << D->getType() << FoundIvar->getType();
2637 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2638 << FoundIvar->getType();
2639 return 0;
2640 }
2641 }
2642
2643 // Import the type.
2644 QualType T = Importer.Import(D->getType());
2645 if (T.isNull())
2646 return 0;
2647
2648 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2649 Expr *BitWidth = Importer.Import(D->getBitWidth());
2650 if (!BitWidth && D->getBitWidth())
2651 return 0;
2652
Daniel Dunbara0654922010-04-02 20:10:03 +00002653 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2654 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002655 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002656 Loc, Name.getAsIdentifierInfo(),
2657 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002658 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002659 ToIvar->setLexicalDeclContext(LexicalDC);
2660 Importer.Imported(D, ToIvar);
2661 LexicalDC->addDecl(ToIvar);
2662 return ToIvar;
2663
2664}
2665
Douglas Gregora404ea62010-02-10 19:54:31 +00002666Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2667 // Import the major distinguishing characteristics of a variable.
2668 DeclContext *DC, *LexicalDC;
2669 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002670 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002671 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002672 return 0;
2673
Douglas Gregor089459a2010-02-08 21:09:39 +00002674 // Try to find a variable in our own ("to") context with the same name and
2675 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002676 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002677 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002678 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002679 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002680 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002681 Lookup.first != Lookup.second;
2682 ++Lookup.first) {
2683 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2684 continue;
2685
2686 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2687 // We have found a variable that we may need to merge with. Check it.
2688 if (isExternalLinkage(FoundVar->getLinkage()) &&
2689 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002690 if (Importer.IsStructurallyEquivalent(D->getType(),
2691 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002692 MergeWithVar = FoundVar;
2693 break;
2694 }
2695
Douglas Gregord0145422010-02-12 17:23:39 +00002696 const ArrayType *FoundArray
2697 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2698 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002699 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002700 if (FoundArray && TArray) {
2701 if (isa<IncompleteArrayType>(FoundArray) &&
2702 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002703 // Import the type.
2704 QualType T = Importer.Import(D->getType());
2705 if (T.isNull())
2706 return 0;
2707
Douglas Gregord0145422010-02-12 17:23:39 +00002708 FoundVar->setType(T);
2709 MergeWithVar = FoundVar;
2710 break;
2711 } else if (isa<IncompleteArrayType>(TArray) &&
2712 isa<ConstantArrayType>(FoundArray)) {
2713 MergeWithVar = FoundVar;
2714 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002715 }
2716 }
2717
Douglas Gregor089459a2010-02-08 21:09:39 +00002718 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002719 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002720 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2721 << FoundVar->getType();
2722 }
2723 }
2724
2725 ConflictingDecls.push_back(*Lookup.first);
2726 }
2727
2728 if (MergeWithVar) {
2729 // An equivalent variable with external linkage has been found. Link
2730 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002731 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002732
2733 if (VarDecl *DDef = D->getDefinition()) {
2734 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2735 Importer.ToDiag(ExistingDef->getLocation(),
2736 diag::err_odr_variable_multiple_def)
2737 << Name;
2738 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2739 } else {
2740 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002741 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002742 }
2743 }
2744
2745 return MergeWithVar;
2746 }
2747
2748 if (!ConflictingDecls.empty()) {
2749 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2750 ConflictingDecls.data(),
2751 ConflictingDecls.size());
2752 if (!Name)
2753 return 0;
2754 }
2755 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002756
Douglas Gregorea35d112010-02-15 23:54:17 +00002757 // Import the type.
2758 QualType T = Importer.Import(D->getType());
2759 if (T.isNull())
2760 return 0;
2761
Douglas Gregor089459a2010-02-08 21:09:39 +00002762 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002763 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002764 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2765 Importer.Import(D->getInnerLocStart()),
2766 Loc, Name.getAsIdentifierInfo(),
2767 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002768 D->getStorageClass(),
2769 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002770 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002771 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002772 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002773 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002774 LexicalDC->addDecl(ToVar);
2775
Douglas Gregor089459a2010-02-08 21:09:39 +00002776 // Merge the initializer.
2777 // FIXME: Can we really import any initializer? Alternatively, we could force
2778 // ourselves to import every declaration of a variable and then only use
2779 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002780 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002781
2782 // FIXME: Other bits to merge?
2783
2784 return ToVar;
2785}
2786
Douglas Gregor2cd00932010-02-17 21:22:52 +00002787Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2788 // Parameters are created in the translation unit's context, then moved
2789 // into the function declaration's context afterward.
2790 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2791
2792 // Import the name of this declaration.
2793 DeclarationName Name = Importer.Import(D->getDeclName());
2794 if (D->getDeclName() && !Name)
2795 return 0;
2796
2797 // Import the location of this declaration.
2798 SourceLocation Loc = Importer.Import(D->getLocation());
2799
2800 // Import the parameter's type.
2801 QualType T = Importer.Import(D->getType());
2802 if (T.isNull())
2803 return 0;
2804
2805 // Create the imported parameter.
2806 ImplicitParamDecl *ToParm
2807 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2808 Loc, Name.getAsIdentifierInfo(),
2809 T);
2810 return Importer.Imported(D, ToParm);
2811}
2812
Douglas Gregora404ea62010-02-10 19:54:31 +00002813Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2814 // Parameters are created in the translation unit's context, then moved
2815 // into the function declaration's context afterward.
2816 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2817
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002818 // Import the name of this declaration.
2819 DeclarationName Name = Importer.Import(D->getDeclName());
2820 if (D->getDeclName() && !Name)
2821 return 0;
2822
Douglas Gregora404ea62010-02-10 19:54:31 +00002823 // Import the location of this declaration.
2824 SourceLocation Loc = Importer.Import(D->getLocation());
2825
2826 // Import the parameter's type.
2827 QualType T = Importer.Import(D->getType());
2828 if (T.isNull())
2829 return 0;
2830
2831 // Create the imported parameter.
2832 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2833 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002834 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002835 Loc, Name.getAsIdentifierInfo(),
2836 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002837 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002838 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002839 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002840 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002841}
2842
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002843Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2844 // Import the major distinguishing characteristics of a method.
2845 DeclContext *DC, *LexicalDC;
2846 DeclarationName Name;
2847 SourceLocation Loc;
2848 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2849 return 0;
2850
2851 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2852 Lookup.first != Lookup.second;
2853 ++Lookup.first) {
2854 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2855 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2856 continue;
2857
2858 // Check return types.
2859 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2860 FoundMethod->getResultType())) {
2861 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2862 << D->isInstanceMethod() << Name
2863 << D->getResultType() << FoundMethod->getResultType();
2864 Importer.ToDiag(FoundMethod->getLocation(),
2865 diag::note_odr_objc_method_here)
2866 << D->isInstanceMethod() << Name;
2867 return 0;
2868 }
2869
2870 // Check the number of parameters.
2871 if (D->param_size() != FoundMethod->param_size()) {
2872 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2873 << D->isInstanceMethod() << Name
2874 << D->param_size() << FoundMethod->param_size();
2875 Importer.ToDiag(FoundMethod->getLocation(),
2876 diag::note_odr_objc_method_here)
2877 << D->isInstanceMethod() << Name;
2878 return 0;
2879 }
2880
2881 // Check parameter types.
2882 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2883 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2884 P != PEnd; ++P, ++FoundP) {
2885 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2886 (*FoundP)->getType())) {
2887 Importer.FromDiag((*P)->getLocation(),
2888 diag::err_odr_objc_method_param_type_inconsistent)
2889 << D->isInstanceMethod() << Name
2890 << (*P)->getType() << (*FoundP)->getType();
2891 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2892 << (*FoundP)->getType();
2893 return 0;
2894 }
2895 }
2896
2897 // Check variadic/non-variadic.
2898 // Check the number of parameters.
2899 if (D->isVariadic() != FoundMethod->isVariadic()) {
2900 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2901 << D->isInstanceMethod() << Name;
2902 Importer.ToDiag(FoundMethod->getLocation(),
2903 diag::note_odr_objc_method_here)
2904 << D->isInstanceMethod() << Name;
2905 return 0;
2906 }
2907
2908 // FIXME: Any other bits we need to merge?
2909 return Importer.Imported(D, FoundMethod);
2910 }
2911 }
2912
2913 // Import the result type.
2914 QualType ResultTy = Importer.Import(D->getResultType());
2915 if (ResultTy.isNull())
2916 return 0;
2917
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002918 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2919
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002920 ObjCMethodDecl *ToMethod
2921 = ObjCMethodDecl::Create(Importer.getToContext(),
2922 Loc,
2923 Importer.Import(D->getLocEnd()),
2924 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002925 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002926 D->isInstanceMethod(),
2927 D->isVariadic(),
2928 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002929 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002930 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002931 D->getImplementationControl(),
2932 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002933
2934 // FIXME: When we decide to merge method definitions, we'll need to
2935 // deal with implicit parameters.
2936
2937 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002938 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002939 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2940 FromPEnd = D->param_end();
2941 FromP != FromPEnd;
2942 ++FromP) {
2943 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2944 if (!ToP)
2945 return 0;
2946
2947 ToParams.push_back(ToP);
2948 }
2949
2950 // Set the parameters.
2951 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2952 ToParams[I]->setOwningFunction(ToMethod);
2953 ToMethod->addDecl(ToParams[I]);
2954 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002955 SmallVector<SourceLocation, 12> SelLocs;
2956 D->getSelectorLocs(SelLocs);
2957 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002958
2959 ToMethod->setLexicalDeclContext(LexicalDC);
2960 Importer.Imported(D, ToMethod);
2961 LexicalDC->addDecl(ToMethod);
2962 return ToMethod;
2963}
2964
Douglas Gregorb4677b62010-02-18 01:47:50 +00002965Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2966 // Import the major distinguishing characteristics of a category.
2967 DeclContext *DC, *LexicalDC;
2968 DeclarationName Name;
2969 SourceLocation Loc;
2970 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2971 return 0;
2972
2973 ObjCInterfaceDecl *ToInterface
2974 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2975 if (!ToInterface)
2976 return 0;
2977
2978 // Determine if we've already encountered this category.
2979 ObjCCategoryDecl *MergeWithCategory
2980 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2981 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2982 if (!ToCategory) {
2983 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00002984 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00002985 Loc,
2986 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00002987 Name.getAsIdentifierInfo(),
2988 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002989 ToCategory->setLexicalDeclContext(LexicalDC);
2990 LexicalDC->addDecl(ToCategory);
2991 Importer.Imported(D, ToCategory);
2992
Douglas Gregorb4677b62010-02-18 01:47:50 +00002993 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00002994 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2995 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00002996 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2997 = D->protocol_loc_begin();
2998 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2999 FromProtoEnd = D->protocol_end();
3000 FromProto != FromProtoEnd;
3001 ++FromProto, ++FromProtoLoc) {
3002 ObjCProtocolDecl *ToProto
3003 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3004 if (!ToProto)
3005 return 0;
3006 Protocols.push_back(ToProto);
3007 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3008 }
3009
3010 // FIXME: If we're merging, make sure that the protocol list is the same.
3011 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3012 ProtocolLocs.data(), Importer.getToContext());
3013
3014 } else {
3015 Importer.Imported(D, ToCategory);
3016 }
3017
3018 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003019 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003020
3021 // If we have an implementation, import it as well.
3022 if (D->getImplementation()) {
3023 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003024 = cast_or_null<ObjCCategoryImplDecl>(
3025 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003026 if (!Impl)
3027 return 0;
3028
3029 ToCategory->setImplementation(Impl);
3030 }
3031
3032 return ToCategory;
3033}
3034
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003035Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003036 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003037 DeclContext *DC, *LexicalDC;
3038 DeclarationName Name;
3039 SourceLocation Loc;
3040 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3041 return 0;
3042
3043 ObjCProtocolDecl *MergeWithProtocol = 0;
3044 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3045 Lookup.first != Lookup.second;
3046 ++Lookup.first) {
3047 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3048 continue;
3049
3050 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
3051 break;
3052 }
3053
3054 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3055 if (!ToProto || ToProto->isForwardDecl()) {
3056 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003057 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3058 Name.getAsIdentifierInfo(), Loc,
3059 Importer.Import(D->getAtStartLoc()));
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003060 ToProto->setForwardDecl(D->isForwardDecl());
3061 ToProto->setLexicalDeclContext(LexicalDC);
3062 LexicalDC->addDecl(ToProto);
3063 }
3064 Importer.Imported(D, ToProto);
3065
3066 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003067 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3068 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003069 ObjCProtocolDecl::protocol_loc_iterator
3070 FromProtoLoc = D->protocol_loc_begin();
3071 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3072 FromProtoEnd = D->protocol_end();
3073 FromProto != FromProtoEnd;
3074 ++FromProto, ++FromProtoLoc) {
3075 ObjCProtocolDecl *ToProto
3076 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3077 if (!ToProto)
3078 return 0;
3079 Protocols.push_back(ToProto);
3080 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3081 }
3082
3083 // FIXME: If we're merging, make sure that the protocol list is the same.
3084 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3085 ProtocolLocs.data(), Importer.getToContext());
3086 } else {
3087 Importer.Imported(D, ToProto);
3088 }
3089
Douglas Gregorb4677b62010-02-18 01:47:50 +00003090 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003091 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003092
3093 return ToProto;
3094}
3095
Douglas Gregora12d2942010-02-16 01:20:57 +00003096Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3097 // Import the major distinguishing characteristics of an @interface.
3098 DeclContext *DC, *LexicalDC;
3099 DeclarationName Name;
3100 SourceLocation Loc;
3101 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3102 return 0;
3103
3104 ObjCInterfaceDecl *MergeWithIface = 0;
3105 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3106 Lookup.first != Lookup.second;
3107 ++Lookup.first) {
3108 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3109 continue;
3110
3111 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3112 break;
3113 }
3114
3115 ObjCInterfaceDecl *ToIface = MergeWithIface;
3116 if (!ToIface || ToIface->isForwardDecl()) {
3117 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003118 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3119 Importer.Import(D->getAtStartLoc()),
3120 Name.getAsIdentifierInfo(), Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003121 D->isForwardDecl(),
3122 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003123 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003124 ToIface->setLexicalDeclContext(LexicalDC);
3125 LexicalDC->addDecl(ToIface);
3126 }
3127 Importer.Imported(D, ToIface);
3128
Douglas Gregora12d2942010-02-16 01:20:57 +00003129 if (D->getSuperClass()) {
3130 ObjCInterfaceDecl *Super
3131 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3132 if (!Super)
3133 return 0;
3134
3135 ToIface->setSuperClass(Super);
3136 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3137 }
3138
3139 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003140 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3141 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003142 ObjCInterfaceDecl::protocol_loc_iterator
3143 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003144
3145 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003146 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3147 FromProtoEnd = D->protocol_end();
3148 FromProto != FromProtoEnd;
3149 ++FromProto, ++FromProtoLoc) {
3150 ObjCProtocolDecl *ToProto
3151 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3152 if (!ToProto)
3153 return 0;
3154 Protocols.push_back(ToProto);
3155 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3156 }
3157
3158 // FIXME: If we're merging, make sure that the protocol list is the same.
3159 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3160 ProtocolLocs.data(), Importer.getToContext());
3161
Douglas Gregora12d2942010-02-16 01:20:57 +00003162 // Import @end range
3163 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3164 } else {
3165 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003166
3167 // Check for consistency of superclasses.
3168 DeclarationName FromSuperName, ToSuperName;
3169 if (D->getSuperClass())
3170 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3171 if (ToIface->getSuperClass())
3172 ToSuperName = ToIface->getSuperClass()->getDeclName();
3173 if (FromSuperName != ToSuperName) {
3174 Importer.ToDiag(ToIface->getLocation(),
3175 diag::err_odr_objc_superclass_inconsistent)
3176 << ToIface->getDeclName();
3177 if (ToIface->getSuperClass())
3178 Importer.ToDiag(ToIface->getSuperClassLoc(),
3179 diag::note_odr_objc_superclass)
3180 << ToIface->getSuperClass()->getDeclName();
3181 else
3182 Importer.ToDiag(ToIface->getLocation(),
3183 diag::note_odr_objc_missing_superclass);
3184 if (D->getSuperClass())
3185 Importer.FromDiag(D->getSuperClassLoc(),
3186 diag::note_odr_objc_superclass)
3187 << D->getSuperClass()->getDeclName();
3188 else
3189 Importer.FromDiag(D->getLocation(),
3190 diag::note_odr_objc_missing_superclass);
3191 return 0;
3192 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003193 }
3194
Douglas Gregorb4677b62010-02-18 01:47:50 +00003195 // Import categories. When the categories themselves are imported, they'll
3196 // hook themselves into this interface.
3197 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3198 FromCat = FromCat->getNextClassCategory())
3199 Importer.Import(FromCat);
3200
Douglas Gregora12d2942010-02-16 01:20:57 +00003201 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003202 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003203
3204 // If we have an @implementation, import it as well.
3205 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003206 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3207 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003208 if (!Impl)
3209 return 0;
3210
3211 ToIface->setImplementation(Impl);
3212 }
3213
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003214 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003215}
3216
Douglas Gregor3daef292010-12-07 15:32:12 +00003217Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3218 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3219 Importer.Import(D->getCategoryDecl()));
3220 if (!Category)
3221 return 0;
3222
3223 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3224 if (!ToImpl) {
3225 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3226 if (!DC)
3227 return 0;
3228
3229 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003230 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003231 Category->getClassInterface(),
3232 Importer.Import(D->getLocation()),
3233 Importer.Import(D->getAtStartLoc()));
Douglas Gregor3daef292010-12-07 15:32:12 +00003234
3235 DeclContext *LexicalDC = DC;
3236 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3237 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3238 if (!LexicalDC)
3239 return 0;
3240
3241 ToImpl->setLexicalDeclContext(LexicalDC);
3242 }
3243
3244 LexicalDC->addDecl(ToImpl);
3245 Category->setImplementation(ToImpl);
3246 }
3247
3248 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003249 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003250 return ToImpl;
3251}
3252
Douglas Gregordd182ff2010-12-07 01:26:03 +00003253Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3254 // Find the corresponding interface.
3255 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3256 Importer.Import(D->getClassInterface()));
3257 if (!Iface)
3258 return 0;
3259
3260 // Import the superclass, if any.
3261 ObjCInterfaceDecl *Super = 0;
3262 if (D->getSuperClass()) {
3263 Super = cast_or_null<ObjCInterfaceDecl>(
3264 Importer.Import(D->getSuperClass()));
3265 if (!Super)
3266 return 0;
3267 }
3268
3269 ObjCImplementationDecl *Impl = Iface->getImplementation();
3270 if (!Impl) {
3271 // We haven't imported an implementation yet. Create a new @implementation
3272 // now.
3273 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3274 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003275 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003276 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003277 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003278
3279 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3280 DeclContext *LexicalDC
3281 = Importer.ImportContext(D->getLexicalDeclContext());
3282 if (!LexicalDC)
3283 return 0;
3284 Impl->setLexicalDeclContext(LexicalDC);
3285 }
3286
3287 // Associate the implementation with the class it implements.
3288 Iface->setImplementation(Impl);
3289 Importer.Imported(D, Iface->getImplementation());
3290 } else {
3291 Importer.Imported(D, Iface->getImplementation());
3292
3293 // Verify that the existing @implementation has the same superclass.
3294 if ((Super && !Impl->getSuperClass()) ||
3295 (!Super && Impl->getSuperClass()) ||
3296 (Super && Impl->getSuperClass() &&
3297 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3298 Importer.ToDiag(Impl->getLocation(),
3299 diag::err_odr_objc_superclass_inconsistent)
3300 << Iface->getDeclName();
3301 // FIXME: It would be nice to have the location of the superclass
3302 // below.
3303 if (Impl->getSuperClass())
3304 Importer.ToDiag(Impl->getLocation(),
3305 diag::note_odr_objc_superclass)
3306 << Impl->getSuperClass()->getDeclName();
3307 else
3308 Importer.ToDiag(Impl->getLocation(),
3309 diag::note_odr_objc_missing_superclass);
3310 if (D->getSuperClass())
3311 Importer.FromDiag(D->getLocation(),
3312 diag::note_odr_objc_superclass)
3313 << D->getSuperClass()->getDeclName();
3314 else
3315 Importer.FromDiag(D->getLocation(),
3316 diag::note_odr_objc_missing_superclass);
3317 return 0;
3318 }
3319 }
3320
3321 // Import all of the members of this @implementation.
3322 ImportDeclContext(D);
3323
3324 return Impl;
3325}
3326
Douglas Gregore3261622010-02-17 18:02:10 +00003327Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3328 // Import the major distinguishing characteristics of an @property.
3329 DeclContext *DC, *LexicalDC;
3330 DeclarationName Name;
3331 SourceLocation Loc;
3332 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3333 return 0;
3334
3335 // Check whether we have already imported this property.
3336 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3337 Lookup.first != Lookup.second;
3338 ++Lookup.first) {
3339 if (ObjCPropertyDecl *FoundProp
3340 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3341 // Check property types.
3342 if (!Importer.IsStructurallyEquivalent(D->getType(),
3343 FoundProp->getType())) {
3344 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3345 << Name << D->getType() << FoundProp->getType();
3346 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3347 << FoundProp->getType();
3348 return 0;
3349 }
3350
3351 // FIXME: Check property attributes, getters, setters, etc.?
3352
3353 // Consider these properties to be equivalent.
3354 Importer.Imported(D, FoundProp);
3355 return FoundProp;
3356 }
3357 }
3358
3359 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003360 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3361 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003362 return 0;
3363
3364 // Create the new property.
3365 ObjCPropertyDecl *ToProperty
3366 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3367 Name.getAsIdentifierInfo(),
3368 Importer.Import(D->getAtLoc()),
3369 T,
3370 D->getPropertyImplementation());
3371 Importer.Imported(D, ToProperty);
3372 ToProperty->setLexicalDeclContext(LexicalDC);
3373 LexicalDC->addDecl(ToProperty);
3374
3375 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003376 ToProperty->setPropertyAttributesAsWritten(
3377 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003378 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3379 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3380 ToProperty->setGetterMethodDecl(
3381 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3382 ToProperty->setSetterMethodDecl(
3383 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3384 ToProperty->setPropertyIvarDecl(
3385 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3386 return ToProperty;
3387}
3388
Douglas Gregor954e0c72010-12-07 18:32:03 +00003389Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3390 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3391 Importer.Import(D->getPropertyDecl()));
3392 if (!Property)
3393 return 0;
3394
3395 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3396 if (!DC)
3397 return 0;
3398
3399 // Import the lexical declaration context.
3400 DeclContext *LexicalDC = DC;
3401 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3402 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3403 if (!LexicalDC)
3404 return 0;
3405 }
3406
3407 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3408 if (!InImpl)
3409 return 0;
3410
3411 // Import the ivar (for an @synthesize).
3412 ObjCIvarDecl *Ivar = 0;
3413 if (D->getPropertyIvarDecl()) {
3414 Ivar = cast_or_null<ObjCIvarDecl>(
3415 Importer.Import(D->getPropertyIvarDecl()));
3416 if (!Ivar)
3417 return 0;
3418 }
3419
3420 ObjCPropertyImplDecl *ToImpl
3421 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3422 if (!ToImpl) {
3423 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3424 Importer.Import(D->getLocStart()),
3425 Importer.Import(D->getLocation()),
3426 Property,
3427 D->getPropertyImplementation(),
3428 Ivar,
3429 Importer.Import(D->getPropertyIvarDeclLoc()));
3430 ToImpl->setLexicalDeclContext(LexicalDC);
3431 Importer.Imported(D, ToImpl);
3432 LexicalDC->addDecl(ToImpl);
3433 } else {
3434 // Check that we have the same kind of property implementation (@synthesize
3435 // vs. @dynamic).
3436 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3437 Importer.ToDiag(ToImpl->getLocation(),
3438 diag::err_odr_objc_property_impl_kind_inconsistent)
3439 << Property->getDeclName()
3440 << (ToImpl->getPropertyImplementation()
3441 == ObjCPropertyImplDecl::Dynamic);
3442 Importer.FromDiag(D->getLocation(),
3443 diag::note_odr_objc_property_impl_kind)
3444 << D->getPropertyDecl()->getDeclName()
3445 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3446 return 0;
3447 }
3448
3449 // For @synthesize, check that we have the same
3450 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3451 Ivar != ToImpl->getPropertyIvarDecl()) {
3452 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3453 diag::err_odr_objc_synthesize_ivar_inconsistent)
3454 << Property->getDeclName()
3455 << ToImpl->getPropertyIvarDecl()->getDeclName()
3456 << Ivar->getDeclName();
3457 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3458 diag::note_odr_objc_synthesize_ivar_here)
3459 << D->getPropertyIvarDecl()->getDeclName();
3460 return 0;
3461 }
3462
3463 // Merge the existing implementation with the new implementation.
3464 Importer.Imported(D, ToImpl);
3465 }
3466
3467 return ToImpl;
3468}
3469
Douglas Gregor2b785022010-02-18 02:12:22 +00003470Decl *
3471ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3472 // Import the context of this declaration.
3473 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3474 if (!DC)
3475 return 0;
3476
3477 DeclContext *LexicalDC = DC;
3478 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3479 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3480 if (!LexicalDC)
3481 return 0;
3482 }
3483
3484 // Import the location of this declaration.
3485 SourceLocation Loc = Importer.Import(D->getLocation());
3486
Chris Lattner5f9e2722011-07-23 10:55:15 +00003487 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3488 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003489 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3490 = D->protocol_loc_begin();
3491 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3492 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3493 FromProto != FromProtoEnd;
3494 ++FromProto, ++FromProtoLoc) {
3495 ObjCProtocolDecl *ToProto
3496 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3497 if (!ToProto)
3498 continue;
3499
3500 Protocols.push_back(ToProto);
3501 Locations.push_back(Importer.Import(*FromProtoLoc));
3502 }
3503
3504 ObjCForwardProtocolDecl *ToForward
3505 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3506 Protocols.data(), Protocols.size(),
3507 Locations.data());
3508 ToForward->setLexicalDeclContext(LexicalDC);
3509 LexicalDC->addDecl(ToForward);
3510 Importer.Imported(D, ToForward);
3511 return ToForward;
3512}
3513
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003514Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3515 // Import the context of this declaration.
3516 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3517 if (!DC)
3518 return 0;
3519
3520 DeclContext *LexicalDC = DC;
3521 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3522 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3523 if (!LexicalDC)
3524 return 0;
3525 }
3526
3527 // Import the location of this declaration.
3528 SourceLocation Loc = Importer.Import(D->getLocation());
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003529 ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
3530 ObjCInterfaceDecl *ToIface
3531 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003532 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003533 Loc,
3534 ToIface,
3535 Importer.Import(From->getLocation()));
3536
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003537 ToClass->setLexicalDeclContext(LexicalDC);
3538 LexicalDC->addDecl(ToClass);
3539 Importer.Imported(D, ToClass);
3540 return ToClass;
3541}
3542
Douglas Gregor040afae2010-11-30 19:14:50 +00003543Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3544 // For template arguments, we adopt the translation unit as our declaration
3545 // context. This context will be fixed when the actual template declaration
3546 // is created.
3547
3548 // FIXME: Import default argument.
3549 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3550 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003551 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003552 Importer.Import(D->getLocation()),
3553 D->getDepth(),
3554 D->getIndex(),
3555 Importer.Import(D->getIdentifier()),
3556 D->wasDeclaredWithTypename(),
3557 D->isParameterPack());
3558}
3559
3560Decl *
3561ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3562 // Import the name of this declaration.
3563 DeclarationName Name = Importer.Import(D->getDeclName());
3564 if (D->getDeclName() && !Name)
3565 return 0;
3566
3567 // Import the location of this declaration.
3568 SourceLocation Loc = Importer.Import(D->getLocation());
3569
3570 // Import the type of this declaration.
3571 QualType T = Importer.Import(D->getType());
3572 if (T.isNull())
3573 return 0;
3574
3575 // Import type-source information.
3576 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3577 if (D->getTypeSourceInfo() && !TInfo)
3578 return 0;
3579
3580 // FIXME: Import default argument.
3581
3582 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3583 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003584 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003585 Loc, D->getDepth(), D->getPosition(),
3586 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003587 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003588}
3589
3590Decl *
3591ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3592 // Import the name of this declaration.
3593 DeclarationName Name = Importer.Import(D->getDeclName());
3594 if (D->getDeclName() && !Name)
3595 return 0;
3596
3597 // Import the location of this declaration.
3598 SourceLocation Loc = Importer.Import(D->getLocation());
3599
3600 // Import template parameters.
3601 TemplateParameterList *TemplateParams
3602 = ImportTemplateParameterList(D->getTemplateParameters());
3603 if (!TemplateParams)
3604 return 0;
3605
3606 // FIXME: Import default argument.
3607
3608 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3609 Importer.getToContext().getTranslationUnitDecl(),
3610 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003611 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003612 Name.getAsIdentifierInfo(),
3613 TemplateParams);
3614}
3615
3616Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3617 // If this record has a definition in the translation unit we're coming from,
3618 // but this particular declaration is not that definition, import the
3619 // definition and map to that.
3620 CXXRecordDecl *Definition
3621 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3622 if (Definition && Definition != D->getTemplatedDecl()) {
3623 Decl *ImportedDef
3624 = Importer.Import(Definition->getDescribedClassTemplate());
3625 if (!ImportedDef)
3626 return 0;
3627
3628 return Importer.Imported(D, ImportedDef);
3629 }
3630
3631 // Import the major distinguishing characteristics of this class template.
3632 DeclContext *DC, *LexicalDC;
3633 DeclarationName Name;
3634 SourceLocation Loc;
3635 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3636 return 0;
3637
3638 // We may already have a template of the same name; try to find and match it.
3639 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003640 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor040afae2010-11-30 19:14:50 +00003641 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3642 Lookup.first != Lookup.second;
3643 ++Lookup.first) {
3644 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3645 continue;
3646
3647 Decl *Found = *Lookup.first;
3648 if (ClassTemplateDecl *FoundTemplate
3649 = dyn_cast<ClassTemplateDecl>(Found)) {
3650 if (IsStructuralMatch(D, FoundTemplate)) {
3651 // The class templates structurally match; call it the same template.
3652 // FIXME: We may be filling in a forward declaration here. Handle
3653 // this case!
3654 Importer.Imported(D->getTemplatedDecl(),
3655 FoundTemplate->getTemplatedDecl());
3656 return Importer.Imported(D, FoundTemplate);
3657 }
3658 }
3659
3660 ConflictingDecls.push_back(*Lookup.first);
3661 }
3662
3663 if (!ConflictingDecls.empty()) {
3664 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3665 ConflictingDecls.data(),
3666 ConflictingDecls.size());
3667 }
3668
3669 if (!Name)
3670 return 0;
3671 }
3672
3673 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3674
3675 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003676 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3677 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003678 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3679 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003680 DC, StartLoc, IdLoc,
3681 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003682 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003683 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003684 D2Templated->setLexicalDeclContext(LexicalDC);
3685
3686 // Create the class template declaration itself.
3687 TemplateParameterList *TemplateParams
3688 = ImportTemplateParameterList(D->getTemplateParameters());
3689 if (!TemplateParams)
3690 return 0;
3691
3692 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3693 Loc, Name, TemplateParams,
3694 D2Templated,
3695 /*PrevDecl=*/0);
3696 D2Templated->setDescribedClassTemplate(D2);
3697
3698 D2->setAccess(D->getAccess());
3699 D2->setLexicalDeclContext(LexicalDC);
3700 LexicalDC->addDecl(D2);
3701
3702 // Note the relationship between the class templates.
3703 Importer.Imported(D, D2);
3704 Importer.Imported(DTemplated, D2Templated);
3705
John McCall5e1cdac2011-10-07 06:10:15 +00003706 if (DTemplated->isCompleteDefinition() &&
3707 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003708 // FIXME: Import definition!
3709 }
3710
3711 return D2;
3712}
3713
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003714Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3715 ClassTemplateSpecializationDecl *D) {
3716 // If this record has a definition in the translation unit we're coming from,
3717 // but this particular declaration is not that definition, import the
3718 // definition and map to that.
3719 TagDecl *Definition = D->getDefinition();
3720 if (Definition && Definition != D) {
3721 Decl *ImportedDef = Importer.Import(Definition);
3722 if (!ImportedDef)
3723 return 0;
3724
3725 return Importer.Imported(D, ImportedDef);
3726 }
3727
3728 ClassTemplateDecl *ClassTemplate
3729 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3730 D->getSpecializedTemplate()));
3731 if (!ClassTemplate)
3732 return 0;
3733
3734 // Import the context of this declaration.
3735 DeclContext *DC = ClassTemplate->getDeclContext();
3736 if (!DC)
3737 return 0;
3738
3739 DeclContext *LexicalDC = DC;
3740 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3741 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3742 if (!LexicalDC)
3743 return 0;
3744 }
3745
3746 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003747 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3748 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003749
3750 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003751 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003752 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3753 D->getTemplateArgs().size(),
3754 TemplateArgs))
3755 return 0;
3756
3757 // Try to find an existing specialization with these template arguments.
3758 void *InsertPos = 0;
3759 ClassTemplateSpecializationDecl *D2
3760 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3761 TemplateArgs.size(), InsertPos);
3762 if (D2) {
3763 // We already have a class template specialization with these template
3764 // arguments.
3765
3766 // FIXME: Check for specialization vs. instantiation errors.
3767
3768 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003769 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003770 // The record types structurally match, or the "from" translation
3771 // unit only had a forward declaration anyway; call it the same
3772 // function.
3773 return Importer.Imported(D, FoundDef);
3774 }
3775 }
3776 } else {
3777 // Create a new specialization.
3778 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3779 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003780 StartLoc, IdLoc,
3781 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003782 TemplateArgs.data(),
3783 TemplateArgs.size(),
3784 /*PrevDecl=*/0);
3785 D2->setSpecializationKind(D->getSpecializationKind());
3786
3787 // Add this specialization to the class template.
3788 ClassTemplate->AddSpecialization(D2, InsertPos);
3789
3790 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003791 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003792
3793 // Add the specialization to this context.
3794 D2->setLexicalDeclContext(LexicalDC);
3795 LexicalDC->addDecl(D2);
3796 }
3797 Importer.Imported(D, D2);
3798
John McCall5e1cdac2011-10-07 06:10:15 +00003799 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003800 return 0;
3801
3802 return D2;
3803}
3804
Douglas Gregor4800d952010-02-11 19:21:55 +00003805//----------------------------------------------------------------------------
3806// Import Statements
3807//----------------------------------------------------------------------------
3808
3809Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3810 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3811 << S->getStmtClassName();
3812 return 0;
3813}
3814
3815//----------------------------------------------------------------------------
3816// Import Expressions
3817//----------------------------------------------------------------------------
3818Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3819 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3820 << E->getStmtClassName();
3821 return 0;
3822}
3823
Douglas Gregor44080632010-02-19 01:17:02 +00003824Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003825 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3826 if (!ToD)
3827 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003828
3829 NamedDecl *FoundD = 0;
3830 if (E->getDecl() != E->getFoundDecl()) {
3831 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3832 if (!FoundD)
3833 return 0;
3834 }
Douglas Gregor44080632010-02-19 01:17:02 +00003835
3836 QualType T = Importer.Import(E->getType());
3837 if (T.isNull())
3838 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003839
3840 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3841 Importer.Import(E->getQualifierLoc()),
3842 ToD,
3843 Importer.Import(E->getLocation()),
3844 T, E->getValueKind(),
3845 FoundD,
3846 /*FIXME:TemplateArgs=*/0);
3847 if (E->hadMultipleCandidates())
3848 DRE->setHadMultipleCandidates(true);
3849 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003850}
3851
Douglas Gregor4800d952010-02-11 19:21:55 +00003852Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3853 QualType T = Importer.Import(E->getType());
3854 if (T.isNull())
3855 return 0;
3856
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003857 return IntegerLiteral::Create(Importer.getToContext(),
3858 E->getValue(), T,
3859 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003860}
3861
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003862Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3863 QualType T = Importer.Import(E->getType());
3864 if (T.isNull())
3865 return 0;
3866
Douglas Gregor5cee1192011-07-27 05:40:30 +00003867 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3868 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003869 Importer.Import(E->getLocation()));
3870}
3871
Douglas Gregorf638f952010-02-19 01:07:06 +00003872Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3873 Expr *SubExpr = Importer.Import(E->getSubExpr());
3874 if (!SubExpr)
3875 return 0;
3876
3877 return new (Importer.getToContext())
3878 ParenExpr(Importer.Import(E->getLParen()),
3879 Importer.Import(E->getRParen()),
3880 SubExpr);
3881}
3882
3883Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3884 QualType T = Importer.Import(E->getType());
3885 if (T.isNull())
3886 return 0;
3887
3888 Expr *SubExpr = Importer.Import(E->getSubExpr());
3889 if (!SubExpr)
3890 return 0;
3891
3892 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003893 T, E->getValueKind(),
3894 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003895 Importer.Import(E->getOperatorLoc()));
3896}
3897
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003898Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3899 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003900 QualType ResultType = Importer.Import(E->getType());
3901
3902 if (E->isArgumentType()) {
3903 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3904 if (!TInfo)
3905 return 0;
3906
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003907 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3908 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003909 Importer.Import(E->getOperatorLoc()),
3910 Importer.Import(E->getRParenLoc()));
3911 }
3912
3913 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3914 if (!SubExpr)
3915 return 0;
3916
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003917 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3918 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003919 Importer.Import(E->getOperatorLoc()),
3920 Importer.Import(E->getRParenLoc()));
3921}
3922
Douglas Gregorf638f952010-02-19 01:07:06 +00003923Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3924 QualType T = Importer.Import(E->getType());
3925 if (T.isNull())
3926 return 0;
3927
3928 Expr *LHS = Importer.Import(E->getLHS());
3929 if (!LHS)
3930 return 0;
3931
3932 Expr *RHS = Importer.Import(E->getRHS());
3933 if (!RHS)
3934 return 0;
3935
3936 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003937 T, E->getValueKind(),
3938 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003939 Importer.Import(E->getOperatorLoc()));
3940}
3941
3942Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3943 QualType T = Importer.Import(E->getType());
3944 if (T.isNull())
3945 return 0;
3946
3947 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3948 if (CompLHSType.isNull())
3949 return 0;
3950
3951 QualType CompResultType = Importer.Import(E->getComputationResultType());
3952 if (CompResultType.isNull())
3953 return 0;
3954
3955 Expr *LHS = Importer.Import(E->getLHS());
3956 if (!LHS)
3957 return 0;
3958
3959 Expr *RHS = Importer.Import(E->getRHS());
3960 if (!RHS)
3961 return 0;
3962
3963 return new (Importer.getToContext())
3964 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003965 T, E->getValueKind(),
3966 E->getObjectKind(),
3967 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003968 Importer.Import(E->getOperatorLoc()));
3969}
3970
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003971static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003972 if (E->path_empty()) return false;
3973
3974 // TODO: import cast paths
3975 return true;
3976}
3977
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003978Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3979 QualType T = Importer.Import(E->getType());
3980 if (T.isNull())
3981 return 0;
3982
3983 Expr *SubExpr = Importer.Import(E->getSubExpr());
3984 if (!SubExpr)
3985 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003986
3987 CXXCastPath BasePath;
3988 if (ImportCastPath(E, BasePath))
3989 return 0;
3990
3991 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003992 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003993}
3994
Douglas Gregor008847a2010-02-19 01:32:14 +00003995Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3996 QualType T = Importer.Import(E->getType());
3997 if (T.isNull())
3998 return 0;
3999
4000 Expr *SubExpr = Importer.Import(E->getSubExpr());
4001 if (!SubExpr)
4002 return 0;
4003
4004 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4005 if (!TInfo && E->getTypeInfoAsWritten())
4006 return 0;
4007
John McCallf871d0c2010-08-07 06:22:56 +00004008 CXXCastPath BasePath;
4009 if (ImportCastPath(E, BasePath))
4010 return 0;
4011
John McCallf89e55a2010-11-18 06:31:45 +00004012 return CStyleCastExpr::Create(Importer.getToContext(), T,
4013 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004014 SubExpr, &BasePath, TInfo,
4015 Importer.Import(E->getLParenLoc()),
4016 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004017}
4018
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004019ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004020 ASTContext &FromContext, FileManager &FromFileManager,
4021 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004022 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004023 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4024 Minimal(MinimalImport)
4025{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004026 ImportedDecls[FromContext.getTranslationUnitDecl()]
4027 = ToContext.getTranslationUnitDecl();
4028}
4029
4030ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004031
4032QualType ASTImporter::Import(QualType FromT) {
4033 if (FromT.isNull())
4034 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004035
4036 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004037
Douglas Gregor169fba52010-02-08 15:18:58 +00004038 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004039 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4040 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004041 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004042 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004043
Douglas Gregor169fba52010-02-08 15:18:58 +00004044 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004045 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004046 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004047 if (ToT.isNull())
4048 return ToT;
4049
Douglas Gregor169fba52010-02-08 15:18:58 +00004050 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004051 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004052
John McCallf4c73712011-01-19 06:33:43 +00004053 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004054}
4055
Douglas Gregor9bed8792010-02-09 19:21:46 +00004056TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004057 if (!FromTSI)
4058 return FromTSI;
4059
4060 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004061 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004062 QualType T = Import(FromTSI->getType());
4063 if (T.isNull())
4064 return 0;
4065
4066 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004067 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004068}
4069
4070Decl *ASTImporter::Import(Decl *FromD) {
4071 if (!FromD)
4072 return 0;
4073
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004074 ASTNodeImporter Importer(*this);
4075
Douglas Gregor9bed8792010-02-09 19:21:46 +00004076 // Check whether we've already imported this declaration.
4077 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004078 if (Pos != ImportedDecls.end()) {
4079 Decl *ToD = Pos->second;
4080 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4081 return ToD;
4082 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004083
4084 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004085 Decl *ToD = Importer.Visit(FromD);
4086 if (!ToD)
4087 return 0;
4088
4089 // Record the imported declaration.
4090 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004091
4092 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4093 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004094 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004095 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004096 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004097 // When we've finished transforming a typedef, see whether it was the
4098 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004099 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004100 FromTag = AnonTagsWithPendingTypedefs.begin(),
4101 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4102 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004103 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004104 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4105 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004106 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004107 AnonTagsWithPendingTypedefs.erase(FromTag);
4108 break;
4109 }
4110 }
4111 }
4112 }
4113
Douglas Gregor9bed8792010-02-09 19:21:46 +00004114 return ToD;
4115}
4116
4117DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4118 if (!FromDC)
4119 return FromDC;
4120
4121 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4122}
4123
4124Expr *ASTImporter::Import(Expr *FromE) {
4125 if (!FromE)
4126 return 0;
4127
4128 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4129}
4130
4131Stmt *ASTImporter::Import(Stmt *FromS) {
4132 if (!FromS)
4133 return 0;
4134
Douglas Gregor4800d952010-02-11 19:21:55 +00004135 // Check whether we've already imported this declaration.
4136 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4137 if (Pos != ImportedStmts.end())
4138 return Pos->second;
4139
4140 // Import the type
4141 ASTNodeImporter Importer(*this);
4142 Stmt *ToS = Importer.Visit(FromS);
4143 if (!ToS)
4144 return 0;
4145
4146 // Record the imported declaration.
4147 ImportedStmts[FromS] = ToS;
4148 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004149}
4150
4151NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4152 if (!FromNNS)
4153 return 0;
4154
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004155 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4156
4157 switch (FromNNS->getKind()) {
4158 case NestedNameSpecifier::Identifier:
4159 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4160 return NestedNameSpecifier::Create(ToContext, prefix, II);
4161 }
4162 return 0;
4163
4164 case NestedNameSpecifier::Namespace:
4165 if (NamespaceDecl *NS =
4166 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4167 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4168 }
4169 return 0;
4170
4171 case NestedNameSpecifier::NamespaceAlias:
4172 if (NamespaceAliasDecl *NSAD =
4173 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4174 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4175 }
4176 return 0;
4177
4178 case NestedNameSpecifier::Global:
4179 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4180
4181 case NestedNameSpecifier::TypeSpec:
4182 case NestedNameSpecifier::TypeSpecWithTemplate: {
4183 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4184 if (!T.isNull()) {
4185 bool bTemplate = FromNNS->getKind() ==
4186 NestedNameSpecifier::TypeSpecWithTemplate;
4187 return NestedNameSpecifier::Create(ToContext, prefix,
4188 bTemplate, T.getTypePtr());
4189 }
4190 }
4191 return 0;
4192 }
4193
4194 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004195 return 0;
4196}
4197
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004198NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4199 // FIXME: Implement!
4200 return NestedNameSpecifierLoc();
4201}
4202
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004203TemplateName ASTImporter::Import(TemplateName From) {
4204 switch (From.getKind()) {
4205 case TemplateName::Template:
4206 if (TemplateDecl *ToTemplate
4207 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4208 return TemplateName(ToTemplate);
4209
4210 return TemplateName();
4211
4212 case TemplateName::OverloadedTemplate: {
4213 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4214 UnresolvedSet<2> ToTemplates;
4215 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4216 E = FromStorage->end();
4217 I != E; ++I) {
4218 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4219 ToTemplates.addDecl(To);
4220 else
4221 return TemplateName();
4222 }
4223 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4224 ToTemplates.end());
4225 }
4226
4227 case TemplateName::QualifiedTemplate: {
4228 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4229 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4230 if (!Qualifier)
4231 return TemplateName();
4232
4233 if (TemplateDecl *ToTemplate
4234 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4235 return ToContext.getQualifiedTemplateName(Qualifier,
4236 QTN->hasTemplateKeyword(),
4237 ToTemplate);
4238
4239 return TemplateName();
4240 }
4241
4242 case TemplateName::DependentTemplate: {
4243 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4244 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4245 if (!Qualifier)
4246 return TemplateName();
4247
4248 if (DTN->isIdentifier()) {
4249 return ToContext.getDependentTemplateName(Qualifier,
4250 Import(DTN->getIdentifier()));
4251 }
4252
4253 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4254 }
John McCall14606042011-06-30 08:33:18 +00004255
4256 case TemplateName::SubstTemplateTemplateParm: {
4257 SubstTemplateTemplateParmStorage *subst
4258 = From.getAsSubstTemplateTemplateParm();
4259 TemplateTemplateParmDecl *param
4260 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4261 if (!param)
4262 return TemplateName();
4263
4264 TemplateName replacement = Import(subst->getReplacement());
4265 if (replacement.isNull()) return TemplateName();
4266
4267 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4268 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004269
4270 case TemplateName::SubstTemplateTemplateParmPack: {
4271 SubstTemplateTemplateParmPackStorage *SubstPack
4272 = From.getAsSubstTemplateTemplateParmPack();
4273 TemplateTemplateParmDecl *Param
4274 = cast_or_null<TemplateTemplateParmDecl>(
4275 Import(SubstPack->getParameterPack()));
4276 if (!Param)
4277 return TemplateName();
4278
4279 ASTNodeImporter Importer(*this);
4280 TemplateArgument ArgPack
4281 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4282 if (ArgPack.isNull())
4283 return TemplateName();
4284
4285 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4286 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004287 }
4288
4289 llvm_unreachable("Invalid template name kind");
4290 return TemplateName();
4291}
4292
Douglas Gregor9bed8792010-02-09 19:21:46 +00004293SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4294 if (FromLoc.isInvalid())
4295 return SourceLocation();
4296
Douglas Gregor88523732010-02-10 00:15:17 +00004297 SourceManager &FromSM = FromContext.getSourceManager();
4298
4299 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004300 // don't have to import macro expansions.
4301 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004302 FromLoc = FromSM.getSpellingLoc(FromLoc);
4303 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4304 SourceManager &ToSM = ToContext.getSourceManager();
4305 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004306 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004307}
4308
4309SourceRange ASTImporter::Import(SourceRange FromRange) {
4310 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4311}
4312
Douglas Gregor88523732010-02-10 00:15:17 +00004313FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004314 llvm::DenseMap<FileID, FileID>::iterator Pos
4315 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004316 if (Pos != ImportedFileIDs.end())
4317 return Pos->second;
4318
4319 SourceManager &FromSM = FromContext.getSourceManager();
4320 SourceManager &ToSM = ToContext.getSourceManager();
4321 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004322 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004323
4324 // Include location of this file.
4325 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4326
4327 // Map the FileID for to the "to" source manager.
4328 FileID ToID;
4329 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004330 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004331 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4332 // disk again
4333 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4334 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004335 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004336 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4337 FromSLoc.getFile().getFileCharacteristic());
4338 } else {
4339 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004340 const llvm::MemoryBuffer *
4341 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004342 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004343 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004344 FromBuf->getBufferIdentifier());
4345 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4346 }
4347
4348
Sebastian Redl535a3e22010-09-30 01:03:06 +00004349 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004350 return ToID;
4351}
4352
Douglas Gregord8868a62011-01-18 03:11:38 +00004353void ASTImporter::ImportDefinition(Decl *From) {
4354 Decl *To = Import(From);
4355 if (!To)
4356 return;
4357
4358 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4359 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004360
4361 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4362 if (!ToRecord->getDefinition()) {
4363 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4364 /*ForceImport=*/true);
4365 return;
4366 }
4367 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004368
4369 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4370 if (!ToEnum->getDefinition()) {
4371 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4372 /*ForceImport=*/true);
4373 return;
4374 }
4375 }
4376
Douglas Gregord8868a62011-01-18 03:11:38 +00004377 Importer.ImportDeclContext(FromDC, true);
4378 }
4379}
4380
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004381DeclarationName ASTImporter::Import(DeclarationName FromName) {
4382 if (!FromName)
4383 return DeclarationName();
4384
4385 switch (FromName.getNameKind()) {
4386 case DeclarationName::Identifier:
4387 return Import(FromName.getAsIdentifierInfo());
4388
4389 case DeclarationName::ObjCZeroArgSelector:
4390 case DeclarationName::ObjCOneArgSelector:
4391 case DeclarationName::ObjCMultiArgSelector:
4392 return Import(FromName.getObjCSelector());
4393
4394 case DeclarationName::CXXConstructorName: {
4395 QualType T = Import(FromName.getCXXNameType());
4396 if (T.isNull())
4397 return DeclarationName();
4398
4399 return ToContext.DeclarationNames.getCXXConstructorName(
4400 ToContext.getCanonicalType(T));
4401 }
4402
4403 case DeclarationName::CXXDestructorName: {
4404 QualType T = Import(FromName.getCXXNameType());
4405 if (T.isNull())
4406 return DeclarationName();
4407
4408 return ToContext.DeclarationNames.getCXXDestructorName(
4409 ToContext.getCanonicalType(T));
4410 }
4411
4412 case DeclarationName::CXXConversionFunctionName: {
4413 QualType T = Import(FromName.getCXXNameType());
4414 if (T.isNull())
4415 return DeclarationName();
4416
4417 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4418 ToContext.getCanonicalType(T));
4419 }
4420
4421 case DeclarationName::CXXOperatorName:
4422 return ToContext.DeclarationNames.getCXXOperatorName(
4423 FromName.getCXXOverloadedOperator());
4424
4425 case DeclarationName::CXXLiteralOperatorName:
4426 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4427 Import(FromName.getCXXLiteralIdentifier()));
4428
4429 case DeclarationName::CXXUsingDirective:
4430 // FIXME: STATICS!
4431 return DeclarationName::getUsingDirectiveName();
4432 }
4433
4434 // Silence bogus GCC warning
4435 return DeclarationName();
4436}
4437
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004438IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004439 if (!FromId)
4440 return 0;
4441
4442 return &ToContext.Idents.get(FromId->getName());
4443}
Douglas Gregor089459a2010-02-08 21:09:39 +00004444
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004445Selector ASTImporter::Import(Selector FromSel) {
4446 if (FromSel.isNull())
4447 return Selector();
4448
Chris Lattner5f9e2722011-07-23 10:55:15 +00004449 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004450 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4451 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4452 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4453 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4454}
4455
Douglas Gregor089459a2010-02-08 21:09:39 +00004456DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4457 DeclContext *DC,
4458 unsigned IDNS,
4459 NamedDecl **Decls,
4460 unsigned NumDecls) {
4461 return Name;
4462}
4463
4464DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004465 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004466}
4467
4468DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004469 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004470}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004471
4472Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4473 ImportedDecls[From] = To;
4474 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004475}
Douglas Gregorea35d112010-02-15 23:54:17 +00004476
4477bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004478 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004479 = ImportedTypes.find(From.getTypePtr());
4480 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4481 return true;
4482
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004483 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004484 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004485}