blob: d68d240346fd1cfae644f38a4cf37d18a45b68be [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);
Sean Callanan673e7752011-07-19 22:38:25 +000089 bool ImportDefinition(RecordDecl *From, RecordDecl *To, bool ForceImport = false);
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) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001770 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001771 return;
1772 }
1773
Douglas Gregor083a8212010-02-21 18:24:45 +00001774 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1775 FromEnd = FromDC->decls_end();
1776 From != FromEnd;
1777 ++From)
1778 Importer.Import(*From);
1779}
1780
Sean Callanan673e7752011-07-19 22:38:25 +00001781bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To, bool ForceImport) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001782 if (To->getDefinition())
1783 return false;
1784
1785 To->startDefinition();
1786
1787 // Add base classes.
1788 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1789 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1790
1791 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1792 for (CXXRecordDecl::base_class_iterator
1793 Base1 = FromCXX->bases_begin(),
1794 FromBaseEnd = FromCXX->bases_end();
1795 Base1 != FromBaseEnd;
1796 ++Base1) {
1797 QualType T = Importer.Import(Base1->getType());
1798 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001799 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001800
1801 SourceLocation EllipsisLoc;
1802 if (Base1->isPackExpansion())
1803 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001804
1805 Bases.push_back(
1806 new (Importer.getToContext())
1807 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1808 Base1->isVirtual(),
1809 Base1->isBaseOfClass(),
1810 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001811 Importer.Import(Base1->getTypeSourceInfo()),
1812 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001813 }
1814 if (!Bases.empty())
1815 ToCXX->setBases(Bases.data(), Bases.size());
1816 }
1817
Sean Callanan673e7752011-07-19 22:38:25 +00001818 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001819 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001820 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001821}
1822
Douglas Gregor040afae2010-11-30 19:14:50 +00001823TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1824 TemplateParameterList *Params) {
1825 llvm::SmallVector<NamedDecl *, 4> ToParams;
1826 ToParams.reserve(Params->size());
1827 for (TemplateParameterList::iterator P = Params->begin(),
1828 PEnd = Params->end();
1829 P != PEnd; ++P) {
1830 Decl *To = Importer.Import(*P);
1831 if (!To)
1832 return 0;
1833
1834 ToParams.push_back(cast<NamedDecl>(To));
1835 }
1836
1837 return TemplateParameterList::Create(Importer.getToContext(),
1838 Importer.Import(Params->getTemplateLoc()),
1839 Importer.Import(Params->getLAngleLoc()),
1840 ToParams.data(), ToParams.size(),
1841 Importer.Import(Params->getRAngleLoc()));
1842}
1843
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001844TemplateArgument
1845ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1846 switch (From.getKind()) {
1847 case TemplateArgument::Null:
1848 return TemplateArgument();
1849
1850 case TemplateArgument::Type: {
1851 QualType ToType = Importer.Import(From.getAsType());
1852 if (ToType.isNull())
1853 return TemplateArgument();
1854 return TemplateArgument(ToType);
1855 }
1856
1857 case TemplateArgument::Integral: {
1858 QualType ToType = Importer.Import(From.getIntegralType());
1859 if (ToType.isNull())
1860 return TemplateArgument();
1861 return TemplateArgument(*From.getAsIntegral(), ToType);
1862 }
1863
1864 case TemplateArgument::Declaration:
1865 if (Decl *To = Importer.Import(From.getAsDecl()))
1866 return TemplateArgument(To);
1867 return TemplateArgument();
1868
1869 case TemplateArgument::Template: {
1870 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1871 if (ToTemplate.isNull())
1872 return TemplateArgument();
1873
1874 return TemplateArgument(ToTemplate);
1875 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001876
1877 case TemplateArgument::TemplateExpansion: {
1878 TemplateName ToTemplate
1879 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1880 if (ToTemplate.isNull())
1881 return TemplateArgument();
1882
Douglas Gregor2be29f42011-01-14 23:41:42 +00001883 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001884 }
1885
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001886 case TemplateArgument::Expression:
1887 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1888 return TemplateArgument(ToExpr);
1889 return TemplateArgument();
1890
1891 case TemplateArgument::Pack: {
1892 llvm::SmallVector<TemplateArgument, 2> ToPack;
1893 ToPack.reserve(From.pack_size());
1894 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1895 return TemplateArgument();
1896
1897 TemplateArgument *ToArgs
1898 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1899 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1900 return TemplateArgument(ToArgs, ToPack.size());
1901 }
1902 }
1903
1904 llvm_unreachable("Invalid template argument kind");
1905 return TemplateArgument();
1906}
1907
1908bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1909 unsigned NumFromArgs,
1910 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1911 for (unsigned I = 0; I != NumFromArgs; ++I) {
1912 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1913 if (To.isNull() && !FromArgs[I].isNull())
1914 return true;
1915
1916 ToArgs.push_back(To);
1917 }
1918
1919 return false;
1920}
1921
Douglas Gregor96a01b42010-02-11 00:48:18 +00001922bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001923 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001924 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001925 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001926 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001927 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001928}
1929
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001930bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001931 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001932 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001933 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001934 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001935}
1936
Douglas Gregor040afae2010-11-30 19:14:50 +00001937bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1938 ClassTemplateDecl *To) {
1939 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1940 Importer.getToContext(),
1941 Importer.getNonEquivalentDecls());
1942 return Ctx.IsStructurallyEquivalent(From, To);
1943}
1944
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001945Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001946 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001947 << D->getDeclKindName();
1948 return 0;
1949}
1950
Douglas Gregor788c62d2010-02-21 18:26:36 +00001951Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1952 // Import the major distinguishing characteristics of this namespace.
1953 DeclContext *DC, *LexicalDC;
1954 DeclarationName Name;
1955 SourceLocation Loc;
1956 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1957 return 0;
1958
1959 NamespaceDecl *MergeWithNamespace = 0;
1960 if (!Name) {
1961 // This is an anonymous namespace. Adopt an existing anonymous
1962 // namespace if we can.
1963 // FIXME: Not testable.
1964 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1965 MergeWithNamespace = TU->getAnonymousNamespace();
1966 else
1967 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1968 } else {
1969 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1970 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1971 Lookup.first != Lookup.second;
1972 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001973 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001974 continue;
1975
1976 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1977 MergeWithNamespace = FoundNS;
1978 ConflictingDecls.clear();
1979 break;
1980 }
1981
1982 ConflictingDecls.push_back(*Lookup.first);
1983 }
1984
1985 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001986 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001987 ConflictingDecls.data(),
1988 ConflictingDecls.size());
1989 }
1990 }
1991
1992 // Create the "to" namespace, if needed.
1993 NamespaceDecl *ToNamespace = MergeWithNamespace;
1994 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00001995 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1996 Importer.Import(D->getLocStart()),
1997 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00001998 ToNamespace->setLexicalDeclContext(LexicalDC);
1999 LexicalDC->addDecl(ToNamespace);
2000
2001 // If this is an anonymous namespace, register it as the anonymous
2002 // namespace within its context.
2003 if (!Name) {
2004 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2005 TU->setAnonymousNamespace(ToNamespace);
2006 else
2007 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2008 }
2009 }
2010 Importer.Imported(D, ToNamespace);
2011
2012 ImportDeclContext(D);
2013
2014 return ToNamespace;
2015}
2016
Richard Smith162e1c12011-04-15 14:24:37 +00002017Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002018 // Import the major distinguishing characteristics of this typedef.
2019 DeclContext *DC, *LexicalDC;
2020 DeclarationName Name;
2021 SourceLocation Loc;
2022 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2023 return 0;
2024
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002025 // If this typedef is not in block scope, determine whether we've
2026 // seen a typedef with the same name (that we can merge with) or any
2027 // other entity by that name (which name lookup could conflict with).
2028 if (!DC->isFunctionOrMethod()) {
2029 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2030 unsigned IDNS = Decl::IDNS_Ordinary;
2031 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2032 Lookup.first != Lookup.second;
2033 ++Lookup.first) {
2034 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2035 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002036 if (TypedefNameDecl *FoundTypedef =
2037 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002038 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2039 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002040 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002041 }
2042
2043 ConflictingDecls.push_back(*Lookup.first);
2044 }
2045
2046 if (!ConflictingDecls.empty()) {
2047 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2048 ConflictingDecls.data(),
2049 ConflictingDecls.size());
2050 if (!Name)
2051 return 0;
2052 }
2053 }
2054
Douglas Gregorea35d112010-02-15 23:54:17 +00002055 // Import the underlying type of this typedef;
2056 QualType T = Importer.Import(D->getUnderlyingType());
2057 if (T.isNull())
2058 return 0;
2059
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002060 // Create the new typedef node.
2061 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002062 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002063 TypedefNameDecl *ToTypedef;
2064 if (IsAlias)
2065 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2066 StartL, Loc,
2067 Name.getAsIdentifierInfo(),
2068 TInfo);
2069 else
2070 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2071 StartL, Loc,
2072 Name.getAsIdentifierInfo(),
2073 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002074 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002075 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002076 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002077 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002078
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002079 return ToTypedef;
2080}
2081
Richard Smith162e1c12011-04-15 14:24:37 +00002082Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2083 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2084}
2085
2086Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2087 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2088}
2089
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002090Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2091 // Import the major distinguishing characteristics of this enum.
2092 DeclContext *DC, *LexicalDC;
2093 DeclarationName Name;
2094 SourceLocation Loc;
2095 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2096 return 0;
2097
2098 // Figure out what enum name we're looking for.
2099 unsigned IDNS = Decl::IDNS_Tag;
2100 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002101 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2102 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002103 IDNS = Decl::IDNS_Ordinary;
2104 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2105 IDNS |= Decl::IDNS_Ordinary;
2106
2107 // We may already have an enum of the same name; try to find and match it.
2108 if (!DC->isFunctionOrMethod() && SearchName) {
2109 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2110 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2111 Lookup.first != Lookup.second;
2112 ++Lookup.first) {
2113 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2114 continue;
2115
2116 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002117 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002118 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2119 Found = Tag->getDecl();
2120 }
2121
2122 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002123 if (IsStructuralMatch(D, FoundEnum))
2124 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002125 }
2126
2127 ConflictingDecls.push_back(*Lookup.first);
2128 }
2129
2130 if (!ConflictingDecls.empty()) {
2131 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2132 ConflictingDecls.data(),
2133 ConflictingDecls.size());
2134 }
2135 }
2136
2137 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002138 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2139 Importer.Import(D->getLocStart()),
2140 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002141 D->isScoped(), D->isScopedUsingClassTag(),
2142 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002143 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002144 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002145 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002146 D2->setLexicalDeclContext(LexicalDC);
2147 Importer.Imported(D, D2);
2148 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002149
2150 // Import the integer type.
2151 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2152 if (ToIntegerType.isNull())
2153 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002154 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002155
2156 // Import the definition
2157 if (D->isDefinition()) {
2158 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2159 if (T.isNull())
2160 return 0;
2161
2162 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2163 if (ToPromotionType.isNull())
2164 return 0;
2165
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002166 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002167 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002168
2169 // FIXME: we might need to merge the number of positive or negative bits
2170 // if the enumerator lists don't match.
2171 D2->completeDefinition(T, ToPromotionType,
2172 D->getNumPositiveBits(),
2173 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002174 }
2175
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002176 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002177}
2178
Douglas Gregor96a01b42010-02-11 00:48:18 +00002179Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2180 // If this record has a definition in the translation unit we're coming from,
2181 // but this particular declaration is not that definition, import the
2182 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002183 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002184 if (Definition && Definition != D) {
2185 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002186 if (!ImportedDef)
2187 return 0;
2188
2189 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002190 }
2191
2192 // Import the major distinguishing characteristics of this record.
2193 DeclContext *DC, *LexicalDC;
2194 DeclarationName Name;
2195 SourceLocation Loc;
2196 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2197 return 0;
2198
2199 // Figure out what structure name we're looking for.
2200 unsigned IDNS = Decl::IDNS_Tag;
2201 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002202 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2203 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002204 IDNS = Decl::IDNS_Ordinary;
2205 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2206 IDNS |= Decl::IDNS_Ordinary;
2207
2208 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002209 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002210 if (!DC->isFunctionOrMethod() && SearchName) {
2211 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2212 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2213 Lookup.first != Lookup.second;
2214 ++Lookup.first) {
2215 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2216 continue;
2217
2218 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002219 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002220 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2221 Found = Tag->getDecl();
2222 }
2223
2224 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002225 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2226 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2227 // The record types structurally match, or the "from" translation
2228 // unit only had a forward declaration anyway; call it the same
2229 // function.
2230 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002231 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002232 }
2233 } else {
2234 // We have a forward declaration of this type, so adopt that forward
2235 // declaration rather than building a new one.
2236 AdoptDecl = FoundRecord;
2237 continue;
2238 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002239 }
2240
2241 ConflictingDecls.push_back(*Lookup.first);
2242 }
2243
2244 if (!ConflictingDecls.empty()) {
2245 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2246 ConflictingDecls.data(),
2247 ConflictingDecls.size());
2248 }
2249 }
2250
2251 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002252 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002253 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002254 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002255 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002256 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002257 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002258 DC, StartLoc, Loc,
2259 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002260 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002261 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002262 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002263 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002264 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002265 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002266
2267 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002268 D2->setLexicalDeclContext(LexicalDC);
2269 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002270 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002271
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002272 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002273
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002274 if (D->isDefinition() && ImportDefinition(D, D2))
2275 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002276
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002277 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002278}
2279
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002280Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2281 // Import the major distinguishing characteristics of this enumerator.
2282 DeclContext *DC, *LexicalDC;
2283 DeclarationName Name;
2284 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002285 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002286 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002287
2288 QualType T = Importer.Import(D->getType());
2289 if (T.isNull())
2290 return 0;
2291
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002292 // Determine whether there are any other declarations with the same name and
2293 // in the same context.
2294 if (!LexicalDC->isFunctionOrMethod()) {
2295 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2296 unsigned IDNS = Decl::IDNS_Ordinary;
2297 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2298 Lookup.first != Lookup.second;
2299 ++Lookup.first) {
2300 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2301 continue;
2302
2303 ConflictingDecls.push_back(*Lookup.first);
2304 }
2305
2306 if (!ConflictingDecls.empty()) {
2307 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2308 ConflictingDecls.data(),
2309 ConflictingDecls.size());
2310 if (!Name)
2311 return 0;
2312 }
2313 }
2314
2315 Expr *Init = Importer.Import(D->getInitExpr());
2316 if (D->getInitExpr() && !Init)
2317 return 0;
2318
2319 EnumConstantDecl *ToEnumerator
2320 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2321 Name.getAsIdentifierInfo(), T,
2322 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002323 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002324 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002325 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002326 LexicalDC->addDecl(ToEnumerator);
2327 return ToEnumerator;
2328}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002329
Douglas Gregora404ea62010-02-10 19:54:31 +00002330Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2331 // Import the major distinguishing characteristics of this function.
2332 DeclContext *DC, *LexicalDC;
2333 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002334 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002335 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002336 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002337
Douglas Gregora404ea62010-02-10 19:54:31 +00002338 // Try to find a function in our own ("to") context with the same name, same
2339 // type, and in the same context as the function we're importing.
2340 if (!LexicalDC->isFunctionOrMethod()) {
2341 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2342 unsigned IDNS = Decl::IDNS_Ordinary;
2343 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2344 Lookup.first != Lookup.second;
2345 ++Lookup.first) {
2346 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2347 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002348
Douglas Gregora404ea62010-02-10 19:54:31 +00002349 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2350 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2351 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002352 if (Importer.IsStructurallyEquivalent(D->getType(),
2353 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002354 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002355 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002356 }
2357
2358 // FIXME: Check for overloading more carefully, e.g., by boosting
2359 // Sema::IsOverload out to the AST library.
2360
2361 // Function overloading is okay in C++.
2362 if (Importer.getToContext().getLangOptions().CPlusPlus)
2363 continue;
2364
2365 // Complain about inconsistent function types.
2366 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002367 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002368 Importer.ToDiag(FoundFunction->getLocation(),
2369 diag::note_odr_value_here)
2370 << FoundFunction->getType();
2371 }
2372 }
2373
2374 ConflictingDecls.push_back(*Lookup.first);
2375 }
2376
2377 if (!ConflictingDecls.empty()) {
2378 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2379 ConflictingDecls.data(),
2380 ConflictingDecls.size());
2381 if (!Name)
2382 return 0;
2383 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002384 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002385
Abramo Bagnara25777432010-08-11 22:01:17 +00002386 DeclarationNameInfo NameInfo(Name, Loc);
2387 // Import additional name location/type info.
2388 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2389
Douglas Gregorea35d112010-02-15 23:54:17 +00002390 // Import the type.
2391 QualType T = Importer.Import(D->getType());
2392 if (T.isNull())
2393 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002394
2395 // Import the function parameters.
2396 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2397 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2398 P != PEnd; ++P) {
2399 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2400 if (!ToP)
2401 return 0;
2402
2403 Parameters.push_back(ToP);
2404 }
2405
2406 // Create the imported function.
2407 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002408 FunctionDecl *ToFunction = 0;
2409 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2410 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2411 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002412 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002413 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002414 FromConstructor->isExplicit(),
2415 D->isInlineSpecified(),
Sean Hunt5f802e52011-05-06 00:11:07 +00002416 D->isImplicit());
Douglas Gregorc144f352010-02-21 18:29:16 +00002417 } else if (isa<CXXDestructorDecl>(D)) {
2418 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2419 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002420 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002421 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002422 D->isInlineSpecified(),
2423 D->isImplicit());
2424 } else if (CXXConversionDecl *FromConversion
2425 = dyn_cast<CXXConversionDecl>(D)) {
2426 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2427 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002428 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002429 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002430 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002431 FromConversion->isExplicit(),
2432 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002433 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2434 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2435 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002436 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002437 NameInfo, T, TInfo,
2438 Method->isStatic(),
2439 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002440 Method->isInlineSpecified(),
2441 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002442 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002443 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002444 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002445 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002446 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002447 D->isInlineSpecified(),
2448 D->hasWrittenPrototype());
2449 }
John McCallb6217662010-03-15 10:12:16 +00002450
2451 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002452 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002453 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002454 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002455 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2456 ToFunction->setTrivial(D->isTrivial());
2457 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002458 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002459
Douglas Gregora404ea62010-02-10 19:54:31 +00002460 // Set the parameters.
2461 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002462 Parameters[I]->setOwningFunction(ToFunction);
2463 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002464 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002465 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002466
2467 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002468
2469 // Add this function to the lexical context.
2470 LexicalDC->addDecl(ToFunction);
2471
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002472 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002473}
2474
Douglas Gregorc144f352010-02-21 18:29:16 +00002475Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2476 return VisitFunctionDecl(D);
2477}
2478
2479Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2480 return VisitCXXMethodDecl(D);
2481}
2482
2483Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2484 return VisitCXXMethodDecl(D);
2485}
2486
2487Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2488 return VisitCXXMethodDecl(D);
2489}
2490
Douglas Gregor96a01b42010-02-11 00:48:18 +00002491Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2492 // Import the major distinguishing characteristics of a variable.
2493 DeclContext *DC, *LexicalDC;
2494 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002495 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002496 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2497 return 0;
2498
2499 // Import the type.
2500 QualType T = Importer.Import(D->getType());
2501 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002502 return 0;
2503
2504 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2505 Expr *BitWidth = Importer.Import(D->getBitWidth());
2506 if (!BitWidth && D->getBitWidth())
2507 return 0;
2508
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002509 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2510 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002511 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002512 T, TInfo, BitWidth, D->isMutable(),
2513 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002514 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002515 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002516 if (ToField->hasInClassInitializer())
2517 ToField->setInClassInitializer(D->getInClassInitializer());
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 Gregor926df6c2011-06-11 01:09:30 +00002873 D->getImplementationControl(),
2874 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002875
2876 // FIXME: When we decide to merge method definitions, we'll need to
2877 // deal with implicit parameters.
2878
2879 // Import the parameters
2880 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2881 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2882 FromPEnd = D->param_end();
2883 FromP != FromPEnd;
2884 ++FromP) {
2885 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2886 if (!ToP)
2887 return 0;
2888
2889 ToParams.push_back(ToP);
2890 }
2891
2892 // Set the parameters.
2893 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2894 ToParams[I]->setOwningFunction(ToMethod);
2895 ToMethod->addDecl(ToParams[I]);
2896 }
2897 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002898 ToParams.data(), ToParams.size(),
2899 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002900
2901 ToMethod->setLexicalDeclContext(LexicalDC);
2902 Importer.Imported(D, ToMethod);
2903 LexicalDC->addDecl(ToMethod);
2904 return ToMethod;
2905}
2906
Douglas Gregorb4677b62010-02-18 01:47:50 +00002907Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2908 // Import the major distinguishing characteristics of a category.
2909 DeclContext *DC, *LexicalDC;
2910 DeclarationName Name;
2911 SourceLocation Loc;
2912 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2913 return 0;
2914
2915 ObjCInterfaceDecl *ToInterface
2916 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2917 if (!ToInterface)
2918 return 0;
2919
2920 // Determine if we've already encountered this category.
2921 ObjCCategoryDecl *MergeWithCategory
2922 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2923 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2924 if (!ToCategory) {
2925 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2926 Importer.Import(D->getAtLoc()),
2927 Loc,
2928 Importer.Import(D->getCategoryNameLoc()),
2929 Name.getAsIdentifierInfo());
2930 ToCategory->setLexicalDeclContext(LexicalDC);
2931 LexicalDC->addDecl(ToCategory);
2932 Importer.Imported(D, ToCategory);
2933
2934 // Link this category into its class's category list.
2935 ToCategory->setClassInterface(ToInterface);
2936 ToCategory->insertNextClassCategory();
2937
2938 // Import protocols
2939 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2940 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2941 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2942 = D->protocol_loc_begin();
2943 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2944 FromProtoEnd = D->protocol_end();
2945 FromProto != FromProtoEnd;
2946 ++FromProto, ++FromProtoLoc) {
2947 ObjCProtocolDecl *ToProto
2948 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2949 if (!ToProto)
2950 return 0;
2951 Protocols.push_back(ToProto);
2952 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2953 }
2954
2955 // FIXME: If we're merging, make sure that the protocol list is the same.
2956 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2957 ProtocolLocs.data(), Importer.getToContext());
2958
2959 } else {
2960 Importer.Imported(D, ToCategory);
2961 }
2962
2963 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002964 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002965
2966 // If we have an implementation, import it as well.
2967 if (D->getImplementation()) {
2968 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002969 = cast_or_null<ObjCCategoryImplDecl>(
2970 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002971 if (!Impl)
2972 return 0;
2973
2974 ToCategory->setImplementation(Impl);
2975 }
2976
2977 return ToCategory;
2978}
2979
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002980Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002981 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002982 DeclContext *DC, *LexicalDC;
2983 DeclarationName Name;
2984 SourceLocation Loc;
2985 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2986 return 0;
2987
2988 ObjCProtocolDecl *MergeWithProtocol = 0;
2989 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2990 Lookup.first != Lookup.second;
2991 ++Lookup.first) {
2992 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2993 continue;
2994
2995 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2996 break;
2997 }
2998
2999 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3000 if (!ToProto || ToProto->isForwardDecl()) {
3001 if (!ToProto) {
3002 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3003 Name.getAsIdentifierInfo());
3004 ToProto->setForwardDecl(D->isForwardDecl());
3005 ToProto->setLexicalDeclContext(LexicalDC);
3006 LexicalDC->addDecl(ToProto);
3007 }
3008 Importer.Imported(D, ToProto);
3009
3010 // Import protocols
3011 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3012 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3013 ObjCProtocolDecl::protocol_loc_iterator
3014 FromProtoLoc = D->protocol_loc_begin();
3015 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3016 FromProtoEnd = D->protocol_end();
3017 FromProto != FromProtoEnd;
3018 ++FromProto, ++FromProtoLoc) {
3019 ObjCProtocolDecl *ToProto
3020 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3021 if (!ToProto)
3022 return 0;
3023 Protocols.push_back(ToProto);
3024 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3025 }
3026
3027 // FIXME: If we're merging, make sure that the protocol list is the same.
3028 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3029 ProtocolLocs.data(), Importer.getToContext());
3030 } else {
3031 Importer.Imported(D, ToProto);
3032 }
3033
Douglas Gregorb4677b62010-02-18 01:47:50 +00003034 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003035 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003036
3037 return ToProto;
3038}
3039
Douglas Gregora12d2942010-02-16 01:20:57 +00003040Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3041 // Import the major distinguishing characteristics of an @interface.
3042 DeclContext *DC, *LexicalDC;
3043 DeclarationName Name;
3044 SourceLocation Loc;
3045 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3046 return 0;
3047
3048 ObjCInterfaceDecl *MergeWithIface = 0;
3049 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3050 Lookup.first != Lookup.second;
3051 ++Lookup.first) {
3052 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3053 continue;
3054
3055 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3056 break;
3057 }
3058
3059 ObjCInterfaceDecl *ToIface = MergeWithIface;
3060 if (!ToIface || ToIface->isForwardDecl()) {
3061 if (!ToIface) {
3062 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3063 DC, Loc,
3064 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00003065 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00003066 D->isForwardDecl(),
3067 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003068 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003069 ToIface->setLexicalDeclContext(LexicalDC);
3070 LexicalDC->addDecl(ToIface);
3071 }
3072 Importer.Imported(D, ToIface);
3073
Douglas Gregora12d2942010-02-16 01:20:57 +00003074 if (D->getSuperClass()) {
3075 ObjCInterfaceDecl *Super
3076 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3077 if (!Super)
3078 return 0;
3079
3080 ToIface->setSuperClass(Super);
3081 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3082 }
3083
3084 // Import protocols
3085 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3086 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3087 ObjCInterfaceDecl::protocol_loc_iterator
3088 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003089
3090 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003091 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3092 FromProtoEnd = D->protocol_end();
3093 FromProto != FromProtoEnd;
3094 ++FromProto, ++FromProtoLoc) {
3095 ObjCProtocolDecl *ToProto
3096 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3097 if (!ToProto)
3098 return 0;
3099 Protocols.push_back(ToProto);
3100 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3101 }
3102
3103 // FIXME: If we're merging, make sure that the protocol list is the same.
3104 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3105 ProtocolLocs.data(), Importer.getToContext());
3106
Douglas Gregora12d2942010-02-16 01:20:57 +00003107 // Import @end range
3108 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3109 } else {
3110 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003111
3112 // Check for consistency of superclasses.
3113 DeclarationName FromSuperName, ToSuperName;
3114 if (D->getSuperClass())
3115 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3116 if (ToIface->getSuperClass())
3117 ToSuperName = ToIface->getSuperClass()->getDeclName();
3118 if (FromSuperName != ToSuperName) {
3119 Importer.ToDiag(ToIface->getLocation(),
3120 diag::err_odr_objc_superclass_inconsistent)
3121 << ToIface->getDeclName();
3122 if (ToIface->getSuperClass())
3123 Importer.ToDiag(ToIface->getSuperClassLoc(),
3124 diag::note_odr_objc_superclass)
3125 << ToIface->getSuperClass()->getDeclName();
3126 else
3127 Importer.ToDiag(ToIface->getLocation(),
3128 diag::note_odr_objc_missing_superclass);
3129 if (D->getSuperClass())
3130 Importer.FromDiag(D->getSuperClassLoc(),
3131 diag::note_odr_objc_superclass)
3132 << D->getSuperClass()->getDeclName();
3133 else
3134 Importer.FromDiag(D->getLocation(),
3135 diag::note_odr_objc_missing_superclass);
3136 return 0;
3137 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003138 }
3139
Douglas Gregorb4677b62010-02-18 01:47:50 +00003140 // Import categories. When the categories themselves are imported, they'll
3141 // hook themselves into this interface.
3142 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3143 FromCat = FromCat->getNextClassCategory())
3144 Importer.Import(FromCat);
3145
Douglas Gregora12d2942010-02-16 01:20:57 +00003146 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003147 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003148
3149 // If we have an @implementation, import it as well.
3150 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003151 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3152 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003153 if (!Impl)
3154 return 0;
3155
3156 ToIface->setImplementation(Impl);
3157 }
3158
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003159 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003160}
3161
Douglas Gregor3daef292010-12-07 15:32:12 +00003162Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3163 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3164 Importer.Import(D->getCategoryDecl()));
3165 if (!Category)
3166 return 0;
3167
3168 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3169 if (!ToImpl) {
3170 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3171 if (!DC)
3172 return 0;
3173
3174 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3175 Importer.Import(D->getLocation()),
3176 Importer.Import(D->getIdentifier()),
3177 Category->getClassInterface());
3178
3179 DeclContext *LexicalDC = DC;
3180 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3181 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3182 if (!LexicalDC)
3183 return 0;
3184
3185 ToImpl->setLexicalDeclContext(LexicalDC);
3186 }
3187
3188 LexicalDC->addDecl(ToImpl);
3189 Category->setImplementation(ToImpl);
3190 }
3191
3192 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003193 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003194 return ToImpl;
3195}
3196
Douglas Gregordd182ff2010-12-07 01:26:03 +00003197Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3198 // Find the corresponding interface.
3199 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3200 Importer.Import(D->getClassInterface()));
3201 if (!Iface)
3202 return 0;
3203
3204 // Import the superclass, if any.
3205 ObjCInterfaceDecl *Super = 0;
3206 if (D->getSuperClass()) {
3207 Super = cast_or_null<ObjCInterfaceDecl>(
3208 Importer.Import(D->getSuperClass()));
3209 if (!Super)
3210 return 0;
3211 }
3212
3213 ObjCImplementationDecl *Impl = Iface->getImplementation();
3214 if (!Impl) {
3215 // We haven't imported an implementation yet. Create a new @implementation
3216 // now.
3217 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3218 Importer.ImportContext(D->getDeclContext()),
3219 Importer.Import(D->getLocation()),
3220 Iface, Super);
3221
3222 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3223 DeclContext *LexicalDC
3224 = Importer.ImportContext(D->getLexicalDeclContext());
3225 if (!LexicalDC)
3226 return 0;
3227 Impl->setLexicalDeclContext(LexicalDC);
3228 }
3229
3230 // Associate the implementation with the class it implements.
3231 Iface->setImplementation(Impl);
3232 Importer.Imported(D, Iface->getImplementation());
3233 } else {
3234 Importer.Imported(D, Iface->getImplementation());
3235
3236 // Verify that the existing @implementation has the same superclass.
3237 if ((Super && !Impl->getSuperClass()) ||
3238 (!Super && Impl->getSuperClass()) ||
3239 (Super && Impl->getSuperClass() &&
3240 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3241 Importer.ToDiag(Impl->getLocation(),
3242 diag::err_odr_objc_superclass_inconsistent)
3243 << Iface->getDeclName();
3244 // FIXME: It would be nice to have the location of the superclass
3245 // below.
3246 if (Impl->getSuperClass())
3247 Importer.ToDiag(Impl->getLocation(),
3248 diag::note_odr_objc_superclass)
3249 << Impl->getSuperClass()->getDeclName();
3250 else
3251 Importer.ToDiag(Impl->getLocation(),
3252 diag::note_odr_objc_missing_superclass);
3253 if (D->getSuperClass())
3254 Importer.FromDiag(D->getLocation(),
3255 diag::note_odr_objc_superclass)
3256 << D->getSuperClass()->getDeclName();
3257 else
3258 Importer.FromDiag(D->getLocation(),
3259 diag::note_odr_objc_missing_superclass);
3260 return 0;
3261 }
3262 }
3263
3264 // Import all of the members of this @implementation.
3265 ImportDeclContext(D);
3266
3267 return Impl;
3268}
3269
Douglas Gregore3261622010-02-17 18:02:10 +00003270Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3271 // Import the major distinguishing characteristics of an @property.
3272 DeclContext *DC, *LexicalDC;
3273 DeclarationName Name;
3274 SourceLocation Loc;
3275 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3276 return 0;
3277
3278 // Check whether we have already imported this property.
3279 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3280 Lookup.first != Lookup.second;
3281 ++Lookup.first) {
3282 if (ObjCPropertyDecl *FoundProp
3283 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3284 // Check property types.
3285 if (!Importer.IsStructurallyEquivalent(D->getType(),
3286 FoundProp->getType())) {
3287 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3288 << Name << D->getType() << FoundProp->getType();
3289 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3290 << FoundProp->getType();
3291 return 0;
3292 }
3293
3294 // FIXME: Check property attributes, getters, setters, etc.?
3295
3296 // Consider these properties to be equivalent.
3297 Importer.Imported(D, FoundProp);
3298 return FoundProp;
3299 }
3300 }
3301
3302 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003303 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3304 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003305 return 0;
3306
3307 // Create the new property.
3308 ObjCPropertyDecl *ToProperty
3309 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3310 Name.getAsIdentifierInfo(),
3311 Importer.Import(D->getAtLoc()),
3312 T,
3313 D->getPropertyImplementation());
3314 Importer.Imported(D, ToProperty);
3315 ToProperty->setLexicalDeclContext(LexicalDC);
3316 LexicalDC->addDecl(ToProperty);
3317
3318 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003319 ToProperty->setPropertyAttributesAsWritten(
3320 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003321 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3322 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3323 ToProperty->setGetterMethodDecl(
3324 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3325 ToProperty->setSetterMethodDecl(
3326 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3327 ToProperty->setPropertyIvarDecl(
3328 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3329 return ToProperty;
3330}
3331
Douglas Gregor954e0c72010-12-07 18:32:03 +00003332Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3333 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3334 Importer.Import(D->getPropertyDecl()));
3335 if (!Property)
3336 return 0;
3337
3338 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3339 if (!DC)
3340 return 0;
3341
3342 // Import the lexical declaration context.
3343 DeclContext *LexicalDC = DC;
3344 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3345 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3346 if (!LexicalDC)
3347 return 0;
3348 }
3349
3350 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3351 if (!InImpl)
3352 return 0;
3353
3354 // Import the ivar (for an @synthesize).
3355 ObjCIvarDecl *Ivar = 0;
3356 if (D->getPropertyIvarDecl()) {
3357 Ivar = cast_or_null<ObjCIvarDecl>(
3358 Importer.Import(D->getPropertyIvarDecl()));
3359 if (!Ivar)
3360 return 0;
3361 }
3362
3363 ObjCPropertyImplDecl *ToImpl
3364 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3365 if (!ToImpl) {
3366 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3367 Importer.Import(D->getLocStart()),
3368 Importer.Import(D->getLocation()),
3369 Property,
3370 D->getPropertyImplementation(),
3371 Ivar,
3372 Importer.Import(D->getPropertyIvarDeclLoc()));
3373 ToImpl->setLexicalDeclContext(LexicalDC);
3374 Importer.Imported(D, ToImpl);
3375 LexicalDC->addDecl(ToImpl);
3376 } else {
3377 // Check that we have the same kind of property implementation (@synthesize
3378 // vs. @dynamic).
3379 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3380 Importer.ToDiag(ToImpl->getLocation(),
3381 diag::err_odr_objc_property_impl_kind_inconsistent)
3382 << Property->getDeclName()
3383 << (ToImpl->getPropertyImplementation()
3384 == ObjCPropertyImplDecl::Dynamic);
3385 Importer.FromDiag(D->getLocation(),
3386 diag::note_odr_objc_property_impl_kind)
3387 << D->getPropertyDecl()->getDeclName()
3388 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3389 return 0;
3390 }
3391
3392 // For @synthesize, check that we have the same
3393 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3394 Ivar != ToImpl->getPropertyIvarDecl()) {
3395 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3396 diag::err_odr_objc_synthesize_ivar_inconsistent)
3397 << Property->getDeclName()
3398 << ToImpl->getPropertyIvarDecl()->getDeclName()
3399 << Ivar->getDeclName();
3400 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3401 diag::note_odr_objc_synthesize_ivar_here)
3402 << D->getPropertyIvarDecl()->getDeclName();
3403 return 0;
3404 }
3405
3406 // Merge the existing implementation with the new implementation.
3407 Importer.Imported(D, ToImpl);
3408 }
3409
3410 return ToImpl;
3411}
3412
Douglas Gregor2b785022010-02-18 02:12:22 +00003413Decl *
3414ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3415 // Import the context of this declaration.
3416 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3417 if (!DC)
3418 return 0;
3419
3420 DeclContext *LexicalDC = DC;
3421 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3422 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3423 if (!LexicalDC)
3424 return 0;
3425 }
3426
3427 // Import the location of this declaration.
3428 SourceLocation Loc = Importer.Import(D->getLocation());
3429
3430 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3431 llvm::SmallVector<SourceLocation, 4> Locations;
3432 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3433 = D->protocol_loc_begin();
3434 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3435 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3436 FromProto != FromProtoEnd;
3437 ++FromProto, ++FromProtoLoc) {
3438 ObjCProtocolDecl *ToProto
3439 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3440 if (!ToProto)
3441 continue;
3442
3443 Protocols.push_back(ToProto);
3444 Locations.push_back(Importer.Import(*FromProtoLoc));
3445 }
3446
3447 ObjCForwardProtocolDecl *ToForward
3448 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3449 Protocols.data(), Protocols.size(),
3450 Locations.data());
3451 ToForward->setLexicalDeclContext(LexicalDC);
3452 LexicalDC->addDecl(ToForward);
3453 Importer.Imported(D, ToForward);
3454 return ToForward;
3455}
3456
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003457Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3458 // Import the context of this declaration.
3459 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3460 if (!DC)
3461 return 0;
3462
3463 DeclContext *LexicalDC = DC;
3464 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3465 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3466 if (!LexicalDC)
3467 return 0;
3468 }
3469
3470 // Import the location of this declaration.
3471 SourceLocation Loc = Importer.Import(D->getLocation());
3472
3473 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3474 llvm::SmallVector<SourceLocation, 4> Locations;
3475 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3476 From != FromEnd; ++From) {
3477 ObjCInterfaceDecl *ToIface
3478 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3479 if (!ToIface)
3480 continue;
3481
3482 Interfaces.push_back(ToIface);
3483 Locations.push_back(Importer.Import(From->getLocation()));
3484 }
3485
3486 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3487 Loc,
3488 Interfaces.data(),
3489 Locations.data(),
3490 Interfaces.size());
3491 ToClass->setLexicalDeclContext(LexicalDC);
3492 LexicalDC->addDecl(ToClass);
3493 Importer.Imported(D, ToClass);
3494 return ToClass;
3495}
3496
Douglas Gregor040afae2010-11-30 19:14:50 +00003497Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3498 // For template arguments, we adopt the translation unit as our declaration
3499 // context. This context will be fixed when the actual template declaration
3500 // is created.
3501
3502 // FIXME: Import default argument.
3503 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3504 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003505 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003506 Importer.Import(D->getLocation()),
3507 D->getDepth(),
3508 D->getIndex(),
3509 Importer.Import(D->getIdentifier()),
3510 D->wasDeclaredWithTypename(),
3511 D->isParameterPack());
3512}
3513
3514Decl *
3515ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3516 // Import the name of this declaration.
3517 DeclarationName Name = Importer.Import(D->getDeclName());
3518 if (D->getDeclName() && !Name)
3519 return 0;
3520
3521 // Import the location of this declaration.
3522 SourceLocation Loc = Importer.Import(D->getLocation());
3523
3524 // Import the type of this declaration.
3525 QualType T = Importer.Import(D->getType());
3526 if (T.isNull())
3527 return 0;
3528
3529 // Import type-source information.
3530 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3531 if (D->getTypeSourceInfo() && !TInfo)
3532 return 0;
3533
3534 // FIXME: Import default argument.
3535
3536 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3537 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003538 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003539 Loc, D->getDepth(), D->getPosition(),
3540 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003541 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003542}
3543
3544Decl *
3545ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3546 // Import the name of this declaration.
3547 DeclarationName Name = Importer.Import(D->getDeclName());
3548 if (D->getDeclName() && !Name)
3549 return 0;
3550
3551 // Import the location of this declaration.
3552 SourceLocation Loc = Importer.Import(D->getLocation());
3553
3554 // Import template parameters.
3555 TemplateParameterList *TemplateParams
3556 = ImportTemplateParameterList(D->getTemplateParameters());
3557 if (!TemplateParams)
3558 return 0;
3559
3560 // FIXME: Import default argument.
3561
3562 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3563 Importer.getToContext().getTranslationUnitDecl(),
3564 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003565 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003566 Name.getAsIdentifierInfo(),
3567 TemplateParams);
3568}
3569
3570Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3571 // If this record has a definition in the translation unit we're coming from,
3572 // but this particular declaration is not that definition, import the
3573 // definition and map to that.
3574 CXXRecordDecl *Definition
3575 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3576 if (Definition && Definition != D->getTemplatedDecl()) {
3577 Decl *ImportedDef
3578 = Importer.Import(Definition->getDescribedClassTemplate());
3579 if (!ImportedDef)
3580 return 0;
3581
3582 return Importer.Imported(D, ImportedDef);
3583 }
3584
3585 // Import the major distinguishing characteristics of this class template.
3586 DeclContext *DC, *LexicalDC;
3587 DeclarationName Name;
3588 SourceLocation Loc;
3589 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3590 return 0;
3591
3592 // We may already have a template of the same name; try to find and match it.
3593 if (!DC->isFunctionOrMethod()) {
3594 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3595 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3596 Lookup.first != Lookup.second;
3597 ++Lookup.first) {
3598 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3599 continue;
3600
3601 Decl *Found = *Lookup.first;
3602 if (ClassTemplateDecl *FoundTemplate
3603 = dyn_cast<ClassTemplateDecl>(Found)) {
3604 if (IsStructuralMatch(D, FoundTemplate)) {
3605 // The class templates structurally match; call it the same template.
3606 // FIXME: We may be filling in a forward declaration here. Handle
3607 // this case!
3608 Importer.Imported(D->getTemplatedDecl(),
3609 FoundTemplate->getTemplatedDecl());
3610 return Importer.Imported(D, FoundTemplate);
3611 }
3612 }
3613
3614 ConflictingDecls.push_back(*Lookup.first);
3615 }
3616
3617 if (!ConflictingDecls.empty()) {
3618 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3619 ConflictingDecls.data(),
3620 ConflictingDecls.size());
3621 }
3622
3623 if (!Name)
3624 return 0;
3625 }
3626
3627 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3628
3629 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003630 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3631 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003632 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3633 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003634 DC, StartLoc, IdLoc,
3635 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003636 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003637 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003638 D2Templated->setLexicalDeclContext(LexicalDC);
3639
3640 // Create the class template declaration itself.
3641 TemplateParameterList *TemplateParams
3642 = ImportTemplateParameterList(D->getTemplateParameters());
3643 if (!TemplateParams)
3644 return 0;
3645
3646 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3647 Loc, Name, TemplateParams,
3648 D2Templated,
3649 /*PrevDecl=*/0);
3650 D2Templated->setDescribedClassTemplate(D2);
3651
3652 D2->setAccess(D->getAccess());
3653 D2->setLexicalDeclContext(LexicalDC);
3654 LexicalDC->addDecl(D2);
3655
3656 // Note the relationship between the class templates.
3657 Importer.Imported(D, D2);
3658 Importer.Imported(DTemplated, D2Templated);
3659
3660 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3661 // FIXME: Import definition!
3662 }
3663
3664 return D2;
3665}
3666
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003667Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3668 ClassTemplateSpecializationDecl *D) {
3669 // If this record has a definition in the translation unit we're coming from,
3670 // but this particular declaration is not that definition, import the
3671 // definition and map to that.
3672 TagDecl *Definition = D->getDefinition();
3673 if (Definition && Definition != D) {
3674 Decl *ImportedDef = Importer.Import(Definition);
3675 if (!ImportedDef)
3676 return 0;
3677
3678 return Importer.Imported(D, ImportedDef);
3679 }
3680
3681 ClassTemplateDecl *ClassTemplate
3682 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3683 D->getSpecializedTemplate()));
3684 if (!ClassTemplate)
3685 return 0;
3686
3687 // Import the context of this declaration.
3688 DeclContext *DC = ClassTemplate->getDeclContext();
3689 if (!DC)
3690 return 0;
3691
3692 DeclContext *LexicalDC = DC;
3693 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3694 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3695 if (!LexicalDC)
3696 return 0;
3697 }
3698
3699 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003700 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3701 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003702
3703 // Import template arguments.
3704 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3705 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3706 D->getTemplateArgs().size(),
3707 TemplateArgs))
3708 return 0;
3709
3710 // Try to find an existing specialization with these template arguments.
3711 void *InsertPos = 0;
3712 ClassTemplateSpecializationDecl *D2
3713 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3714 TemplateArgs.size(), InsertPos);
3715 if (D2) {
3716 // We already have a class template specialization with these template
3717 // arguments.
3718
3719 // FIXME: Check for specialization vs. instantiation errors.
3720
3721 if (RecordDecl *FoundDef = D2->getDefinition()) {
3722 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3723 // The record types structurally match, or the "from" translation
3724 // unit only had a forward declaration anyway; call it the same
3725 // function.
3726 return Importer.Imported(D, FoundDef);
3727 }
3728 }
3729 } else {
3730 // Create a new specialization.
3731 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3732 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003733 StartLoc, IdLoc,
3734 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003735 TemplateArgs.data(),
3736 TemplateArgs.size(),
3737 /*PrevDecl=*/0);
3738 D2->setSpecializationKind(D->getSpecializationKind());
3739
3740 // Add this specialization to the class template.
3741 ClassTemplate->AddSpecialization(D2, InsertPos);
3742
3743 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003744 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003745
3746 // Add the specialization to this context.
3747 D2->setLexicalDeclContext(LexicalDC);
3748 LexicalDC->addDecl(D2);
3749 }
3750 Importer.Imported(D, D2);
3751
3752 if (D->isDefinition() && ImportDefinition(D, D2))
3753 return 0;
3754
3755 return D2;
3756}
3757
Douglas Gregor4800d952010-02-11 19:21:55 +00003758//----------------------------------------------------------------------------
3759// Import Statements
3760//----------------------------------------------------------------------------
3761
3762Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3763 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3764 << S->getStmtClassName();
3765 return 0;
3766}
3767
3768//----------------------------------------------------------------------------
3769// Import Expressions
3770//----------------------------------------------------------------------------
3771Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3772 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3773 << E->getStmtClassName();
3774 return 0;
3775}
3776
Douglas Gregor44080632010-02-19 01:17:02 +00003777Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003778 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3779 if (!ToD)
3780 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003781
3782 NamedDecl *FoundD = 0;
3783 if (E->getDecl() != E->getFoundDecl()) {
3784 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3785 if (!FoundD)
3786 return 0;
3787 }
Douglas Gregor44080632010-02-19 01:17:02 +00003788
3789 QualType T = Importer.Import(E->getType());
3790 if (T.isNull())
3791 return 0;
3792
Douglas Gregor40d96a62011-02-28 21:54:11 +00003793 return DeclRefExpr::Create(Importer.getToContext(),
3794 Importer.Import(E->getQualifierLoc()),
Douglas Gregor44080632010-02-19 01:17:02 +00003795 ToD,
3796 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003797 T, E->getValueKind(),
Chandler Carruth3aa81402011-05-01 23:48:14 +00003798 FoundD,
Douglas Gregor44080632010-02-19 01:17:02 +00003799 /*FIXME:TemplateArgs=*/0);
3800}
3801
Douglas Gregor4800d952010-02-11 19:21:55 +00003802Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3803 QualType T = Importer.Import(E->getType());
3804 if (T.isNull())
3805 return 0;
3806
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003807 return IntegerLiteral::Create(Importer.getToContext(),
3808 E->getValue(), T,
3809 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003810}
3811
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003812Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3813 QualType T = Importer.Import(E->getType());
3814 if (T.isNull())
3815 return 0;
3816
3817 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3818 E->isWide(), T,
3819 Importer.Import(E->getLocation()));
3820}
3821
Douglas Gregorf638f952010-02-19 01:07:06 +00003822Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3823 Expr *SubExpr = Importer.Import(E->getSubExpr());
3824 if (!SubExpr)
3825 return 0;
3826
3827 return new (Importer.getToContext())
3828 ParenExpr(Importer.Import(E->getLParen()),
3829 Importer.Import(E->getRParen()),
3830 SubExpr);
3831}
3832
3833Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3834 QualType T = Importer.Import(E->getType());
3835 if (T.isNull())
3836 return 0;
3837
3838 Expr *SubExpr = Importer.Import(E->getSubExpr());
3839 if (!SubExpr)
3840 return 0;
3841
3842 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003843 T, E->getValueKind(),
3844 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003845 Importer.Import(E->getOperatorLoc()));
3846}
3847
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003848Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3849 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003850 QualType ResultType = Importer.Import(E->getType());
3851
3852 if (E->isArgumentType()) {
3853 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3854 if (!TInfo)
3855 return 0;
3856
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003857 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3858 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003859 Importer.Import(E->getOperatorLoc()),
3860 Importer.Import(E->getRParenLoc()));
3861 }
3862
3863 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3864 if (!SubExpr)
3865 return 0;
3866
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003867 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3868 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003869 Importer.Import(E->getOperatorLoc()),
3870 Importer.Import(E->getRParenLoc()));
3871}
3872
Douglas Gregorf638f952010-02-19 01:07:06 +00003873Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3874 QualType T = Importer.Import(E->getType());
3875 if (T.isNull())
3876 return 0;
3877
3878 Expr *LHS = Importer.Import(E->getLHS());
3879 if (!LHS)
3880 return 0;
3881
3882 Expr *RHS = Importer.Import(E->getRHS());
3883 if (!RHS)
3884 return 0;
3885
3886 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003887 T, E->getValueKind(),
3888 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003889 Importer.Import(E->getOperatorLoc()));
3890}
3891
3892Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3893 QualType T = Importer.Import(E->getType());
3894 if (T.isNull())
3895 return 0;
3896
3897 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3898 if (CompLHSType.isNull())
3899 return 0;
3900
3901 QualType CompResultType = Importer.Import(E->getComputationResultType());
3902 if (CompResultType.isNull())
3903 return 0;
3904
3905 Expr *LHS = Importer.Import(E->getLHS());
3906 if (!LHS)
3907 return 0;
3908
3909 Expr *RHS = Importer.Import(E->getRHS());
3910 if (!RHS)
3911 return 0;
3912
3913 return new (Importer.getToContext())
3914 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003915 T, E->getValueKind(),
3916 E->getObjectKind(),
3917 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003918 Importer.Import(E->getOperatorLoc()));
3919}
3920
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003921static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003922 if (E->path_empty()) return false;
3923
3924 // TODO: import cast paths
3925 return true;
3926}
3927
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003928Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3929 QualType T = Importer.Import(E->getType());
3930 if (T.isNull())
3931 return 0;
3932
3933 Expr *SubExpr = Importer.Import(E->getSubExpr());
3934 if (!SubExpr)
3935 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003936
3937 CXXCastPath BasePath;
3938 if (ImportCastPath(E, BasePath))
3939 return 0;
3940
3941 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003942 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003943}
3944
Douglas Gregor008847a2010-02-19 01:32:14 +00003945Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3946 QualType T = Importer.Import(E->getType());
3947 if (T.isNull())
3948 return 0;
3949
3950 Expr *SubExpr = Importer.Import(E->getSubExpr());
3951 if (!SubExpr)
3952 return 0;
3953
3954 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3955 if (!TInfo && E->getTypeInfoAsWritten())
3956 return 0;
3957
John McCallf871d0c2010-08-07 06:22:56 +00003958 CXXCastPath BasePath;
3959 if (ImportCastPath(E, BasePath))
3960 return 0;
3961
John McCallf89e55a2010-11-18 06:31:45 +00003962 return CStyleCastExpr::Create(Importer.getToContext(), T,
3963 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003964 SubExpr, &BasePath, TInfo,
3965 Importer.Import(E->getLParenLoc()),
3966 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003967}
3968
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003969ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00003970 ASTContext &FromContext, FileManager &FromFileManager,
3971 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003972 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00003973 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3974 Minimal(MinimalImport)
3975{
Douglas Gregor9bed8792010-02-09 19:21:46 +00003976 ImportedDecls[FromContext.getTranslationUnitDecl()]
3977 = ToContext.getTranslationUnitDecl();
3978}
3979
3980ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003981
3982QualType ASTImporter::Import(QualType FromT) {
3983 if (FromT.isNull())
3984 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00003985
3986 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003987
Douglas Gregor169fba52010-02-08 15:18:58 +00003988 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00003989 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3990 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00003991 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00003992 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003993
Douglas Gregor169fba52010-02-08 15:18:58 +00003994 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003995 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00003996 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003997 if (ToT.isNull())
3998 return ToT;
3999
Douglas Gregor169fba52010-02-08 15:18:58 +00004000 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004001 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004002
John McCallf4c73712011-01-19 06:33:43 +00004003 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004004}
4005
Douglas Gregor9bed8792010-02-09 19:21:46 +00004006TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004007 if (!FromTSI)
4008 return FromTSI;
4009
4010 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004011 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004012 QualType T = Import(FromTSI->getType());
4013 if (T.isNull())
4014 return 0;
4015
4016 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004017 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004018}
4019
4020Decl *ASTImporter::Import(Decl *FromD) {
4021 if (!FromD)
4022 return 0;
4023
4024 // Check whether we've already imported this declaration.
4025 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
4026 if (Pos != ImportedDecls.end())
4027 return Pos->second;
4028
4029 // Import the type
4030 ASTNodeImporter Importer(*this);
4031 Decl *ToD = Importer.Visit(FromD);
4032 if (!ToD)
4033 return 0;
4034
4035 // Record the imported declaration.
4036 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004037
4038 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4039 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004040 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004041 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004042 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004043 // When we've finished transforming a typedef, see whether it was the
4044 // typedef for an anonymous tag.
4045 for (llvm::SmallVector<TagDecl *, 4>::iterator
4046 FromTag = AnonTagsWithPendingTypedefs.begin(),
4047 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4048 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004049 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004050 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4051 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004052 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004053 AnonTagsWithPendingTypedefs.erase(FromTag);
4054 break;
4055 }
4056 }
4057 }
4058 }
4059
Douglas Gregor9bed8792010-02-09 19:21:46 +00004060 return ToD;
4061}
4062
4063DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4064 if (!FromDC)
4065 return FromDC;
4066
4067 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4068}
4069
4070Expr *ASTImporter::Import(Expr *FromE) {
4071 if (!FromE)
4072 return 0;
4073
4074 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4075}
4076
4077Stmt *ASTImporter::Import(Stmt *FromS) {
4078 if (!FromS)
4079 return 0;
4080
Douglas Gregor4800d952010-02-11 19:21:55 +00004081 // Check whether we've already imported this declaration.
4082 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4083 if (Pos != ImportedStmts.end())
4084 return Pos->second;
4085
4086 // Import the type
4087 ASTNodeImporter Importer(*this);
4088 Stmt *ToS = Importer.Visit(FromS);
4089 if (!ToS)
4090 return 0;
4091
4092 // Record the imported declaration.
4093 ImportedStmts[FromS] = ToS;
4094 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004095}
4096
4097NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4098 if (!FromNNS)
4099 return 0;
4100
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004101 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4102
4103 switch (FromNNS->getKind()) {
4104 case NestedNameSpecifier::Identifier:
4105 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4106 return NestedNameSpecifier::Create(ToContext, prefix, II);
4107 }
4108 return 0;
4109
4110 case NestedNameSpecifier::Namespace:
4111 if (NamespaceDecl *NS =
4112 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4113 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4114 }
4115 return 0;
4116
4117 case NestedNameSpecifier::NamespaceAlias:
4118 if (NamespaceAliasDecl *NSAD =
4119 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4120 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4121 }
4122 return 0;
4123
4124 case NestedNameSpecifier::Global:
4125 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4126
4127 case NestedNameSpecifier::TypeSpec:
4128 case NestedNameSpecifier::TypeSpecWithTemplate: {
4129 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4130 if (!T.isNull()) {
4131 bool bTemplate = FromNNS->getKind() ==
4132 NestedNameSpecifier::TypeSpecWithTemplate;
4133 return NestedNameSpecifier::Create(ToContext, prefix,
4134 bTemplate, T.getTypePtr());
4135 }
4136 }
4137 return 0;
4138 }
4139
4140 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004141 return 0;
4142}
4143
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004144NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4145 // FIXME: Implement!
4146 return NestedNameSpecifierLoc();
4147}
4148
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004149TemplateName ASTImporter::Import(TemplateName From) {
4150 switch (From.getKind()) {
4151 case TemplateName::Template:
4152 if (TemplateDecl *ToTemplate
4153 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4154 return TemplateName(ToTemplate);
4155
4156 return TemplateName();
4157
4158 case TemplateName::OverloadedTemplate: {
4159 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4160 UnresolvedSet<2> ToTemplates;
4161 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4162 E = FromStorage->end();
4163 I != E; ++I) {
4164 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4165 ToTemplates.addDecl(To);
4166 else
4167 return TemplateName();
4168 }
4169 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4170 ToTemplates.end());
4171 }
4172
4173 case TemplateName::QualifiedTemplate: {
4174 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4175 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4176 if (!Qualifier)
4177 return TemplateName();
4178
4179 if (TemplateDecl *ToTemplate
4180 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4181 return ToContext.getQualifiedTemplateName(Qualifier,
4182 QTN->hasTemplateKeyword(),
4183 ToTemplate);
4184
4185 return TemplateName();
4186 }
4187
4188 case TemplateName::DependentTemplate: {
4189 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4190 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4191 if (!Qualifier)
4192 return TemplateName();
4193
4194 if (DTN->isIdentifier()) {
4195 return ToContext.getDependentTemplateName(Qualifier,
4196 Import(DTN->getIdentifier()));
4197 }
4198
4199 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4200 }
John McCall14606042011-06-30 08:33:18 +00004201
4202 case TemplateName::SubstTemplateTemplateParm: {
4203 SubstTemplateTemplateParmStorage *subst
4204 = From.getAsSubstTemplateTemplateParm();
4205 TemplateTemplateParmDecl *param
4206 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4207 if (!param)
4208 return TemplateName();
4209
4210 TemplateName replacement = Import(subst->getReplacement());
4211 if (replacement.isNull()) return TemplateName();
4212
4213 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4214 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004215
4216 case TemplateName::SubstTemplateTemplateParmPack: {
4217 SubstTemplateTemplateParmPackStorage *SubstPack
4218 = From.getAsSubstTemplateTemplateParmPack();
4219 TemplateTemplateParmDecl *Param
4220 = cast_or_null<TemplateTemplateParmDecl>(
4221 Import(SubstPack->getParameterPack()));
4222 if (!Param)
4223 return TemplateName();
4224
4225 ASTNodeImporter Importer(*this);
4226 TemplateArgument ArgPack
4227 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4228 if (ArgPack.isNull())
4229 return TemplateName();
4230
4231 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4232 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004233 }
4234
4235 llvm_unreachable("Invalid template name kind");
4236 return TemplateName();
4237}
4238
Douglas Gregor9bed8792010-02-09 19:21:46 +00004239SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4240 if (FromLoc.isInvalid())
4241 return SourceLocation();
4242
Douglas Gregor88523732010-02-10 00:15:17 +00004243 SourceManager &FromSM = FromContext.getSourceManager();
4244
4245 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004246 // don't have to import macro expansions.
4247 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004248 FromLoc = FromSM.getSpellingLoc(FromLoc);
4249 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4250 SourceManager &ToSM = ToContext.getSourceManager();
4251 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4252 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004253}
4254
4255SourceRange ASTImporter::Import(SourceRange FromRange) {
4256 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4257}
4258
Douglas Gregor88523732010-02-10 00:15:17 +00004259FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004260 llvm::DenseMap<FileID, FileID>::iterator Pos
4261 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004262 if (Pos != ImportedFileIDs.end())
4263 return Pos->second;
4264
4265 SourceManager &FromSM = FromContext.getSourceManager();
4266 SourceManager &ToSM = ToContext.getSourceManager();
4267 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004268 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004269
4270 // Include location of this file.
4271 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4272
4273 // Map the FileID for to the "to" source manager.
4274 FileID ToID;
4275 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004276 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004277 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4278 // disk again
4279 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4280 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004281 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004282 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4283 FromSLoc.getFile().getFileCharacteristic());
4284 } else {
4285 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004286 const llvm::MemoryBuffer *
4287 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004288 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004289 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004290 FromBuf->getBufferIdentifier());
4291 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4292 }
4293
4294
Sebastian Redl535a3e22010-09-30 01:03:06 +00004295 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004296 return ToID;
4297}
4298
Douglas Gregord8868a62011-01-18 03:11:38 +00004299void ASTImporter::ImportDefinition(Decl *From) {
4300 Decl *To = Import(From);
4301 if (!To)
4302 return;
4303
4304 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4305 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004306
4307 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4308 if (!ToRecord->getDefinition()) {
4309 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4310 /*ForceImport=*/true);
4311 return;
4312 }
4313 }
4314
Douglas Gregord8868a62011-01-18 03:11:38 +00004315 Importer.ImportDeclContext(FromDC, true);
4316 }
4317}
4318
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004319DeclarationName ASTImporter::Import(DeclarationName FromName) {
4320 if (!FromName)
4321 return DeclarationName();
4322
4323 switch (FromName.getNameKind()) {
4324 case DeclarationName::Identifier:
4325 return Import(FromName.getAsIdentifierInfo());
4326
4327 case DeclarationName::ObjCZeroArgSelector:
4328 case DeclarationName::ObjCOneArgSelector:
4329 case DeclarationName::ObjCMultiArgSelector:
4330 return Import(FromName.getObjCSelector());
4331
4332 case DeclarationName::CXXConstructorName: {
4333 QualType T = Import(FromName.getCXXNameType());
4334 if (T.isNull())
4335 return DeclarationName();
4336
4337 return ToContext.DeclarationNames.getCXXConstructorName(
4338 ToContext.getCanonicalType(T));
4339 }
4340
4341 case DeclarationName::CXXDestructorName: {
4342 QualType T = Import(FromName.getCXXNameType());
4343 if (T.isNull())
4344 return DeclarationName();
4345
4346 return ToContext.DeclarationNames.getCXXDestructorName(
4347 ToContext.getCanonicalType(T));
4348 }
4349
4350 case DeclarationName::CXXConversionFunctionName: {
4351 QualType T = Import(FromName.getCXXNameType());
4352 if (T.isNull())
4353 return DeclarationName();
4354
4355 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4356 ToContext.getCanonicalType(T));
4357 }
4358
4359 case DeclarationName::CXXOperatorName:
4360 return ToContext.DeclarationNames.getCXXOperatorName(
4361 FromName.getCXXOverloadedOperator());
4362
4363 case DeclarationName::CXXLiteralOperatorName:
4364 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4365 Import(FromName.getCXXLiteralIdentifier()));
4366
4367 case DeclarationName::CXXUsingDirective:
4368 // FIXME: STATICS!
4369 return DeclarationName::getUsingDirectiveName();
4370 }
4371
4372 // Silence bogus GCC warning
4373 return DeclarationName();
4374}
4375
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004376IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004377 if (!FromId)
4378 return 0;
4379
4380 return &ToContext.Idents.get(FromId->getName());
4381}
Douglas Gregor089459a2010-02-08 21:09:39 +00004382
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004383Selector ASTImporter::Import(Selector FromSel) {
4384 if (FromSel.isNull())
4385 return Selector();
4386
4387 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4388 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4389 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4390 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4391 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4392}
4393
Douglas Gregor089459a2010-02-08 21:09:39 +00004394DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4395 DeclContext *DC,
4396 unsigned IDNS,
4397 NamedDecl **Decls,
4398 unsigned NumDecls) {
4399 return Name;
4400}
4401
4402DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004403 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004404}
4405
4406DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004407 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004408}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004409
4410Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4411 ImportedDecls[From] = To;
4412 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004413}
Douglas Gregorea35d112010-02-15 23:54:17 +00004414
4415bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004416 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004417 = ImportedTypes.find(From.getTypePtr());
4418 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4419 return true;
4420
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004421 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004422 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004423}