blob: 050a9ab2c65a66bb20ca2efb20e16f75a2af4acb [file] [log] [blame]
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
John McCallf4c73712011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000073 // FIXME: TemplateTypeParmType
74 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000075 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000077 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000078 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000079 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80 QualType VisitObjCObjectType(const ObjCObjectType *T);
81 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000082
83 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000084 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000086 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000087 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000088 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
89 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000090 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000091 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
92 bool ForceImport = false);
93 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
94 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000095 TemplateParameterList *ImportTemplateParameterList(
96 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000097 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
98 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
99 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000102 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000103 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000104 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000105 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000106 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000107 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000108 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000109 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000110 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000111 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000112 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000113 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
114 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
115 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
116 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000117 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000118 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000119 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000120 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000121 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000122 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000123 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000124 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000125 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000126 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000127 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000128 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000129 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000130 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000131 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000132 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000133 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
134 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
135 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
136 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000137 Decl *VisitClassTemplateSpecializationDecl(
138 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000139
Douglas Gregor4800d952010-02-11 19:21:55 +0000140 // Importing statements
141 Stmt *VisitStmt(Stmt *S);
142
143 // Importing expressions
144 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000145 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000146 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000147 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitParenExpr(ParenExpr *E);
149 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000150 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000151 Expr *VisitBinaryOperator(BinaryOperator *E);
152 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000153 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000154 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155 };
156}
157
158//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000159// Structural Equivalence
160//----------------------------------------------------------------------------
161
162namespace {
163 struct StructuralEquivalenceContext {
164 /// \brief AST contexts for which we are checking structural equivalence.
165 ASTContext &C1, &C2;
166
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000167 /// \brief The set of "tentative" equivalences between two canonical
168 /// declarations, mapping from a declaration in the first context to the
169 /// declaration in the second context that we believe to be equivalent.
170 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
171
172 /// \brief Queue of declarations in the first context whose equivalence
173 /// with a declaration in the second context still needs to be verified.
174 std::deque<Decl *> DeclsToCheck;
175
Douglas Gregorea35d112010-02-15 23:54:17 +0000176 /// \brief Declaration (from, to) pairs that are known not to be equivalent
177 /// (which we have already complained about).
178 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
179
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180 /// \brief Whether we're being strict about the spelling of types when
181 /// unifying two types.
182 bool StrictTypeSpelling;
183
184 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000185 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000186 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000187 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000188 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000189
190 /// \brief Determine whether the two declarations are structurally
191 /// equivalent.
192 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
193
194 /// \brief Determine whether the two types are structurally equivalent.
195 bool IsStructurallyEquivalent(QualType T1, QualType T2);
196
197 private:
198 /// \brief Finish checking all of the structural equivalences.
199 ///
200 /// \returns true if an error occurred, false otherwise.
201 bool Finish();
202
203 public:
204 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000205 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000206 }
207
208 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000209 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000210 }
211 };
212}
213
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 QualType T1, QualType T2);
216static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
217 Decl *D1, Decl *D2);
218
219/// \brief Determine if two APInts have the same value, after zero-extending
220/// one of them (if needed!) to ensure that the bit-widths match.
221static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
222 if (I1.getBitWidth() == I2.getBitWidth())
223 return I1 == I2;
224
225 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227
Jay Foad9f71a8f2010-12-07 08:25:34 +0000228 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000229}
230
231/// \brief Determine if two APSInts have the same value, zero- or sign-extending
232/// as needed.
233static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
234 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
235 return I1 == I2;
236
237 // Check for a bit-width mismatch.
238 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000241 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000242
243 // We have a signedness mismatch. Turn the signed value into an unsigned
244 // value.
245 if (I1.isSigned()) {
246 if (I1.isNegative())
247 return false;
248
249 return llvm::APSInt(I1, true) == I2;
250 }
251
252 if (I2.isNegative())
253 return false;
254
255 return I1 == llvm::APSInt(I2, true);
256}
257
258/// \brief Determine structural equivalence of two expressions.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 Expr *E1, Expr *E2) {
261 if (!E1 || !E2)
262 return E1 == E2;
263
264 // FIXME: Actually perform a structural comparison!
265 return true;
266}
267
268/// \brief Determine whether two identifiers are equivalent.
269static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
270 const IdentifierInfo *Name2) {
271 if (!Name1 || !Name2)
272 return Name1 == Name2;
273
274 return Name1->getName() == Name2->getName();
275}
276
277/// \brief Determine whether two nested-name-specifiers are equivalent.
278static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
279 NestedNameSpecifier *NNS1,
280 NestedNameSpecifier *NNS2) {
281 // FIXME: Implement!
282 return true;
283}
284
285/// \brief Determine whether two template arguments are equivalent.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 const TemplateArgument &Arg1,
288 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000289 if (Arg1.getKind() != Arg2.getKind())
290 return false;
291
292 switch (Arg1.getKind()) {
293 case TemplateArgument::Null:
294 return true;
295
296 case TemplateArgument::Type:
297 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
298
299 case TemplateArgument::Integral:
300 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
301 Arg2.getIntegralType()))
302 return false;
303
304 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
305
306 case TemplateArgument::Declaration:
307 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
308
309 case TemplateArgument::Template:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplate(),
312 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000313
314 case TemplateArgument::TemplateExpansion:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsTemplateOrTemplatePattern(),
317 Arg2.getAsTemplateOrTemplatePattern());
318
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000319 case TemplateArgument::Expression:
320 return IsStructurallyEquivalent(Context,
321 Arg1.getAsExpr(), Arg2.getAsExpr());
322
323 case TemplateArgument::Pack:
324 if (Arg1.pack_size() != Arg2.pack_size())
325 return false;
326
327 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
328 if (!IsStructurallyEquivalent(Context,
329 Arg1.pack_begin()[I],
330 Arg2.pack_begin()[I]))
331 return false;
332
333 return true;
334 }
335
336 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000337 return true;
338}
339
340/// \brief Determine structural equivalence for the common part of array
341/// types.
342static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
343 const ArrayType *Array1,
344 const ArrayType *Array2) {
345 if (!IsStructurallyEquivalent(Context,
346 Array1->getElementType(),
347 Array2->getElementType()))
348 return false;
349 if (Array1->getSizeModifier() != Array2->getSizeModifier())
350 return false;
351 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
352 return false;
353
354 return true;
355}
356
357/// \brief Determine structural equivalence of two types.
358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
359 QualType T1, QualType T2) {
360 if (T1.isNull() || T2.isNull())
361 return T1.isNull() && T2.isNull();
362
363 if (!Context.StrictTypeSpelling) {
364 // We aren't being strict about token-to-token equivalence of types,
365 // so map down to the canonical type.
366 T1 = Context.C1.getCanonicalType(T1);
367 T2 = Context.C2.getCanonicalType(T2);
368 }
369
370 if (T1.getQualifiers() != T2.getQualifiers())
371 return false;
372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000374
Douglas Gregorea35d112010-02-15 23:54:17 +0000375 if (T1->getTypeClass() != T2->getTypeClass()) {
376 // Compare function types with prototypes vs. without prototypes as if
377 // both did not have prototypes.
378 if (T1->getTypeClass() == Type::FunctionProto &&
379 T2->getTypeClass() == Type::FunctionNoProto)
380 TC = Type::FunctionNoProto;
381 else if (T1->getTypeClass() == Type::FunctionNoProto &&
382 T2->getTypeClass() == Type::FunctionProto)
383 TC = Type::FunctionNoProto;
384 else
385 return false;
386 }
387
388 switch (TC) {
389 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000390 // FIXME: Deal with Char_S/Char_U.
391 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
392 return false;
393 break;
394
395 case Type::Complex:
396 if (!IsStructurallyEquivalent(Context,
397 cast<ComplexType>(T1)->getElementType(),
398 cast<ComplexType>(T2)->getElementType()))
399 return false;
400 break;
401
402 case Type::Pointer:
403 if (!IsStructurallyEquivalent(Context,
404 cast<PointerType>(T1)->getPointeeType(),
405 cast<PointerType>(T2)->getPointeeType()))
406 return false;
407 break;
408
409 case Type::BlockPointer:
410 if (!IsStructurallyEquivalent(Context,
411 cast<BlockPointerType>(T1)->getPointeeType(),
412 cast<BlockPointerType>(T2)->getPointeeType()))
413 return false;
414 break;
415
416 case Type::LValueReference:
417 case Type::RValueReference: {
418 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
419 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
420 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
421 return false;
422 if (Ref1->isInnerRef() != Ref2->isInnerRef())
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 Ref1->getPointeeTypeAsWritten(),
426 Ref2->getPointeeTypeAsWritten()))
427 return false;
428 break;
429 }
430
431 case Type::MemberPointer: {
432 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
433 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
434 if (!IsStructurallyEquivalent(Context,
435 MemPtr1->getPointeeType(),
436 MemPtr2->getPointeeType()))
437 return false;
438 if (!IsStructurallyEquivalent(Context,
439 QualType(MemPtr1->getClass(), 0),
440 QualType(MemPtr2->getClass(), 0)))
441 return false;
442 break;
443 }
444
445 case Type::ConstantArray: {
446 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
447 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
448 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
449 return false;
450
451 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
452 return false;
453 break;
454 }
455
456 case Type::IncompleteArray:
457 if (!IsArrayStructurallyEquivalent(Context,
458 cast<ArrayType>(T1),
459 cast<ArrayType>(T2)))
460 return false;
461 break;
462
463 case Type::VariableArray: {
464 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
465 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
466 if (!IsStructurallyEquivalent(Context,
467 Array1->getSizeExpr(), Array2->getSizeExpr()))
468 return false;
469
470 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
471 return false;
472
473 break;
474 }
475
476 case Type::DependentSizedArray: {
477 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
478 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Array1->getSizeExpr(), Array2->getSizeExpr()))
481 return false;
482
483 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
484 return false;
485
486 break;
487 }
488
489 case Type::DependentSizedExtVector: {
490 const DependentSizedExtVectorType *Vec1
491 = cast<DependentSizedExtVectorType>(T1);
492 const DependentSizedExtVectorType *Vec2
493 = cast<DependentSizedExtVectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
496 return false;
497 if (!IsStructurallyEquivalent(Context,
498 Vec1->getElementType(),
499 Vec2->getElementType()))
500 return false;
501 break;
502 }
503
504 case Type::Vector:
505 case Type::ExtVector: {
506 const VectorType *Vec1 = cast<VectorType>(T1);
507 const VectorType *Vec2 = cast<VectorType>(T2);
508 if (!IsStructurallyEquivalent(Context,
509 Vec1->getElementType(),
510 Vec2->getElementType()))
511 return false;
512 if (Vec1->getNumElements() != Vec2->getNumElements())
513 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000514 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000516 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000517 }
518
519 case Type::FunctionProto: {
520 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
521 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
522 if (Proto1->getNumArgs() != Proto2->getNumArgs())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getArgType(I),
527 Proto2->getArgType(I)))
528 return false;
529 }
530 if (Proto1->isVariadic() != Proto2->isVariadic())
531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000533 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000534 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
535 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
536 return false;
537 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
538 if (!IsStructurallyEquivalent(Context,
539 Proto1->getExceptionType(I),
540 Proto2->getExceptionType(I)))
541 return false;
542 }
543 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000544 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000545 Proto1->getNoexceptExpr(),
546 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000547 return false;
548 }
549 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
550 return false;
551
552 // Fall through to check the bits common with FunctionNoProtoType.
553 }
554
555 case Type::FunctionNoProto: {
556 const FunctionType *Function1 = cast<FunctionType>(T1);
557 const FunctionType *Function2 = cast<FunctionType>(T2);
558 if (!IsStructurallyEquivalent(Context,
559 Function1->getResultType(),
560 Function2->getResultType()))
561 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000562 if (Function1->getExtInfo() != Function2->getExtInfo())
563 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 break;
565 }
566
567 case Type::UnresolvedUsing:
568 if (!IsStructurallyEquivalent(Context,
569 cast<UnresolvedUsingType>(T1)->getDecl(),
570 cast<UnresolvedUsingType>(T2)->getDecl()))
571 return false;
572
573 break;
John McCall9d156a72011-01-06 01:58:22 +0000574
575 case Type::Attributed:
576 if (!IsStructurallyEquivalent(Context,
577 cast<AttributedType>(T1)->getModifiedType(),
578 cast<AttributedType>(T2)->getModifiedType()))
579 return false;
580 if (!IsStructurallyEquivalent(Context,
581 cast<AttributedType>(T1)->getEquivalentType(),
582 cast<AttributedType>(T2)->getEquivalentType()))
583 return false;
584 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000585
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000586 case Type::Paren:
587 if (!IsStructurallyEquivalent(Context,
588 cast<ParenType>(T1)->getInnerType(),
589 cast<ParenType>(T2)->getInnerType()))
590 return false;
591 break;
592
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000593 case Type::Typedef:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypedefType>(T1)->getDecl(),
596 cast<TypedefType>(T2)->getDecl()))
597 return false;
598 break;
599
600 case Type::TypeOfExpr:
601 if (!IsStructurallyEquivalent(Context,
602 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
603 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::TypeOf:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypeOfType>(T1)->getUnderlyingType(),
610 cast<TypeOfType>(T2)->getUnderlyingType()))
611 return false;
612 break;
Sean Huntca63c202011-05-24 22:41:36 +0000613
614 case Type::UnaryTransform:
615 if (!IsStructurallyEquivalent(Context,
616 cast<UnaryTransformType>(T1)->getUnderlyingType(),
617 cast<UnaryTransformType>(T1)->getUnderlyingType()))
618 return false;
619 break;
620
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000621 case Type::Decltype:
622 if (!IsStructurallyEquivalent(Context,
623 cast<DecltypeType>(T1)->getUnderlyingExpr(),
624 cast<DecltypeType>(T2)->getUnderlyingExpr()))
625 return false;
626 break;
627
Richard Smith34b41d92011-02-20 03:19:35 +0000628 case Type::Auto:
629 if (!IsStructurallyEquivalent(Context,
630 cast<AutoType>(T1)->getDeducedType(),
631 cast<AutoType>(T2)->getDeducedType()))
632 return false;
633 break;
634
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000635 case Type::Record:
636 case Type::Enum:
637 if (!IsStructurallyEquivalent(Context,
638 cast<TagType>(T1)->getDecl(),
639 cast<TagType>(T2)->getDecl()))
640 return false;
641 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000642
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000643 case Type::TemplateTypeParm: {
644 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
645 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
646 if (Parm1->getDepth() != Parm2->getDepth())
647 return false;
648 if (Parm1->getIndex() != Parm2->getIndex())
649 return false;
650 if (Parm1->isParameterPack() != Parm2->isParameterPack())
651 return false;
652
653 // Names of template type parameters are never significant.
654 break;
655 }
656
657 case Type::SubstTemplateTypeParm: {
658 const SubstTemplateTypeParmType *Subst1
659 = cast<SubstTemplateTypeParmType>(T1);
660 const SubstTemplateTypeParmType *Subst2
661 = cast<SubstTemplateTypeParmType>(T2);
662 if (!IsStructurallyEquivalent(Context,
663 QualType(Subst1->getReplacedParameter(), 0),
664 QualType(Subst2->getReplacedParameter(), 0)))
665 return false;
666 if (!IsStructurallyEquivalent(Context,
667 Subst1->getReplacementType(),
668 Subst2->getReplacementType()))
669 return false;
670 break;
671 }
672
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000673 case Type::SubstTemplateTypeParmPack: {
674 const SubstTemplateTypeParmPackType *Subst1
675 = cast<SubstTemplateTypeParmPackType>(T1);
676 const SubstTemplateTypeParmPackType *Subst2
677 = cast<SubstTemplateTypeParmPackType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 QualType(Subst1->getReplacedParameter(), 0),
680 QualType(Subst2->getReplacedParameter(), 0)))
681 return false;
682 if (!IsStructurallyEquivalent(Context,
683 Subst1->getArgumentPack(),
684 Subst2->getArgumentPack()))
685 return false;
686 break;
687 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000688 case Type::TemplateSpecialization: {
689 const TemplateSpecializationType *Spec1
690 = cast<TemplateSpecializationType>(T1);
691 const TemplateSpecializationType *Spec2
692 = cast<TemplateSpecializationType>(T2);
693 if (!IsStructurallyEquivalent(Context,
694 Spec1->getTemplateName(),
695 Spec2->getTemplateName()))
696 return false;
697 if (Spec1->getNumArgs() != Spec2->getNumArgs())
698 return false;
699 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
700 if (!IsStructurallyEquivalent(Context,
701 Spec1->getArg(I), Spec2->getArg(I)))
702 return false;
703 }
704 break;
705 }
706
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000707 case Type::Elaborated: {
708 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
709 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
710 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
711 if (Elab1->getKeyword() != Elab2->getKeyword())
712 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000713 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000714 Elab1->getQualifier(),
715 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000716 return false;
717 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000718 Elab1->getNamedType(),
719 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000720 return false;
721 break;
722 }
723
John McCall3cb0ebd2010-03-10 03:28:59 +0000724 case Type::InjectedClassName: {
725 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
726 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
727 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000728 Inj1->getInjectedSpecializationType(),
729 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000730 return false;
731 break;
732 }
733
Douglas Gregor4714c122010-03-31 17:34:00 +0000734 case Type::DependentName: {
735 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
736 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000737 if (!IsStructurallyEquivalent(Context,
738 Typename1->getQualifier(),
739 Typename2->getQualifier()))
740 return false;
741 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
742 Typename2->getIdentifier()))
743 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000744
745 break;
746 }
747
John McCall33500952010-06-11 00:33:02 +0000748 case Type::DependentTemplateSpecialization: {
749 const DependentTemplateSpecializationType *Spec1 =
750 cast<DependentTemplateSpecializationType>(T1);
751 const DependentTemplateSpecializationType *Spec2 =
752 cast<DependentTemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getQualifier(),
755 Spec2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
758 Spec2->getIdentifier()))
759 return false;
760 if (Spec1->getNumArgs() != Spec2->getNumArgs())
761 return false;
762 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
763 if (!IsStructurallyEquivalent(Context,
764 Spec1->getArg(I), Spec2->getArg(I)))
765 return false;
766 }
767 break;
768 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000769
770 case Type::PackExpansion:
771 if (!IsStructurallyEquivalent(Context,
772 cast<PackExpansionType>(T1)->getPattern(),
773 cast<PackExpansionType>(T2)->getPattern()))
774 return false;
775 break;
776
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000777 case Type::ObjCInterface: {
778 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
779 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Iface1->getDecl(), Iface2->getDecl()))
782 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000783 break;
784 }
785
786 case Type::ObjCObject: {
787 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
788 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
789 if (!IsStructurallyEquivalent(Context,
790 Obj1->getBaseType(),
791 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000792 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000793 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
794 return false;
795 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000797 Obj1->getProtocol(I),
798 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000799 return false;
800 }
801 break;
802 }
803
804 case Type::ObjCObjectPointer: {
805 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
806 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Ptr1->getPointeeType(),
809 Ptr2->getPointeeType()))
810 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000811 break;
812 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000813
814 case Type::Atomic: {
815 if (!IsStructurallyEquivalent(Context,
816 cast<AtomicType>(T1)->getValueType(),
817 cast<AtomicType>(T2)->getValueType()))
818 return false;
819 break;
820 }
821
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000822 } // end switch
823
824 return true;
825}
826
827/// \brief Determine structural equivalence of two records.
828static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
829 RecordDecl *D1, RecordDecl *D2) {
830 if (D1->isUnion() != D2->isUnion()) {
831 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
832 << Context.C2.getTypeDeclType(D2);
833 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
834 << D1->getDeclName() << (unsigned)D1->getTagKind();
835 return false;
836 }
837
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000838 // If both declarations are class template specializations, we know
839 // the ODR applies, so check the template and template arguments.
840 ClassTemplateSpecializationDecl *Spec1
841 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
842 ClassTemplateSpecializationDecl *Spec2
843 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
844 if (Spec1 && Spec2) {
845 // Check that the specialized templates are the same.
846 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
847 Spec2->getSpecializedTemplate()))
848 return false;
849
850 // Check that the template arguments are the same.
851 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
852 return false;
853
854 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
855 if (!IsStructurallyEquivalent(Context,
856 Spec1->getTemplateArgs().get(I),
857 Spec2->getTemplateArgs().get(I)))
858 return false;
859 }
860 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000861 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000862 else if (Spec1 || Spec2)
863 return false;
864
Douglas Gregorea35d112010-02-15 23:54:17 +0000865 // Compare the definitions of these two records. If either or both are
866 // incomplete, we assume that they are equivalent.
867 D1 = D1->getDefinition();
868 D2 = D2->getDefinition();
869 if (!D1 || !D2)
870 return true;
871
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000872 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
873 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
874 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
875 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000876 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000877 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000878 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000879 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000880 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000881 return false;
882 }
883
884 // Check the base classes.
885 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
886 BaseEnd1 = D1CXX->bases_end(),
887 Base2 = D2CXX->bases_begin();
888 Base1 != BaseEnd1;
889 ++Base1, ++Base2) {
890 if (!IsStructurallyEquivalent(Context,
891 Base1->getType(), Base2->getType())) {
892 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
893 << Context.C2.getTypeDeclType(D2);
894 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
895 << Base2->getType()
896 << Base2->getSourceRange();
897 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
898 << Base1->getType()
899 << Base1->getSourceRange();
900 return false;
901 }
902
903 // Check virtual vs. non-virtual inheritance mismatch.
904 if (Base1->isVirtual() != Base2->isVirtual()) {
905 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
906 << Context.C2.getTypeDeclType(D2);
907 Context.Diag2(Base2->getSourceRange().getBegin(),
908 diag::note_odr_virtual_base)
909 << Base2->isVirtual() << Base2->getSourceRange();
910 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
911 << Base1->isVirtual()
912 << Base1->getSourceRange();
913 return false;
914 }
915 }
916 } else if (D1CXX->getNumBases() > 0) {
917 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
918 << Context.C2.getTypeDeclType(D2);
919 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
920 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
921 << Base1->getType()
922 << Base1->getSourceRange();
923 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
924 return false;
925 }
926 }
927
928 // Check the fields for consistency.
929 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
930 Field2End = D2->field_end();
931 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
932 Field1End = D1->field_end();
933 Field1 != Field1End;
934 ++Field1, ++Field2) {
935 if (Field2 == Field2End) {
936 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
937 << Context.C2.getTypeDeclType(D2);
938 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
939 << Field1->getDeclName() << Field1->getType();
940 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
941 return false;
942 }
943
944 if (!IsStructurallyEquivalent(Context,
945 Field1->getType(), Field2->getType())) {
946 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
947 << Context.C2.getTypeDeclType(D2);
948 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
949 << Field2->getDeclName() << Field2->getType();
950 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
951 << Field1->getDeclName() << Field1->getType();
952 return false;
953 }
954
955 if (Field1->isBitField() != Field2->isBitField()) {
956 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
957 << Context.C2.getTypeDeclType(D2);
958 if (Field1->isBitField()) {
959 llvm::APSInt Bits;
960 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
961 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
962 << Field1->getDeclName() << Field1->getType()
963 << Bits.toString(10, false);
964 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
965 << Field2->getDeclName();
966 } else {
967 llvm::APSInt Bits;
968 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
969 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
970 << Field2->getDeclName() << Field2->getType()
971 << Bits.toString(10, false);
972 Context.Diag1(Field1->getLocation(),
973 diag::note_odr_not_bit_field)
974 << Field1->getDeclName();
975 }
976 return false;
977 }
978
979 if (Field1->isBitField()) {
980 // Make sure that the bit-fields are the same length.
981 llvm::APSInt Bits1, Bits2;
982 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
983 return false;
984 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
985 return false;
986
987 if (!IsSameValue(Bits1, Bits2)) {
988 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
989 << Context.C2.getTypeDeclType(D2);
990 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
991 << Field2->getDeclName() << Field2->getType()
992 << Bits2.toString(10, false);
993 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
994 << Field1->getDeclName() << Field1->getType()
995 << Bits1.toString(10, false);
996 return false;
997 }
998 }
999 }
1000
1001 if (Field2 != Field2End) {
1002 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1003 << Context.C2.getTypeDeclType(D2);
1004 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1005 << Field2->getDeclName() << Field2->getType();
1006 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1007 return false;
1008 }
1009
1010 return true;
1011}
1012
1013/// \brief Determine structural equivalence of two enums.
1014static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1015 EnumDecl *D1, EnumDecl *D2) {
1016 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1017 EC2End = D2->enumerator_end();
1018 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1019 EC1End = D1->enumerator_end();
1020 EC1 != EC1End; ++EC1, ++EC2) {
1021 if (EC2 == EC2End) {
1022 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1023 << Context.C2.getTypeDeclType(D2);
1024 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1025 << EC1->getDeclName()
1026 << EC1->getInitVal().toString(10);
1027 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1028 return false;
1029 }
1030
1031 llvm::APSInt Val1 = EC1->getInitVal();
1032 llvm::APSInt Val2 = EC2->getInitVal();
1033 if (!IsSameValue(Val1, Val2) ||
1034 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
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(EC1->getLocation(), diag::note_odr_enumerator)
1041 << EC1->getDeclName()
1042 << EC1->getInitVal().toString(10);
1043 return false;
1044 }
1045 }
1046
1047 if (EC2 != EC2End) {
1048 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1049 << Context.C2.getTypeDeclType(D2);
1050 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1051 << EC2->getDeclName()
1052 << EC2->getInitVal().toString(10);
1053 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1054 return false;
1055 }
1056
1057 return true;
1058}
Douglas Gregor040afae2010-11-30 19:14:50 +00001059
1060static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1061 TemplateParameterList *Params1,
1062 TemplateParameterList *Params2) {
1063 if (Params1->size() != Params2->size()) {
1064 Context.Diag2(Params2->getTemplateLoc(),
1065 diag::err_odr_different_num_template_parameters)
1066 << Params1->size() << Params2->size();
1067 Context.Diag1(Params1->getTemplateLoc(),
1068 diag::note_odr_template_parameter_list);
1069 return false;
1070 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001071
Douglas Gregor040afae2010-11-30 19:14:50 +00001072 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1073 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1074 Context.Diag2(Params2->getParam(I)->getLocation(),
1075 diag::err_odr_different_template_parameter_kind);
1076 Context.Diag1(Params1->getParam(I)->getLocation(),
1077 diag::note_odr_template_parameter_here);
1078 return false;
1079 }
1080
1081 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1082 Params2->getParam(I))) {
1083
1084 return false;
1085 }
1086 }
1087
1088 return true;
1089}
1090
1091static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1092 TemplateTypeParmDecl *D1,
1093 TemplateTypeParmDecl *D2) {
1094 if (D1->isParameterPack() != D2->isParameterPack()) {
1095 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1096 << D2->isParameterPack();
1097 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1098 << D1->isParameterPack();
1099 return false;
1100 }
1101
1102 return true;
1103}
1104
1105static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1106 NonTypeTemplateParmDecl *D1,
1107 NonTypeTemplateParmDecl *D2) {
1108 // FIXME: Enable once we have variadic templates.
1109#if 0
1110 if (D1->isParameterPack() != D2->isParameterPack()) {
1111 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1112 << D2->isParameterPack();
1113 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1114 << D1->isParameterPack();
1115 return false;
1116 }
1117#endif
1118
1119 // Check types.
1120 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1121 Context.Diag2(D2->getLocation(),
1122 diag::err_odr_non_type_parameter_type_inconsistent)
1123 << D2->getType() << D1->getType();
1124 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1125 << D1->getType();
1126 return false;
1127 }
1128
1129 return true;
1130}
1131
1132static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1133 TemplateTemplateParmDecl *D1,
1134 TemplateTemplateParmDecl *D2) {
1135 // FIXME: Enable once we have variadic templates.
1136#if 0
1137 if (D1->isParameterPack() != D2->isParameterPack()) {
1138 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1139 << D2->isParameterPack();
1140 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1141 << D1->isParameterPack();
1142 return false;
1143 }
1144#endif
1145
1146 // Check template parameter lists.
1147 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1148 D2->getTemplateParameters());
1149}
1150
1151static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1152 ClassTemplateDecl *D1,
1153 ClassTemplateDecl *D2) {
1154 // Check template parameters.
1155 if (!IsStructurallyEquivalent(Context,
1156 D1->getTemplateParameters(),
1157 D2->getTemplateParameters()))
1158 return false;
1159
1160 // Check the templated declaration.
1161 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1162 D2->getTemplatedDecl());
1163}
1164
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001165/// \brief Determine structural equivalence of two declarations.
1166static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1167 Decl *D1, Decl *D2) {
1168 // FIXME: Check for known structural equivalences via a callback of some sort.
1169
Douglas Gregorea35d112010-02-15 23:54:17 +00001170 // Check whether we already know that these two declarations are not
1171 // structurally equivalent.
1172 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1173 D2->getCanonicalDecl())))
1174 return false;
1175
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001176 // Determine whether we've already produced a tentative equivalence for D1.
1177 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1178 if (EquivToD1)
1179 return EquivToD1 == D2->getCanonicalDecl();
1180
1181 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1182 EquivToD1 = D2->getCanonicalDecl();
1183 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1184 return true;
1185}
1186
1187bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1188 Decl *D2) {
1189 if (!::IsStructurallyEquivalent(*this, D1, D2))
1190 return false;
1191
1192 return !Finish();
1193}
1194
1195bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1196 QualType T2) {
1197 if (!::IsStructurallyEquivalent(*this, T1, T2))
1198 return false;
1199
1200 return !Finish();
1201}
1202
1203bool StructuralEquivalenceContext::Finish() {
1204 while (!DeclsToCheck.empty()) {
1205 // Check the next declaration.
1206 Decl *D1 = DeclsToCheck.front();
1207 DeclsToCheck.pop_front();
1208
1209 Decl *D2 = TentativeEquivalences[D1];
1210 assert(D2 && "Unrecorded tentative equivalence?");
1211
Douglas Gregorea35d112010-02-15 23:54:17 +00001212 bool Equivalent = true;
1213
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001214 // FIXME: Switch on all declaration kinds. For now, we're just going to
1215 // check the obvious ones.
1216 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1217 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1218 // Check for equivalent structure names.
1219 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001220 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1221 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001222 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001223 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1224 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001225 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1226 !::IsStructurallyEquivalent(*this, Record1, Record2))
1227 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001228 } else {
1229 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001230 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001231 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001232 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001233 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1234 // Check for equivalent enum names.
1235 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001236 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1237 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001238 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001239 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1240 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001241 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1242 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1243 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001244 } else {
1245 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001246 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001247 }
Richard Smith162e1c12011-04-15 14:24:37 +00001248 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1249 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001250 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001251 Typedef2->getIdentifier()) ||
1252 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001253 Typedef1->getUnderlyingType(),
1254 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001255 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001256 } else {
1257 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001258 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001259 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001260 } else if (ClassTemplateDecl *ClassTemplate1
1261 = dyn_cast<ClassTemplateDecl>(D1)) {
1262 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1263 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1264 ClassTemplate2->getIdentifier()) ||
1265 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1266 Equivalent = false;
1267 } else {
1268 // Class template/non-class-template mismatch.
1269 Equivalent = false;
1270 }
1271 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1272 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1273 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1274 Equivalent = false;
1275 } else {
1276 // Kind mismatch.
1277 Equivalent = false;
1278 }
1279 } else if (NonTypeTemplateParmDecl *NTTP1
1280 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1281 if (NonTypeTemplateParmDecl *NTTP2
1282 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1283 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1284 Equivalent = false;
1285 } else {
1286 // Kind mismatch.
1287 Equivalent = false;
1288 }
1289 } else if (TemplateTemplateParmDecl *TTP1
1290 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1291 if (TemplateTemplateParmDecl *TTP2
1292 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1293 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1294 Equivalent = false;
1295 } else {
1296 // Kind mismatch.
1297 Equivalent = false;
1298 }
1299 }
1300
Douglas Gregorea35d112010-02-15 23:54:17 +00001301 if (!Equivalent) {
1302 // Note that these two declarations are not equivalent (and we already
1303 // know about it).
1304 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1305 D2->getCanonicalDecl()));
1306 return true;
1307 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001308 // FIXME: Check other declaration kinds!
1309 }
1310
1311 return false;
1312}
1313
1314//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001315// Import Types
1316//----------------------------------------------------------------------------
1317
John McCallf4c73712011-01-19 06:33:43 +00001318QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001319 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1320 << T->getTypeClassName();
1321 return QualType();
1322}
1323
John McCallf4c73712011-01-19 06:33:43 +00001324QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001325 switch (T->getKind()) {
1326 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1327 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1328
1329 case BuiltinType::Char_U:
1330 // The context we're importing from has an unsigned 'char'. If we're
1331 // importing into a context with a signed 'char', translate to
1332 // 'unsigned char' instead.
1333 if (Importer.getToContext().getLangOptions().CharIsSigned)
1334 return Importer.getToContext().UnsignedCharTy;
1335
1336 return Importer.getToContext().CharTy;
1337
1338 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1339
1340 case BuiltinType::Char16:
1341 // FIXME: Make sure that the "to" context supports C++!
1342 return Importer.getToContext().Char16Ty;
1343
1344 case BuiltinType::Char32:
1345 // FIXME: Make sure that the "to" context supports C++!
1346 return Importer.getToContext().Char32Ty;
1347
1348 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1349 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1350 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1351 case BuiltinType::ULongLong:
1352 return Importer.getToContext().UnsignedLongLongTy;
1353 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1354
1355 case BuiltinType::Char_S:
1356 // The context we're importing from has an unsigned 'char'. If we're
1357 // importing into a context with a signed 'char', translate to
1358 // 'unsigned char' instead.
1359 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1360 return Importer.getToContext().SignedCharTy;
1361
1362 return Importer.getToContext().CharTy;
1363
1364 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001365 case BuiltinType::WChar_S:
1366 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001367 // FIXME: If not in C++, shall we translate to the C equivalent of
1368 // wchar_t?
1369 return Importer.getToContext().WCharTy;
1370
1371 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1372 case BuiltinType::Int : return Importer.getToContext().IntTy;
1373 case BuiltinType::Long : return Importer.getToContext().LongTy;
1374 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1375 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1376 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1377 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1378 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1379
1380 case BuiltinType::NullPtr:
1381 // FIXME: Make sure that the "to" context supports C++0x!
1382 return Importer.getToContext().NullPtrTy;
1383
1384 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1385 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001386 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001387 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001388
1389 case BuiltinType::ObjCId:
1390 // FIXME: Make sure that the "to" context supports Objective-C!
1391 return Importer.getToContext().ObjCBuiltinIdTy;
1392
1393 case BuiltinType::ObjCClass:
1394 return Importer.getToContext().ObjCBuiltinClassTy;
1395
1396 case BuiltinType::ObjCSel:
1397 return Importer.getToContext().ObjCBuiltinSelTy;
1398 }
1399
1400 return QualType();
1401}
1402
John McCallf4c73712011-01-19 06:33:43 +00001403QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001404 QualType ToElementType = Importer.Import(T->getElementType());
1405 if (ToElementType.isNull())
1406 return QualType();
1407
1408 return Importer.getToContext().getComplexType(ToElementType);
1409}
1410
John McCallf4c73712011-01-19 06:33:43 +00001411QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001412 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1413 if (ToPointeeType.isNull())
1414 return QualType();
1415
1416 return Importer.getToContext().getPointerType(ToPointeeType);
1417}
1418
John McCallf4c73712011-01-19 06:33:43 +00001419QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001420 // FIXME: Check for blocks support in "to" context.
1421 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1422 if (ToPointeeType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1426}
1427
John McCallf4c73712011-01-19 06:33:43 +00001428QualType
1429ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001430 // FIXME: Check for C++ support in "to" context.
1431 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1432 if (ToPointeeType.isNull())
1433 return QualType();
1434
1435 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1436}
1437
John McCallf4c73712011-01-19 06:33:43 +00001438QualType
1439ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001440 // FIXME: Check for C++0x support in "to" context.
1441 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1442 if (ToPointeeType.isNull())
1443 return QualType();
1444
1445 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1446}
1447
John McCallf4c73712011-01-19 06:33:43 +00001448QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001449 // FIXME: Check for C++ support in "to" context.
1450 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1451 if (ToPointeeType.isNull())
1452 return QualType();
1453
1454 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1455 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1456 ClassType.getTypePtr());
1457}
1458
John McCallf4c73712011-01-19 06:33:43 +00001459QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001460 QualType ToElementType = Importer.Import(T->getElementType());
1461 if (ToElementType.isNull())
1462 return QualType();
1463
1464 return Importer.getToContext().getConstantArrayType(ToElementType,
1465 T->getSize(),
1466 T->getSizeModifier(),
1467 T->getIndexTypeCVRQualifiers());
1468}
1469
John McCallf4c73712011-01-19 06:33:43 +00001470QualType
1471ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001472 QualType ToElementType = Importer.Import(T->getElementType());
1473 if (ToElementType.isNull())
1474 return QualType();
1475
1476 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1477 T->getSizeModifier(),
1478 T->getIndexTypeCVRQualifiers());
1479}
1480
John McCallf4c73712011-01-19 06:33:43 +00001481QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001482 QualType ToElementType = Importer.Import(T->getElementType());
1483 if (ToElementType.isNull())
1484 return QualType();
1485
1486 Expr *Size = Importer.Import(T->getSizeExpr());
1487 if (!Size)
1488 return QualType();
1489
1490 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1491 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1492 T->getSizeModifier(),
1493 T->getIndexTypeCVRQualifiers(),
1494 Brackets);
1495}
1496
John McCallf4c73712011-01-19 06:33:43 +00001497QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001498 QualType ToElementType = Importer.Import(T->getElementType());
1499 if (ToElementType.isNull())
1500 return QualType();
1501
1502 return Importer.getToContext().getVectorType(ToElementType,
1503 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001504 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001505}
1506
John McCallf4c73712011-01-19 06:33:43 +00001507QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001508 QualType ToElementType = Importer.Import(T->getElementType());
1509 if (ToElementType.isNull())
1510 return QualType();
1511
1512 return Importer.getToContext().getExtVectorType(ToElementType,
1513 T->getNumElements());
1514}
1515
John McCallf4c73712011-01-19 06:33:43 +00001516QualType
1517ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001518 // FIXME: What happens if we're importing a function without a prototype
1519 // into C++? Should we make it variadic?
1520 QualType ToResultType = Importer.Import(T->getResultType());
1521 if (ToResultType.isNull())
1522 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001523
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001524 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001525 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001526}
1527
John McCallf4c73712011-01-19 06:33:43 +00001528QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001529 QualType ToResultType = Importer.Import(T->getResultType());
1530 if (ToResultType.isNull())
1531 return QualType();
1532
1533 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001534 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001535 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1536 AEnd = T->arg_type_end();
1537 A != AEnd; ++A) {
1538 QualType ArgType = Importer.Import(*A);
1539 if (ArgType.isNull())
1540 return QualType();
1541 ArgTypes.push_back(ArgType);
1542 }
1543
1544 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001545 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001546 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1547 EEnd = T->exception_end();
1548 E != EEnd; ++E) {
1549 QualType ExceptionType = Importer.Import(*E);
1550 if (ExceptionType.isNull())
1551 return QualType();
1552 ExceptionTypes.push_back(ExceptionType);
1553 }
John McCalle23cf432010-12-14 08:05:40 +00001554
1555 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1556 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001557
1558 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001559 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001560}
1561
Sean Callanan0aeb2892011-08-11 16:56:07 +00001562QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1563 QualType ToInnerType = Importer.Import(T->getInnerType());
1564 if (ToInnerType.isNull())
1565 return QualType();
1566
1567 return Importer.getToContext().getParenType(ToInnerType);
1568}
1569
John McCallf4c73712011-01-19 06:33:43 +00001570QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001571 TypedefNameDecl *ToDecl
1572 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001573 if (!ToDecl)
1574 return QualType();
1575
1576 return Importer.getToContext().getTypeDeclType(ToDecl);
1577}
1578
John McCallf4c73712011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001580 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1581 if (!ToExpr)
1582 return QualType();
1583
1584 return Importer.getToContext().getTypeOfExprType(ToExpr);
1585}
1586
John McCallf4c73712011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001588 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1589 if (ToUnderlyingType.isNull())
1590 return QualType();
1591
1592 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1593}
1594
John McCallf4c73712011-01-19 06:33:43 +00001595QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001596 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001597 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1598 if (!ToExpr)
1599 return QualType();
1600
1601 return Importer.getToContext().getDecltypeType(ToExpr);
1602}
1603
Sean Huntca63c202011-05-24 22:41:36 +00001604QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1605 QualType ToBaseType = Importer.Import(T->getBaseType());
1606 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1607 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1608 return QualType();
1609
1610 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1611 ToUnderlyingType,
1612 T->getUTTKind());
1613}
1614
Richard Smith34b41d92011-02-20 03:19:35 +00001615QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1616 // FIXME: Make sure that the "to" context supports C++0x!
1617 QualType FromDeduced = T->getDeducedType();
1618 QualType ToDeduced;
1619 if (!FromDeduced.isNull()) {
1620 ToDeduced = Importer.Import(FromDeduced);
1621 if (ToDeduced.isNull())
1622 return QualType();
1623 }
1624
1625 return Importer.getToContext().getAutoType(ToDeduced);
1626}
1627
John McCallf4c73712011-01-19 06:33:43 +00001628QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001629 RecordDecl *ToDecl
1630 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1631 if (!ToDecl)
1632 return QualType();
1633
1634 return Importer.getToContext().getTagDeclType(ToDecl);
1635}
1636
John McCallf4c73712011-01-19 06:33:43 +00001637QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001638 EnumDecl *ToDecl
1639 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1640 if (!ToDecl)
1641 return QualType();
1642
1643 return Importer.getToContext().getTagDeclType(ToDecl);
1644}
1645
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001646QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001647 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001648 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1649 if (ToTemplate.isNull())
1650 return QualType();
1651
Chris Lattner5f9e2722011-07-23 10:55:15 +00001652 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001653 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1654 return QualType();
1655
1656 QualType ToCanonType;
1657 if (!QualType(T, 0).isCanonical()) {
1658 QualType FromCanonType
1659 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1660 ToCanonType =Importer.Import(FromCanonType);
1661 if (ToCanonType.isNull())
1662 return QualType();
1663 }
1664 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1665 ToTemplateArgs.data(),
1666 ToTemplateArgs.size(),
1667 ToCanonType);
1668}
1669
John McCallf4c73712011-01-19 06:33:43 +00001670QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001671 NestedNameSpecifier *ToQualifier = 0;
1672 // Note: the qualifier in an ElaboratedType is optional.
1673 if (T->getQualifier()) {
1674 ToQualifier = Importer.Import(T->getQualifier());
1675 if (!ToQualifier)
1676 return QualType();
1677 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001678
1679 QualType ToNamedType = Importer.Import(T->getNamedType());
1680 if (ToNamedType.isNull())
1681 return QualType();
1682
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001683 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1684 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001685}
1686
John McCallf4c73712011-01-19 06:33:43 +00001687QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001688 ObjCInterfaceDecl *Class
1689 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1690 if (!Class)
1691 return QualType();
1692
John McCallc12c5bb2010-05-15 11:32:37 +00001693 return Importer.getToContext().getObjCInterfaceType(Class);
1694}
1695
John McCallf4c73712011-01-19 06:33:43 +00001696QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001697 QualType ToBaseType = Importer.Import(T->getBaseType());
1698 if (ToBaseType.isNull())
1699 return QualType();
1700
Chris Lattner5f9e2722011-07-23 10:55:15 +00001701 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001702 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001703 PEnd = T->qual_end();
1704 P != PEnd; ++P) {
1705 ObjCProtocolDecl *Protocol
1706 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1707 if (!Protocol)
1708 return QualType();
1709 Protocols.push_back(Protocol);
1710 }
1711
John McCallc12c5bb2010-05-15 11:32:37 +00001712 return Importer.getToContext().getObjCObjectType(ToBaseType,
1713 Protocols.data(),
1714 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001715}
1716
John McCallf4c73712011-01-19 06:33:43 +00001717QualType
1718ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001719 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1720 if (ToPointeeType.isNull())
1721 return QualType();
1722
John McCallc12c5bb2010-05-15 11:32:37 +00001723 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001724}
1725
Douglas Gregor089459a2010-02-08 21:09:39 +00001726//----------------------------------------------------------------------------
1727// Import Declarations
1728//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001729bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1730 DeclContext *&LexicalDC,
1731 DeclarationName &Name,
1732 SourceLocation &Loc) {
1733 // Import the context of this declaration.
1734 DC = Importer.ImportContext(D->getDeclContext());
1735 if (!DC)
1736 return true;
1737
1738 LexicalDC = DC;
1739 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1740 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1741 if (!LexicalDC)
1742 return true;
1743 }
1744
1745 // Import the name of this declaration.
1746 Name = Importer.Import(D->getDeclName());
1747 if (D->getDeclName() && !Name)
1748 return true;
1749
1750 // Import the location of this declaration.
1751 Loc = Importer.Import(D->getLocation());
1752 return false;
1753}
1754
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001755void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1756 if (!FromD)
1757 return;
1758
1759 if (!ToD) {
1760 ToD = Importer.Import(FromD);
1761 if (!ToD)
1762 return;
1763 }
1764
1765 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1766 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1767 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1768 ImportDefinition(FromRecord, ToRecord);
1769 }
1770 }
1771 return;
1772 }
1773
1774 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1775 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1776 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1777 ImportDefinition(FromEnum, ToEnum);
1778 }
1779 }
1780 return;
1781 }
1782}
1783
Abramo Bagnara25777432010-08-11 22:01:17 +00001784void
1785ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1786 DeclarationNameInfo& To) {
1787 // NOTE: To.Name and To.Loc are already imported.
1788 // We only have to import To.LocInfo.
1789 switch (To.getName().getNameKind()) {
1790 case DeclarationName::Identifier:
1791 case DeclarationName::ObjCZeroArgSelector:
1792 case DeclarationName::ObjCOneArgSelector:
1793 case DeclarationName::ObjCMultiArgSelector:
1794 case DeclarationName::CXXUsingDirective:
1795 return;
1796
1797 case DeclarationName::CXXOperatorName: {
1798 SourceRange Range = From.getCXXOperatorNameRange();
1799 To.setCXXOperatorNameRange(Importer.Import(Range));
1800 return;
1801 }
1802 case DeclarationName::CXXLiteralOperatorName: {
1803 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1804 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1805 return;
1806 }
1807 case DeclarationName::CXXConstructorName:
1808 case DeclarationName::CXXDestructorName:
1809 case DeclarationName::CXXConversionFunctionName: {
1810 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1811 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1812 return;
1813 }
David Blaikieb219cfc2011-09-23 05:06:16 +00001814 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001815 }
1816}
1817
Douglas Gregord8868a62011-01-18 03:11:38 +00001818void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1819 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001820 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001821 return;
1822 }
1823
Douglas Gregor083a8212010-02-21 18:24:45 +00001824 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1825 FromEnd = FromDC->decls_end();
1826 From != FromEnd;
1827 ++From)
1828 Importer.Import(*From);
1829}
1830
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001831bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1832 bool ForceImport) {
1833 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001834 return false;
1835
1836 To->startDefinition();
1837
1838 // Add base classes.
1839 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1840 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1841
Chris Lattner5f9e2722011-07-23 10:55:15 +00001842 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001843 for (CXXRecordDecl::base_class_iterator
1844 Base1 = FromCXX->bases_begin(),
1845 FromBaseEnd = FromCXX->bases_end();
1846 Base1 != FromBaseEnd;
1847 ++Base1) {
1848 QualType T = Importer.Import(Base1->getType());
1849 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001850 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001851
1852 SourceLocation EllipsisLoc;
1853 if (Base1->isPackExpansion())
1854 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001855
1856 // Ensure that we have a definition for the base.
1857 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1858
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001859 Bases.push_back(
1860 new (Importer.getToContext())
1861 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1862 Base1->isVirtual(),
1863 Base1->isBaseOfClass(),
1864 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001865 Importer.Import(Base1->getTypeSourceInfo()),
1866 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001867 }
1868 if (!Bases.empty())
1869 ToCXX->setBases(Bases.data(), Bases.size());
1870 }
1871
Sean Callanan673e7752011-07-19 22:38:25 +00001872 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001873 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001874 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001875}
1876
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001877bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1878 bool ForceImport) {
1879 if (To->getDefinition() || To->isBeingDefined())
1880 return false;
1881
1882 To->startDefinition();
1883
1884 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1885 if (T.isNull())
1886 return true;
1887
1888 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1889 if (ToPromotionType.isNull())
1890 return true;
1891
1892 ImportDeclContext(From, ForceImport);
1893
1894 // FIXME: we might need to merge the number of positive or negative bits
1895 // if the enumerator lists don't match.
1896 To->completeDefinition(T, ToPromotionType,
1897 From->getNumPositiveBits(),
1898 From->getNumNegativeBits());
1899 return false;
1900}
1901
Douglas Gregor040afae2010-11-30 19:14:50 +00001902TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1903 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001904 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001905 ToParams.reserve(Params->size());
1906 for (TemplateParameterList::iterator P = Params->begin(),
1907 PEnd = Params->end();
1908 P != PEnd; ++P) {
1909 Decl *To = Importer.Import(*P);
1910 if (!To)
1911 return 0;
1912
1913 ToParams.push_back(cast<NamedDecl>(To));
1914 }
1915
1916 return TemplateParameterList::Create(Importer.getToContext(),
1917 Importer.Import(Params->getTemplateLoc()),
1918 Importer.Import(Params->getLAngleLoc()),
1919 ToParams.data(), ToParams.size(),
1920 Importer.Import(Params->getRAngleLoc()));
1921}
1922
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001923TemplateArgument
1924ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1925 switch (From.getKind()) {
1926 case TemplateArgument::Null:
1927 return TemplateArgument();
1928
1929 case TemplateArgument::Type: {
1930 QualType ToType = Importer.Import(From.getAsType());
1931 if (ToType.isNull())
1932 return TemplateArgument();
1933 return TemplateArgument(ToType);
1934 }
1935
1936 case TemplateArgument::Integral: {
1937 QualType ToType = Importer.Import(From.getIntegralType());
1938 if (ToType.isNull())
1939 return TemplateArgument();
1940 return TemplateArgument(*From.getAsIntegral(), ToType);
1941 }
1942
1943 case TemplateArgument::Declaration:
1944 if (Decl *To = Importer.Import(From.getAsDecl()))
1945 return TemplateArgument(To);
1946 return TemplateArgument();
1947
1948 case TemplateArgument::Template: {
1949 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1950 if (ToTemplate.isNull())
1951 return TemplateArgument();
1952
1953 return TemplateArgument(ToTemplate);
1954 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001955
1956 case TemplateArgument::TemplateExpansion: {
1957 TemplateName ToTemplate
1958 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1959 if (ToTemplate.isNull())
1960 return TemplateArgument();
1961
Douglas Gregor2be29f42011-01-14 23:41:42 +00001962 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001963 }
1964
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001965 case TemplateArgument::Expression:
1966 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1967 return TemplateArgument(ToExpr);
1968 return TemplateArgument();
1969
1970 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001971 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001972 ToPack.reserve(From.pack_size());
1973 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1974 return TemplateArgument();
1975
1976 TemplateArgument *ToArgs
1977 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1978 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1979 return TemplateArgument(ToArgs, ToPack.size());
1980 }
1981 }
1982
1983 llvm_unreachable("Invalid template argument kind");
1984 return TemplateArgument();
1985}
1986
1987bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1988 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001989 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001990 for (unsigned I = 0; I != NumFromArgs; ++I) {
1991 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1992 if (To.isNull() && !FromArgs[I].isNull())
1993 return true;
1994
1995 ToArgs.push_back(To);
1996 }
1997
1998 return false;
1999}
2000
Douglas Gregor96a01b42010-02-11 00:48:18 +00002001bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002002 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002003 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002004 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002005 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002006 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002007}
2008
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002009bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002010 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002011 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002012 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002013 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002014}
2015
Douglas Gregor040afae2010-11-30 19:14:50 +00002016bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2017 ClassTemplateDecl *To) {
2018 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2019 Importer.getToContext(),
2020 Importer.getNonEquivalentDecls());
2021 return Ctx.IsStructurallyEquivalent(From, To);
2022}
2023
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002024Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002025 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002026 << D->getDeclKindName();
2027 return 0;
2028}
2029
Douglas Gregor788c62d2010-02-21 18:26:36 +00002030Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2031 // Import the major distinguishing characteristics of this namespace.
2032 DeclContext *DC, *LexicalDC;
2033 DeclarationName Name;
2034 SourceLocation Loc;
2035 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2036 return 0;
2037
2038 NamespaceDecl *MergeWithNamespace = 0;
2039 if (!Name) {
2040 // This is an anonymous namespace. Adopt an existing anonymous
2041 // namespace if we can.
2042 // FIXME: Not testable.
2043 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2044 MergeWithNamespace = TU->getAnonymousNamespace();
2045 else
2046 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2047 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002048 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002049 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2050 Lookup.first != Lookup.second;
2051 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00002052 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002053 continue;
2054
2055 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2056 MergeWithNamespace = FoundNS;
2057 ConflictingDecls.clear();
2058 break;
2059 }
2060
2061 ConflictingDecls.push_back(*Lookup.first);
2062 }
2063
2064 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002065 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002066 ConflictingDecls.data(),
2067 ConflictingDecls.size());
2068 }
2069 }
2070
2071 // Create the "to" namespace, if needed.
2072 NamespaceDecl *ToNamespace = MergeWithNamespace;
2073 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002074 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2075 Importer.Import(D->getLocStart()),
2076 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002077 ToNamespace->setLexicalDeclContext(LexicalDC);
2078 LexicalDC->addDecl(ToNamespace);
2079
2080 // If this is an anonymous namespace, register it as the anonymous
2081 // namespace within its context.
2082 if (!Name) {
2083 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2084 TU->setAnonymousNamespace(ToNamespace);
2085 else
2086 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2087 }
2088 }
2089 Importer.Imported(D, ToNamespace);
2090
2091 ImportDeclContext(D);
2092
2093 return ToNamespace;
2094}
2095
Richard Smith162e1c12011-04-15 14:24:37 +00002096Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002097 // Import the major distinguishing characteristics of this typedef.
2098 DeclContext *DC, *LexicalDC;
2099 DeclarationName Name;
2100 SourceLocation Loc;
2101 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2102 return 0;
2103
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002104 // If this typedef is not in block scope, determine whether we've
2105 // seen a typedef with the same name (that we can merge with) or any
2106 // other entity by that name (which name lookup could conflict with).
2107 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002108 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002109 unsigned IDNS = Decl::IDNS_Ordinary;
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;
Richard Smith162e1c12011-04-15 14:24:37 +00002115 if (TypedefNameDecl *FoundTypedef =
2116 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002117 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2118 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002119 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002120 }
2121
2122 ConflictingDecls.push_back(*Lookup.first);
2123 }
2124
2125 if (!ConflictingDecls.empty()) {
2126 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2127 ConflictingDecls.data(),
2128 ConflictingDecls.size());
2129 if (!Name)
2130 return 0;
2131 }
2132 }
2133
Douglas Gregorea35d112010-02-15 23:54:17 +00002134 // Import the underlying type of this typedef;
2135 QualType T = Importer.Import(D->getUnderlyingType());
2136 if (T.isNull())
2137 return 0;
2138
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002139 // Create the new typedef node.
2140 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002141 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002142 TypedefNameDecl *ToTypedef;
2143 if (IsAlias)
2144 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2145 StartL, Loc,
2146 Name.getAsIdentifierInfo(),
2147 TInfo);
2148 else
2149 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2150 StartL, Loc,
2151 Name.getAsIdentifierInfo(),
2152 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002153 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002154 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002155 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002156 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002157
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002158 return ToTypedef;
2159}
2160
Richard Smith162e1c12011-04-15 14:24:37 +00002161Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2162 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2163}
2164
2165Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2166 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2167}
2168
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002169Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2170 // Import the major distinguishing characteristics of this enum.
2171 DeclContext *DC, *LexicalDC;
2172 DeclarationName Name;
2173 SourceLocation Loc;
2174 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2175 return 0;
2176
2177 // Figure out what enum name we're looking for.
2178 unsigned IDNS = Decl::IDNS_Tag;
2179 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002180 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2181 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002182 IDNS = Decl::IDNS_Ordinary;
2183 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2184 IDNS |= Decl::IDNS_Ordinary;
2185
2186 // We may already have an enum of the same name; try to find and match it.
2187 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002188 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002189 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002190 Lookup.first != Lookup.second;
2191 ++Lookup.first) {
2192 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2193 continue;
2194
2195 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002196 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002197 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2198 Found = Tag->getDecl();
2199 }
2200
2201 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002202 if (IsStructuralMatch(D, FoundEnum))
2203 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002204 }
2205
2206 ConflictingDecls.push_back(*Lookup.first);
2207 }
2208
2209 if (!ConflictingDecls.empty()) {
2210 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2211 ConflictingDecls.data(),
2212 ConflictingDecls.size());
2213 }
2214 }
2215
2216 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002217 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2218 Importer.Import(D->getLocStart()),
2219 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002220 D->isScoped(), D->isScopedUsingClassTag(),
2221 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002222 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002223 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002224 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002225 D2->setLexicalDeclContext(LexicalDC);
2226 Importer.Imported(D, D2);
2227 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002228
2229 // Import the integer type.
2230 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2231 if (ToIntegerType.isNull())
2232 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002233 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002234
2235 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002236 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002237 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002238
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002239 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002240}
2241
Douglas Gregor96a01b42010-02-11 00:48:18 +00002242Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2243 // If this record has a definition in the translation unit we're coming from,
2244 // but this particular declaration is not that definition, import the
2245 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002246 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002247 if (Definition && Definition != D) {
2248 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002249 if (!ImportedDef)
2250 return 0;
2251
2252 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002253 }
2254
2255 // Import the major distinguishing characteristics of this record.
2256 DeclContext *DC, *LexicalDC;
2257 DeclarationName Name;
2258 SourceLocation Loc;
2259 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2260 return 0;
2261
2262 // Figure out what structure name we're looking for.
2263 unsigned IDNS = Decl::IDNS_Tag;
2264 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002265 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2266 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002267 IDNS = Decl::IDNS_Ordinary;
2268 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2269 IDNS |= Decl::IDNS_Ordinary;
2270
2271 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002272 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002273 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002274 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002275 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002276 Lookup.first != Lookup.second;
2277 ++Lookup.first) {
2278 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2279 continue;
2280
2281 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002282 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002283 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2284 Found = Tag->getDecl();
2285 }
2286
2287 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002288 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002289 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002290 // The record types structurally match, or the "from" translation
2291 // unit only had a forward declaration anyway; call it the same
2292 // function.
2293 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002294 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002295 }
2296 } else {
2297 // We have a forward declaration of this type, so adopt that forward
2298 // declaration rather than building a new one.
2299 AdoptDecl = FoundRecord;
2300 continue;
2301 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002302 }
2303
2304 ConflictingDecls.push_back(*Lookup.first);
2305 }
2306
2307 if (!ConflictingDecls.empty()) {
2308 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2309 ConflictingDecls.data(),
2310 ConflictingDecls.size());
2311 }
2312 }
2313
2314 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002315 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002316 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002317 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002318 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002319 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002320 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002321 DC, StartLoc, Loc,
2322 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002323 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002324 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002325 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002326 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002327 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002328 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002329
2330 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 D2->setLexicalDeclContext(LexicalDC);
2332 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002333 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002334
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002335 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002336
John McCall5e1cdac2011-10-07 06:10:15 +00002337 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002338 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002339
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002340 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002341}
2342
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002343Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2344 // Import the major distinguishing characteristics of this enumerator.
2345 DeclContext *DC, *LexicalDC;
2346 DeclarationName Name;
2347 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002348 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002349 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002350
2351 QualType T = Importer.Import(D->getType());
2352 if (T.isNull())
2353 return 0;
2354
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002355 // Determine whether there are any other declarations with the same name and
2356 // in the same context.
2357 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002358 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002359 unsigned IDNS = Decl::IDNS_Ordinary;
2360 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2361 Lookup.first != Lookup.second;
2362 ++Lookup.first) {
2363 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2364 continue;
2365
2366 ConflictingDecls.push_back(*Lookup.first);
2367 }
2368
2369 if (!ConflictingDecls.empty()) {
2370 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2371 ConflictingDecls.data(),
2372 ConflictingDecls.size());
2373 if (!Name)
2374 return 0;
2375 }
2376 }
2377
2378 Expr *Init = Importer.Import(D->getInitExpr());
2379 if (D->getInitExpr() && !Init)
2380 return 0;
2381
2382 EnumConstantDecl *ToEnumerator
2383 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2384 Name.getAsIdentifierInfo(), T,
2385 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002386 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002387 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002388 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002389 LexicalDC->addDecl(ToEnumerator);
2390 return ToEnumerator;
2391}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002392
Douglas Gregora404ea62010-02-10 19:54:31 +00002393Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2394 // Import the major distinguishing characteristics of this function.
2395 DeclContext *DC, *LexicalDC;
2396 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002397 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002398 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002399 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002400
Douglas Gregora404ea62010-02-10 19:54:31 +00002401 // Try to find a function in our own ("to") context with the same name, same
2402 // type, and in the same context as the function we're importing.
2403 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002404 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002405 unsigned IDNS = Decl::IDNS_Ordinary;
2406 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2407 Lookup.first != Lookup.second;
2408 ++Lookup.first) {
2409 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2410 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002411
Douglas Gregora404ea62010-02-10 19:54:31 +00002412 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2413 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2414 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002415 if (Importer.IsStructurallyEquivalent(D->getType(),
2416 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002417 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002418 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002419 }
2420
2421 // FIXME: Check for overloading more carefully, e.g., by boosting
2422 // Sema::IsOverload out to the AST library.
2423
2424 // Function overloading is okay in C++.
2425 if (Importer.getToContext().getLangOptions().CPlusPlus)
2426 continue;
2427
2428 // Complain about inconsistent function types.
2429 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002430 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002431 Importer.ToDiag(FoundFunction->getLocation(),
2432 diag::note_odr_value_here)
2433 << FoundFunction->getType();
2434 }
2435 }
2436
2437 ConflictingDecls.push_back(*Lookup.first);
2438 }
2439
2440 if (!ConflictingDecls.empty()) {
2441 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2442 ConflictingDecls.data(),
2443 ConflictingDecls.size());
2444 if (!Name)
2445 return 0;
2446 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002447 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002448
Abramo Bagnara25777432010-08-11 22:01:17 +00002449 DeclarationNameInfo NameInfo(Name, Loc);
2450 // Import additional name location/type info.
2451 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2452
Douglas Gregorea35d112010-02-15 23:54:17 +00002453 // Import the type.
2454 QualType T = Importer.Import(D->getType());
2455 if (T.isNull())
2456 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002457
2458 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002459 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002460 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2461 P != PEnd; ++P) {
2462 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2463 if (!ToP)
2464 return 0;
2465
2466 Parameters.push_back(ToP);
2467 }
2468
2469 // Create the imported function.
2470 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002471 FunctionDecl *ToFunction = 0;
2472 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2473 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2474 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002475 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002476 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002477 FromConstructor->isExplicit(),
2478 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002479 D->isImplicit(),
2480 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002481 } else if (isa<CXXDestructorDecl>(D)) {
2482 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2483 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002484 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002485 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002486 D->isInlineSpecified(),
2487 D->isImplicit());
2488 } else if (CXXConversionDecl *FromConversion
2489 = dyn_cast<CXXConversionDecl>(D)) {
2490 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2491 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002492 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002493 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002494 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002495 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002496 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002497 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002498 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2499 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2500 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002501 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002502 NameInfo, T, TInfo,
2503 Method->isStatic(),
2504 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002505 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002506 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002507 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002508 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002509 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002510 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002511 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002512 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002513 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002514 D->hasWrittenPrototype(),
2515 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002516 }
John McCallb6217662010-03-15 10:12:16 +00002517
2518 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002519 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002520 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002521 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002522 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2523 ToFunction->setTrivial(D->isTrivial());
2524 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002525 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002526
Douglas Gregora404ea62010-02-10 19:54:31 +00002527 // Set the parameters.
2528 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002529 Parameters[I]->setOwningFunction(ToFunction);
2530 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002531 }
David Blaikie4278c652011-09-21 18:16:56 +00002532 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002533
2534 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002535
2536 // Add this function to the lexical context.
2537 LexicalDC->addDecl(ToFunction);
2538
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002539 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002540}
2541
Douglas Gregorc144f352010-02-21 18:29:16 +00002542Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2543 return VisitFunctionDecl(D);
2544}
2545
2546Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2547 return VisitCXXMethodDecl(D);
2548}
2549
2550Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2551 return VisitCXXMethodDecl(D);
2552}
2553
2554Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2555 return VisitCXXMethodDecl(D);
2556}
2557
Douglas Gregor96a01b42010-02-11 00:48:18 +00002558Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2559 // Import the major distinguishing characteristics of a variable.
2560 DeclContext *DC, *LexicalDC;
2561 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002562 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002563 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2564 return 0;
2565
2566 // Import the type.
2567 QualType T = Importer.Import(D->getType());
2568 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002569 return 0;
2570
2571 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2572 Expr *BitWidth = Importer.Import(D->getBitWidth());
2573 if (!BitWidth && D->getBitWidth())
2574 return 0;
2575
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002576 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2577 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002578 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002579 T, TInfo, BitWidth, D->isMutable(),
2580 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002581 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002582 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002583 if (ToField->hasInClassInitializer())
2584 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002585 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002586 LexicalDC->addDecl(ToField);
2587 return ToField;
2588}
2589
Francois Pichet87c2e122010-11-21 06:08:52 +00002590Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2591 // Import the major distinguishing characteristics of a variable.
2592 DeclContext *DC, *LexicalDC;
2593 DeclarationName Name;
2594 SourceLocation Loc;
2595 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2596 return 0;
2597
2598 // Import the type.
2599 QualType T = Importer.Import(D->getType());
2600 if (T.isNull())
2601 return 0;
2602
2603 NamedDecl **NamedChain =
2604 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2605
2606 unsigned i = 0;
2607 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2608 PE = D->chain_end(); PI != PE; ++PI) {
2609 Decl* D = Importer.Import(*PI);
2610 if (!D)
2611 return 0;
2612 NamedChain[i++] = cast<NamedDecl>(D);
2613 }
2614
2615 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2616 Importer.getToContext(), DC,
2617 Loc, Name.getAsIdentifierInfo(), T,
2618 NamedChain, D->getChainingSize());
2619 ToIndirectField->setAccess(D->getAccess());
2620 ToIndirectField->setLexicalDeclContext(LexicalDC);
2621 Importer.Imported(D, ToIndirectField);
2622 LexicalDC->addDecl(ToIndirectField);
2623 return ToIndirectField;
2624}
2625
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002626Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2627 // Import the major distinguishing characteristics of an ivar.
2628 DeclContext *DC, *LexicalDC;
2629 DeclarationName Name;
2630 SourceLocation Loc;
2631 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2632 return 0;
2633
2634 // Determine whether we've already imported this ivar
2635 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2636 Lookup.first != Lookup.second;
2637 ++Lookup.first) {
2638 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2639 if (Importer.IsStructurallyEquivalent(D->getType(),
2640 FoundIvar->getType())) {
2641 Importer.Imported(D, FoundIvar);
2642 return FoundIvar;
2643 }
2644
2645 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2646 << Name << D->getType() << FoundIvar->getType();
2647 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2648 << FoundIvar->getType();
2649 return 0;
2650 }
2651 }
2652
2653 // Import the type.
2654 QualType T = Importer.Import(D->getType());
2655 if (T.isNull())
2656 return 0;
2657
2658 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2659 Expr *BitWidth = Importer.Import(D->getBitWidth());
2660 if (!BitWidth && D->getBitWidth())
2661 return 0;
2662
Daniel Dunbara0654922010-04-02 20:10:03 +00002663 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2664 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002665 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002666 Loc, Name.getAsIdentifierInfo(),
2667 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002668 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002669 ToIvar->setLexicalDeclContext(LexicalDC);
2670 Importer.Imported(D, ToIvar);
2671 LexicalDC->addDecl(ToIvar);
2672 return ToIvar;
2673
2674}
2675
Douglas Gregora404ea62010-02-10 19:54:31 +00002676Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2677 // Import the major distinguishing characteristics of a variable.
2678 DeclContext *DC, *LexicalDC;
2679 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002680 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002681 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002682 return 0;
2683
Douglas Gregor089459a2010-02-08 21:09:39 +00002684 // Try to find a variable in our own ("to") context with the same name and
2685 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002686 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002687 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002688 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002689 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002690 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002691 Lookup.first != Lookup.second;
2692 ++Lookup.first) {
2693 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2694 continue;
2695
2696 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2697 // We have found a variable that we may need to merge with. Check it.
2698 if (isExternalLinkage(FoundVar->getLinkage()) &&
2699 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002700 if (Importer.IsStructurallyEquivalent(D->getType(),
2701 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002702 MergeWithVar = FoundVar;
2703 break;
2704 }
2705
Douglas Gregord0145422010-02-12 17:23:39 +00002706 const ArrayType *FoundArray
2707 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2708 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002709 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002710 if (FoundArray && TArray) {
2711 if (isa<IncompleteArrayType>(FoundArray) &&
2712 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002713 // Import the type.
2714 QualType T = Importer.Import(D->getType());
2715 if (T.isNull())
2716 return 0;
2717
Douglas Gregord0145422010-02-12 17:23:39 +00002718 FoundVar->setType(T);
2719 MergeWithVar = FoundVar;
2720 break;
2721 } else if (isa<IncompleteArrayType>(TArray) &&
2722 isa<ConstantArrayType>(FoundArray)) {
2723 MergeWithVar = FoundVar;
2724 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002725 }
2726 }
2727
Douglas Gregor089459a2010-02-08 21:09:39 +00002728 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002729 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002730 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2731 << FoundVar->getType();
2732 }
2733 }
2734
2735 ConflictingDecls.push_back(*Lookup.first);
2736 }
2737
2738 if (MergeWithVar) {
2739 // An equivalent variable with external linkage has been found. Link
2740 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002741 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002742
2743 if (VarDecl *DDef = D->getDefinition()) {
2744 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2745 Importer.ToDiag(ExistingDef->getLocation(),
2746 diag::err_odr_variable_multiple_def)
2747 << Name;
2748 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2749 } else {
2750 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002751 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002752 }
2753 }
2754
2755 return MergeWithVar;
2756 }
2757
2758 if (!ConflictingDecls.empty()) {
2759 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2760 ConflictingDecls.data(),
2761 ConflictingDecls.size());
2762 if (!Name)
2763 return 0;
2764 }
2765 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002766
Douglas Gregorea35d112010-02-15 23:54:17 +00002767 // Import the type.
2768 QualType T = Importer.Import(D->getType());
2769 if (T.isNull())
2770 return 0;
2771
Douglas Gregor089459a2010-02-08 21:09:39 +00002772 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002773 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002774 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2775 Importer.Import(D->getInnerLocStart()),
2776 Loc, Name.getAsIdentifierInfo(),
2777 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002778 D->getStorageClass(),
2779 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002780 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002781 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002782 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002783 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002784 LexicalDC->addDecl(ToVar);
2785
Douglas Gregor089459a2010-02-08 21:09:39 +00002786 // Merge the initializer.
2787 // FIXME: Can we really import any initializer? Alternatively, we could force
2788 // ourselves to import every declaration of a variable and then only use
2789 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002790 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002791
2792 // FIXME: Other bits to merge?
2793
2794 return ToVar;
2795}
2796
Douglas Gregor2cd00932010-02-17 21:22:52 +00002797Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2798 // Parameters are created in the translation unit's context, then moved
2799 // into the function declaration's context afterward.
2800 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2801
2802 // Import the name of this declaration.
2803 DeclarationName Name = Importer.Import(D->getDeclName());
2804 if (D->getDeclName() && !Name)
2805 return 0;
2806
2807 // Import the location of this declaration.
2808 SourceLocation Loc = Importer.Import(D->getLocation());
2809
2810 // Import the parameter's type.
2811 QualType T = Importer.Import(D->getType());
2812 if (T.isNull())
2813 return 0;
2814
2815 // Create the imported parameter.
2816 ImplicitParamDecl *ToParm
2817 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2818 Loc, Name.getAsIdentifierInfo(),
2819 T);
2820 return Importer.Imported(D, ToParm);
2821}
2822
Douglas Gregora404ea62010-02-10 19:54:31 +00002823Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2824 // Parameters are created in the translation unit's context, then moved
2825 // into the function declaration's context afterward.
2826 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2827
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002828 // Import the name of this declaration.
2829 DeclarationName Name = Importer.Import(D->getDeclName());
2830 if (D->getDeclName() && !Name)
2831 return 0;
2832
Douglas Gregora404ea62010-02-10 19:54:31 +00002833 // Import the location of this declaration.
2834 SourceLocation Loc = Importer.Import(D->getLocation());
2835
2836 // Import the parameter's type.
2837 QualType T = Importer.Import(D->getType());
2838 if (T.isNull())
2839 return 0;
2840
2841 // Create the imported parameter.
2842 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2843 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002844 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002845 Loc, Name.getAsIdentifierInfo(),
2846 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002847 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002848 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002849 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002850 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002851}
2852
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002853Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2854 // Import the major distinguishing characteristics of a method.
2855 DeclContext *DC, *LexicalDC;
2856 DeclarationName Name;
2857 SourceLocation Loc;
2858 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2859 return 0;
2860
2861 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2862 Lookup.first != Lookup.second;
2863 ++Lookup.first) {
2864 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2865 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2866 continue;
2867
2868 // Check return types.
2869 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2870 FoundMethod->getResultType())) {
2871 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2872 << D->isInstanceMethod() << Name
2873 << D->getResultType() << FoundMethod->getResultType();
2874 Importer.ToDiag(FoundMethod->getLocation(),
2875 diag::note_odr_objc_method_here)
2876 << D->isInstanceMethod() << Name;
2877 return 0;
2878 }
2879
2880 // Check the number of parameters.
2881 if (D->param_size() != FoundMethod->param_size()) {
2882 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2883 << D->isInstanceMethod() << Name
2884 << D->param_size() << FoundMethod->param_size();
2885 Importer.ToDiag(FoundMethod->getLocation(),
2886 diag::note_odr_objc_method_here)
2887 << D->isInstanceMethod() << Name;
2888 return 0;
2889 }
2890
2891 // Check parameter types.
2892 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2893 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2894 P != PEnd; ++P, ++FoundP) {
2895 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2896 (*FoundP)->getType())) {
2897 Importer.FromDiag((*P)->getLocation(),
2898 diag::err_odr_objc_method_param_type_inconsistent)
2899 << D->isInstanceMethod() << Name
2900 << (*P)->getType() << (*FoundP)->getType();
2901 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2902 << (*FoundP)->getType();
2903 return 0;
2904 }
2905 }
2906
2907 // Check variadic/non-variadic.
2908 // Check the number of parameters.
2909 if (D->isVariadic() != FoundMethod->isVariadic()) {
2910 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2911 << D->isInstanceMethod() << Name;
2912 Importer.ToDiag(FoundMethod->getLocation(),
2913 diag::note_odr_objc_method_here)
2914 << D->isInstanceMethod() << Name;
2915 return 0;
2916 }
2917
2918 // FIXME: Any other bits we need to merge?
2919 return Importer.Imported(D, FoundMethod);
2920 }
2921 }
2922
2923 // Import the result type.
2924 QualType ResultTy = Importer.Import(D->getResultType());
2925 if (ResultTy.isNull())
2926 return 0;
2927
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002928 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2929
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002930 ObjCMethodDecl *ToMethod
2931 = ObjCMethodDecl::Create(Importer.getToContext(),
2932 Loc,
2933 Importer.Import(D->getLocEnd()),
2934 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002935 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002936 D->isInstanceMethod(),
2937 D->isVariadic(),
2938 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002939 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002940 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002941 D->getImplementationControl(),
2942 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002943
2944 // FIXME: When we decide to merge method definitions, we'll need to
2945 // deal with implicit parameters.
2946
2947 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002948 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002949 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2950 FromPEnd = D->param_end();
2951 FromP != FromPEnd;
2952 ++FromP) {
2953 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2954 if (!ToP)
2955 return 0;
2956
2957 ToParams.push_back(ToP);
2958 }
2959
2960 // Set the parameters.
2961 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2962 ToParams[I]->setOwningFunction(ToMethod);
2963 ToMethod->addDecl(ToParams[I]);
2964 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002965 SmallVector<SourceLocation, 12> SelLocs;
2966 D->getSelectorLocs(SelLocs);
2967 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002968
2969 ToMethod->setLexicalDeclContext(LexicalDC);
2970 Importer.Imported(D, ToMethod);
2971 LexicalDC->addDecl(ToMethod);
2972 return ToMethod;
2973}
2974
Douglas Gregorb4677b62010-02-18 01:47:50 +00002975Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2976 // Import the major distinguishing characteristics of a category.
2977 DeclContext *DC, *LexicalDC;
2978 DeclarationName Name;
2979 SourceLocation Loc;
2980 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2981 return 0;
2982
2983 ObjCInterfaceDecl *ToInterface
2984 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2985 if (!ToInterface)
2986 return 0;
2987
2988 // Determine if we've already encountered this category.
2989 ObjCCategoryDecl *MergeWithCategory
2990 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2991 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2992 if (!ToCategory) {
2993 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00002994 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00002995 Loc,
2996 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00002997 Name.getAsIdentifierInfo(),
2998 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002999 ToCategory->setLexicalDeclContext(LexicalDC);
3000 LexicalDC->addDecl(ToCategory);
3001 Importer.Imported(D, ToCategory);
3002
Douglas Gregorb4677b62010-02-18 01:47:50 +00003003 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3005 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003006 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3007 = D->protocol_loc_begin();
3008 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3009 FromProtoEnd = D->protocol_end();
3010 FromProto != FromProtoEnd;
3011 ++FromProto, ++FromProtoLoc) {
3012 ObjCProtocolDecl *ToProto
3013 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3014 if (!ToProto)
3015 return 0;
3016 Protocols.push_back(ToProto);
3017 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3018 }
3019
3020 // FIXME: If we're merging, make sure that the protocol list is the same.
3021 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3022 ProtocolLocs.data(), Importer.getToContext());
3023
3024 } else {
3025 Importer.Imported(D, ToCategory);
3026 }
3027
3028 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003029 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003030
3031 // If we have an implementation, import it as well.
3032 if (D->getImplementation()) {
3033 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003034 = cast_or_null<ObjCCategoryImplDecl>(
3035 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003036 if (!Impl)
3037 return 0;
3038
3039 ToCategory->setImplementation(Impl);
3040 }
3041
3042 return ToCategory;
3043}
3044
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003045Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003046 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003047 DeclContext *DC, *LexicalDC;
3048 DeclarationName Name;
3049 SourceLocation Loc;
3050 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3051 return 0;
3052
3053 ObjCProtocolDecl *MergeWithProtocol = 0;
3054 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3055 Lookup.first != Lookup.second;
3056 ++Lookup.first) {
3057 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3058 continue;
3059
3060 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
3061 break;
3062 }
3063
3064 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3065 if (!ToProto || ToProto->isForwardDecl()) {
3066 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003067 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3068 Name.getAsIdentifierInfo(), Loc,
3069 Importer.Import(D->getAtStartLoc()));
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003070 ToProto->setForwardDecl(D->isForwardDecl());
3071 ToProto->setLexicalDeclContext(LexicalDC);
3072 LexicalDC->addDecl(ToProto);
3073 }
3074 Importer.Imported(D, ToProto);
3075
3076 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003077 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3078 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003079 ObjCProtocolDecl::protocol_loc_iterator
3080 FromProtoLoc = D->protocol_loc_begin();
3081 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3082 FromProtoEnd = D->protocol_end();
3083 FromProto != FromProtoEnd;
3084 ++FromProto, ++FromProtoLoc) {
3085 ObjCProtocolDecl *ToProto
3086 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3087 if (!ToProto)
3088 return 0;
3089 Protocols.push_back(ToProto);
3090 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3091 }
3092
3093 // FIXME: If we're merging, make sure that the protocol list is the same.
3094 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3095 ProtocolLocs.data(), Importer.getToContext());
3096 } else {
3097 Importer.Imported(D, ToProto);
3098 }
3099
Douglas Gregorb4677b62010-02-18 01:47:50 +00003100 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003101 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003102
3103 return ToProto;
3104}
3105
Douglas Gregora12d2942010-02-16 01:20:57 +00003106Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3107 // Import the major distinguishing characteristics of an @interface.
3108 DeclContext *DC, *LexicalDC;
3109 DeclarationName Name;
3110 SourceLocation Loc;
3111 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3112 return 0;
3113
3114 ObjCInterfaceDecl *MergeWithIface = 0;
3115 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3116 Lookup.first != Lookup.second;
3117 ++Lookup.first) {
3118 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3119 continue;
3120
3121 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3122 break;
3123 }
3124
3125 ObjCInterfaceDecl *ToIface = MergeWithIface;
3126 if (!ToIface || ToIface->isForwardDecl()) {
3127 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003128 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3129 Importer.Import(D->getAtStartLoc()),
3130 Name.getAsIdentifierInfo(), Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003131 D->isForwardDecl(),
3132 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003133 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003134 ToIface->setLexicalDeclContext(LexicalDC);
3135 LexicalDC->addDecl(ToIface);
3136 }
3137 Importer.Imported(D, ToIface);
3138
Douglas Gregora12d2942010-02-16 01:20:57 +00003139 if (D->getSuperClass()) {
3140 ObjCInterfaceDecl *Super
3141 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3142 if (!Super)
3143 return 0;
3144
3145 ToIface->setSuperClass(Super);
3146 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3147 }
3148
3149 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003150 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3151 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003152 ObjCInterfaceDecl::protocol_loc_iterator
3153 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003154
3155 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003156 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3157 FromProtoEnd = D->protocol_end();
3158 FromProto != FromProtoEnd;
3159 ++FromProto, ++FromProtoLoc) {
3160 ObjCProtocolDecl *ToProto
3161 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3162 if (!ToProto)
3163 return 0;
3164 Protocols.push_back(ToProto);
3165 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3166 }
3167
3168 // FIXME: If we're merging, make sure that the protocol list is the same.
3169 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3170 ProtocolLocs.data(), Importer.getToContext());
3171
Douglas Gregora12d2942010-02-16 01:20:57 +00003172 // Import @end range
3173 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3174 } else {
3175 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003176
3177 // Check for consistency of superclasses.
3178 DeclarationName FromSuperName, ToSuperName;
3179 if (D->getSuperClass())
3180 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3181 if (ToIface->getSuperClass())
3182 ToSuperName = ToIface->getSuperClass()->getDeclName();
3183 if (FromSuperName != ToSuperName) {
3184 Importer.ToDiag(ToIface->getLocation(),
3185 diag::err_odr_objc_superclass_inconsistent)
3186 << ToIface->getDeclName();
3187 if (ToIface->getSuperClass())
3188 Importer.ToDiag(ToIface->getSuperClassLoc(),
3189 diag::note_odr_objc_superclass)
3190 << ToIface->getSuperClass()->getDeclName();
3191 else
3192 Importer.ToDiag(ToIface->getLocation(),
3193 diag::note_odr_objc_missing_superclass);
3194 if (D->getSuperClass())
3195 Importer.FromDiag(D->getSuperClassLoc(),
3196 diag::note_odr_objc_superclass)
3197 << D->getSuperClass()->getDeclName();
3198 else
3199 Importer.FromDiag(D->getLocation(),
3200 diag::note_odr_objc_missing_superclass);
3201 return 0;
3202 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003203 }
3204
Douglas Gregorb4677b62010-02-18 01:47:50 +00003205 // Import categories. When the categories themselves are imported, they'll
3206 // hook themselves into this interface.
3207 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3208 FromCat = FromCat->getNextClassCategory())
3209 Importer.Import(FromCat);
3210
Douglas Gregora12d2942010-02-16 01:20:57 +00003211 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003212 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003213
3214 // If we have an @implementation, import it as well.
3215 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003216 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3217 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003218 if (!Impl)
3219 return 0;
3220
3221 ToIface->setImplementation(Impl);
3222 }
3223
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003224 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003225}
3226
Douglas Gregor3daef292010-12-07 15:32:12 +00003227Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3228 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3229 Importer.Import(D->getCategoryDecl()));
3230 if (!Category)
3231 return 0;
3232
3233 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3234 if (!ToImpl) {
3235 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3236 if (!DC)
3237 return 0;
3238
3239 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003240 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003241 Category->getClassInterface(),
3242 Importer.Import(D->getLocation()),
3243 Importer.Import(D->getAtStartLoc()));
Douglas Gregor3daef292010-12-07 15:32:12 +00003244
3245 DeclContext *LexicalDC = DC;
3246 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3247 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3248 if (!LexicalDC)
3249 return 0;
3250
3251 ToImpl->setLexicalDeclContext(LexicalDC);
3252 }
3253
3254 LexicalDC->addDecl(ToImpl);
3255 Category->setImplementation(ToImpl);
3256 }
3257
3258 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003259 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003260 return ToImpl;
3261}
3262
Douglas Gregordd182ff2010-12-07 01:26:03 +00003263Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3264 // Find the corresponding interface.
3265 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3266 Importer.Import(D->getClassInterface()));
3267 if (!Iface)
3268 return 0;
3269
3270 // Import the superclass, if any.
3271 ObjCInterfaceDecl *Super = 0;
3272 if (D->getSuperClass()) {
3273 Super = cast_or_null<ObjCInterfaceDecl>(
3274 Importer.Import(D->getSuperClass()));
3275 if (!Super)
3276 return 0;
3277 }
3278
3279 ObjCImplementationDecl *Impl = Iface->getImplementation();
3280 if (!Impl) {
3281 // We haven't imported an implementation yet. Create a new @implementation
3282 // now.
3283 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3284 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003285 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003286 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003287 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003288
3289 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3290 DeclContext *LexicalDC
3291 = Importer.ImportContext(D->getLexicalDeclContext());
3292 if (!LexicalDC)
3293 return 0;
3294 Impl->setLexicalDeclContext(LexicalDC);
3295 }
3296
3297 // Associate the implementation with the class it implements.
3298 Iface->setImplementation(Impl);
3299 Importer.Imported(D, Iface->getImplementation());
3300 } else {
3301 Importer.Imported(D, Iface->getImplementation());
3302
3303 // Verify that the existing @implementation has the same superclass.
3304 if ((Super && !Impl->getSuperClass()) ||
3305 (!Super && Impl->getSuperClass()) ||
3306 (Super && Impl->getSuperClass() &&
3307 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3308 Importer.ToDiag(Impl->getLocation(),
3309 diag::err_odr_objc_superclass_inconsistent)
3310 << Iface->getDeclName();
3311 // FIXME: It would be nice to have the location of the superclass
3312 // below.
3313 if (Impl->getSuperClass())
3314 Importer.ToDiag(Impl->getLocation(),
3315 diag::note_odr_objc_superclass)
3316 << Impl->getSuperClass()->getDeclName();
3317 else
3318 Importer.ToDiag(Impl->getLocation(),
3319 diag::note_odr_objc_missing_superclass);
3320 if (D->getSuperClass())
3321 Importer.FromDiag(D->getLocation(),
3322 diag::note_odr_objc_superclass)
3323 << D->getSuperClass()->getDeclName();
3324 else
3325 Importer.FromDiag(D->getLocation(),
3326 diag::note_odr_objc_missing_superclass);
3327 return 0;
3328 }
3329 }
3330
3331 // Import all of the members of this @implementation.
3332 ImportDeclContext(D);
3333
3334 return Impl;
3335}
3336
Douglas Gregore3261622010-02-17 18:02:10 +00003337Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3338 // Import the major distinguishing characteristics of an @property.
3339 DeclContext *DC, *LexicalDC;
3340 DeclarationName Name;
3341 SourceLocation Loc;
3342 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3343 return 0;
3344
3345 // Check whether we have already imported this property.
3346 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3347 Lookup.first != Lookup.second;
3348 ++Lookup.first) {
3349 if (ObjCPropertyDecl *FoundProp
3350 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3351 // Check property types.
3352 if (!Importer.IsStructurallyEquivalent(D->getType(),
3353 FoundProp->getType())) {
3354 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3355 << Name << D->getType() << FoundProp->getType();
3356 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3357 << FoundProp->getType();
3358 return 0;
3359 }
3360
3361 // FIXME: Check property attributes, getters, setters, etc.?
3362
3363 // Consider these properties to be equivalent.
3364 Importer.Imported(D, FoundProp);
3365 return FoundProp;
3366 }
3367 }
3368
3369 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003370 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3371 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003372 return 0;
3373
3374 // Create the new property.
3375 ObjCPropertyDecl *ToProperty
3376 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3377 Name.getAsIdentifierInfo(),
3378 Importer.Import(D->getAtLoc()),
3379 T,
3380 D->getPropertyImplementation());
3381 Importer.Imported(D, ToProperty);
3382 ToProperty->setLexicalDeclContext(LexicalDC);
3383 LexicalDC->addDecl(ToProperty);
3384
3385 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003386 ToProperty->setPropertyAttributesAsWritten(
3387 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003388 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3389 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3390 ToProperty->setGetterMethodDecl(
3391 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3392 ToProperty->setSetterMethodDecl(
3393 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3394 ToProperty->setPropertyIvarDecl(
3395 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3396 return ToProperty;
3397}
3398
Douglas Gregor954e0c72010-12-07 18:32:03 +00003399Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3400 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3401 Importer.Import(D->getPropertyDecl()));
3402 if (!Property)
3403 return 0;
3404
3405 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3406 if (!DC)
3407 return 0;
3408
3409 // Import the lexical declaration context.
3410 DeclContext *LexicalDC = DC;
3411 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3412 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3413 if (!LexicalDC)
3414 return 0;
3415 }
3416
3417 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3418 if (!InImpl)
3419 return 0;
3420
3421 // Import the ivar (for an @synthesize).
3422 ObjCIvarDecl *Ivar = 0;
3423 if (D->getPropertyIvarDecl()) {
3424 Ivar = cast_or_null<ObjCIvarDecl>(
3425 Importer.Import(D->getPropertyIvarDecl()));
3426 if (!Ivar)
3427 return 0;
3428 }
3429
3430 ObjCPropertyImplDecl *ToImpl
3431 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3432 if (!ToImpl) {
3433 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3434 Importer.Import(D->getLocStart()),
3435 Importer.Import(D->getLocation()),
3436 Property,
3437 D->getPropertyImplementation(),
3438 Ivar,
3439 Importer.Import(D->getPropertyIvarDeclLoc()));
3440 ToImpl->setLexicalDeclContext(LexicalDC);
3441 Importer.Imported(D, ToImpl);
3442 LexicalDC->addDecl(ToImpl);
3443 } else {
3444 // Check that we have the same kind of property implementation (@synthesize
3445 // vs. @dynamic).
3446 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3447 Importer.ToDiag(ToImpl->getLocation(),
3448 diag::err_odr_objc_property_impl_kind_inconsistent)
3449 << Property->getDeclName()
3450 << (ToImpl->getPropertyImplementation()
3451 == ObjCPropertyImplDecl::Dynamic);
3452 Importer.FromDiag(D->getLocation(),
3453 diag::note_odr_objc_property_impl_kind)
3454 << D->getPropertyDecl()->getDeclName()
3455 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3456 return 0;
3457 }
3458
3459 // For @synthesize, check that we have the same
3460 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3461 Ivar != ToImpl->getPropertyIvarDecl()) {
3462 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3463 diag::err_odr_objc_synthesize_ivar_inconsistent)
3464 << Property->getDeclName()
3465 << ToImpl->getPropertyIvarDecl()->getDeclName()
3466 << Ivar->getDeclName();
3467 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3468 diag::note_odr_objc_synthesize_ivar_here)
3469 << D->getPropertyIvarDecl()->getDeclName();
3470 return 0;
3471 }
3472
3473 // Merge the existing implementation with the new implementation.
3474 Importer.Imported(D, ToImpl);
3475 }
3476
3477 return ToImpl;
3478}
3479
Douglas Gregor2b785022010-02-18 02:12:22 +00003480Decl *
3481ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3482 // Import the context of this declaration.
3483 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3484 if (!DC)
3485 return 0;
3486
3487 DeclContext *LexicalDC = DC;
3488 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3489 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3490 if (!LexicalDC)
3491 return 0;
3492 }
3493
3494 // Import the location of this declaration.
3495 SourceLocation Loc = Importer.Import(D->getLocation());
3496
Chris Lattner5f9e2722011-07-23 10:55:15 +00003497 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3498 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003499 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3500 = D->protocol_loc_begin();
3501 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3502 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3503 FromProto != FromProtoEnd;
3504 ++FromProto, ++FromProtoLoc) {
3505 ObjCProtocolDecl *ToProto
3506 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3507 if (!ToProto)
3508 continue;
3509
3510 Protocols.push_back(ToProto);
3511 Locations.push_back(Importer.Import(*FromProtoLoc));
3512 }
3513
3514 ObjCForwardProtocolDecl *ToForward
3515 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3516 Protocols.data(), Protocols.size(),
3517 Locations.data());
3518 ToForward->setLexicalDeclContext(LexicalDC);
3519 LexicalDC->addDecl(ToForward);
3520 Importer.Imported(D, ToForward);
3521 return ToForward;
3522}
3523
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003524Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3525 // Import the context of this declaration.
3526 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3527 if (!DC)
3528 return 0;
3529
3530 DeclContext *LexicalDC = DC;
3531 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3532 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3533 if (!LexicalDC)
3534 return 0;
3535 }
3536
3537 // Import the location of this declaration.
3538 SourceLocation Loc = Importer.Import(D->getLocation());
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003539 ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
3540 ObjCInterfaceDecl *ToIface
3541 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003542 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003543 Loc,
3544 ToIface,
3545 Importer.Import(From->getLocation()));
3546
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003547 ToClass->setLexicalDeclContext(LexicalDC);
3548 LexicalDC->addDecl(ToClass);
3549 Importer.Imported(D, ToClass);
3550 return ToClass;
3551}
3552
Douglas Gregor040afae2010-11-30 19:14:50 +00003553Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3554 // For template arguments, we adopt the translation unit as our declaration
3555 // context. This context will be fixed when the actual template declaration
3556 // is created.
3557
3558 // FIXME: Import default argument.
3559 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3560 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003561 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003562 Importer.Import(D->getLocation()),
3563 D->getDepth(),
3564 D->getIndex(),
3565 Importer.Import(D->getIdentifier()),
3566 D->wasDeclaredWithTypename(),
3567 D->isParameterPack());
3568}
3569
3570Decl *
3571ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3572 // Import the name of this declaration.
3573 DeclarationName Name = Importer.Import(D->getDeclName());
3574 if (D->getDeclName() && !Name)
3575 return 0;
3576
3577 // Import the location of this declaration.
3578 SourceLocation Loc = Importer.Import(D->getLocation());
3579
3580 // Import the type of this declaration.
3581 QualType T = Importer.Import(D->getType());
3582 if (T.isNull())
3583 return 0;
3584
3585 // Import type-source information.
3586 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3587 if (D->getTypeSourceInfo() && !TInfo)
3588 return 0;
3589
3590 // FIXME: Import default argument.
3591
3592 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3593 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003594 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003595 Loc, D->getDepth(), D->getPosition(),
3596 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003597 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003598}
3599
3600Decl *
3601ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3602 // Import the name of this declaration.
3603 DeclarationName Name = Importer.Import(D->getDeclName());
3604 if (D->getDeclName() && !Name)
3605 return 0;
3606
3607 // Import the location of this declaration.
3608 SourceLocation Loc = Importer.Import(D->getLocation());
3609
3610 // Import template parameters.
3611 TemplateParameterList *TemplateParams
3612 = ImportTemplateParameterList(D->getTemplateParameters());
3613 if (!TemplateParams)
3614 return 0;
3615
3616 // FIXME: Import default argument.
3617
3618 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3619 Importer.getToContext().getTranslationUnitDecl(),
3620 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003621 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003622 Name.getAsIdentifierInfo(),
3623 TemplateParams);
3624}
3625
3626Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3627 // If this record has a definition in the translation unit we're coming from,
3628 // but this particular declaration is not that definition, import the
3629 // definition and map to that.
3630 CXXRecordDecl *Definition
3631 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3632 if (Definition && Definition != D->getTemplatedDecl()) {
3633 Decl *ImportedDef
3634 = Importer.Import(Definition->getDescribedClassTemplate());
3635 if (!ImportedDef)
3636 return 0;
3637
3638 return Importer.Imported(D, ImportedDef);
3639 }
3640
3641 // Import the major distinguishing characteristics of this class template.
3642 DeclContext *DC, *LexicalDC;
3643 DeclarationName Name;
3644 SourceLocation Loc;
3645 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3646 return 0;
3647
3648 // We may already have a template of the same name; try to find and match it.
3649 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003650 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor040afae2010-11-30 19:14:50 +00003651 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3652 Lookup.first != Lookup.second;
3653 ++Lookup.first) {
3654 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3655 continue;
3656
3657 Decl *Found = *Lookup.first;
3658 if (ClassTemplateDecl *FoundTemplate
3659 = dyn_cast<ClassTemplateDecl>(Found)) {
3660 if (IsStructuralMatch(D, FoundTemplate)) {
3661 // The class templates structurally match; call it the same template.
3662 // FIXME: We may be filling in a forward declaration here. Handle
3663 // this case!
3664 Importer.Imported(D->getTemplatedDecl(),
3665 FoundTemplate->getTemplatedDecl());
3666 return Importer.Imported(D, FoundTemplate);
3667 }
3668 }
3669
3670 ConflictingDecls.push_back(*Lookup.first);
3671 }
3672
3673 if (!ConflictingDecls.empty()) {
3674 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3675 ConflictingDecls.data(),
3676 ConflictingDecls.size());
3677 }
3678
3679 if (!Name)
3680 return 0;
3681 }
3682
3683 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3684
3685 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003686 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3687 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003688 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3689 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003690 DC, StartLoc, IdLoc,
3691 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003692 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003693 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003694 D2Templated->setLexicalDeclContext(LexicalDC);
3695
3696 // Create the class template declaration itself.
3697 TemplateParameterList *TemplateParams
3698 = ImportTemplateParameterList(D->getTemplateParameters());
3699 if (!TemplateParams)
3700 return 0;
3701
3702 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3703 Loc, Name, TemplateParams,
3704 D2Templated,
3705 /*PrevDecl=*/0);
3706 D2Templated->setDescribedClassTemplate(D2);
3707
3708 D2->setAccess(D->getAccess());
3709 D2->setLexicalDeclContext(LexicalDC);
3710 LexicalDC->addDecl(D2);
3711
3712 // Note the relationship between the class templates.
3713 Importer.Imported(D, D2);
3714 Importer.Imported(DTemplated, D2Templated);
3715
John McCall5e1cdac2011-10-07 06:10:15 +00003716 if (DTemplated->isCompleteDefinition() &&
3717 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003718 // FIXME: Import definition!
3719 }
3720
3721 return D2;
3722}
3723
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003724Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3725 ClassTemplateSpecializationDecl *D) {
3726 // If this record has a definition in the translation unit we're coming from,
3727 // but this particular declaration is not that definition, import the
3728 // definition and map to that.
3729 TagDecl *Definition = D->getDefinition();
3730 if (Definition && Definition != D) {
3731 Decl *ImportedDef = Importer.Import(Definition);
3732 if (!ImportedDef)
3733 return 0;
3734
3735 return Importer.Imported(D, ImportedDef);
3736 }
3737
3738 ClassTemplateDecl *ClassTemplate
3739 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3740 D->getSpecializedTemplate()));
3741 if (!ClassTemplate)
3742 return 0;
3743
3744 // Import the context of this declaration.
3745 DeclContext *DC = ClassTemplate->getDeclContext();
3746 if (!DC)
3747 return 0;
3748
3749 DeclContext *LexicalDC = DC;
3750 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3751 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3752 if (!LexicalDC)
3753 return 0;
3754 }
3755
3756 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003757 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3758 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003759
3760 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003761 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003762 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3763 D->getTemplateArgs().size(),
3764 TemplateArgs))
3765 return 0;
3766
3767 // Try to find an existing specialization with these template arguments.
3768 void *InsertPos = 0;
3769 ClassTemplateSpecializationDecl *D2
3770 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3771 TemplateArgs.size(), InsertPos);
3772 if (D2) {
3773 // We already have a class template specialization with these template
3774 // arguments.
3775
3776 // FIXME: Check for specialization vs. instantiation errors.
3777
3778 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003779 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003780 // The record types structurally match, or the "from" translation
3781 // unit only had a forward declaration anyway; call it the same
3782 // function.
3783 return Importer.Imported(D, FoundDef);
3784 }
3785 }
3786 } else {
3787 // Create a new specialization.
3788 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3789 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003790 StartLoc, IdLoc,
3791 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003792 TemplateArgs.data(),
3793 TemplateArgs.size(),
3794 /*PrevDecl=*/0);
3795 D2->setSpecializationKind(D->getSpecializationKind());
3796
3797 // Add this specialization to the class template.
3798 ClassTemplate->AddSpecialization(D2, InsertPos);
3799
3800 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003801 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003802
3803 // Add the specialization to this context.
3804 D2->setLexicalDeclContext(LexicalDC);
3805 LexicalDC->addDecl(D2);
3806 }
3807 Importer.Imported(D, D2);
3808
John McCall5e1cdac2011-10-07 06:10:15 +00003809 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003810 return 0;
3811
3812 return D2;
3813}
3814
Douglas Gregor4800d952010-02-11 19:21:55 +00003815//----------------------------------------------------------------------------
3816// Import Statements
3817//----------------------------------------------------------------------------
3818
3819Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3820 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3821 << S->getStmtClassName();
3822 return 0;
3823}
3824
3825//----------------------------------------------------------------------------
3826// Import Expressions
3827//----------------------------------------------------------------------------
3828Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3829 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3830 << E->getStmtClassName();
3831 return 0;
3832}
3833
Douglas Gregor44080632010-02-19 01:17:02 +00003834Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003835 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3836 if (!ToD)
3837 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003838
3839 NamedDecl *FoundD = 0;
3840 if (E->getDecl() != E->getFoundDecl()) {
3841 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3842 if (!FoundD)
3843 return 0;
3844 }
Douglas Gregor44080632010-02-19 01:17:02 +00003845
3846 QualType T = Importer.Import(E->getType());
3847 if (T.isNull())
3848 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003849
3850 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3851 Importer.Import(E->getQualifierLoc()),
3852 ToD,
3853 Importer.Import(E->getLocation()),
3854 T, E->getValueKind(),
3855 FoundD,
3856 /*FIXME:TemplateArgs=*/0);
3857 if (E->hadMultipleCandidates())
3858 DRE->setHadMultipleCandidates(true);
3859 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003860}
3861
Douglas Gregor4800d952010-02-11 19:21:55 +00003862Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3863 QualType T = Importer.Import(E->getType());
3864 if (T.isNull())
3865 return 0;
3866
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003867 return IntegerLiteral::Create(Importer.getToContext(),
3868 E->getValue(), T,
3869 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003870}
3871
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003872Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3873 QualType T = Importer.Import(E->getType());
3874 if (T.isNull())
3875 return 0;
3876
Douglas Gregor5cee1192011-07-27 05:40:30 +00003877 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3878 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003879 Importer.Import(E->getLocation()));
3880}
3881
Douglas Gregorf638f952010-02-19 01:07:06 +00003882Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3883 Expr *SubExpr = Importer.Import(E->getSubExpr());
3884 if (!SubExpr)
3885 return 0;
3886
3887 return new (Importer.getToContext())
3888 ParenExpr(Importer.Import(E->getLParen()),
3889 Importer.Import(E->getRParen()),
3890 SubExpr);
3891}
3892
3893Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3894 QualType T = Importer.Import(E->getType());
3895 if (T.isNull())
3896 return 0;
3897
3898 Expr *SubExpr = Importer.Import(E->getSubExpr());
3899 if (!SubExpr)
3900 return 0;
3901
3902 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003903 T, E->getValueKind(),
3904 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003905 Importer.Import(E->getOperatorLoc()));
3906}
3907
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003908Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3909 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003910 QualType ResultType = Importer.Import(E->getType());
3911
3912 if (E->isArgumentType()) {
3913 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3914 if (!TInfo)
3915 return 0;
3916
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003917 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3918 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003919 Importer.Import(E->getOperatorLoc()),
3920 Importer.Import(E->getRParenLoc()));
3921 }
3922
3923 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3924 if (!SubExpr)
3925 return 0;
3926
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003927 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3928 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003929 Importer.Import(E->getOperatorLoc()),
3930 Importer.Import(E->getRParenLoc()));
3931}
3932
Douglas Gregorf638f952010-02-19 01:07:06 +00003933Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3934 QualType T = Importer.Import(E->getType());
3935 if (T.isNull())
3936 return 0;
3937
3938 Expr *LHS = Importer.Import(E->getLHS());
3939 if (!LHS)
3940 return 0;
3941
3942 Expr *RHS = Importer.Import(E->getRHS());
3943 if (!RHS)
3944 return 0;
3945
3946 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003947 T, E->getValueKind(),
3948 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003949 Importer.Import(E->getOperatorLoc()));
3950}
3951
3952Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3953 QualType T = Importer.Import(E->getType());
3954 if (T.isNull())
3955 return 0;
3956
3957 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3958 if (CompLHSType.isNull())
3959 return 0;
3960
3961 QualType CompResultType = Importer.Import(E->getComputationResultType());
3962 if (CompResultType.isNull())
3963 return 0;
3964
3965 Expr *LHS = Importer.Import(E->getLHS());
3966 if (!LHS)
3967 return 0;
3968
3969 Expr *RHS = Importer.Import(E->getRHS());
3970 if (!RHS)
3971 return 0;
3972
3973 return new (Importer.getToContext())
3974 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003975 T, E->getValueKind(),
3976 E->getObjectKind(),
3977 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003978 Importer.Import(E->getOperatorLoc()));
3979}
3980
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003981static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003982 if (E->path_empty()) return false;
3983
3984 // TODO: import cast paths
3985 return true;
3986}
3987
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003988Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3989 QualType T = Importer.Import(E->getType());
3990 if (T.isNull())
3991 return 0;
3992
3993 Expr *SubExpr = Importer.Import(E->getSubExpr());
3994 if (!SubExpr)
3995 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003996
3997 CXXCastPath BasePath;
3998 if (ImportCastPath(E, BasePath))
3999 return 0;
4000
4001 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004002 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004003}
4004
Douglas Gregor008847a2010-02-19 01:32:14 +00004005Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4006 QualType T = Importer.Import(E->getType());
4007 if (T.isNull())
4008 return 0;
4009
4010 Expr *SubExpr = Importer.Import(E->getSubExpr());
4011 if (!SubExpr)
4012 return 0;
4013
4014 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4015 if (!TInfo && E->getTypeInfoAsWritten())
4016 return 0;
4017
John McCallf871d0c2010-08-07 06:22:56 +00004018 CXXCastPath BasePath;
4019 if (ImportCastPath(E, BasePath))
4020 return 0;
4021
John McCallf89e55a2010-11-18 06:31:45 +00004022 return CStyleCastExpr::Create(Importer.getToContext(), T,
4023 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004024 SubExpr, &BasePath, TInfo,
4025 Importer.Import(E->getLParenLoc()),
4026 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004027}
4028
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004029ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004030 ASTContext &FromContext, FileManager &FromFileManager,
4031 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004032 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004033 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4034 Minimal(MinimalImport)
4035{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004036 ImportedDecls[FromContext.getTranslationUnitDecl()]
4037 = ToContext.getTranslationUnitDecl();
4038}
4039
4040ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004041
4042QualType ASTImporter::Import(QualType FromT) {
4043 if (FromT.isNull())
4044 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004045
4046 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004047
Douglas Gregor169fba52010-02-08 15:18:58 +00004048 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004049 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4050 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004051 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004052 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004053
Douglas Gregor169fba52010-02-08 15:18:58 +00004054 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004055 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004056 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004057 if (ToT.isNull())
4058 return ToT;
4059
Douglas Gregor169fba52010-02-08 15:18:58 +00004060 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004061 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004062
John McCallf4c73712011-01-19 06:33:43 +00004063 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004064}
4065
Douglas Gregor9bed8792010-02-09 19:21:46 +00004066TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004067 if (!FromTSI)
4068 return FromTSI;
4069
4070 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004071 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004072 QualType T = Import(FromTSI->getType());
4073 if (T.isNull())
4074 return 0;
4075
4076 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004077 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004078}
4079
4080Decl *ASTImporter::Import(Decl *FromD) {
4081 if (!FromD)
4082 return 0;
4083
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004084 ASTNodeImporter Importer(*this);
4085
Douglas Gregor9bed8792010-02-09 19:21:46 +00004086 // Check whether we've already imported this declaration.
4087 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004088 if (Pos != ImportedDecls.end()) {
4089 Decl *ToD = Pos->second;
4090 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4091 return ToD;
4092 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004093
4094 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004095 Decl *ToD = Importer.Visit(FromD);
4096 if (!ToD)
4097 return 0;
4098
4099 // Record the imported declaration.
4100 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004101
4102 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4103 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004104 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004105 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004106 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004107 // When we've finished transforming a typedef, see whether it was the
4108 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004109 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004110 FromTag = AnonTagsWithPendingTypedefs.begin(),
4111 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4112 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004113 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004114 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4115 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004116 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004117 AnonTagsWithPendingTypedefs.erase(FromTag);
4118 break;
4119 }
4120 }
4121 }
4122 }
4123
Douglas Gregor9bed8792010-02-09 19:21:46 +00004124 return ToD;
4125}
4126
4127DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4128 if (!FromDC)
4129 return FromDC;
4130
4131 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4132}
4133
4134Expr *ASTImporter::Import(Expr *FromE) {
4135 if (!FromE)
4136 return 0;
4137
4138 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4139}
4140
4141Stmt *ASTImporter::Import(Stmt *FromS) {
4142 if (!FromS)
4143 return 0;
4144
Douglas Gregor4800d952010-02-11 19:21:55 +00004145 // Check whether we've already imported this declaration.
4146 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4147 if (Pos != ImportedStmts.end())
4148 return Pos->second;
4149
4150 // Import the type
4151 ASTNodeImporter Importer(*this);
4152 Stmt *ToS = Importer.Visit(FromS);
4153 if (!ToS)
4154 return 0;
4155
4156 // Record the imported declaration.
4157 ImportedStmts[FromS] = ToS;
4158 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004159}
4160
4161NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4162 if (!FromNNS)
4163 return 0;
4164
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004165 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4166
4167 switch (FromNNS->getKind()) {
4168 case NestedNameSpecifier::Identifier:
4169 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4170 return NestedNameSpecifier::Create(ToContext, prefix, II);
4171 }
4172 return 0;
4173
4174 case NestedNameSpecifier::Namespace:
4175 if (NamespaceDecl *NS =
4176 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4177 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4178 }
4179 return 0;
4180
4181 case NestedNameSpecifier::NamespaceAlias:
4182 if (NamespaceAliasDecl *NSAD =
4183 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4184 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4185 }
4186 return 0;
4187
4188 case NestedNameSpecifier::Global:
4189 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4190
4191 case NestedNameSpecifier::TypeSpec:
4192 case NestedNameSpecifier::TypeSpecWithTemplate: {
4193 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4194 if (!T.isNull()) {
4195 bool bTemplate = FromNNS->getKind() ==
4196 NestedNameSpecifier::TypeSpecWithTemplate;
4197 return NestedNameSpecifier::Create(ToContext, prefix,
4198 bTemplate, T.getTypePtr());
4199 }
4200 }
4201 return 0;
4202 }
4203
4204 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004205 return 0;
4206}
4207
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004208NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4209 // FIXME: Implement!
4210 return NestedNameSpecifierLoc();
4211}
4212
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004213TemplateName ASTImporter::Import(TemplateName From) {
4214 switch (From.getKind()) {
4215 case TemplateName::Template:
4216 if (TemplateDecl *ToTemplate
4217 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4218 return TemplateName(ToTemplate);
4219
4220 return TemplateName();
4221
4222 case TemplateName::OverloadedTemplate: {
4223 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4224 UnresolvedSet<2> ToTemplates;
4225 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4226 E = FromStorage->end();
4227 I != E; ++I) {
4228 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4229 ToTemplates.addDecl(To);
4230 else
4231 return TemplateName();
4232 }
4233 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4234 ToTemplates.end());
4235 }
4236
4237 case TemplateName::QualifiedTemplate: {
4238 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4239 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4240 if (!Qualifier)
4241 return TemplateName();
4242
4243 if (TemplateDecl *ToTemplate
4244 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4245 return ToContext.getQualifiedTemplateName(Qualifier,
4246 QTN->hasTemplateKeyword(),
4247 ToTemplate);
4248
4249 return TemplateName();
4250 }
4251
4252 case TemplateName::DependentTemplate: {
4253 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4254 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4255 if (!Qualifier)
4256 return TemplateName();
4257
4258 if (DTN->isIdentifier()) {
4259 return ToContext.getDependentTemplateName(Qualifier,
4260 Import(DTN->getIdentifier()));
4261 }
4262
4263 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4264 }
John McCall14606042011-06-30 08:33:18 +00004265
4266 case TemplateName::SubstTemplateTemplateParm: {
4267 SubstTemplateTemplateParmStorage *subst
4268 = From.getAsSubstTemplateTemplateParm();
4269 TemplateTemplateParmDecl *param
4270 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4271 if (!param)
4272 return TemplateName();
4273
4274 TemplateName replacement = Import(subst->getReplacement());
4275 if (replacement.isNull()) return TemplateName();
4276
4277 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4278 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004279
4280 case TemplateName::SubstTemplateTemplateParmPack: {
4281 SubstTemplateTemplateParmPackStorage *SubstPack
4282 = From.getAsSubstTemplateTemplateParmPack();
4283 TemplateTemplateParmDecl *Param
4284 = cast_or_null<TemplateTemplateParmDecl>(
4285 Import(SubstPack->getParameterPack()));
4286 if (!Param)
4287 return TemplateName();
4288
4289 ASTNodeImporter Importer(*this);
4290 TemplateArgument ArgPack
4291 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4292 if (ArgPack.isNull())
4293 return TemplateName();
4294
4295 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4296 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004297 }
4298
4299 llvm_unreachable("Invalid template name kind");
4300 return TemplateName();
4301}
4302
Douglas Gregor9bed8792010-02-09 19:21:46 +00004303SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4304 if (FromLoc.isInvalid())
4305 return SourceLocation();
4306
Douglas Gregor88523732010-02-10 00:15:17 +00004307 SourceManager &FromSM = FromContext.getSourceManager();
4308
4309 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004310 // don't have to import macro expansions.
4311 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004312 FromLoc = FromSM.getSpellingLoc(FromLoc);
4313 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4314 SourceManager &ToSM = ToContext.getSourceManager();
4315 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004316 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004317}
4318
4319SourceRange ASTImporter::Import(SourceRange FromRange) {
4320 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4321}
4322
Douglas Gregor88523732010-02-10 00:15:17 +00004323FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004324 llvm::DenseMap<FileID, FileID>::iterator Pos
4325 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004326 if (Pos != ImportedFileIDs.end())
4327 return Pos->second;
4328
4329 SourceManager &FromSM = FromContext.getSourceManager();
4330 SourceManager &ToSM = ToContext.getSourceManager();
4331 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004332 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004333
4334 // Include location of this file.
4335 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4336
4337 // Map the FileID for to the "to" source manager.
4338 FileID ToID;
4339 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004340 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004341 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4342 // disk again
4343 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4344 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004345 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004346 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4347 FromSLoc.getFile().getFileCharacteristic());
4348 } else {
4349 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004350 const llvm::MemoryBuffer *
4351 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004352 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004353 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004354 FromBuf->getBufferIdentifier());
4355 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4356 }
4357
4358
Sebastian Redl535a3e22010-09-30 01:03:06 +00004359 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004360 return ToID;
4361}
4362
Douglas Gregord8868a62011-01-18 03:11:38 +00004363void ASTImporter::ImportDefinition(Decl *From) {
4364 Decl *To = Import(From);
4365 if (!To)
4366 return;
4367
4368 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4369 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004370
4371 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4372 if (!ToRecord->getDefinition()) {
4373 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4374 /*ForceImport=*/true);
4375 return;
4376 }
4377 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004378
4379 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4380 if (!ToEnum->getDefinition()) {
4381 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4382 /*ForceImport=*/true);
4383 return;
4384 }
4385 }
4386
Douglas Gregord8868a62011-01-18 03:11:38 +00004387 Importer.ImportDeclContext(FromDC, true);
4388 }
4389}
4390
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004391DeclarationName ASTImporter::Import(DeclarationName FromName) {
4392 if (!FromName)
4393 return DeclarationName();
4394
4395 switch (FromName.getNameKind()) {
4396 case DeclarationName::Identifier:
4397 return Import(FromName.getAsIdentifierInfo());
4398
4399 case DeclarationName::ObjCZeroArgSelector:
4400 case DeclarationName::ObjCOneArgSelector:
4401 case DeclarationName::ObjCMultiArgSelector:
4402 return Import(FromName.getObjCSelector());
4403
4404 case DeclarationName::CXXConstructorName: {
4405 QualType T = Import(FromName.getCXXNameType());
4406 if (T.isNull())
4407 return DeclarationName();
4408
4409 return ToContext.DeclarationNames.getCXXConstructorName(
4410 ToContext.getCanonicalType(T));
4411 }
4412
4413 case DeclarationName::CXXDestructorName: {
4414 QualType T = Import(FromName.getCXXNameType());
4415 if (T.isNull())
4416 return DeclarationName();
4417
4418 return ToContext.DeclarationNames.getCXXDestructorName(
4419 ToContext.getCanonicalType(T));
4420 }
4421
4422 case DeclarationName::CXXConversionFunctionName: {
4423 QualType T = Import(FromName.getCXXNameType());
4424 if (T.isNull())
4425 return DeclarationName();
4426
4427 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4428 ToContext.getCanonicalType(T));
4429 }
4430
4431 case DeclarationName::CXXOperatorName:
4432 return ToContext.DeclarationNames.getCXXOperatorName(
4433 FromName.getCXXOverloadedOperator());
4434
4435 case DeclarationName::CXXLiteralOperatorName:
4436 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4437 Import(FromName.getCXXLiteralIdentifier()));
4438
4439 case DeclarationName::CXXUsingDirective:
4440 // FIXME: STATICS!
4441 return DeclarationName::getUsingDirectiveName();
4442 }
4443
4444 // Silence bogus GCC warning
4445 return DeclarationName();
4446}
4447
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004448IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004449 if (!FromId)
4450 return 0;
4451
4452 return &ToContext.Idents.get(FromId->getName());
4453}
Douglas Gregor089459a2010-02-08 21:09:39 +00004454
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004455Selector ASTImporter::Import(Selector FromSel) {
4456 if (FromSel.isNull())
4457 return Selector();
4458
Chris Lattner5f9e2722011-07-23 10:55:15 +00004459 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004460 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4461 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4462 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4463 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4464}
4465
Douglas Gregor089459a2010-02-08 21:09:39 +00004466DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4467 DeclContext *DC,
4468 unsigned IDNS,
4469 NamedDecl **Decls,
4470 unsigned NumDecls) {
4471 return Name;
4472}
4473
4474DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004475 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004476}
4477
4478DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004479 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004480}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004481
4482Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4483 ImportedDecls[From] = To;
4484 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004485}
Douglas Gregorea35d112010-02-15 23:54:17 +00004486
4487bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004488 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004489 = ImportedTypes.find(From.getTypePtr());
4490 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4491 return true;
4492
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004493 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004494 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004495}