blob: 8904a9561cd2d6d42248d3cf5a865edd1d7d48a9 [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
John McCallf4c73712011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000067 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000068 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000069 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000070 QualType VisitRecordType(const RecordType *T);
71 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000072 // FIXME: TemplateTypeParmType
73 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000074 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
75 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000076 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000077 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000078 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
79 QualType VisitObjCObjectType(const ObjCObjectType *T);
80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000081
82 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000083 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
84 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000085 SourceLocation &Loc);
Abramo Bagnara25777432010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000089 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregor040afae2010-11-30 19:14:50 +000090 TemplateParameterList *ImportTemplateParameterList(
91 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000092 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
93 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
94 unsigned NumFromArgs,
95 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +000096 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000097 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +000098 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000099 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000100 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000101 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000102 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000103 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000104 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000105 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000106 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000107 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000108 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
109 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
110 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
111 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000112 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000113 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000114 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000115 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000116 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000117 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000118 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000119 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000120 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000121 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000122 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000123 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000124 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000125 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000126 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000127 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000128 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
129 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
130 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
131 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000132 Decl *VisitClassTemplateSpecializationDecl(
133 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000134
Douglas Gregor4800d952010-02-11 19:21:55 +0000135 // Importing statements
136 Stmt *VisitStmt(Stmt *S);
137
138 // Importing expressions
139 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000140 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000141 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000142 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000143 Expr *VisitParenExpr(ParenExpr *E);
144 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000145 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000146 Expr *VisitBinaryOperator(BinaryOperator *E);
147 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000148 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000149 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000150 };
151}
152
153//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000154// Structural Equivalence
155//----------------------------------------------------------------------------
156
157namespace {
158 struct StructuralEquivalenceContext {
159 /// \brief AST contexts for which we are checking structural equivalence.
160 ASTContext &C1, &C2;
161
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000162 /// \brief The set of "tentative" equivalences between two canonical
163 /// declarations, mapping from a declaration in the first context to the
164 /// declaration in the second context that we believe to be equivalent.
165 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
166
167 /// \brief Queue of declarations in the first context whose equivalence
168 /// with a declaration in the second context still needs to be verified.
169 std::deque<Decl *> DeclsToCheck;
170
Douglas Gregorea35d112010-02-15 23:54:17 +0000171 /// \brief Declaration (from, to) pairs that are known not to be equivalent
172 /// (which we have already complained about).
173 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
174
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000175 /// \brief Whether we're being strict about the spelling of types when
176 /// unifying two types.
177 bool StrictTypeSpelling;
178
179 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000180 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000181 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000182 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000183 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000184
185 /// \brief Determine whether the two declarations are structurally
186 /// equivalent.
187 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
188
189 /// \brief Determine whether the two types are structurally equivalent.
190 bool IsStructurallyEquivalent(QualType T1, QualType T2);
191
192 private:
193 /// \brief Finish checking all of the structural equivalences.
194 ///
195 /// \returns true if an error occurred, false otherwise.
196 bool Finish();
197
198 public:
199 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000200 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000201 }
202
203 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000204 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000205 }
206 };
207}
208
209static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
210 QualType T1, QualType T2);
211static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
212 Decl *D1, Decl *D2);
213
214/// \brief Determine if two APInts have the same value, after zero-extending
215/// one of them (if needed!) to ensure that the bit-widths match.
216static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
217 if (I1.getBitWidth() == I2.getBitWidth())
218 return I1 == I2;
219
220 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000221 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000222
Jay Foad9f71a8f2010-12-07 08:25:34 +0000223 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000224}
225
226/// \brief Determine if two APSInts have the same value, zero- or sign-extending
227/// as needed.
228static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
229 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
230 return I1 == I2;
231
232 // Check for a bit-width mismatch.
233 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000234 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000235 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000236 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000237
238 // We have a signedness mismatch. Turn the signed value into an unsigned
239 // value.
240 if (I1.isSigned()) {
241 if (I1.isNegative())
242 return false;
243
244 return llvm::APSInt(I1, true) == I2;
245 }
246
247 if (I2.isNegative())
248 return false;
249
250 return I1 == llvm::APSInt(I2, true);
251}
252
253/// \brief Determine structural equivalence of two expressions.
254static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
255 Expr *E1, Expr *E2) {
256 if (!E1 || !E2)
257 return E1 == E2;
258
259 // FIXME: Actually perform a structural comparison!
260 return true;
261}
262
263/// \brief Determine whether two identifiers are equivalent.
264static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
265 const IdentifierInfo *Name2) {
266 if (!Name1 || !Name2)
267 return Name1 == Name2;
268
269 return Name1->getName() == Name2->getName();
270}
271
272/// \brief Determine whether two nested-name-specifiers are equivalent.
273static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
274 NestedNameSpecifier *NNS1,
275 NestedNameSpecifier *NNS2) {
276 // FIXME: Implement!
277 return true;
278}
279
280/// \brief Determine whether two template arguments are equivalent.
281static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
282 const TemplateArgument &Arg1,
283 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000284 if (Arg1.getKind() != Arg2.getKind())
285 return false;
286
287 switch (Arg1.getKind()) {
288 case TemplateArgument::Null:
289 return true;
290
291 case TemplateArgument::Type:
292 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
293
294 case TemplateArgument::Integral:
295 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
296 Arg2.getIntegralType()))
297 return false;
298
299 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
300
301 case TemplateArgument::Declaration:
302 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
303
304 case TemplateArgument::Template:
305 return IsStructurallyEquivalent(Context,
306 Arg1.getAsTemplate(),
307 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000308
309 case TemplateArgument::TemplateExpansion:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplateOrTemplatePattern(),
312 Arg2.getAsTemplateOrTemplatePattern());
313
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000314 case TemplateArgument::Expression:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsExpr(), Arg2.getAsExpr());
317
318 case TemplateArgument::Pack:
319 if (Arg1.pack_size() != Arg2.pack_size())
320 return false;
321
322 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
323 if (!IsStructurallyEquivalent(Context,
324 Arg1.pack_begin()[I],
325 Arg2.pack_begin()[I]))
326 return false;
327
328 return true;
329 }
330
331 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000332 return true;
333}
334
335/// \brief Determine structural equivalence for the common part of array
336/// types.
337static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
338 const ArrayType *Array1,
339 const ArrayType *Array2) {
340 if (!IsStructurallyEquivalent(Context,
341 Array1->getElementType(),
342 Array2->getElementType()))
343 return false;
344 if (Array1->getSizeModifier() != Array2->getSizeModifier())
345 return false;
346 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
347 return false;
348
349 return true;
350}
351
352/// \brief Determine structural equivalence of two types.
353static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
354 QualType T1, QualType T2) {
355 if (T1.isNull() || T2.isNull())
356 return T1.isNull() && T2.isNull();
357
358 if (!Context.StrictTypeSpelling) {
359 // We aren't being strict about token-to-token equivalence of types,
360 // so map down to the canonical type.
361 T1 = Context.C1.getCanonicalType(T1);
362 T2 = Context.C2.getCanonicalType(T2);
363 }
364
365 if (T1.getQualifiers() != T2.getQualifiers())
366 return false;
367
Douglas Gregorea35d112010-02-15 23:54:17 +0000368 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000369
Douglas Gregorea35d112010-02-15 23:54:17 +0000370 if (T1->getTypeClass() != T2->getTypeClass()) {
371 // Compare function types with prototypes vs. without prototypes as if
372 // both did not have prototypes.
373 if (T1->getTypeClass() == Type::FunctionProto &&
374 T2->getTypeClass() == Type::FunctionNoProto)
375 TC = Type::FunctionNoProto;
376 else if (T1->getTypeClass() == Type::FunctionNoProto &&
377 T2->getTypeClass() == Type::FunctionProto)
378 TC = Type::FunctionNoProto;
379 else
380 return false;
381 }
382
383 switch (TC) {
384 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000385 // FIXME: Deal with Char_S/Char_U.
386 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
387 return false;
388 break;
389
390 case Type::Complex:
391 if (!IsStructurallyEquivalent(Context,
392 cast<ComplexType>(T1)->getElementType(),
393 cast<ComplexType>(T2)->getElementType()))
394 return false;
395 break;
396
397 case Type::Pointer:
398 if (!IsStructurallyEquivalent(Context,
399 cast<PointerType>(T1)->getPointeeType(),
400 cast<PointerType>(T2)->getPointeeType()))
401 return false;
402 break;
403
404 case Type::BlockPointer:
405 if (!IsStructurallyEquivalent(Context,
406 cast<BlockPointerType>(T1)->getPointeeType(),
407 cast<BlockPointerType>(T2)->getPointeeType()))
408 return false;
409 break;
410
411 case Type::LValueReference:
412 case Type::RValueReference: {
413 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
414 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
415 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
416 return false;
417 if (Ref1->isInnerRef() != Ref2->isInnerRef())
418 return false;
419 if (!IsStructurallyEquivalent(Context,
420 Ref1->getPointeeTypeAsWritten(),
421 Ref2->getPointeeTypeAsWritten()))
422 return false;
423 break;
424 }
425
426 case Type::MemberPointer: {
427 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
428 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
429 if (!IsStructurallyEquivalent(Context,
430 MemPtr1->getPointeeType(),
431 MemPtr2->getPointeeType()))
432 return false;
433 if (!IsStructurallyEquivalent(Context,
434 QualType(MemPtr1->getClass(), 0),
435 QualType(MemPtr2->getClass(), 0)))
436 return false;
437 break;
438 }
439
440 case Type::ConstantArray: {
441 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
442 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
443 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
444 return false;
445
446 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
447 return false;
448 break;
449 }
450
451 case Type::IncompleteArray:
452 if (!IsArrayStructurallyEquivalent(Context,
453 cast<ArrayType>(T1),
454 cast<ArrayType>(T2)))
455 return false;
456 break;
457
458 case Type::VariableArray: {
459 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
460 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
461 if (!IsStructurallyEquivalent(Context,
462 Array1->getSizeExpr(), Array2->getSizeExpr()))
463 return false;
464
465 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
466 return false;
467
468 break;
469 }
470
471 case Type::DependentSizedArray: {
472 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
473 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
474 if (!IsStructurallyEquivalent(Context,
475 Array1->getSizeExpr(), Array2->getSizeExpr()))
476 return false;
477
478 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
479 return false;
480
481 break;
482 }
483
484 case Type::DependentSizedExtVector: {
485 const DependentSizedExtVectorType *Vec1
486 = cast<DependentSizedExtVectorType>(T1);
487 const DependentSizedExtVectorType *Vec2
488 = cast<DependentSizedExtVectorType>(T2);
489 if (!IsStructurallyEquivalent(Context,
490 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
491 return false;
492 if (!IsStructurallyEquivalent(Context,
493 Vec1->getElementType(),
494 Vec2->getElementType()))
495 return false;
496 break;
497 }
498
499 case Type::Vector:
500 case Type::ExtVector: {
501 const VectorType *Vec1 = cast<VectorType>(T1);
502 const VectorType *Vec2 = cast<VectorType>(T2);
503 if (!IsStructurallyEquivalent(Context,
504 Vec1->getElementType(),
505 Vec2->getElementType()))
506 return false;
507 if (Vec1->getNumElements() != Vec2->getNumElements())
508 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000509 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000510 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000511 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000512 }
513
514 case Type::FunctionProto: {
515 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
516 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
517 if (Proto1->getNumArgs() != Proto2->getNumArgs())
518 return false;
519 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
520 if (!IsStructurallyEquivalent(Context,
521 Proto1->getArgType(I),
522 Proto2->getArgType(I)))
523 return false;
524 }
525 if (Proto1->isVariadic() != Proto2->isVariadic())
526 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000527 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000528 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000529 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
530 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
531 return false;
532 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
533 if (!IsStructurallyEquivalent(Context,
534 Proto1->getExceptionType(I),
535 Proto2->getExceptionType(I)))
536 return false;
537 }
538 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000539 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000540 Proto1->getNoexceptExpr(),
541 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000542 return false;
543 }
544 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
545 return false;
546
547 // Fall through to check the bits common with FunctionNoProtoType.
548 }
549
550 case Type::FunctionNoProto: {
551 const FunctionType *Function1 = cast<FunctionType>(T1);
552 const FunctionType *Function2 = cast<FunctionType>(T2);
553 if (!IsStructurallyEquivalent(Context,
554 Function1->getResultType(),
555 Function2->getResultType()))
556 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000557 if (Function1->getExtInfo() != Function2->getExtInfo())
558 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000559 break;
560 }
561
562 case Type::UnresolvedUsing:
563 if (!IsStructurallyEquivalent(Context,
564 cast<UnresolvedUsingType>(T1)->getDecl(),
565 cast<UnresolvedUsingType>(T2)->getDecl()))
566 return false;
567
568 break;
John McCall9d156a72011-01-06 01:58:22 +0000569
570 case Type::Attributed:
571 if (!IsStructurallyEquivalent(Context,
572 cast<AttributedType>(T1)->getModifiedType(),
573 cast<AttributedType>(T2)->getModifiedType()))
574 return false;
575 if (!IsStructurallyEquivalent(Context,
576 cast<AttributedType>(T1)->getEquivalentType(),
577 cast<AttributedType>(T2)->getEquivalentType()))
578 return false;
579 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000580
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000581 case Type::Paren:
582 if (!IsStructurallyEquivalent(Context,
583 cast<ParenType>(T1)->getInnerType(),
584 cast<ParenType>(T2)->getInnerType()))
585 return false;
586 break;
587
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000588 case Type::Typedef:
589 if (!IsStructurallyEquivalent(Context,
590 cast<TypedefType>(T1)->getDecl(),
591 cast<TypedefType>(T2)->getDecl()))
592 return false;
593 break;
594
595 case Type::TypeOfExpr:
596 if (!IsStructurallyEquivalent(Context,
597 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
598 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
599 return false;
600 break;
601
602 case Type::TypeOf:
603 if (!IsStructurallyEquivalent(Context,
604 cast<TypeOfType>(T1)->getUnderlyingType(),
605 cast<TypeOfType>(T2)->getUnderlyingType()))
606 return false;
607 break;
Sean Huntca63c202011-05-24 22:41:36 +0000608
609 case Type::UnaryTransform:
610 if (!IsStructurallyEquivalent(Context,
611 cast<UnaryTransformType>(T1)->getUnderlyingType(),
612 cast<UnaryTransformType>(T1)->getUnderlyingType()))
613 return false;
614 break;
615
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000616 case Type::Decltype:
617 if (!IsStructurallyEquivalent(Context,
618 cast<DecltypeType>(T1)->getUnderlyingExpr(),
619 cast<DecltypeType>(T2)->getUnderlyingExpr()))
620 return false;
621 break;
622
Richard Smith34b41d92011-02-20 03:19:35 +0000623 case Type::Auto:
624 if (!IsStructurallyEquivalent(Context,
625 cast<AutoType>(T1)->getDeducedType(),
626 cast<AutoType>(T2)->getDeducedType()))
627 return false;
628 break;
629
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000630 case Type::Record:
631 case Type::Enum:
632 if (!IsStructurallyEquivalent(Context,
633 cast<TagType>(T1)->getDecl(),
634 cast<TagType>(T2)->getDecl()))
635 return false;
636 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000637
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000638 case Type::TemplateTypeParm: {
639 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
640 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
641 if (Parm1->getDepth() != Parm2->getDepth())
642 return false;
643 if (Parm1->getIndex() != Parm2->getIndex())
644 return false;
645 if (Parm1->isParameterPack() != Parm2->isParameterPack())
646 return false;
647
648 // Names of template type parameters are never significant.
649 break;
650 }
651
652 case Type::SubstTemplateTypeParm: {
653 const SubstTemplateTypeParmType *Subst1
654 = cast<SubstTemplateTypeParmType>(T1);
655 const SubstTemplateTypeParmType *Subst2
656 = cast<SubstTemplateTypeParmType>(T2);
657 if (!IsStructurallyEquivalent(Context,
658 QualType(Subst1->getReplacedParameter(), 0),
659 QualType(Subst2->getReplacedParameter(), 0)))
660 return false;
661 if (!IsStructurallyEquivalent(Context,
662 Subst1->getReplacementType(),
663 Subst2->getReplacementType()))
664 return false;
665 break;
666 }
667
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000668 case Type::SubstTemplateTypeParmPack: {
669 const SubstTemplateTypeParmPackType *Subst1
670 = cast<SubstTemplateTypeParmPackType>(T1);
671 const SubstTemplateTypeParmPackType *Subst2
672 = cast<SubstTemplateTypeParmPackType>(T2);
673 if (!IsStructurallyEquivalent(Context,
674 QualType(Subst1->getReplacedParameter(), 0),
675 QualType(Subst2->getReplacedParameter(), 0)))
676 return false;
677 if (!IsStructurallyEquivalent(Context,
678 Subst1->getArgumentPack(),
679 Subst2->getArgumentPack()))
680 return false;
681 break;
682 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000683 case Type::TemplateSpecialization: {
684 const TemplateSpecializationType *Spec1
685 = cast<TemplateSpecializationType>(T1);
686 const TemplateSpecializationType *Spec2
687 = cast<TemplateSpecializationType>(T2);
688 if (!IsStructurallyEquivalent(Context,
689 Spec1->getTemplateName(),
690 Spec2->getTemplateName()))
691 return false;
692 if (Spec1->getNumArgs() != Spec2->getNumArgs())
693 return false;
694 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
695 if (!IsStructurallyEquivalent(Context,
696 Spec1->getArg(I), Spec2->getArg(I)))
697 return false;
698 }
699 break;
700 }
701
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000702 case Type::Elaborated: {
703 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
704 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
705 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
706 if (Elab1->getKeyword() != Elab2->getKeyword())
707 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000708 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000709 Elab1->getQualifier(),
710 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000711 return false;
712 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000713 Elab1->getNamedType(),
714 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000715 return false;
716 break;
717 }
718
John McCall3cb0ebd2010-03-10 03:28:59 +0000719 case Type::InjectedClassName: {
720 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
721 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
722 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000723 Inj1->getInjectedSpecializationType(),
724 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000725 return false;
726 break;
727 }
728
Douglas Gregor4714c122010-03-31 17:34:00 +0000729 case Type::DependentName: {
730 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
731 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000732 if (!IsStructurallyEquivalent(Context,
733 Typename1->getQualifier(),
734 Typename2->getQualifier()))
735 return false;
736 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
737 Typename2->getIdentifier()))
738 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000739
740 break;
741 }
742
John McCall33500952010-06-11 00:33:02 +0000743 case Type::DependentTemplateSpecialization: {
744 const DependentTemplateSpecializationType *Spec1 =
745 cast<DependentTemplateSpecializationType>(T1);
746 const DependentTemplateSpecializationType *Spec2 =
747 cast<DependentTemplateSpecializationType>(T2);
748 if (!IsStructurallyEquivalent(Context,
749 Spec1->getQualifier(),
750 Spec2->getQualifier()))
751 return false;
752 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
753 Spec2->getIdentifier()))
754 return false;
755 if (Spec1->getNumArgs() != Spec2->getNumArgs())
756 return false;
757 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
758 if (!IsStructurallyEquivalent(Context,
759 Spec1->getArg(I), Spec2->getArg(I)))
760 return false;
761 }
762 break;
763 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000764
765 case Type::PackExpansion:
766 if (!IsStructurallyEquivalent(Context,
767 cast<PackExpansionType>(T1)->getPattern(),
768 cast<PackExpansionType>(T2)->getPattern()))
769 return false;
770 break;
771
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000772 case Type::ObjCInterface: {
773 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
774 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
775 if (!IsStructurallyEquivalent(Context,
776 Iface1->getDecl(), Iface2->getDecl()))
777 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000778 break;
779 }
780
781 case Type::ObjCObject: {
782 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
783 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
784 if (!IsStructurallyEquivalent(Context,
785 Obj1->getBaseType(),
786 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000787 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000788 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
789 return false;
790 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000791 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000792 Obj1->getProtocol(I),
793 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000794 return false;
795 }
796 break;
797 }
798
799 case Type::ObjCObjectPointer: {
800 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
801 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
802 if (!IsStructurallyEquivalent(Context,
803 Ptr1->getPointeeType(),
804 Ptr2->getPointeeType()))
805 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000806 break;
807 }
808
809 } // end switch
810
811 return true;
812}
813
814/// \brief Determine structural equivalence of two records.
815static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
816 RecordDecl *D1, RecordDecl *D2) {
817 if (D1->isUnion() != D2->isUnion()) {
818 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
819 << Context.C2.getTypeDeclType(D2);
820 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
821 << D1->getDeclName() << (unsigned)D1->getTagKind();
822 return false;
823 }
824
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000825 // If both declarations are class template specializations, we know
826 // the ODR applies, so check the template and template arguments.
827 ClassTemplateSpecializationDecl *Spec1
828 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
829 ClassTemplateSpecializationDecl *Spec2
830 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
831 if (Spec1 && Spec2) {
832 // Check that the specialized templates are the same.
833 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
834 Spec2->getSpecializedTemplate()))
835 return false;
836
837 // Check that the template arguments are the same.
838 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
839 return false;
840
841 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
842 if (!IsStructurallyEquivalent(Context,
843 Spec1->getTemplateArgs().get(I),
844 Spec2->getTemplateArgs().get(I)))
845 return false;
846 }
847 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000848 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000849 else if (Spec1 || Spec2)
850 return false;
851
Douglas Gregorea35d112010-02-15 23:54:17 +0000852 // Compare the definitions of these two records. If either or both are
853 // incomplete, we assume that they are equivalent.
854 D1 = D1->getDefinition();
855 D2 = D2->getDefinition();
856 if (!D1 || !D2)
857 return true;
858
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000859 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
860 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
861 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
862 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000863 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000864 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000865 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000866 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000867 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000868 return false;
869 }
870
871 // Check the base classes.
872 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
873 BaseEnd1 = D1CXX->bases_end(),
874 Base2 = D2CXX->bases_begin();
875 Base1 != BaseEnd1;
876 ++Base1, ++Base2) {
877 if (!IsStructurallyEquivalent(Context,
878 Base1->getType(), Base2->getType())) {
879 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
880 << Context.C2.getTypeDeclType(D2);
881 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
882 << Base2->getType()
883 << Base2->getSourceRange();
884 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
885 << Base1->getType()
886 << Base1->getSourceRange();
887 return false;
888 }
889
890 // Check virtual vs. non-virtual inheritance mismatch.
891 if (Base1->isVirtual() != Base2->isVirtual()) {
892 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
893 << Context.C2.getTypeDeclType(D2);
894 Context.Diag2(Base2->getSourceRange().getBegin(),
895 diag::note_odr_virtual_base)
896 << Base2->isVirtual() << Base2->getSourceRange();
897 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
898 << Base1->isVirtual()
899 << Base1->getSourceRange();
900 return false;
901 }
902 }
903 } else if (D1CXX->getNumBases() > 0) {
904 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
905 << Context.C2.getTypeDeclType(D2);
906 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
907 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
908 << Base1->getType()
909 << Base1->getSourceRange();
910 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
911 return false;
912 }
913 }
914
915 // Check the fields for consistency.
916 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
917 Field2End = D2->field_end();
918 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
919 Field1End = D1->field_end();
920 Field1 != Field1End;
921 ++Field1, ++Field2) {
922 if (Field2 == Field2End) {
923 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
924 << Context.C2.getTypeDeclType(D2);
925 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
926 << Field1->getDeclName() << Field1->getType();
927 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
928 return false;
929 }
930
931 if (!IsStructurallyEquivalent(Context,
932 Field1->getType(), Field2->getType())) {
933 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
934 << Context.C2.getTypeDeclType(D2);
935 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
936 << Field2->getDeclName() << Field2->getType();
937 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
938 << Field1->getDeclName() << Field1->getType();
939 return false;
940 }
941
942 if (Field1->isBitField() != Field2->isBitField()) {
943 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
944 << Context.C2.getTypeDeclType(D2);
945 if (Field1->isBitField()) {
946 llvm::APSInt Bits;
947 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
948 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
949 << Field1->getDeclName() << Field1->getType()
950 << Bits.toString(10, false);
951 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
952 << Field2->getDeclName();
953 } else {
954 llvm::APSInt Bits;
955 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
956 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
957 << Field2->getDeclName() << Field2->getType()
958 << Bits.toString(10, false);
959 Context.Diag1(Field1->getLocation(),
960 diag::note_odr_not_bit_field)
961 << Field1->getDeclName();
962 }
963 return false;
964 }
965
966 if (Field1->isBitField()) {
967 // Make sure that the bit-fields are the same length.
968 llvm::APSInt Bits1, Bits2;
969 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
970 return false;
971 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
972 return false;
973
974 if (!IsSameValue(Bits1, Bits2)) {
975 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
976 << Context.C2.getTypeDeclType(D2);
977 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
978 << Field2->getDeclName() << Field2->getType()
979 << Bits2.toString(10, false);
980 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
981 << Field1->getDeclName() << Field1->getType()
982 << Bits1.toString(10, false);
983 return false;
984 }
985 }
986 }
987
988 if (Field2 != Field2End) {
989 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
990 << Context.C2.getTypeDeclType(D2);
991 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
992 << Field2->getDeclName() << Field2->getType();
993 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
994 return false;
995 }
996
997 return true;
998}
999
1000/// \brief Determine structural equivalence of two enums.
1001static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1002 EnumDecl *D1, EnumDecl *D2) {
1003 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1004 EC2End = D2->enumerator_end();
1005 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1006 EC1End = D1->enumerator_end();
1007 EC1 != EC1End; ++EC1, ++EC2) {
1008 if (EC2 == EC2End) {
1009 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1010 << Context.C2.getTypeDeclType(D2);
1011 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1012 << EC1->getDeclName()
1013 << EC1->getInitVal().toString(10);
1014 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1015 return false;
1016 }
1017
1018 llvm::APSInt Val1 = EC1->getInitVal();
1019 llvm::APSInt Val2 = EC2->getInitVal();
1020 if (!IsSameValue(Val1, Val2) ||
1021 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1022 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1023 << Context.C2.getTypeDeclType(D2);
1024 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1025 << EC2->getDeclName()
1026 << EC2->getInitVal().toString(10);
1027 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1028 << EC1->getDeclName()
1029 << EC1->getInitVal().toString(10);
1030 return false;
1031 }
1032 }
1033
1034 if (EC2 != EC2End) {
1035 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1036 << Context.C2.getTypeDeclType(D2);
1037 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1038 << EC2->getDeclName()
1039 << EC2->getInitVal().toString(10);
1040 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1041 return false;
1042 }
1043
1044 return true;
1045}
Douglas Gregor040afae2010-11-30 19:14:50 +00001046
1047static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1048 TemplateParameterList *Params1,
1049 TemplateParameterList *Params2) {
1050 if (Params1->size() != Params2->size()) {
1051 Context.Diag2(Params2->getTemplateLoc(),
1052 diag::err_odr_different_num_template_parameters)
1053 << Params1->size() << Params2->size();
1054 Context.Diag1(Params1->getTemplateLoc(),
1055 diag::note_odr_template_parameter_list);
1056 return false;
1057 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001058
Douglas Gregor040afae2010-11-30 19:14:50 +00001059 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1060 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1061 Context.Diag2(Params2->getParam(I)->getLocation(),
1062 diag::err_odr_different_template_parameter_kind);
1063 Context.Diag1(Params1->getParam(I)->getLocation(),
1064 diag::note_odr_template_parameter_here);
1065 return false;
1066 }
1067
1068 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1069 Params2->getParam(I))) {
1070
1071 return false;
1072 }
1073 }
1074
1075 return true;
1076}
1077
1078static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1079 TemplateTypeParmDecl *D1,
1080 TemplateTypeParmDecl *D2) {
1081 if (D1->isParameterPack() != D2->isParameterPack()) {
1082 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1083 << D2->isParameterPack();
1084 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1085 << D1->isParameterPack();
1086 return false;
1087 }
1088
1089 return true;
1090}
1091
1092static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1093 NonTypeTemplateParmDecl *D1,
1094 NonTypeTemplateParmDecl *D2) {
1095 // FIXME: Enable once we have variadic templates.
1096#if 0
1097 if (D1->isParameterPack() != D2->isParameterPack()) {
1098 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1099 << D2->isParameterPack();
1100 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1101 << D1->isParameterPack();
1102 return false;
1103 }
1104#endif
1105
1106 // Check types.
1107 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1108 Context.Diag2(D2->getLocation(),
1109 diag::err_odr_non_type_parameter_type_inconsistent)
1110 << D2->getType() << D1->getType();
1111 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1112 << D1->getType();
1113 return false;
1114 }
1115
1116 return true;
1117}
1118
1119static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1120 TemplateTemplateParmDecl *D1,
1121 TemplateTemplateParmDecl *D2) {
1122 // FIXME: Enable once we have variadic templates.
1123#if 0
1124 if (D1->isParameterPack() != D2->isParameterPack()) {
1125 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1126 << D2->isParameterPack();
1127 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1128 << D1->isParameterPack();
1129 return false;
1130 }
1131#endif
1132
1133 // Check template parameter lists.
1134 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1135 D2->getTemplateParameters());
1136}
1137
1138static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1139 ClassTemplateDecl *D1,
1140 ClassTemplateDecl *D2) {
1141 // Check template parameters.
1142 if (!IsStructurallyEquivalent(Context,
1143 D1->getTemplateParameters(),
1144 D2->getTemplateParameters()))
1145 return false;
1146
1147 // Check the templated declaration.
1148 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1149 D2->getTemplatedDecl());
1150}
1151
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001152/// \brief Determine structural equivalence of two declarations.
1153static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1154 Decl *D1, Decl *D2) {
1155 // FIXME: Check for known structural equivalences via a callback of some sort.
1156
Douglas Gregorea35d112010-02-15 23:54:17 +00001157 // Check whether we already know that these two declarations are not
1158 // structurally equivalent.
1159 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1160 D2->getCanonicalDecl())))
1161 return false;
1162
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001163 // Determine whether we've already produced a tentative equivalence for D1.
1164 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1165 if (EquivToD1)
1166 return EquivToD1 == D2->getCanonicalDecl();
1167
1168 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1169 EquivToD1 = D2->getCanonicalDecl();
1170 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1171 return true;
1172}
1173
1174bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1175 Decl *D2) {
1176 if (!::IsStructurallyEquivalent(*this, D1, D2))
1177 return false;
1178
1179 return !Finish();
1180}
1181
1182bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1183 QualType T2) {
1184 if (!::IsStructurallyEquivalent(*this, T1, T2))
1185 return false;
1186
1187 return !Finish();
1188}
1189
1190bool StructuralEquivalenceContext::Finish() {
1191 while (!DeclsToCheck.empty()) {
1192 // Check the next declaration.
1193 Decl *D1 = DeclsToCheck.front();
1194 DeclsToCheck.pop_front();
1195
1196 Decl *D2 = TentativeEquivalences[D1];
1197 assert(D2 && "Unrecorded tentative equivalence?");
1198
Douglas Gregorea35d112010-02-15 23:54:17 +00001199 bool Equivalent = true;
1200
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001201 // FIXME: Switch on all declaration kinds. For now, we're just going to
1202 // check the obvious ones.
1203 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1204 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1205 // Check for equivalent structure names.
1206 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001207 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1208 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001209 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001210 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1211 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001212 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1213 !::IsStructurallyEquivalent(*this, Record1, Record2))
1214 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001215 } else {
1216 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001217 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001218 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001219 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001220 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1221 // Check for equivalent enum names.
1222 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001223 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1224 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001225 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001226 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1227 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001228 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1229 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1230 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001231 } else {
1232 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001233 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001234 }
Richard Smith162e1c12011-04-15 14:24:37 +00001235 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1236 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001237 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001238 Typedef2->getIdentifier()) ||
1239 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001240 Typedef1->getUnderlyingType(),
1241 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001242 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001243 } else {
1244 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001245 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001246 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001247 } else if (ClassTemplateDecl *ClassTemplate1
1248 = dyn_cast<ClassTemplateDecl>(D1)) {
1249 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1250 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1251 ClassTemplate2->getIdentifier()) ||
1252 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1253 Equivalent = false;
1254 } else {
1255 // Class template/non-class-template mismatch.
1256 Equivalent = false;
1257 }
1258 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1259 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1260 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1261 Equivalent = false;
1262 } else {
1263 // Kind mismatch.
1264 Equivalent = false;
1265 }
1266 } else if (NonTypeTemplateParmDecl *NTTP1
1267 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1268 if (NonTypeTemplateParmDecl *NTTP2
1269 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1270 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1271 Equivalent = false;
1272 } else {
1273 // Kind mismatch.
1274 Equivalent = false;
1275 }
1276 } else if (TemplateTemplateParmDecl *TTP1
1277 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1278 if (TemplateTemplateParmDecl *TTP2
1279 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1280 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1281 Equivalent = false;
1282 } else {
1283 // Kind mismatch.
1284 Equivalent = false;
1285 }
1286 }
1287
Douglas Gregorea35d112010-02-15 23:54:17 +00001288 if (!Equivalent) {
1289 // Note that these two declarations are not equivalent (and we already
1290 // know about it).
1291 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1292 D2->getCanonicalDecl()));
1293 return true;
1294 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001295 // FIXME: Check other declaration kinds!
1296 }
1297
1298 return false;
1299}
1300
1301//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001302// Import Types
1303//----------------------------------------------------------------------------
1304
John McCallf4c73712011-01-19 06:33:43 +00001305QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001306 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1307 << T->getTypeClassName();
1308 return QualType();
1309}
1310
John McCallf4c73712011-01-19 06:33:43 +00001311QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001312 switch (T->getKind()) {
1313 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1314 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1315
1316 case BuiltinType::Char_U:
1317 // The context we're importing from has an unsigned 'char'. If we're
1318 // importing into a context with a signed 'char', translate to
1319 // 'unsigned char' instead.
1320 if (Importer.getToContext().getLangOptions().CharIsSigned)
1321 return Importer.getToContext().UnsignedCharTy;
1322
1323 return Importer.getToContext().CharTy;
1324
1325 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1326
1327 case BuiltinType::Char16:
1328 // FIXME: Make sure that the "to" context supports C++!
1329 return Importer.getToContext().Char16Ty;
1330
1331 case BuiltinType::Char32:
1332 // FIXME: Make sure that the "to" context supports C++!
1333 return Importer.getToContext().Char32Ty;
1334
1335 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1336 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1337 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1338 case BuiltinType::ULongLong:
1339 return Importer.getToContext().UnsignedLongLongTy;
1340 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1341
1342 case BuiltinType::Char_S:
1343 // The context we're importing from has an unsigned 'char'. If we're
1344 // importing into a context with a signed 'char', translate to
1345 // 'unsigned char' instead.
1346 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1347 return Importer.getToContext().SignedCharTy;
1348
1349 return Importer.getToContext().CharTy;
1350
1351 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001352 case BuiltinType::WChar_S:
1353 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001354 // FIXME: If not in C++, shall we translate to the C equivalent of
1355 // wchar_t?
1356 return Importer.getToContext().WCharTy;
1357
1358 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1359 case BuiltinType::Int : return Importer.getToContext().IntTy;
1360 case BuiltinType::Long : return Importer.getToContext().LongTy;
1361 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1362 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1363 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1364 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1365 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1366
1367 case BuiltinType::NullPtr:
1368 // FIXME: Make sure that the "to" context supports C++0x!
1369 return Importer.getToContext().NullPtrTy;
1370
1371 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1372 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001373 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001374 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001375
1376 case BuiltinType::ObjCId:
1377 // FIXME: Make sure that the "to" context supports Objective-C!
1378 return Importer.getToContext().ObjCBuiltinIdTy;
1379
1380 case BuiltinType::ObjCClass:
1381 return Importer.getToContext().ObjCBuiltinClassTy;
1382
1383 case BuiltinType::ObjCSel:
1384 return Importer.getToContext().ObjCBuiltinSelTy;
1385 }
1386
1387 return QualType();
1388}
1389
John McCallf4c73712011-01-19 06:33:43 +00001390QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001391 QualType ToElementType = Importer.Import(T->getElementType());
1392 if (ToElementType.isNull())
1393 return QualType();
1394
1395 return Importer.getToContext().getComplexType(ToElementType);
1396}
1397
John McCallf4c73712011-01-19 06:33:43 +00001398QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001399 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1400 if (ToPointeeType.isNull())
1401 return QualType();
1402
1403 return Importer.getToContext().getPointerType(ToPointeeType);
1404}
1405
John McCallf4c73712011-01-19 06:33:43 +00001406QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001407 // FIXME: Check for blocks support in "to" context.
1408 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1409 if (ToPointeeType.isNull())
1410 return QualType();
1411
1412 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1413}
1414
John McCallf4c73712011-01-19 06:33:43 +00001415QualType
1416ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001417 // FIXME: Check for C++ support in "to" context.
1418 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1419 if (ToPointeeType.isNull())
1420 return QualType();
1421
1422 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1423}
1424
John McCallf4c73712011-01-19 06:33:43 +00001425QualType
1426ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001427 // FIXME: Check for C++0x support in "to" context.
1428 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1429 if (ToPointeeType.isNull())
1430 return QualType();
1431
1432 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1433}
1434
John McCallf4c73712011-01-19 06:33:43 +00001435QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001436 // FIXME: Check for C++ support in "to" context.
1437 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1438 if (ToPointeeType.isNull())
1439 return QualType();
1440
1441 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1442 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1443 ClassType.getTypePtr());
1444}
1445
John McCallf4c73712011-01-19 06:33:43 +00001446QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001447 QualType ToElementType = Importer.Import(T->getElementType());
1448 if (ToElementType.isNull())
1449 return QualType();
1450
1451 return Importer.getToContext().getConstantArrayType(ToElementType,
1452 T->getSize(),
1453 T->getSizeModifier(),
1454 T->getIndexTypeCVRQualifiers());
1455}
1456
John McCallf4c73712011-01-19 06:33:43 +00001457QualType
1458ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001459 QualType ToElementType = Importer.Import(T->getElementType());
1460 if (ToElementType.isNull())
1461 return QualType();
1462
1463 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1464 T->getSizeModifier(),
1465 T->getIndexTypeCVRQualifiers());
1466}
1467
John McCallf4c73712011-01-19 06:33:43 +00001468QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001469 QualType ToElementType = Importer.Import(T->getElementType());
1470 if (ToElementType.isNull())
1471 return QualType();
1472
1473 Expr *Size = Importer.Import(T->getSizeExpr());
1474 if (!Size)
1475 return QualType();
1476
1477 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1478 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1479 T->getSizeModifier(),
1480 T->getIndexTypeCVRQualifiers(),
1481 Brackets);
1482}
1483
John McCallf4c73712011-01-19 06:33:43 +00001484QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001485 QualType ToElementType = Importer.Import(T->getElementType());
1486 if (ToElementType.isNull())
1487 return QualType();
1488
1489 return Importer.getToContext().getVectorType(ToElementType,
1490 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001491 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001492}
1493
John McCallf4c73712011-01-19 06:33:43 +00001494QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001495 QualType ToElementType = Importer.Import(T->getElementType());
1496 if (ToElementType.isNull())
1497 return QualType();
1498
1499 return Importer.getToContext().getExtVectorType(ToElementType,
1500 T->getNumElements());
1501}
1502
John McCallf4c73712011-01-19 06:33:43 +00001503QualType
1504ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001505 // FIXME: What happens if we're importing a function without a prototype
1506 // into C++? Should we make it variadic?
1507 QualType ToResultType = Importer.Import(T->getResultType());
1508 if (ToResultType.isNull())
1509 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001510
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001511 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001512 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001513}
1514
John McCallf4c73712011-01-19 06:33:43 +00001515QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001516 QualType ToResultType = Importer.Import(T->getResultType());
1517 if (ToResultType.isNull())
1518 return QualType();
1519
1520 // Import argument types
1521 llvm::SmallVector<QualType, 4> ArgTypes;
1522 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1523 AEnd = T->arg_type_end();
1524 A != AEnd; ++A) {
1525 QualType ArgType = Importer.Import(*A);
1526 if (ArgType.isNull())
1527 return QualType();
1528 ArgTypes.push_back(ArgType);
1529 }
1530
1531 // Import exception types
1532 llvm::SmallVector<QualType, 4> ExceptionTypes;
1533 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1534 EEnd = T->exception_end();
1535 E != EEnd; ++E) {
1536 QualType ExceptionType = Importer.Import(*E);
1537 if (ExceptionType.isNull())
1538 return QualType();
1539 ExceptionTypes.push_back(ExceptionType);
1540 }
John McCalle23cf432010-12-14 08:05:40 +00001541
1542 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1543 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001544
1545 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001546 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001547}
1548
John McCallf4c73712011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001550 TypedefNameDecl *ToDecl
1551 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001552 if (!ToDecl)
1553 return QualType();
1554
1555 return Importer.getToContext().getTypeDeclType(ToDecl);
1556}
1557
John McCallf4c73712011-01-19 06:33:43 +00001558QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001559 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1560 if (!ToExpr)
1561 return QualType();
1562
1563 return Importer.getToContext().getTypeOfExprType(ToExpr);
1564}
1565
John McCallf4c73712011-01-19 06:33:43 +00001566QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001567 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1568 if (ToUnderlyingType.isNull())
1569 return QualType();
1570
1571 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1572}
1573
John McCallf4c73712011-01-19 06:33:43 +00001574QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001575 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001576 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1577 if (!ToExpr)
1578 return QualType();
1579
1580 return Importer.getToContext().getDecltypeType(ToExpr);
1581}
1582
Sean Huntca63c202011-05-24 22:41:36 +00001583QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1584 QualType ToBaseType = Importer.Import(T->getBaseType());
1585 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1586 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1587 return QualType();
1588
1589 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1590 ToUnderlyingType,
1591 T->getUTTKind());
1592}
1593
Richard Smith34b41d92011-02-20 03:19:35 +00001594QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1595 // FIXME: Make sure that the "to" context supports C++0x!
1596 QualType FromDeduced = T->getDeducedType();
1597 QualType ToDeduced;
1598 if (!FromDeduced.isNull()) {
1599 ToDeduced = Importer.Import(FromDeduced);
1600 if (ToDeduced.isNull())
1601 return QualType();
1602 }
1603
1604 return Importer.getToContext().getAutoType(ToDeduced);
1605}
1606
John McCallf4c73712011-01-19 06:33:43 +00001607QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001608 RecordDecl *ToDecl
1609 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1610 if (!ToDecl)
1611 return QualType();
1612
1613 return Importer.getToContext().getTagDeclType(ToDecl);
1614}
1615
John McCallf4c73712011-01-19 06:33:43 +00001616QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001617 EnumDecl *ToDecl
1618 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1619 if (!ToDecl)
1620 return QualType();
1621
1622 return Importer.getToContext().getTagDeclType(ToDecl);
1623}
1624
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001625QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001626 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001627 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1628 if (ToTemplate.isNull())
1629 return QualType();
1630
1631 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1632 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1633 return QualType();
1634
1635 QualType ToCanonType;
1636 if (!QualType(T, 0).isCanonical()) {
1637 QualType FromCanonType
1638 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1639 ToCanonType =Importer.Import(FromCanonType);
1640 if (ToCanonType.isNull())
1641 return QualType();
1642 }
1643 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1644 ToTemplateArgs.data(),
1645 ToTemplateArgs.size(),
1646 ToCanonType);
1647}
1648
John McCallf4c73712011-01-19 06:33:43 +00001649QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001650 NestedNameSpecifier *ToQualifier = 0;
1651 // Note: the qualifier in an ElaboratedType is optional.
1652 if (T->getQualifier()) {
1653 ToQualifier = Importer.Import(T->getQualifier());
1654 if (!ToQualifier)
1655 return QualType();
1656 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001657
1658 QualType ToNamedType = Importer.Import(T->getNamedType());
1659 if (ToNamedType.isNull())
1660 return QualType();
1661
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001662 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1663 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001664}
1665
John McCallf4c73712011-01-19 06:33:43 +00001666QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001667 ObjCInterfaceDecl *Class
1668 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1669 if (!Class)
1670 return QualType();
1671
John McCallc12c5bb2010-05-15 11:32:37 +00001672 return Importer.getToContext().getObjCInterfaceType(Class);
1673}
1674
John McCallf4c73712011-01-19 06:33:43 +00001675QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001676 QualType ToBaseType = Importer.Import(T->getBaseType());
1677 if (ToBaseType.isNull())
1678 return QualType();
1679
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001680 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001681 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001682 PEnd = T->qual_end();
1683 P != PEnd; ++P) {
1684 ObjCProtocolDecl *Protocol
1685 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1686 if (!Protocol)
1687 return QualType();
1688 Protocols.push_back(Protocol);
1689 }
1690
John McCallc12c5bb2010-05-15 11:32:37 +00001691 return Importer.getToContext().getObjCObjectType(ToBaseType,
1692 Protocols.data(),
1693 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001694}
1695
John McCallf4c73712011-01-19 06:33:43 +00001696QualType
1697ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001698 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1699 if (ToPointeeType.isNull())
1700 return QualType();
1701
John McCallc12c5bb2010-05-15 11:32:37 +00001702 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001703}
1704
Douglas Gregor089459a2010-02-08 21:09:39 +00001705//----------------------------------------------------------------------------
1706// Import Declarations
1707//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001708bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1709 DeclContext *&LexicalDC,
1710 DeclarationName &Name,
1711 SourceLocation &Loc) {
1712 // Import the context of this declaration.
1713 DC = Importer.ImportContext(D->getDeclContext());
1714 if (!DC)
1715 return true;
1716
1717 LexicalDC = DC;
1718 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1719 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1720 if (!LexicalDC)
1721 return true;
1722 }
1723
1724 // Import the name of this declaration.
1725 Name = Importer.Import(D->getDeclName());
1726 if (D->getDeclName() && !Name)
1727 return true;
1728
1729 // Import the location of this declaration.
1730 Loc = Importer.Import(D->getLocation());
1731 return false;
1732}
1733
Abramo Bagnara25777432010-08-11 22:01:17 +00001734void
1735ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1736 DeclarationNameInfo& To) {
1737 // NOTE: To.Name and To.Loc are already imported.
1738 // We only have to import To.LocInfo.
1739 switch (To.getName().getNameKind()) {
1740 case DeclarationName::Identifier:
1741 case DeclarationName::ObjCZeroArgSelector:
1742 case DeclarationName::ObjCOneArgSelector:
1743 case DeclarationName::ObjCMultiArgSelector:
1744 case DeclarationName::CXXUsingDirective:
1745 return;
1746
1747 case DeclarationName::CXXOperatorName: {
1748 SourceRange Range = From.getCXXOperatorNameRange();
1749 To.setCXXOperatorNameRange(Importer.Import(Range));
1750 return;
1751 }
1752 case DeclarationName::CXXLiteralOperatorName: {
1753 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1754 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1755 return;
1756 }
1757 case DeclarationName::CXXConstructorName:
1758 case DeclarationName::CXXDestructorName:
1759 case DeclarationName::CXXConversionFunctionName: {
1760 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1761 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1762 return;
1763 }
1764 assert(0 && "Unknown name kind.");
1765 }
1766}
1767
Douglas Gregord8868a62011-01-18 03:11:38 +00001768void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1769 if (Importer.isMinimalImport() && !ForceImport) {
1770 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1771 ToDC->setHasExternalLexicalStorage();
1772 ToDC->setHasExternalVisibleStorage();
1773 }
1774 return;
1775 }
1776
Douglas Gregor083a8212010-02-21 18:24:45 +00001777 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1778 FromEnd = FromDC->decls_end();
1779 From != FromEnd;
1780 ++From)
1781 Importer.Import(*From);
1782}
1783
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001784bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1785 if (To->getDefinition())
1786 return false;
1787
1788 To->startDefinition();
1789
1790 // Add base classes.
1791 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1792 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1793
1794 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1795 for (CXXRecordDecl::base_class_iterator
1796 Base1 = FromCXX->bases_begin(),
1797 FromBaseEnd = FromCXX->bases_end();
1798 Base1 != FromBaseEnd;
1799 ++Base1) {
1800 QualType T = Importer.Import(Base1->getType());
1801 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001802 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001803
1804 SourceLocation EllipsisLoc;
1805 if (Base1->isPackExpansion())
1806 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001807
1808 Bases.push_back(
1809 new (Importer.getToContext())
1810 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1811 Base1->isVirtual(),
1812 Base1->isBaseOfClass(),
1813 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001814 Importer.Import(Base1->getTypeSourceInfo()),
1815 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001816 }
1817 if (!Bases.empty())
1818 ToCXX->setBases(Bases.data(), Bases.size());
1819 }
1820
1821 ImportDeclContext(From);
1822 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001823 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001824}
1825
Douglas Gregor040afae2010-11-30 19:14:50 +00001826TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1827 TemplateParameterList *Params) {
1828 llvm::SmallVector<NamedDecl *, 4> ToParams;
1829 ToParams.reserve(Params->size());
1830 for (TemplateParameterList::iterator P = Params->begin(),
1831 PEnd = Params->end();
1832 P != PEnd; ++P) {
1833 Decl *To = Importer.Import(*P);
1834 if (!To)
1835 return 0;
1836
1837 ToParams.push_back(cast<NamedDecl>(To));
1838 }
1839
1840 return TemplateParameterList::Create(Importer.getToContext(),
1841 Importer.Import(Params->getTemplateLoc()),
1842 Importer.Import(Params->getLAngleLoc()),
1843 ToParams.data(), ToParams.size(),
1844 Importer.Import(Params->getRAngleLoc()));
1845}
1846
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001847TemplateArgument
1848ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1849 switch (From.getKind()) {
1850 case TemplateArgument::Null:
1851 return TemplateArgument();
1852
1853 case TemplateArgument::Type: {
1854 QualType ToType = Importer.Import(From.getAsType());
1855 if (ToType.isNull())
1856 return TemplateArgument();
1857 return TemplateArgument(ToType);
1858 }
1859
1860 case TemplateArgument::Integral: {
1861 QualType ToType = Importer.Import(From.getIntegralType());
1862 if (ToType.isNull())
1863 return TemplateArgument();
1864 return TemplateArgument(*From.getAsIntegral(), ToType);
1865 }
1866
1867 case TemplateArgument::Declaration:
1868 if (Decl *To = Importer.Import(From.getAsDecl()))
1869 return TemplateArgument(To);
1870 return TemplateArgument();
1871
1872 case TemplateArgument::Template: {
1873 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1874 if (ToTemplate.isNull())
1875 return TemplateArgument();
1876
1877 return TemplateArgument(ToTemplate);
1878 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001879
1880 case TemplateArgument::TemplateExpansion: {
1881 TemplateName ToTemplate
1882 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1883 if (ToTemplate.isNull())
1884 return TemplateArgument();
1885
Douglas Gregor2be29f42011-01-14 23:41:42 +00001886 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001887 }
1888
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001889 case TemplateArgument::Expression:
1890 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1891 return TemplateArgument(ToExpr);
1892 return TemplateArgument();
1893
1894 case TemplateArgument::Pack: {
1895 llvm::SmallVector<TemplateArgument, 2> ToPack;
1896 ToPack.reserve(From.pack_size());
1897 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1898 return TemplateArgument();
1899
1900 TemplateArgument *ToArgs
1901 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1902 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1903 return TemplateArgument(ToArgs, ToPack.size());
1904 }
1905 }
1906
1907 llvm_unreachable("Invalid template argument kind");
1908 return TemplateArgument();
1909}
1910
1911bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1912 unsigned NumFromArgs,
1913 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1914 for (unsigned I = 0; I != NumFromArgs; ++I) {
1915 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1916 if (To.isNull() && !FromArgs[I].isNull())
1917 return true;
1918
1919 ToArgs.push_back(To);
1920 }
1921
1922 return false;
1923}
1924
Douglas Gregor96a01b42010-02-11 00:48:18 +00001925bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001926 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001927 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001928 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001929 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001930 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001931}
1932
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001933bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001934 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001935 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001936 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001937 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001938}
1939
Douglas Gregor040afae2010-11-30 19:14:50 +00001940bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1941 ClassTemplateDecl *To) {
1942 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1943 Importer.getToContext(),
1944 Importer.getNonEquivalentDecls());
1945 return Ctx.IsStructurallyEquivalent(From, To);
1946}
1947
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001948Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001949 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001950 << D->getDeclKindName();
1951 return 0;
1952}
1953
Douglas Gregor788c62d2010-02-21 18:26:36 +00001954Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1955 // Import the major distinguishing characteristics of this namespace.
1956 DeclContext *DC, *LexicalDC;
1957 DeclarationName Name;
1958 SourceLocation Loc;
1959 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1960 return 0;
1961
1962 NamespaceDecl *MergeWithNamespace = 0;
1963 if (!Name) {
1964 // This is an anonymous namespace. Adopt an existing anonymous
1965 // namespace if we can.
1966 // FIXME: Not testable.
1967 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1968 MergeWithNamespace = TU->getAnonymousNamespace();
1969 else
1970 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1971 } else {
1972 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1973 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1974 Lookup.first != Lookup.second;
1975 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001976 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001977 continue;
1978
1979 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1980 MergeWithNamespace = FoundNS;
1981 ConflictingDecls.clear();
1982 break;
1983 }
1984
1985 ConflictingDecls.push_back(*Lookup.first);
1986 }
1987
1988 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001989 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001990 ConflictingDecls.data(),
1991 ConflictingDecls.size());
1992 }
1993 }
1994
1995 // Create the "to" namespace, if needed.
1996 NamespaceDecl *ToNamespace = MergeWithNamespace;
1997 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00001998 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1999 Importer.Import(D->getLocStart()),
2000 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002001 ToNamespace->setLexicalDeclContext(LexicalDC);
2002 LexicalDC->addDecl(ToNamespace);
2003
2004 // If this is an anonymous namespace, register it as the anonymous
2005 // namespace within its context.
2006 if (!Name) {
2007 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2008 TU->setAnonymousNamespace(ToNamespace);
2009 else
2010 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2011 }
2012 }
2013 Importer.Imported(D, ToNamespace);
2014
2015 ImportDeclContext(D);
2016
2017 return ToNamespace;
2018}
2019
Richard Smith162e1c12011-04-15 14:24:37 +00002020Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002021 // Import the major distinguishing characteristics of this typedef.
2022 DeclContext *DC, *LexicalDC;
2023 DeclarationName Name;
2024 SourceLocation Loc;
2025 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2026 return 0;
2027
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002028 // If this typedef is not in block scope, determine whether we've
2029 // seen a typedef with the same name (that we can merge with) or any
2030 // other entity by that name (which name lookup could conflict with).
2031 if (!DC->isFunctionOrMethod()) {
2032 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2033 unsigned IDNS = Decl::IDNS_Ordinary;
2034 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2035 Lookup.first != Lookup.second;
2036 ++Lookup.first) {
2037 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2038 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002039 if (TypedefNameDecl *FoundTypedef =
2040 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002041 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2042 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002043 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002044 }
2045
2046 ConflictingDecls.push_back(*Lookup.first);
2047 }
2048
2049 if (!ConflictingDecls.empty()) {
2050 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2051 ConflictingDecls.data(),
2052 ConflictingDecls.size());
2053 if (!Name)
2054 return 0;
2055 }
2056 }
2057
Douglas Gregorea35d112010-02-15 23:54:17 +00002058 // Import the underlying type of this typedef;
2059 QualType T = Importer.Import(D->getUnderlyingType());
2060 if (T.isNull())
2061 return 0;
2062
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002063 // Create the new typedef node.
2064 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002065 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002066 TypedefNameDecl *ToTypedef;
2067 if (IsAlias)
2068 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2069 StartL, Loc,
2070 Name.getAsIdentifierInfo(),
2071 TInfo);
2072 else
2073 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2074 StartL, Loc,
2075 Name.getAsIdentifierInfo(),
2076 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002077 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002078 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002079 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002080 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002081
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002082 return ToTypedef;
2083}
2084
Richard Smith162e1c12011-04-15 14:24:37 +00002085Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2086 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2087}
2088
2089Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2090 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2091}
2092
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002093Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2094 // Import the major distinguishing characteristics of this enum.
2095 DeclContext *DC, *LexicalDC;
2096 DeclarationName Name;
2097 SourceLocation Loc;
2098 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2099 return 0;
2100
2101 // Figure out what enum name we're looking for.
2102 unsigned IDNS = Decl::IDNS_Tag;
2103 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002104 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2105 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002106 IDNS = Decl::IDNS_Ordinary;
2107 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2108 IDNS |= Decl::IDNS_Ordinary;
2109
2110 // We may already have an enum of the same name; try to find and match it.
2111 if (!DC->isFunctionOrMethod() && SearchName) {
2112 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2113 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2114 Lookup.first != Lookup.second;
2115 ++Lookup.first) {
2116 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2117 continue;
2118
2119 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002120 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002121 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2122 Found = Tag->getDecl();
2123 }
2124
2125 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002126 if (IsStructuralMatch(D, FoundEnum))
2127 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002128 }
2129
2130 ConflictingDecls.push_back(*Lookup.first);
2131 }
2132
2133 if (!ConflictingDecls.empty()) {
2134 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2135 ConflictingDecls.data(),
2136 ConflictingDecls.size());
2137 }
2138 }
2139
2140 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002141 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2142 Importer.Import(D->getLocStart()),
2143 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002144 D->isScoped(), D->isScopedUsingClassTag(),
2145 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002146 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002147 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002148 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002149 D2->setLexicalDeclContext(LexicalDC);
2150 Importer.Imported(D, D2);
2151 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002152
2153 // Import the integer type.
2154 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2155 if (ToIntegerType.isNull())
2156 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002157 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002158
2159 // Import the definition
2160 if (D->isDefinition()) {
2161 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2162 if (T.isNull())
2163 return 0;
2164
2165 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2166 if (ToPromotionType.isNull())
2167 return 0;
2168
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002169 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002170 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002171
2172 // FIXME: we might need to merge the number of positive or negative bits
2173 // if the enumerator lists don't match.
2174 D2->completeDefinition(T, ToPromotionType,
2175 D->getNumPositiveBits(),
2176 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002177 }
2178
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002179 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002180}
2181
Douglas Gregor96a01b42010-02-11 00:48:18 +00002182Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2183 // If this record has a definition in the translation unit we're coming from,
2184 // but this particular declaration is not that definition, import the
2185 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002186 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002187 if (Definition && Definition != D) {
2188 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002189 if (!ImportedDef)
2190 return 0;
2191
2192 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002193 }
2194
2195 // Import the major distinguishing characteristics of this record.
2196 DeclContext *DC, *LexicalDC;
2197 DeclarationName Name;
2198 SourceLocation Loc;
2199 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2200 return 0;
2201
2202 // Figure out what structure name we're looking for.
2203 unsigned IDNS = Decl::IDNS_Tag;
2204 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002205 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2206 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002207 IDNS = Decl::IDNS_Ordinary;
2208 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2209 IDNS |= Decl::IDNS_Ordinary;
2210
2211 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002212 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002213 if (!DC->isFunctionOrMethod() && SearchName) {
2214 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2215 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2216 Lookup.first != Lookup.second;
2217 ++Lookup.first) {
2218 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2219 continue;
2220
2221 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002222 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002223 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2224 Found = Tag->getDecl();
2225 }
2226
2227 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002228 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2229 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2230 // The record types structurally match, or the "from" translation
2231 // unit only had a forward declaration anyway; call it the same
2232 // function.
2233 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002234 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002235 }
2236 } else {
2237 // We have a forward declaration of this type, so adopt that forward
2238 // declaration rather than building a new one.
2239 AdoptDecl = FoundRecord;
2240 continue;
2241 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002242 }
2243
2244 ConflictingDecls.push_back(*Lookup.first);
2245 }
2246
2247 if (!ConflictingDecls.empty()) {
2248 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2249 ConflictingDecls.data(),
2250 ConflictingDecls.size());
2251 }
2252 }
2253
2254 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002255 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002256 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002257 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002258 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002259 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002260 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002261 DC, StartLoc, Loc,
2262 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002263 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002264 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002265 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002266 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002267 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002268 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002269
2270 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002271 D2->setLexicalDeclContext(LexicalDC);
2272 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002273 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002274
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002275 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002276
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002277 if (D->isDefinition() && ImportDefinition(D, D2))
2278 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002279
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002280 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002281}
2282
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002283Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2284 // Import the major distinguishing characteristics of this enumerator.
2285 DeclContext *DC, *LexicalDC;
2286 DeclarationName Name;
2287 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002288 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002289 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002290
2291 QualType T = Importer.Import(D->getType());
2292 if (T.isNull())
2293 return 0;
2294
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002295 // Determine whether there are any other declarations with the same name and
2296 // in the same context.
2297 if (!LexicalDC->isFunctionOrMethod()) {
2298 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2299 unsigned IDNS = Decl::IDNS_Ordinary;
2300 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2301 Lookup.first != Lookup.second;
2302 ++Lookup.first) {
2303 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2304 continue;
2305
2306 ConflictingDecls.push_back(*Lookup.first);
2307 }
2308
2309 if (!ConflictingDecls.empty()) {
2310 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2311 ConflictingDecls.data(),
2312 ConflictingDecls.size());
2313 if (!Name)
2314 return 0;
2315 }
2316 }
2317
2318 Expr *Init = Importer.Import(D->getInitExpr());
2319 if (D->getInitExpr() && !Init)
2320 return 0;
2321
2322 EnumConstantDecl *ToEnumerator
2323 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2324 Name.getAsIdentifierInfo(), T,
2325 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002326 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002327 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002328 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002329 LexicalDC->addDecl(ToEnumerator);
2330 return ToEnumerator;
2331}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002332
Douglas Gregora404ea62010-02-10 19:54:31 +00002333Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2334 // Import the major distinguishing characteristics of this function.
2335 DeclContext *DC, *LexicalDC;
2336 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002337 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002338 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002339 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002340
Douglas Gregora404ea62010-02-10 19:54:31 +00002341 // Try to find a function in our own ("to") context with the same name, same
2342 // type, and in the same context as the function we're importing.
2343 if (!LexicalDC->isFunctionOrMethod()) {
2344 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2345 unsigned IDNS = Decl::IDNS_Ordinary;
2346 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2347 Lookup.first != Lookup.second;
2348 ++Lookup.first) {
2349 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2350 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002351
Douglas Gregora404ea62010-02-10 19:54:31 +00002352 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2353 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2354 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002355 if (Importer.IsStructurallyEquivalent(D->getType(),
2356 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002357 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002358 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002359 }
2360
2361 // FIXME: Check for overloading more carefully, e.g., by boosting
2362 // Sema::IsOverload out to the AST library.
2363
2364 // Function overloading is okay in C++.
2365 if (Importer.getToContext().getLangOptions().CPlusPlus)
2366 continue;
2367
2368 // Complain about inconsistent function types.
2369 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002370 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002371 Importer.ToDiag(FoundFunction->getLocation(),
2372 diag::note_odr_value_here)
2373 << FoundFunction->getType();
2374 }
2375 }
2376
2377 ConflictingDecls.push_back(*Lookup.first);
2378 }
2379
2380 if (!ConflictingDecls.empty()) {
2381 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2382 ConflictingDecls.data(),
2383 ConflictingDecls.size());
2384 if (!Name)
2385 return 0;
2386 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002387 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002388
Abramo Bagnara25777432010-08-11 22:01:17 +00002389 DeclarationNameInfo NameInfo(Name, Loc);
2390 // Import additional name location/type info.
2391 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2392
Douglas Gregorea35d112010-02-15 23:54:17 +00002393 // Import the type.
2394 QualType T = Importer.Import(D->getType());
2395 if (T.isNull())
2396 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002397
2398 // Import the function parameters.
2399 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2400 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2401 P != PEnd; ++P) {
2402 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2403 if (!ToP)
2404 return 0;
2405
2406 Parameters.push_back(ToP);
2407 }
2408
2409 // Create the imported function.
2410 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002411 FunctionDecl *ToFunction = 0;
2412 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2413 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2414 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002415 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002416 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002417 FromConstructor->isExplicit(),
2418 D->isInlineSpecified(),
Sean Hunt5f802e52011-05-06 00:11:07 +00002419 D->isImplicit());
Douglas Gregorc144f352010-02-21 18:29:16 +00002420 } else if (isa<CXXDestructorDecl>(D)) {
2421 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2422 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002423 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002424 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002425 D->isInlineSpecified(),
2426 D->isImplicit());
2427 } else if (CXXConversionDecl *FromConversion
2428 = dyn_cast<CXXConversionDecl>(D)) {
2429 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2430 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002431 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002432 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002433 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002434 FromConversion->isExplicit(),
2435 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002436 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2437 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2438 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002439 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002440 NameInfo, T, TInfo,
2441 Method->isStatic(),
2442 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002443 Method->isInlineSpecified(),
2444 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002445 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002446 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002447 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002448 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002449 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002450 D->isInlineSpecified(),
2451 D->hasWrittenPrototype());
2452 }
John McCallb6217662010-03-15 10:12:16 +00002453
2454 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002455 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002456 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002457 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002458 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2459 ToFunction->setTrivial(D->isTrivial());
2460 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002461 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002462
Douglas Gregora404ea62010-02-10 19:54:31 +00002463 // Set the parameters.
2464 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002465 Parameters[I]->setOwningFunction(ToFunction);
2466 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002467 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002468 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002469
2470 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002471
2472 // Add this function to the lexical context.
2473 LexicalDC->addDecl(ToFunction);
2474
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002475 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002476}
2477
Douglas Gregorc144f352010-02-21 18:29:16 +00002478Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2479 return VisitFunctionDecl(D);
2480}
2481
2482Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2483 return VisitCXXMethodDecl(D);
2484}
2485
2486Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2487 return VisitCXXMethodDecl(D);
2488}
2489
2490Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2491 return VisitCXXMethodDecl(D);
2492}
2493
Douglas Gregor96a01b42010-02-11 00:48:18 +00002494Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2495 // Import the major distinguishing characteristics of a variable.
2496 DeclContext *DC, *LexicalDC;
2497 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002498 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002499 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2500 return 0;
2501
2502 // Import the type.
2503 QualType T = Importer.Import(D->getType());
2504 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002505 return 0;
2506
2507 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2508 Expr *BitWidth = Importer.Import(D->getBitWidth());
2509 if (!BitWidth && D->getBitWidth())
2510 return 0;
2511
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002512 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2513 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002514 Loc, Name.getAsIdentifierInfo(),
2515 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002516 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002517 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002518 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002519 LexicalDC->addDecl(ToField);
2520 return ToField;
2521}
2522
Francois Pichet87c2e122010-11-21 06:08:52 +00002523Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2524 // Import the major distinguishing characteristics of a variable.
2525 DeclContext *DC, *LexicalDC;
2526 DeclarationName Name;
2527 SourceLocation Loc;
2528 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2529 return 0;
2530
2531 // Import the type.
2532 QualType T = Importer.Import(D->getType());
2533 if (T.isNull())
2534 return 0;
2535
2536 NamedDecl **NamedChain =
2537 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2538
2539 unsigned i = 0;
2540 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2541 PE = D->chain_end(); PI != PE; ++PI) {
2542 Decl* D = Importer.Import(*PI);
2543 if (!D)
2544 return 0;
2545 NamedChain[i++] = cast<NamedDecl>(D);
2546 }
2547
2548 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2549 Importer.getToContext(), DC,
2550 Loc, Name.getAsIdentifierInfo(), T,
2551 NamedChain, D->getChainingSize());
2552 ToIndirectField->setAccess(D->getAccess());
2553 ToIndirectField->setLexicalDeclContext(LexicalDC);
2554 Importer.Imported(D, ToIndirectField);
2555 LexicalDC->addDecl(ToIndirectField);
2556 return ToIndirectField;
2557}
2558
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002559Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2560 // Import the major distinguishing characteristics of an ivar.
2561 DeclContext *DC, *LexicalDC;
2562 DeclarationName Name;
2563 SourceLocation Loc;
2564 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2565 return 0;
2566
2567 // Determine whether we've already imported this ivar
2568 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2569 Lookup.first != Lookup.second;
2570 ++Lookup.first) {
2571 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2572 if (Importer.IsStructurallyEquivalent(D->getType(),
2573 FoundIvar->getType())) {
2574 Importer.Imported(D, FoundIvar);
2575 return FoundIvar;
2576 }
2577
2578 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2579 << Name << D->getType() << FoundIvar->getType();
2580 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2581 << FoundIvar->getType();
2582 return 0;
2583 }
2584 }
2585
2586 // Import the type.
2587 QualType T = Importer.Import(D->getType());
2588 if (T.isNull())
2589 return 0;
2590
2591 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2592 Expr *BitWidth = Importer.Import(D->getBitWidth());
2593 if (!BitWidth && D->getBitWidth())
2594 return 0;
2595
Daniel Dunbara0654922010-04-02 20:10:03 +00002596 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2597 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002598 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002599 Loc, Name.getAsIdentifierInfo(),
2600 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002601 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002602 ToIvar->setLexicalDeclContext(LexicalDC);
2603 Importer.Imported(D, ToIvar);
2604 LexicalDC->addDecl(ToIvar);
2605 return ToIvar;
2606
2607}
2608
Douglas Gregora404ea62010-02-10 19:54:31 +00002609Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2610 // Import the major distinguishing characteristics of a variable.
2611 DeclContext *DC, *LexicalDC;
2612 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002613 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002614 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002615 return 0;
2616
Douglas Gregor089459a2010-02-08 21:09:39 +00002617 // Try to find a variable in our own ("to") context with the same name and
2618 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002619 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002620 VarDecl *MergeWithVar = 0;
2621 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2622 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002623 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002624 Lookup.first != Lookup.second;
2625 ++Lookup.first) {
2626 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2627 continue;
2628
2629 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2630 // We have found a variable that we may need to merge with. Check it.
2631 if (isExternalLinkage(FoundVar->getLinkage()) &&
2632 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002633 if (Importer.IsStructurallyEquivalent(D->getType(),
2634 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002635 MergeWithVar = FoundVar;
2636 break;
2637 }
2638
Douglas Gregord0145422010-02-12 17:23:39 +00002639 const ArrayType *FoundArray
2640 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2641 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002642 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002643 if (FoundArray && TArray) {
2644 if (isa<IncompleteArrayType>(FoundArray) &&
2645 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002646 // Import the type.
2647 QualType T = Importer.Import(D->getType());
2648 if (T.isNull())
2649 return 0;
2650
Douglas Gregord0145422010-02-12 17:23:39 +00002651 FoundVar->setType(T);
2652 MergeWithVar = FoundVar;
2653 break;
2654 } else if (isa<IncompleteArrayType>(TArray) &&
2655 isa<ConstantArrayType>(FoundArray)) {
2656 MergeWithVar = FoundVar;
2657 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002658 }
2659 }
2660
Douglas Gregor089459a2010-02-08 21:09:39 +00002661 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002662 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002663 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2664 << FoundVar->getType();
2665 }
2666 }
2667
2668 ConflictingDecls.push_back(*Lookup.first);
2669 }
2670
2671 if (MergeWithVar) {
2672 // An equivalent variable with external linkage has been found. Link
2673 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002674 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002675
2676 if (VarDecl *DDef = D->getDefinition()) {
2677 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2678 Importer.ToDiag(ExistingDef->getLocation(),
2679 diag::err_odr_variable_multiple_def)
2680 << Name;
2681 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2682 } else {
2683 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002684 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002685 }
2686 }
2687
2688 return MergeWithVar;
2689 }
2690
2691 if (!ConflictingDecls.empty()) {
2692 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2693 ConflictingDecls.data(),
2694 ConflictingDecls.size());
2695 if (!Name)
2696 return 0;
2697 }
2698 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002699
Douglas Gregorea35d112010-02-15 23:54:17 +00002700 // Import the type.
2701 QualType T = Importer.Import(D->getType());
2702 if (T.isNull())
2703 return 0;
2704
Douglas Gregor089459a2010-02-08 21:09:39 +00002705 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002706 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002707 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2708 Importer.Import(D->getInnerLocStart()),
2709 Loc, Name.getAsIdentifierInfo(),
2710 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002711 D->getStorageClass(),
2712 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002713 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002714 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002715 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002716 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002717 LexicalDC->addDecl(ToVar);
2718
Douglas Gregor089459a2010-02-08 21:09:39 +00002719 // Merge the initializer.
2720 // FIXME: Can we really import any initializer? Alternatively, we could force
2721 // ourselves to import every declaration of a variable and then only use
2722 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002723 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002724
2725 // FIXME: Other bits to merge?
2726
2727 return ToVar;
2728}
2729
Douglas Gregor2cd00932010-02-17 21:22:52 +00002730Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2731 // Parameters are created in the translation unit's context, then moved
2732 // into the function declaration's context afterward.
2733 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2734
2735 // Import the name of this declaration.
2736 DeclarationName Name = Importer.Import(D->getDeclName());
2737 if (D->getDeclName() && !Name)
2738 return 0;
2739
2740 // Import the location of this declaration.
2741 SourceLocation Loc = Importer.Import(D->getLocation());
2742
2743 // Import the parameter's type.
2744 QualType T = Importer.Import(D->getType());
2745 if (T.isNull())
2746 return 0;
2747
2748 // Create the imported parameter.
2749 ImplicitParamDecl *ToParm
2750 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2751 Loc, Name.getAsIdentifierInfo(),
2752 T);
2753 return Importer.Imported(D, ToParm);
2754}
2755
Douglas Gregora404ea62010-02-10 19:54:31 +00002756Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2757 // Parameters are created in the translation unit's context, then moved
2758 // into the function declaration's context afterward.
2759 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2760
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002761 // Import the name of this declaration.
2762 DeclarationName Name = Importer.Import(D->getDeclName());
2763 if (D->getDeclName() && !Name)
2764 return 0;
2765
Douglas Gregora404ea62010-02-10 19:54:31 +00002766 // Import the location of this declaration.
2767 SourceLocation Loc = Importer.Import(D->getLocation());
2768
2769 // Import the parameter's type.
2770 QualType T = Importer.Import(D->getType());
2771 if (T.isNull())
2772 return 0;
2773
2774 // Create the imported parameter.
2775 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2776 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002777 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002778 Loc, Name.getAsIdentifierInfo(),
2779 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002780 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002781 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002782 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002783 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002784}
2785
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002786Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2787 // Import the major distinguishing characteristics of a method.
2788 DeclContext *DC, *LexicalDC;
2789 DeclarationName Name;
2790 SourceLocation Loc;
2791 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2792 return 0;
2793
2794 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2795 Lookup.first != Lookup.second;
2796 ++Lookup.first) {
2797 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2798 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2799 continue;
2800
2801 // Check return types.
2802 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2803 FoundMethod->getResultType())) {
2804 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2805 << D->isInstanceMethod() << Name
2806 << D->getResultType() << FoundMethod->getResultType();
2807 Importer.ToDiag(FoundMethod->getLocation(),
2808 diag::note_odr_objc_method_here)
2809 << D->isInstanceMethod() << Name;
2810 return 0;
2811 }
2812
2813 // Check the number of parameters.
2814 if (D->param_size() != FoundMethod->param_size()) {
2815 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2816 << D->isInstanceMethod() << Name
2817 << D->param_size() << FoundMethod->param_size();
2818 Importer.ToDiag(FoundMethod->getLocation(),
2819 diag::note_odr_objc_method_here)
2820 << D->isInstanceMethod() << Name;
2821 return 0;
2822 }
2823
2824 // Check parameter types.
2825 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2826 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2827 P != PEnd; ++P, ++FoundP) {
2828 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2829 (*FoundP)->getType())) {
2830 Importer.FromDiag((*P)->getLocation(),
2831 diag::err_odr_objc_method_param_type_inconsistent)
2832 << D->isInstanceMethod() << Name
2833 << (*P)->getType() << (*FoundP)->getType();
2834 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2835 << (*FoundP)->getType();
2836 return 0;
2837 }
2838 }
2839
2840 // Check variadic/non-variadic.
2841 // Check the number of parameters.
2842 if (D->isVariadic() != FoundMethod->isVariadic()) {
2843 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2844 << D->isInstanceMethod() << Name;
2845 Importer.ToDiag(FoundMethod->getLocation(),
2846 diag::note_odr_objc_method_here)
2847 << D->isInstanceMethod() << Name;
2848 return 0;
2849 }
2850
2851 // FIXME: Any other bits we need to merge?
2852 return Importer.Imported(D, FoundMethod);
2853 }
2854 }
2855
2856 // Import the result type.
2857 QualType ResultTy = Importer.Import(D->getResultType());
2858 if (ResultTy.isNull())
2859 return 0;
2860
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002861 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2862
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002863 ObjCMethodDecl *ToMethod
2864 = ObjCMethodDecl::Create(Importer.getToContext(),
2865 Loc,
2866 Importer.Import(D->getLocEnd()),
2867 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002868 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002869 D->isInstanceMethod(),
2870 D->isVariadic(),
2871 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002872 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002873 D->getImplementationControl());
2874
2875 // FIXME: When we decide to merge method definitions, we'll need to
2876 // deal with implicit parameters.
2877
2878 // Import the parameters
2879 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2880 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2881 FromPEnd = D->param_end();
2882 FromP != FromPEnd;
2883 ++FromP) {
2884 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2885 if (!ToP)
2886 return 0;
2887
2888 ToParams.push_back(ToP);
2889 }
2890
2891 // Set the parameters.
2892 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2893 ToParams[I]->setOwningFunction(ToMethod);
2894 ToMethod->addDecl(ToParams[I]);
2895 }
2896 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002897 ToParams.data(), ToParams.size(),
2898 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002899
2900 ToMethod->setLexicalDeclContext(LexicalDC);
2901 Importer.Imported(D, ToMethod);
2902 LexicalDC->addDecl(ToMethod);
2903 return ToMethod;
2904}
2905
Douglas Gregorb4677b62010-02-18 01:47:50 +00002906Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2907 // Import the major distinguishing characteristics of a category.
2908 DeclContext *DC, *LexicalDC;
2909 DeclarationName Name;
2910 SourceLocation Loc;
2911 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2912 return 0;
2913
2914 ObjCInterfaceDecl *ToInterface
2915 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2916 if (!ToInterface)
2917 return 0;
2918
2919 // Determine if we've already encountered this category.
2920 ObjCCategoryDecl *MergeWithCategory
2921 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2922 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2923 if (!ToCategory) {
2924 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2925 Importer.Import(D->getAtLoc()),
2926 Loc,
2927 Importer.Import(D->getCategoryNameLoc()),
2928 Name.getAsIdentifierInfo());
2929 ToCategory->setLexicalDeclContext(LexicalDC);
2930 LexicalDC->addDecl(ToCategory);
2931 Importer.Imported(D, ToCategory);
2932
2933 // Link this category into its class's category list.
2934 ToCategory->setClassInterface(ToInterface);
2935 ToCategory->insertNextClassCategory();
2936
2937 // Import protocols
2938 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2939 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2940 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2941 = D->protocol_loc_begin();
2942 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2943 FromProtoEnd = D->protocol_end();
2944 FromProto != FromProtoEnd;
2945 ++FromProto, ++FromProtoLoc) {
2946 ObjCProtocolDecl *ToProto
2947 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2948 if (!ToProto)
2949 return 0;
2950 Protocols.push_back(ToProto);
2951 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2952 }
2953
2954 // FIXME: If we're merging, make sure that the protocol list is the same.
2955 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2956 ProtocolLocs.data(), Importer.getToContext());
2957
2958 } else {
2959 Importer.Imported(D, ToCategory);
2960 }
2961
2962 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002963 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002964
2965 // If we have an implementation, import it as well.
2966 if (D->getImplementation()) {
2967 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002968 = cast_or_null<ObjCCategoryImplDecl>(
2969 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002970 if (!Impl)
2971 return 0;
2972
2973 ToCategory->setImplementation(Impl);
2974 }
2975
2976 return ToCategory;
2977}
2978
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002979Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002980 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002981 DeclContext *DC, *LexicalDC;
2982 DeclarationName Name;
2983 SourceLocation Loc;
2984 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2985 return 0;
2986
2987 ObjCProtocolDecl *MergeWithProtocol = 0;
2988 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2989 Lookup.first != Lookup.second;
2990 ++Lookup.first) {
2991 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2992 continue;
2993
2994 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2995 break;
2996 }
2997
2998 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2999 if (!ToProto || ToProto->isForwardDecl()) {
3000 if (!ToProto) {
3001 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3002 Name.getAsIdentifierInfo());
3003 ToProto->setForwardDecl(D->isForwardDecl());
3004 ToProto->setLexicalDeclContext(LexicalDC);
3005 LexicalDC->addDecl(ToProto);
3006 }
3007 Importer.Imported(D, ToProto);
3008
3009 // Import protocols
3010 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3011 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3012 ObjCProtocolDecl::protocol_loc_iterator
3013 FromProtoLoc = D->protocol_loc_begin();
3014 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3015 FromProtoEnd = D->protocol_end();
3016 FromProto != FromProtoEnd;
3017 ++FromProto, ++FromProtoLoc) {
3018 ObjCProtocolDecl *ToProto
3019 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3020 if (!ToProto)
3021 return 0;
3022 Protocols.push_back(ToProto);
3023 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3024 }
3025
3026 // FIXME: If we're merging, make sure that the protocol list is the same.
3027 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3028 ProtocolLocs.data(), Importer.getToContext());
3029 } else {
3030 Importer.Imported(D, ToProto);
3031 }
3032
Douglas Gregorb4677b62010-02-18 01:47:50 +00003033 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003034 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003035
3036 return ToProto;
3037}
3038
Douglas Gregora12d2942010-02-16 01:20:57 +00003039Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3040 // Import the major distinguishing characteristics of an @interface.
3041 DeclContext *DC, *LexicalDC;
3042 DeclarationName Name;
3043 SourceLocation Loc;
3044 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3045 return 0;
3046
3047 ObjCInterfaceDecl *MergeWithIface = 0;
3048 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3049 Lookup.first != Lookup.second;
3050 ++Lookup.first) {
3051 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3052 continue;
3053
3054 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3055 break;
3056 }
3057
3058 ObjCInterfaceDecl *ToIface = MergeWithIface;
3059 if (!ToIface || ToIface->isForwardDecl()) {
3060 if (!ToIface) {
3061 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3062 DC, Loc,
3063 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00003064 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00003065 D->isForwardDecl(),
3066 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003067 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003068 ToIface->setLexicalDeclContext(LexicalDC);
3069 LexicalDC->addDecl(ToIface);
3070 }
3071 Importer.Imported(D, ToIface);
3072
Douglas Gregora12d2942010-02-16 01:20:57 +00003073 if (D->getSuperClass()) {
3074 ObjCInterfaceDecl *Super
3075 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3076 if (!Super)
3077 return 0;
3078
3079 ToIface->setSuperClass(Super);
3080 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3081 }
3082
3083 // Import protocols
3084 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3085 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3086 ObjCInterfaceDecl::protocol_loc_iterator
3087 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003088
3089 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003090 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3091 FromProtoEnd = D->protocol_end();
3092 FromProto != FromProtoEnd;
3093 ++FromProto, ++FromProtoLoc) {
3094 ObjCProtocolDecl *ToProto
3095 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3096 if (!ToProto)
3097 return 0;
3098 Protocols.push_back(ToProto);
3099 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3100 }
3101
3102 // FIXME: If we're merging, make sure that the protocol list is the same.
3103 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3104 ProtocolLocs.data(), Importer.getToContext());
3105
Douglas Gregora12d2942010-02-16 01:20:57 +00003106 // Import @end range
3107 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3108 } else {
3109 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003110
3111 // Check for consistency of superclasses.
3112 DeclarationName FromSuperName, ToSuperName;
3113 if (D->getSuperClass())
3114 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3115 if (ToIface->getSuperClass())
3116 ToSuperName = ToIface->getSuperClass()->getDeclName();
3117 if (FromSuperName != ToSuperName) {
3118 Importer.ToDiag(ToIface->getLocation(),
3119 diag::err_odr_objc_superclass_inconsistent)
3120 << ToIface->getDeclName();
3121 if (ToIface->getSuperClass())
3122 Importer.ToDiag(ToIface->getSuperClassLoc(),
3123 diag::note_odr_objc_superclass)
3124 << ToIface->getSuperClass()->getDeclName();
3125 else
3126 Importer.ToDiag(ToIface->getLocation(),
3127 diag::note_odr_objc_missing_superclass);
3128 if (D->getSuperClass())
3129 Importer.FromDiag(D->getSuperClassLoc(),
3130 diag::note_odr_objc_superclass)
3131 << D->getSuperClass()->getDeclName();
3132 else
3133 Importer.FromDiag(D->getLocation(),
3134 diag::note_odr_objc_missing_superclass);
3135 return 0;
3136 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003137 }
3138
Douglas Gregorb4677b62010-02-18 01:47:50 +00003139 // Import categories. When the categories themselves are imported, they'll
3140 // hook themselves into this interface.
3141 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3142 FromCat = FromCat->getNextClassCategory())
3143 Importer.Import(FromCat);
3144
Douglas Gregora12d2942010-02-16 01:20:57 +00003145 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003146 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003147
3148 // If we have an @implementation, import it as well.
3149 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003150 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3151 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003152 if (!Impl)
3153 return 0;
3154
3155 ToIface->setImplementation(Impl);
3156 }
3157
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003158 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003159}
3160
Douglas Gregor3daef292010-12-07 15:32:12 +00003161Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3162 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3163 Importer.Import(D->getCategoryDecl()));
3164 if (!Category)
3165 return 0;
3166
3167 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3168 if (!ToImpl) {
3169 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3170 if (!DC)
3171 return 0;
3172
3173 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3174 Importer.Import(D->getLocation()),
3175 Importer.Import(D->getIdentifier()),
3176 Category->getClassInterface());
3177
3178 DeclContext *LexicalDC = DC;
3179 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3180 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3181 if (!LexicalDC)
3182 return 0;
3183
3184 ToImpl->setLexicalDeclContext(LexicalDC);
3185 }
3186
3187 LexicalDC->addDecl(ToImpl);
3188 Category->setImplementation(ToImpl);
3189 }
3190
3191 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003192 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003193 return ToImpl;
3194}
3195
Douglas Gregordd182ff2010-12-07 01:26:03 +00003196Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3197 // Find the corresponding interface.
3198 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3199 Importer.Import(D->getClassInterface()));
3200 if (!Iface)
3201 return 0;
3202
3203 // Import the superclass, if any.
3204 ObjCInterfaceDecl *Super = 0;
3205 if (D->getSuperClass()) {
3206 Super = cast_or_null<ObjCInterfaceDecl>(
3207 Importer.Import(D->getSuperClass()));
3208 if (!Super)
3209 return 0;
3210 }
3211
3212 ObjCImplementationDecl *Impl = Iface->getImplementation();
3213 if (!Impl) {
3214 // We haven't imported an implementation yet. Create a new @implementation
3215 // now.
3216 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3217 Importer.ImportContext(D->getDeclContext()),
3218 Importer.Import(D->getLocation()),
3219 Iface, Super);
3220
3221 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3222 DeclContext *LexicalDC
3223 = Importer.ImportContext(D->getLexicalDeclContext());
3224 if (!LexicalDC)
3225 return 0;
3226 Impl->setLexicalDeclContext(LexicalDC);
3227 }
3228
3229 // Associate the implementation with the class it implements.
3230 Iface->setImplementation(Impl);
3231 Importer.Imported(D, Iface->getImplementation());
3232 } else {
3233 Importer.Imported(D, Iface->getImplementation());
3234
3235 // Verify that the existing @implementation has the same superclass.
3236 if ((Super && !Impl->getSuperClass()) ||
3237 (!Super && Impl->getSuperClass()) ||
3238 (Super && Impl->getSuperClass() &&
3239 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3240 Importer.ToDiag(Impl->getLocation(),
3241 diag::err_odr_objc_superclass_inconsistent)
3242 << Iface->getDeclName();
3243 // FIXME: It would be nice to have the location of the superclass
3244 // below.
3245 if (Impl->getSuperClass())
3246 Importer.ToDiag(Impl->getLocation(),
3247 diag::note_odr_objc_superclass)
3248 << Impl->getSuperClass()->getDeclName();
3249 else
3250 Importer.ToDiag(Impl->getLocation(),
3251 diag::note_odr_objc_missing_superclass);
3252 if (D->getSuperClass())
3253 Importer.FromDiag(D->getLocation(),
3254 diag::note_odr_objc_superclass)
3255 << D->getSuperClass()->getDeclName();
3256 else
3257 Importer.FromDiag(D->getLocation(),
3258 diag::note_odr_objc_missing_superclass);
3259 return 0;
3260 }
3261 }
3262
3263 // Import all of the members of this @implementation.
3264 ImportDeclContext(D);
3265
3266 return Impl;
3267}
3268
Douglas Gregore3261622010-02-17 18:02:10 +00003269Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3270 // Import the major distinguishing characteristics of an @property.
3271 DeclContext *DC, *LexicalDC;
3272 DeclarationName Name;
3273 SourceLocation Loc;
3274 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3275 return 0;
3276
3277 // Check whether we have already imported this property.
3278 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3279 Lookup.first != Lookup.second;
3280 ++Lookup.first) {
3281 if (ObjCPropertyDecl *FoundProp
3282 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3283 // Check property types.
3284 if (!Importer.IsStructurallyEquivalent(D->getType(),
3285 FoundProp->getType())) {
3286 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3287 << Name << D->getType() << FoundProp->getType();
3288 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3289 << FoundProp->getType();
3290 return 0;
3291 }
3292
3293 // FIXME: Check property attributes, getters, setters, etc.?
3294
3295 // Consider these properties to be equivalent.
3296 Importer.Imported(D, FoundProp);
3297 return FoundProp;
3298 }
3299 }
3300
3301 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003302 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3303 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003304 return 0;
3305
3306 // Create the new property.
3307 ObjCPropertyDecl *ToProperty
3308 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3309 Name.getAsIdentifierInfo(),
3310 Importer.Import(D->getAtLoc()),
3311 T,
3312 D->getPropertyImplementation());
3313 Importer.Imported(D, ToProperty);
3314 ToProperty->setLexicalDeclContext(LexicalDC);
3315 LexicalDC->addDecl(ToProperty);
3316
3317 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003318 ToProperty->setPropertyAttributesAsWritten(
3319 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003320 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3321 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3322 ToProperty->setGetterMethodDecl(
3323 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3324 ToProperty->setSetterMethodDecl(
3325 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3326 ToProperty->setPropertyIvarDecl(
3327 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3328 return ToProperty;
3329}
3330
Douglas Gregor954e0c72010-12-07 18:32:03 +00003331Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3332 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3333 Importer.Import(D->getPropertyDecl()));
3334 if (!Property)
3335 return 0;
3336
3337 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3338 if (!DC)
3339 return 0;
3340
3341 // Import the lexical declaration context.
3342 DeclContext *LexicalDC = DC;
3343 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3344 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3345 if (!LexicalDC)
3346 return 0;
3347 }
3348
3349 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3350 if (!InImpl)
3351 return 0;
3352
3353 // Import the ivar (for an @synthesize).
3354 ObjCIvarDecl *Ivar = 0;
3355 if (D->getPropertyIvarDecl()) {
3356 Ivar = cast_or_null<ObjCIvarDecl>(
3357 Importer.Import(D->getPropertyIvarDecl()));
3358 if (!Ivar)
3359 return 0;
3360 }
3361
3362 ObjCPropertyImplDecl *ToImpl
3363 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3364 if (!ToImpl) {
3365 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3366 Importer.Import(D->getLocStart()),
3367 Importer.Import(D->getLocation()),
3368 Property,
3369 D->getPropertyImplementation(),
3370 Ivar,
3371 Importer.Import(D->getPropertyIvarDeclLoc()));
3372 ToImpl->setLexicalDeclContext(LexicalDC);
3373 Importer.Imported(D, ToImpl);
3374 LexicalDC->addDecl(ToImpl);
3375 } else {
3376 // Check that we have the same kind of property implementation (@synthesize
3377 // vs. @dynamic).
3378 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3379 Importer.ToDiag(ToImpl->getLocation(),
3380 diag::err_odr_objc_property_impl_kind_inconsistent)
3381 << Property->getDeclName()
3382 << (ToImpl->getPropertyImplementation()
3383 == ObjCPropertyImplDecl::Dynamic);
3384 Importer.FromDiag(D->getLocation(),
3385 diag::note_odr_objc_property_impl_kind)
3386 << D->getPropertyDecl()->getDeclName()
3387 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3388 return 0;
3389 }
3390
3391 // For @synthesize, check that we have the same
3392 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3393 Ivar != ToImpl->getPropertyIvarDecl()) {
3394 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3395 diag::err_odr_objc_synthesize_ivar_inconsistent)
3396 << Property->getDeclName()
3397 << ToImpl->getPropertyIvarDecl()->getDeclName()
3398 << Ivar->getDeclName();
3399 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3400 diag::note_odr_objc_synthesize_ivar_here)
3401 << D->getPropertyIvarDecl()->getDeclName();
3402 return 0;
3403 }
3404
3405 // Merge the existing implementation with the new implementation.
3406 Importer.Imported(D, ToImpl);
3407 }
3408
3409 return ToImpl;
3410}
3411
Douglas Gregor2b785022010-02-18 02:12:22 +00003412Decl *
3413ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3414 // Import the context of this declaration.
3415 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3416 if (!DC)
3417 return 0;
3418
3419 DeclContext *LexicalDC = DC;
3420 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3421 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3422 if (!LexicalDC)
3423 return 0;
3424 }
3425
3426 // Import the location of this declaration.
3427 SourceLocation Loc = Importer.Import(D->getLocation());
3428
3429 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3430 llvm::SmallVector<SourceLocation, 4> Locations;
3431 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3432 = D->protocol_loc_begin();
3433 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3434 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3435 FromProto != FromProtoEnd;
3436 ++FromProto, ++FromProtoLoc) {
3437 ObjCProtocolDecl *ToProto
3438 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3439 if (!ToProto)
3440 continue;
3441
3442 Protocols.push_back(ToProto);
3443 Locations.push_back(Importer.Import(*FromProtoLoc));
3444 }
3445
3446 ObjCForwardProtocolDecl *ToForward
3447 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3448 Protocols.data(), Protocols.size(),
3449 Locations.data());
3450 ToForward->setLexicalDeclContext(LexicalDC);
3451 LexicalDC->addDecl(ToForward);
3452 Importer.Imported(D, ToForward);
3453 return ToForward;
3454}
3455
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003456Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3457 // Import the context of this declaration.
3458 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3459 if (!DC)
3460 return 0;
3461
3462 DeclContext *LexicalDC = DC;
3463 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3464 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3465 if (!LexicalDC)
3466 return 0;
3467 }
3468
3469 // Import the location of this declaration.
3470 SourceLocation Loc = Importer.Import(D->getLocation());
3471
3472 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3473 llvm::SmallVector<SourceLocation, 4> Locations;
3474 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3475 From != FromEnd; ++From) {
3476 ObjCInterfaceDecl *ToIface
3477 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3478 if (!ToIface)
3479 continue;
3480
3481 Interfaces.push_back(ToIface);
3482 Locations.push_back(Importer.Import(From->getLocation()));
3483 }
3484
3485 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3486 Loc,
3487 Interfaces.data(),
3488 Locations.data(),
3489 Interfaces.size());
3490 ToClass->setLexicalDeclContext(LexicalDC);
3491 LexicalDC->addDecl(ToClass);
3492 Importer.Imported(D, ToClass);
3493 return ToClass;
3494}
3495
Douglas Gregor040afae2010-11-30 19:14:50 +00003496Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3497 // For template arguments, we adopt the translation unit as our declaration
3498 // context. This context will be fixed when the actual template declaration
3499 // is created.
3500
3501 // FIXME: Import default argument.
3502 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3503 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003504 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003505 Importer.Import(D->getLocation()),
3506 D->getDepth(),
3507 D->getIndex(),
3508 Importer.Import(D->getIdentifier()),
3509 D->wasDeclaredWithTypename(),
3510 D->isParameterPack());
3511}
3512
3513Decl *
3514ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3515 // Import the name of this declaration.
3516 DeclarationName Name = Importer.Import(D->getDeclName());
3517 if (D->getDeclName() && !Name)
3518 return 0;
3519
3520 // Import the location of this declaration.
3521 SourceLocation Loc = Importer.Import(D->getLocation());
3522
3523 // Import the type of this declaration.
3524 QualType T = Importer.Import(D->getType());
3525 if (T.isNull())
3526 return 0;
3527
3528 // Import type-source information.
3529 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3530 if (D->getTypeSourceInfo() && !TInfo)
3531 return 0;
3532
3533 // FIXME: Import default argument.
3534
3535 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3536 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003537 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003538 Loc, D->getDepth(), D->getPosition(),
3539 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003540 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003541}
3542
3543Decl *
3544ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3545 // Import the name of this declaration.
3546 DeclarationName Name = Importer.Import(D->getDeclName());
3547 if (D->getDeclName() && !Name)
3548 return 0;
3549
3550 // Import the location of this declaration.
3551 SourceLocation Loc = Importer.Import(D->getLocation());
3552
3553 // Import template parameters.
3554 TemplateParameterList *TemplateParams
3555 = ImportTemplateParameterList(D->getTemplateParameters());
3556 if (!TemplateParams)
3557 return 0;
3558
3559 // FIXME: Import default argument.
3560
3561 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3562 Importer.getToContext().getTranslationUnitDecl(),
3563 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003564 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003565 Name.getAsIdentifierInfo(),
3566 TemplateParams);
3567}
3568
3569Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3570 // If this record has a definition in the translation unit we're coming from,
3571 // but this particular declaration is not that definition, import the
3572 // definition and map to that.
3573 CXXRecordDecl *Definition
3574 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3575 if (Definition && Definition != D->getTemplatedDecl()) {
3576 Decl *ImportedDef
3577 = Importer.Import(Definition->getDescribedClassTemplate());
3578 if (!ImportedDef)
3579 return 0;
3580
3581 return Importer.Imported(D, ImportedDef);
3582 }
3583
3584 // Import the major distinguishing characteristics of this class template.
3585 DeclContext *DC, *LexicalDC;
3586 DeclarationName Name;
3587 SourceLocation Loc;
3588 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3589 return 0;
3590
3591 // We may already have a template of the same name; try to find and match it.
3592 if (!DC->isFunctionOrMethod()) {
3593 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3594 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3595 Lookup.first != Lookup.second;
3596 ++Lookup.first) {
3597 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3598 continue;
3599
3600 Decl *Found = *Lookup.first;
3601 if (ClassTemplateDecl *FoundTemplate
3602 = dyn_cast<ClassTemplateDecl>(Found)) {
3603 if (IsStructuralMatch(D, FoundTemplate)) {
3604 // The class templates structurally match; call it the same template.
3605 // FIXME: We may be filling in a forward declaration here. Handle
3606 // this case!
3607 Importer.Imported(D->getTemplatedDecl(),
3608 FoundTemplate->getTemplatedDecl());
3609 return Importer.Imported(D, FoundTemplate);
3610 }
3611 }
3612
3613 ConflictingDecls.push_back(*Lookup.first);
3614 }
3615
3616 if (!ConflictingDecls.empty()) {
3617 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3618 ConflictingDecls.data(),
3619 ConflictingDecls.size());
3620 }
3621
3622 if (!Name)
3623 return 0;
3624 }
3625
3626 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3627
3628 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003629 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3630 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003631 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3632 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003633 DC, StartLoc, IdLoc,
3634 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003635 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003636 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003637 D2Templated->setLexicalDeclContext(LexicalDC);
3638
3639 // Create the class template declaration itself.
3640 TemplateParameterList *TemplateParams
3641 = ImportTemplateParameterList(D->getTemplateParameters());
3642 if (!TemplateParams)
3643 return 0;
3644
3645 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3646 Loc, Name, TemplateParams,
3647 D2Templated,
3648 /*PrevDecl=*/0);
3649 D2Templated->setDescribedClassTemplate(D2);
3650
3651 D2->setAccess(D->getAccess());
3652 D2->setLexicalDeclContext(LexicalDC);
3653 LexicalDC->addDecl(D2);
3654
3655 // Note the relationship between the class templates.
3656 Importer.Imported(D, D2);
3657 Importer.Imported(DTemplated, D2Templated);
3658
3659 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3660 // FIXME: Import definition!
3661 }
3662
3663 return D2;
3664}
3665
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003666Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3667 ClassTemplateSpecializationDecl *D) {
3668 // If this record has a definition in the translation unit we're coming from,
3669 // but this particular declaration is not that definition, import the
3670 // definition and map to that.
3671 TagDecl *Definition = D->getDefinition();
3672 if (Definition && Definition != D) {
3673 Decl *ImportedDef = Importer.Import(Definition);
3674 if (!ImportedDef)
3675 return 0;
3676
3677 return Importer.Imported(D, ImportedDef);
3678 }
3679
3680 ClassTemplateDecl *ClassTemplate
3681 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3682 D->getSpecializedTemplate()));
3683 if (!ClassTemplate)
3684 return 0;
3685
3686 // Import the context of this declaration.
3687 DeclContext *DC = ClassTemplate->getDeclContext();
3688 if (!DC)
3689 return 0;
3690
3691 DeclContext *LexicalDC = DC;
3692 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3693 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3694 if (!LexicalDC)
3695 return 0;
3696 }
3697
3698 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003699 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3700 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003701
3702 // Import template arguments.
3703 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3704 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3705 D->getTemplateArgs().size(),
3706 TemplateArgs))
3707 return 0;
3708
3709 // Try to find an existing specialization with these template arguments.
3710 void *InsertPos = 0;
3711 ClassTemplateSpecializationDecl *D2
3712 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3713 TemplateArgs.size(), InsertPos);
3714 if (D2) {
3715 // We already have a class template specialization with these template
3716 // arguments.
3717
3718 // FIXME: Check for specialization vs. instantiation errors.
3719
3720 if (RecordDecl *FoundDef = D2->getDefinition()) {
3721 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3722 // The record types structurally match, or the "from" translation
3723 // unit only had a forward declaration anyway; call it the same
3724 // function.
3725 return Importer.Imported(D, FoundDef);
3726 }
3727 }
3728 } else {
3729 // Create a new specialization.
3730 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3731 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003732 StartLoc, IdLoc,
3733 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003734 TemplateArgs.data(),
3735 TemplateArgs.size(),
3736 /*PrevDecl=*/0);
3737 D2->setSpecializationKind(D->getSpecializationKind());
3738
3739 // Add this specialization to the class template.
3740 ClassTemplate->AddSpecialization(D2, InsertPos);
3741
3742 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003743 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003744
3745 // Add the specialization to this context.
3746 D2->setLexicalDeclContext(LexicalDC);
3747 LexicalDC->addDecl(D2);
3748 }
3749 Importer.Imported(D, D2);
3750
3751 if (D->isDefinition() && ImportDefinition(D, D2))
3752 return 0;
3753
3754 return D2;
3755}
3756
Douglas Gregor4800d952010-02-11 19:21:55 +00003757//----------------------------------------------------------------------------
3758// Import Statements
3759//----------------------------------------------------------------------------
3760
3761Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3762 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3763 << S->getStmtClassName();
3764 return 0;
3765}
3766
3767//----------------------------------------------------------------------------
3768// Import Expressions
3769//----------------------------------------------------------------------------
3770Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3771 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3772 << E->getStmtClassName();
3773 return 0;
3774}
3775
Douglas Gregor44080632010-02-19 01:17:02 +00003776Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003777 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3778 if (!ToD)
3779 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003780
3781 NamedDecl *FoundD = 0;
3782 if (E->getDecl() != E->getFoundDecl()) {
3783 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3784 if (!FoundD)
3785 return 0;
3786 }
Douglas Gregor44080632010-02-19 01:17:02 +00003787
3788 QualType T = Importer.Import(E->getType());
3789 if (T.isNull())
3790 return 0;
3791
Douglas Gregor40d96a62011-02-28 21:54:11 +00003792 return DeclRefExpr::Create(Importer.getToContext(),
3793 Importer.Import(E->getQualifierLoc()),
Douglas Gregor44080632010-02-19 01:17:02 +00003794 ToD,
3795 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003796 T, E->getValueKind(),
Chandler Carruth3aa81402011-05-01 23:48:14 +00003797 FoundD,
Douglas Gregor44080632010-02-19 01:17:02 +00003798 /*FIXME:TemplateArgs=*/0);
3799}
3800
Douglas Gregor4800d952010-02-11 19:21:55 +00003801Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3802 QualType T = Importer.Import(E->getType());
3803 if (T.isNull())
3804 return 0;
3805
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003806 return IntegerLiteral::Create(Importer.getToContext(),
3807 E->getValue(), T,
3808 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003809}
3810
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003811Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3812 QualType T = Importer.Import(E->getType());
3813 if (T.isNull())
3814 return 0;
3815
3816 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3817 E->isWide(), T,
3818 Importer.Import(E->getLocation()));
3819}
3820
Douglas Gregorf638f952010-02-19 01:07:06 +00003821Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3822 Expr *SubExpr = Importer.Import(E->getSubExpr());
3823 if (!SubExpr)
3824 return 0;
3825
3826 return new (Importer.getToContext())
3827 ParenExpr(Importer.Import(E->getLParen()),
3828 Importer.Import(E->getRParen()),
3829 SubExpr);
3830}
3831
3832Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3833 QualType T = Importer.Import(E->getType());
3834 if (T.isNull())
3835 return 0;
3836
3837 Expr *SubExpr = Importer.Import(E->getSubExpr());
3838 if (!SubExpr)
3839 return 0;
3840
3841 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003842 T, E->getValueKind(),
3843 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003844 Importer.Import(E->getOperatorLoc()));
3845}
3846
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003847Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3848 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003849 QualType ResultType = Importer.Import(E->getType());
3850
3851 if (E->isArgumentType()) {
3852 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3853 if (!TInfo)
3854 return 0;
3855
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003856 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3857 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003858 Importer.Import(E->getOperatorLoc()),
3859 Importer.Import(E->getRParenLoc()));
3860 }
3861
3862 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3863 if (!SubExpr)
3864 return 0;
3865
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003866 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3867 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003868 Importer.Import(E->getOperatorLoc()),
3869 Importer.Import(E->getRParenLoc()));
3870}
3871
Douglas Gregorf638f952010-02-19 01:07:06 +00003872Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3873 QualType T = Importer.Import(E->getType());
3874 if (T.isNull())
3875 return 0;
3876
3877 Expr *LHS = Importer.Import(E->getLHS());
3878 if (!LHS)
3879 return 0;
3880
3881 Expr *RHS = Importer.Import(E->getRHS());
3882 if (!RHS)
3883 return 0;
3884
3885 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003886 T, E->getValueKind(),
3887 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003888 Importer.Import(E->getOperatorLoc()));
3889}
3890
3891Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3892 QualType T = Importer.Import(E->getType());
3893 if (T.isNull())
3894 return 0;
3895
3896 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3897 if (CompLHSType.isNull())
3898 return 0;
3899
3900 QualType CompResultType = Importer.Import(E->getComputationResultType());
3901 if (CompResultType.isNull())
3902 return 0;
3903
3904 Expr *LHS = Importer.Import(E->getLHS());
3905 if (!LHS)
3906 return 0;
3907
3908 Expr *RHS = Importer.Import(E->getRHS());
3909 if (!RHS)
3910 return 0;
3911
3912 return new (Importer.getToContext())
3913 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003914 T, E->getValueKind(),
3915 E->getObjectKind(),
3916 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003917 Importer.Import(E->getOperatorLoc()));
3918}
3919
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003920static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003921 if (E->path_empty()) return false;
3922
3923 // TODO: import cast paths
3924 return true;
3925}
3926
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003927Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3928 QualType T = Importer.Import(E->getType());
3929 if (T.isNull())
3930 return 0;
3931
3932 Expr *SubExpr = Importer.Import(E->getSubExpr());
3933 if (!SubExpr)
3934 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003935
3936 CXXCastPath BasePath;
3937 if (ImportCastPath(E, BasePath))
3938 return 0;
3939
3940 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003941 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003942}
3943
Douglas Gregor008847a2010-02-19 01:32:14 +00003944Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3945 QualType T = Importer.Import(E->getType());
3946 if (T.isNull())
3947 return 0;
3948
3949 Expr *SubExpr = Importer.Import(E->getSubExpr());
3950 if (!SubExpr)
3951 return 0;
3952
3953 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3954 if (!TInfo && E->getTypeInfoAsWritten())
3955 return 0;
3956
John McCallf871d0c2010-08-07 06:22:56 +00003957 CXXCastPath BasePath;
3958 if (ImportCastPath(E, BasePath))
3959 return 0;
3960
John McCallf89e55a2010-11-18 06:31:45 +00003961 return CStyleCastExpr::Create(Importer.getToContext(), T,
3962 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003963 SubExpr, &BasePath, TInfo,
3964 Importer.Import(E->getLParenLoc()),
3965 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003966}
3967
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003968ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00003969 ASTContext &FromContext, FileManager &FromFileManager,
3970 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003971 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00003972 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3973 Minimal(MinimalImport)
3974{
Douglas Gregor9bed8792010-02-09 19:21:46 +00003975 ImportedDecls[FromContext.getTranslationUnitDecl()]
3976 = ToContext.getTranslationUnitDecl();
3977}
3978
3979ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003980
3981QualType ASTImporter::Import(QualType FromT) {
3982 if (FromT.isNull())
3983 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00003984
3985 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003986
Douglas Gregor169fba52010-02-08 15:18:58 +00003987 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00003988 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3989 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00003990 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00003991 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003992
Douglas Gregor169fba52010-02-08 15:18:58 +00003993 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003994 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00003995 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003996 if (ToT.isNull())
3997 return ToT;
3998
Douglas Gregor169fba52010-02-08 15:18:58 +00003999 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004000 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004001
John McCallf4c73712011-01-19 06:33:43 +00004002 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004003}
4004
Douglas Gregor9bed8792010-02-09 19:21:46 +00004005TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004006 if (!FromTSI)
4007 return FromTSI;
4008
4009 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004010 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004011 QualType T = Import(FromTSI->getType());
4012 if (T.isNull())
4013 return 0;
4014
4015 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004016 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004017}
4018
4019Decl *ASTImporter::Import(Decl *FromD) {
4020 if (!FromD)
4021 return 0;
4022
4023 // Check whether we've already imported this declaration.
4024 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
4025 if (Pos != ImportedDecls.end())
4026 return Pos->second;
4027
4028 // Import the type
4029 ASTNodeImporter Importer(*this);
4030 Decl *ToD = Importer.Visit(FromD);
4031 if (!ToD)
4032 return 0;
4033
4034 // Record the imported declaration.
4035 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004036
4037 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4038 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004039 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004040 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004041 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004042 // When we've finished transforming a typedef, see whether it was the
4043 // typedef for an anonymous tag.
4044 for (llvm::SmallVector<TagDecl *, 4>::iterator
4045 FromTag = AnonTagsWithPendingTypedefs.begin(),
4046 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4047 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004048 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004049 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4050 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004051 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004052 AnonTagsWithPendingTypedefs.erase(FromTag);
4053 break;
4054 }
4055 }
4056 }
4057 }
4058
Douglas Gregor9bed8792010-02-09 19:21:46 +00004059 return ToD;
4060}
4061
4062DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4063 if (!FromDC)
4064 return FromDC;
4065
4066 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4067}
4068
4069Expr *ASTImporter::Import(Expr *FromE) {
4070 if (!FromE)
4071 return 0;
4072
4073 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4074}
4075
4076Stmt *ASTImporter::Import(Stmt *FromS) {
4077 if (!FromS)
4078 return 0;
4079
Douglas Gregor4800d952010-02-11 19:21:55 +00004080 // Check whether we've already imported this declaration.
4081 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4082 if (Pos != ImportedStmts.end())
4083 return Pos->second;
4084
4085 // Import the type
4086 ASTNodeImporter Importer(*this);
4087 Stmt *ToS = Importer.Visit(FromS);
4088 if (!ToS)
4089 return 0;
4090
4091 // Record the imported declaration.
4092 ImportedStmts[FromS] = ToS;
4093 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004094}
4095
4096NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4097 if (!FromNNS)
4098 return 0;
4099
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004100 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4101
4102 switch (FromNNS->getKind()) {
4103 case NestedNameSpecifier::Identifier:
4104 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4105 return NestedNameSpecifier::Create(ToContext, prefix, II);
4106 }
4107 return 0;
4108
4109 case NestedNameSpecifier::Namespace:
4110 if (NamespaceDecl *NS =
4111 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4112 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4113 }
4114 return 0;
4115
4116 case NestedNameSpecifier::NamespaceAlias:
4117 if (NamespaceAliasDecl *NSAD =
4118 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4119 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4120 }
4121 return 0;
4122
4123 case NestedNameSpecifier::Global:
4124 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4125
4126 case NestedNameSpecifier::TypeSpec:
4127 case NestedNameSpecifier::TypeSpecWithTemplate: {
4128 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4129 if (!T.isNull()) {
4130 bool bTemplate = FromNNS->getKind() ==
4131 NestedNameSpecifier::TypeSpecWithTemplate;
4132 return NestedNameSpecifier::Create(ToContext, prefix,
4133 bTemplate, T.getTypePtr());
4134 }
4135 }
4136 return 0;
4137 }
4138
4139 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004140 return 0;
4141}
4142
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004143NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4144 // FIXME: Implement!
4145 return NestedNameSpecifierLoc();
4146}
4147
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004148TemplateName ASTImporter::Import(TemplateName From) {
4149 switch (From.getKind()) {
4150 case TemplateName::Template:
4151 if (TemplateDecl *ToTemplate
4152 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4153 return TemplateName(ToTemplate);
4154
4155 return TemplateName();
4156
4157 case TemplateName::OverloadedTemplate: {
4158 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4159 UnresolvedSet<2> ToTemplates;
4160 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4161 E = FromStorage->end();
4162 I != E; ++I) {
4163 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4164 ToTemplates.addDecl(To);
4165 else
4166 return TemplateName();
4167 }
4168 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4169 ToTemplates.end());
4170 }
4171
4172 case TemplateName::QualifiedTemplate: {
4173 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4174 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4175 if (!Qualifier)
4176 return TemplateName();
4177
4178 if (TemplateDecl *ToTemplate
4179 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4180 return ToContext.getQualifiedTemplateName(Qualifier,
4181 QTN->hasTemplateKeyword(),
4182 ToTemplate);
4183
4184 return TemplateName();
4185 }
4186
4187 case TemplateName::DependentTemplate: {
4188 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4189 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4190 if (!Qualifier)
4191 return TemplateName();
4192
4193 if (DTN->isIdentifier()) {
4194 return ToContext.getDependentTemplateName(Qualifier,
4195 Import(DTN->getIdentifier()));
4196 }
4197
4198 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4199 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004200
4201 case TemplateName::SubstTemplateTemplateParmPack: {
4202 SubstTemplateTemplateParmPackStorage *SubstPack
4203 = From.getAsSubstTemplateTemplateParmPack();
4204 TemplateTemplateParmDecl *Param
4205 = cast_or_null<TemplateTemplateParmDecl>(
4206 Import(SubstPack->getParameterPack()));
4207 if (!Param)
4208 return TemplateName();
4209
4210 ASTNodeImporter Importer(*this);
4211 TemplateArgument ArgPack
4212 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4213 if (ArgPack.isNull())
4214 return TemplateName();
4215
4216 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4217 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004218 }
4219
4220 llvm_unreachable("Invalid template name kind");
4221 return TemplateName();
4222}
4223
Douglas Gregor9bed8792010-02-09 19:21:46 +00004224SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4225 if (FromLoc.isInvalid())
4226 return SourceLocation();
4227
Douglas Gregor88523732010-02-10 00:15:17 +00004228 SourceManager &FromSM = FromContext.getSourceManager();
4229
4230 // For now, map everything down to its spelling location, so that we
4231 // don't have to import macro instantiations.
4232 // FIXME: Import macro instantiations!
4233 FromLoc = FromSM.getSpellingLoc(FromLoc);
4234 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4235 SourceManager &ToSM = ToContext.getSourceManager();
4236 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4237 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004238}
4239
4240SourceRange ASTImporter::Import(SourceRange FromRange) {
4241 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4242}
4243
Douglas Gregor88523732010-02-10 00:15:17 +00004244FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004245 llvm::DenseMap<FileID, FileID>::iterator Pos
4246 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004247 if (Pos != ImportedFileIDs.end())
4248 return Pos->second;
4249
4250 SourceManager &FromSM = FromContext.getSourceManager();
4251 SourceManager &ToSM = ToContext.getSourceManager();
4252 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4253 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4254
4255 // Include location of this file.
4256 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4257
4258 // Map the FileID for to the "to" source manager.
4259 FileID ToID;
4260 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004261 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004262 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4263 // disk again
4264 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4265 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004266 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004267 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4268 FromSLoc.getFile().getFileCharacteristic());
4269 } else {
4270 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004271 const llvm::MemoryBuffer *
4272 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004273 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004274 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004275 FromBuf->getBufferIdentifier());
4276 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4277 }
4278
4279
Sebastian Redl535a3e22010-09-30 01:03:06 +00004280 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004281 return ToID;
4282}
4283
Douglas Gregord8868a62011-01-18 03:11:38 +00004284void ASTImporter::ImportDefinition(Decl *From) {
4285 Decl *To = Import(From);
4286 if (!To)
4287 return;
4288
4289 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4290 ASTNodeImporter Importer(*this);
4291 Importer.ImportDeclContext(FromDC, true);
4292 }
4293}
4294
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004295DeclarationName ASTImporter::Import(DeclarationName FromName) {
4296 if (!FromName)
4297 return DeclarationName();
4298
4299 switch (FromName.getNameKind()) {
4300 case DeclarationName::Identifier:
4301 return Import(FromName.getAsIdentifierInfo());
4302
4303 case DeclarationName::ObjCZeroArgSelector:
4304 case DeclarationName::ObjCOneArgSelector:
4305 case DeclarationName::ObjCMultiArgSelector:
4306 return Import(FromName.getObjCSelector());
4307
4308 case DeclarationName::CXXConstructorName: {
4309 QualType T = Import(FromName.getCXXNameType());
4310 if (T.isNull())
4311 return DeclarationName();
4312
4313 return ToContext.DeclarationNames.getCXXConstructorName(
4314 ToContext.getCanonicalType(T));
4315 }
4316
4317 case DeclarationName::CXXDestructorName: {
4318 QualType T = Import(FromName.getCXXNameType());
4319 if (T.isNull())
4320 return DeclarationName();
4321
4322 return ToContext.DeclarationNames.getCXXDestructorName(
4323 ToContext.getCanonicalType(T));
4324 }
4325
4326 case DeclarationName::CXXConversionFunctionName: {
4327 QualType T = Import(FromName.getCXXNameType());
4328 if (T.isNull())
4329 return DeclarationName();
4330
4331 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4332 ToContext.getCanonicalType(T));
4333 }
4334
4335 case DeclarationName::CXXOperatorName:
4336 return ToContext.DeclarationNames.getCXXOperatorName(
4337 FromName.getCXXOverloadedOperator());
4338
4339 case DeclarationName::CXXLiteralOperatorName:
4340 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4341 Import(FromName.getCXXLiteralIdentifier()));
4342
4343 case DeclarationName::CXXUsingDirective:
4344 // FIXME: STATICS!
4345 return DeclarationName::getUsingDirectiveName();
4346 }
4347
4348 // Silence bogus GCC warning
4349 return DeclarationName();
4350}
4351
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004352IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004353 if (!FromId)
4354 return 0;
4355
4356 return &ToContext.Idents.get(FromId->getName());
4357}
Douglas Gregor089459a2010-02-08 21:09:39 +00004358
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004359Selector ASTImporter::Import(Selector FromSel) {
4360 if (FromSel.isNull())
4361 return Selector();
4362
4363 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4364 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4365 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4366 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4367 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4368}
4369
Douglas Gregor089459a2010-02-08 21:09:39 +00004370DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4371 DeclContext *DC,
4372 unsigned IDNS,
4373 NamedDecl **Decls,
4374 unsigned NumDecls) {
4375 return Name;
4376}
4377
4378DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004379 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004380}
4381
4382DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004383 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004384}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004385
4386Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4387 ImportedDecls[From] = To;
4388 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004389}
Douglas Gregorea35d112010-02-15 23:54:17 +00004390
4391bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004392 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004393 = ImportedTypes.find(From.getTypePtr());
4394 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4395 return true;
4396
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004397 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004398 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004399}