blob: b74e741d6f6aede4339077fa63f46c9354b3f231 [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
Douglas Gregor27c72d82011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor089459a2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
33
34 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000040
41 // Importing types
John McCallf4c73712011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
43 QualType VisitBuiltinType(const BuiltinType *T);
44 QualType VisitComplexType(const ComplexType *T);
45 QualType VisitPointerType(const PointerType *T);
46 QualType VisitBlockPointerType(const BlockPointerType *T);
47 QualType VisitLValueReferenceType(const LValueReferenceType *T);
48 QualType VisitRValueReferenceType(const RValueReferenceType *T);
49 QualType VisitMemberPointerType(const MemberPointerType *T);
50 QualType VisitConstantArrayType(const ConstantArrayType *T);
51 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
52 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000055 QualType VisitVectorType(const VectorType *T);
56 QualType VisitExtVectorType(const ExtVectorType *T);
57 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
58 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000089 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
90 bool ForceImport = false);
91 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
92 bool ForceImport = false);
Douglas Gregor5602f7e2012-01-24 17:42:07 +000093 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
94 bool ForceImport = false);
95 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
96 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000097 TemplateParameterList *ImportTemplateParameterList(
98 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000099 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
100 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
101 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000102 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000103 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000104 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000105 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000106 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000107 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000108 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000109 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000110 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000111 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000112 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000113 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000114 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000115 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000116 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
117 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
118 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
119 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000120 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000121 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000122 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000123 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000124 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000125 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000126 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000127 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000128 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000129 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000130 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000131 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000132 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000133 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000134 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
135 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
136 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
137 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000138 Decl *VisitClassTemplateSpecializationDecl(
139 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000140
Douglas Gregor4800d952010-02-11 19:21:55 +0000141 // Importing statements
142 Stmt *VisitStmt(Stmt *S);
143
144 // Importing expressions
145 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000146 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000147 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000148 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000149 Expr *VisitParenExpr(ParenExpr *E);
150 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000151 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000152 Expr *VisitBinaryOperator(BinaryOperator *E);
153 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000154 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000155 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000156 };
157}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000158using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000159
160//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000161// Structural Equivalence
162//----------------------------------------------------------------------------
163
164namespace {
165 struct StructuralEquivalenceContext {
166 /// \brief AST contexts for which we are checking structural equivalence.
167 ASTContext &C1, &C2;
168
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000169 /// \brief The set of "tentative" equivalences between two canonical
170 /// declarations, mapping from a declaration in the first context to the
171 /// declaration in the second context that we believe to be equivalent.
172 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
173
174 /// \brief Queue of declarations in the first context whose equivalence
175 /// with a declaration in the second context still needs to be verified.
176 std::deque<Decl *> DeclsToCheck;
177
Douglas Gregorea35d112010-02-15 23:54:17 +0000178 /// \brief Declaration (from, to) pairs that are known not to be equivalent
179 /// (which we have already complained about).
180 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
181
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000182 /// \brief Whether we're being strict about the spelling of types when
183 /// unifying two types.
184 bool StrictTypeSpelling;
185
186 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000187 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000188 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000189 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000190 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000191
192 /// \brief Determine whether the two declarations are structurally
193 /// equivalent.
194 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
195
196 /// \brief Determine whether the two types are structurally equivalent.
197 bool IsStructurallyEquivalent(QualType T1, QualType T2);
198
199 private:
200 /// \brief Finish checking all of the structural equivalences.
201 ///
202 /// \returns true if an error occurred, false otherwise.
203 bool Finish();
204
205 public:
206 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000207 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000208 }
209
210 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000211 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000212 }
213 };
214}
215
216static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
217 QualType T1, QualType T2);
218static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
219 Decl *D1, Decl *D2);
220
221/// \brief Determine if two APInts have the same value, after zero-extending
222/// one of them (if needed!) to ensure that the bit-widths match.
223static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
224 if (I1.getBitWidth() == I2.getBitWidth())
225 return I1 == I2;
226
227 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000228 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000229
Jay Foad9f71a8f2010-12-07 08:25:34 +0000230 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000231}
232
233/// \brief Determine if two APSInts have the same value, zero- or sign-extending
234/// as needed.
235static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
236 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
237 return I1 == I2;
238
239 // Check for a bit-width mismatch.
240 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000241 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000242 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000243 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000244
245 // We have a signedness mismatch. Turn the signed value into an unsigned
246 // value.
247 if (I1.isSigned()) {
248 if (I1.isNegative())
249 return false;
250
251 return llvm::APSInt(I1, true) == I2;
252 }
253
254 if (I2.isNegative())
255 return false;
256
257 return I1 == llvm::APSInt(I2, true);
258}
259
260/// \brief Determine structural equivalence of two expressions.
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 Expr *E1, Expr *E2) {
263 if (!E1 || !E2)
264 return E1 == E2;
265
266 // FIXME: Actually perform a structural comparison!
267 return true;
268}
269
270/// \brief Determine whether two identifiers are equivalent.
271static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
272 const IdentifierInfo *Name2) {
273 if (!Name1 || !Name2)
274 return Name1 == Name2;
275
276 return Name1->getName() == Name2->getName();
277}
278
279/// \brief Determine whether two nested-name-specifiers are equivalent.
280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
281 NestedNameSpecifier *NNS1,
282 NestedNameSpecifier *NNS2) {
283 // FIXME: Implement!
284 return true;
285}
286
287/// \brief Determine whether two template arguments are equivalent.
288static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
289 const TemplateArgument &Arg1,
290 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000291 if (Arg1.getKind() != Arg2.getKind())
292 return false;
293
294 switch (Arg1.getKind()) {
295 case TemplateArgument::Null:
296 return true;
297
298 case TemplateArgument::Type:
299 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
300
301 case TemplateArgument::Integral:
302 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
303 Arg2.getIntegralType()))
304 return false;
305
306 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
307
308 case TemplateArgument::Declaration:
309 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
310
311 case TemplateArgument::Template:
312 return IsStructurallyEquivalent(Context,
313 Arg1.getAsTemplate(),
314 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000315
316 case TemplateArgument::TemplateExpansion:
317 return IsStructurallyEquivalent(Context,
318 Arg1.getAsTemplateOrTemplatePattern(),
319 Arg2.getAsTemplateOrTemplatePattern());
320
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000321 case TemplateArgument::Expression:
322 return IsStructurallyEquivalent(Context,
323 Arg1.getAsExpr(), Arg2.getAsExpr());
324
325 case TemplateArgument::Pack:
326 if (Arg1.pack_size() != Arg2.pack_size())
327 return false;
328
329 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
330 if (!IsStructurallyEquivalent(Context,
331 Arg1.pack_begin()[I],
332 Arg2.pack_begin()[I]))
333 return false;
334
335 return true;
336 }
337
338 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000339}
340
341/// \brief Determine structural equivalence for the common part of array
342/// types.
343static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
344 const ArrayType *Array1,
345 const ArrayType *Array2) {
346 if (!IsStructurallyEquivalent(Context,
347 Array1->getElementType(),
348 Array2->getElementType()))
349 return false;
350 if (Array1->getSizeModifier() != Array2->getSizeModifier())
351 return false;
352 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
353 return false;
354
355 return true;
356}
357
358/// \brief Determine structural equivalence of two types.
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 QualType T1, QualType T2) {
361 if (T1.isNull() || T2.isNull())
362 return T1.isNull() && T2.isNull();
363
364 if (!Context.StrictTypeSpelling) {
365 // We aren't being strict about token-to-token equivalence of types,
366 // so map down to the canonical type.
367 T1 = Context.C1.getCanonicalType(T1);
368 T2 = Context.C2.getCanonicalType(T2);
369 }
370
371 if (T1.getQualifiers() != T2.getQualifiers())
372 return false;
373
Douglas Gregorea35d112010-02-15 23:54:17 +0000374 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000375
Douglas Gregorea35d112010-02-15 23:54:17 +0000376 if (T1->getTypeClass() != T2->getTypeClass()) {
377 // Compare function types with prototypes vs. without prototypes as if
378 // both did not have prototypes.
379 if (T1->getTypeClass() == Type::FunctionProto &&
380 T2->getTypeClass() == Type::FunctionNoProto)
381 TC = Type::FunctionNoProto;
382 else if (T1->getTypeClass() == Type::FunctionNoProto &&
383 T2->getTypeClass() == Type::FunctionProto)
384 TC = Type::FunctionNoProto;
385 else
386 return false;
387 }
388
389 switch (TC) {
390 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000391 // FIXME: Deal with Char_S/Char_U.
392 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
393 return false;
394 break;
395
396 case Type::Complex:
397 if (!IsStructurallyEquivalent(Context,
398 cast<ComplexType>(T1)->getElementType(),
399 cast<ComplexType>(T2)->getElementType()))
400 return false;
401 break;
402
403 case Type::Pointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<PointerType>(T1)->getPointeeType(),
406 cast<PointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::BlockPointer:
411 if (!IsStructurallyEquivalent(Context,
412 cast<BlockPointerType>(T1)->getPointeeType(),
413 cast<BlockPointerType>(T2)->getPointeeType()))
414 return false;
415 break;
416
417 case Type::LValueReference:
418 case Type::RValueReference: {
419 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
420 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
421 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
422 return false;
423 if (Ref1->isInnerRef() != Ref2->isInnerRef())
424 return false;
425 if (!IsStructurallyEquivalent(Context,
426 Ref1->getPointeeTypeAsWritten(),
427 Ref2->getPointeeTypeAsWritten()))
428 return false;
429 break;
430 }
431
432 case Type::MemberPointer: {
433 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
434 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
435 if (!IsStructurallyEquivalent(Context,
436 MemPtr1->getPointeeType(),
437 MemPtr2->getPointeeType()))
438 return false;
439 if (!IsStructurallyEquivalent(Context,
440 QualType(MemPtr1->getClass(), 0),
441 QualType(MemPtr2->getClass(), 0)))
442 return false;
443 break;
444 }
445
446 case Type::ConstantArray: {
447 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
448 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
449 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
450 return false;
451
452 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
453 return false;
454 break;
455 }
456
457 case Type::IncompleteArray:
458 if (!IsArrayStructurallyEquivalent(Context,
459 cast<ArrayType>(T1),
460 cast<ArrayType>(T2)))
461 return false;
462 break;
463
464 case Type::VariableArray: {
465 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
466 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
467 if (!IsStructurallyEquivalent(Context,
468 Array1->getSizeExpr(), Array2->getSizeExpr()))
469 return false;
470
471 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
472 return false;
473
474 break;
475 }
476
477 case Type::DependentSizedArray: {
478 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
479 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
480 if (!IsStructurallyEquivalent(Context,
481 Array1->getSizeExpr(), Array2->getSizeExpr()))
482 return false;
483
484 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
485 return false;
486
487 break;
488 }
489
490 case Type::DependentSizedExtVector: {
491 const DependentSizedExtVectorType *Vec1
492 = cast<DependentSizedExtVectorType>(T1);
493 const DependentSizedExtVectorType *Vec2
494 = cast<DependentSizedExtVectorType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
497 return false;
498 if (!IsStructurallyEquivalent(Context,
499 Vec1->getElementType(),
500 Vec2->getElementType()))
501 return false;
502 break;
503 }
504
505 case Type::Vector:
506 case Type::ExtVector: {
507 const VectorType *Vec1 = cast<VectorType>(T1);
508 const VectorType *Vec2 = cast<VectorType>(T2);
509 if (!IsStructurallyEquivalent(Context,
510 Vec1->getElementType(),
511 Vec2->getElementType()))
512 return false;
513 if (Vec1->getNumElements() != Vec2->getNumElements())
514 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000515 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000516 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000517 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000518 }
519
520 case Type::FunctionProto: {
521 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
522 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
523 if (Proto1->getNumArgs() != Proto2->getNumArgs())
524 return false;
525 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
526 if (!IsStructurallyEquivalent(Context,
527 Proto1->getArgType(I),
528 Proto2->getArgType(I)))
529 return false;
530 }
531 if (Proto1->isVariadic() != Proto2->isVariadic())
532 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000533 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000534 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000535 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
536 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
537 return false;
538 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
539 if (!IsStructurallyEquivalent(Context,
540 Proto1->getExceptionType(I),
541 Proto2->getExceptionType(I)))
542 return false;
543 }
544 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000546 Proto1->getNoexceptExpr(),
547 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000548 return false;
549 }
550 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
551 return false;
552
553 // Fall through to check the bits common with FunctionNoProtoType.
554 }
555
556 case Type::FunctionNoProto: {
557 const FunctionType *Function1 = cast<FunctionType>(T1);
558 const FunctionType *Function2 = cast<FunctionType>(T2);
559 if (!IsStructurallyEquivalent(Context,
560 Function1->getResultType(),
561 Function2->getResultType()))
562 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000563 if (Function1->getExtInfo() != Function2->getExtInfo())
564 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000565 break;
566 }
567
568 case Type::UnresolvedUsing:
569 if (!IsStructurallyEquivalent(Context,
570 cast<UnresolvedUsingType>(T1)->getDecl(),
571 cast<UnresolvedUsingType>(T2)->getDecl()))
572 return false;
573
574 break;
John McCall9d156a72011-01-06 01:58:22 +0000575
576 case Type::Attributed:
577 if (!IsStructurallyEquivalent(Context,
578 cast<AttributedType>(T1)->getModifiedType(),
579 cast<AttributedType>(T2)->getModifiedType()))
580 return false;
581 if (!IsStructurallyEquivalent(Context,
582 cast<AttributedType>(T1)->getEquivalentType(),
583 cast<AttributedType>(T2)->getEquivalentType()))
584 return false;
585 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000586
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000587 case Type::Paren:
588 if (!IsStructurallyEquivalent(Context,
589 cast<ParenType>(T1)->getInnerType(),
590 cast<ParenType>(T2)->getInnerType()))
591 return false;
592 break;
593
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000594 case Type::Typedef:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypedefType>(T1)->getDecl(),
597 cast<TypedefType>(T2)->getDecl()))
598 return false;
599 break;
600
601 case Type::TypeOfExpr:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
604 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
605 return false;
606 break;
607
608 case Type::TypeOf:
609 if (!IsStructurallyEquivalent(Context,
610 cast<TypeOfType>(T1)->getUnderlyingType(),
611 cast<TypeOfType>(T2)->getUnderlyingType()))
612 return false;
613 break;
Sean Huntca63c202011-05-24 22:41:36 +0000614
615 case Type::UnaryTransform:
616 if (!IsStructurallyEquivalent(Context,
617 cast<UnaryTransformType>(T1)->getUnderlyingType(),
618 cast<UnaryTransformType>(T1)->getUnderlyingType()))
619 return false;
620 break;
621
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000622 case Type::Decltype:
623 if (!IsStructurallyEquivalent(Context,
624 cast<DecltypeType>(T1)->getUnderlyingExpr(),
625 cast<DecltypeType>(T2)->getUnderlyingExpr()))
626 return false;
627 break;
628
Richard Smith34b41d92011-02-20 03:19:35 +0000629 case Type::Auto:
630 if (!IsStructurallyEquivalent(Context,
631 cast<AutoType>(T1)->getDeducedType(),
632 cast<AutoType>(T2)->getDeducedType()))
633 return false;
634 break;
635
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000636 case Type::Record:
637 case Type::Enum:
638 if (!IsStructurallyEquivalent(Context,
639 cast<TagType>(T1)->getDecl(),
640 cast<TagType>(T2)->getDecl()))
641 return false;
642 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000643
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000644 case Type::TemplateTypeParm: {
645 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
646 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
647 if (Parm1->getDepth() != Parm2->getDepth())
648 return false;
649 if (Parm1->getIndex() != Parm2->getIndex())
650 return false;
651 if (Parm1->isParameterPack() != Parm2->isParameterPack())
652 return false;
653
654 // Names of template type parameters are never significant.
655 break;
656 }
657
658 case Type::SubstTemplateTypeParm: {
659 const SubstTemplateTypeParmType *Subst1
660 = cast<SubstTemplateTypeParmType>(T1);
661 const SubstTemplateTypeParmType *Subst2
662 = cast<SubstTemplateTypeParmType>(T2);
663 if (!IsStructurallyEquivalent(Context,
664 QualType(Subst1->getReplacedParameter(), 0),
665 QualType(Subst2->getReplacedParameter(), 0)))
666 return false;
667 if (!IsStructurallyEquivalent(Context,
668 Subst1->getReplacementType(),
669 Subst2->getReplacementType()))
670 return false;
671 break;
672 }
673
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000674 case Type::SubstTemplateTypeParmPack: {
675 const SubstTemplateTypeParmPackType *Subst1
676 = cast<SubstTemplateTypeParmPackType>(T1);
677 const SubstTemplateTypeParmPackType *Subst2
678 = cast<SubstTemplateTypeParmPackType>(T2);
679 if (!IsStructurallyEquivalent(Context,
680 QualType(Subst1->getReplacedParameter(), 0),
681 QualType(Subst2->getReplacedParameter(), 0)))
682 return false;
683 if (!IsStructurallyEquivalent(Context,
684 Subst1->getArgumentPack(),
685 Subst2->getArgumentPack()))
686 return false;
687 break;
688 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000689 case Type::TemplateSpecialization: {
690 const TemplateSpecializationType *Spec1
691 = cast<TemplateSpecializationType>(T1);
692 const TemplateSpecializationType *Spec2
693 = cast<TemplateSpecializationType>(T2);
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getTemplateName(),
696 Spec2->getTemplateName()))
697 return false;
698 if (Spec1->getNumArgs() != Spec2->getNumArgs())
699 return false;
700 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
701 if (!IsStructurallyEquivalent(Context,
702 Spec1->getArg(I), Spec2->getArg(I)))
703 return false;
704 }
705 break;
706 }
707
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000708 case Type::Elaborated: {
709 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
710 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
711 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
712 if (Elab1->getKeyword() != Elab2->getKeyword())
713 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000714 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000715 Elab1->getQualifier(),
716 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000717 return false;
718 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000719 Elab1->getNamedType(),
720 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000721 return false;
722 break;
723 }
724
John McCall3cb0ebd2010-03-10 03:28:59 +0000725 case Type::InjectedClassName: {
726 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
727 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
728 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000729 Inj1->getInjectedSpecializationType(),
730 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000731 return false;
732 break;
733 }
734
Douglas Gregor4714c122010-03-31 17:34:00 +0000735 case Type::DependentName: {
736 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
737 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000738 if (!IsStructurallyEquivalent(Context,
739 Typename1->getQualifier(),
740 Typename2->getQualifier()))
741 return false;
742 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
743 Typename2->getIdentifier()))
744 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000745
746 break;
747 }
748
John McCall33500952010-06-11 00:33:02 +0000749 case Type::DependentTemplateSpecialization: {
750 const DependentTemplateSpecializationType *Spec1 =
751 cast<DependentTemplateSpecializationType>(T1);
752 const DependentTemplateSpecializationType *Spec2 =
753 cast<DependentTemplateSpecializationType>(T2);
754 if (!IsStructurallyEquivalent(Context,
755 Spec1->getQualifier(),
756 Spec2->getQualifier()))
757 return false;
758 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
759 Spec2->getIdentifier()))
760 return false;
761 if (Spec1->getNumArgs() != Spec2->getNumArgs())
762 return false;
763 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
764 if (!IsStructurallyEquivalent(Context,
765 Spec1->getArg(I), Spec2->getArg(I)))
766 return false;
767 }
768 break;
769 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000770
771 case Type::PackExpansion:
772 if (!IsStructurallyEquivalent(Context,
773 cast<PackExpansionType>(T1)->getPattern(),
774 cast<PackExpansionType>(T2)->getPattern()))
775 return false;
776 break;
777
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000778 case Type::ObjCInterface: {
779 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
780 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
781 if (!IsStructurallyEquivalent(Context,
782 Iface1->getDecl(), Iface2->getDecl()))
783 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000784 break;
785 }
786
787 case Type::ObjCObject: {
788 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
789 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
790 if (!IsStructurallyEquivalent(Context,
791 Obj1->getBaseType(),
792 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000793 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000794 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
795 return false;
796 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000797 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000798 Obj1->getProtocol(I),
799 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000800 return false;
801 }
802 break;
803 }
804
805 case Type::ObjCObjectPointer: {
806 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
807 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
808 if (!IsStructurallyEquivalent(Context,
809 Ptr1->getPointeeType(),
810 Ptr2->getPointeeType()))
811 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000812 break;
813 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000814
815 case Type::Atomic: {
816 if (!IsStructurallyEquivalent(Context,
817 cast<AtomicType>(T1)->getValueType(),
818 cast<AtomicType>(T2)->getValueType()))
819 return false;
820 break;
821 }
822
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000823 } // end switch
824
825 return true;
826}
827
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000828/// \brief Determine structural equivalence of two fields.
829static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
830 FieldDecl *Field1, FieldDecl *Field2) {
831 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
832
833 if (!IsStructurallyEquivalent(Context,
834 Field1->getType(), Field2->getType())) {
835 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
836 << Context.C2.getTypeDeclType(Owner2);
837 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
838 << Field2->getDeclName() << Field2->getType();
839 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
840 << Field1->getDeclName() << Field1->getType();
841 return false;
842 }
843
844 if (Field1->isBitField() != Field2->isBitField()) {
845 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
846 << Context.C2.getTypeDeclType(Owner2);
847 if (Field1->isBitField()) {
848 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
849 << Field1->getDeclName() << Field1->getType()
850 << Field1->getBitWidthValue(Context.C1);
851 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
852 << Field2->getDeclName();
853 } else {
854 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
855 << Field2->getDeclName() << Field2->getType()
856 << Field2->getBitWidthValue(Context.C2);
857 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
858 << Field1->getDeclName();
859 }
860 return false;
861 }
862
863 if (Field1->isBitField()) {
864 // Make sure that the bit-fields are the same length.
865 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
866 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
867
868 if (Bits1 != Bits2) {
869 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
870 << Context.C2.getTypeDeclType(Owner2);
871 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
872 << Field2->getDeclName() << Field2->getType() << Bits2;
873 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
874 << Field1->getDeclName() << Field1->getType() << Bits1;
875 return false;
876 }
877 }
878
879 return true;
880}
881
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000882/// \brief Determine structural equivalence of two records.
883static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
884 RecordDecl *D1, RecordDecl *D2) {
885 if (D1->isUnion() != D2->isUnion()) {
886 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
887 << Context.C2.getTypeDeclType(D2);
888 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
889 << D1->getDeclName() << (unsigned)D1->getTagKind();
890 return false;
891 }
892
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000893 // If both declarations are class template specializations, we know
894 // the ODR applies, so check the template and template arguments.
895 ClassTemplateSpecializationDecl *Spec1
896 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
897 ClassTemplateSpecializationDecl *Spec2
898 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
899 if (Spec1 && Spec2) {
900 // Check that the specialized templates are the same.
901 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
902 Spec2->getSpecializedTemplate()))
903 return false;
904
905 // Check that the template arguments are the same.
906 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
907 return false;
908
909 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
910 if (!IsStructurallyEquivalent(Context,
911 Spec1->getTemplateArgs().get(I),
912 Spec2->getTemplateArgs().get(I)))
913 return false;
914 }
915 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000916 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000917 else if (Spec1 || Spec2)
918 return false;
919
Douglas Gregorea35d112010-02-15 23:54:17 +0000920 // Compare the definitions of these two records. If either or both are
921 // incomplete, we assume that they are equivalent.
922 D1 = D1->getDefinition();
923 D2 = D2->getDefinition();
924 if (!D1 || !D2)
925 return true;
926
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000927 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
928 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
929 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
930 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000931 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000932 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000933 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000934 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000935 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000936 return false;
937 }
938
939 // Check the base classes.
940 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
941 BaseEnd1 = D1CXX->bases_end(),
942 Base2 = D2CXX->bases_begin();
943 Base1 != BaseEnd1;
944 ++Base1, ++Base2) {
945 if (!IsStructurallyEquivalent(Context,
946 Base1->getType(), Base2->getType())) {
947 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
948 << Context.C2.getTypeDeclType(D2);
949 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
950 << Base2->getType()
951 << Base2->getSourceRange();
952 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
953 << Base1->getType()
954 << Base1->getSourceRange();
955 return false;
956 }
957
958 // Check virtual vs. non-virtual inheritance mismatch.
959 if (Base1->isVirtual() != Base2->isVirtual()) {
960 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
961 << Context.C2.getTypeDeclType(D2);
962 Context.Diag2(Base2->getSourceRange().getBegin(),
963 diag::note_odr_virtual_base)
964 << Base2->isVirtual() << Base2->getSourceRange();
965 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
966 << Base1->isVirtual()
967 << Base1->getSourceRange();
968 return false;
969 }
970 }
971 } else if (D1CXX->getNumBases() > 0) {
972 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
973 << Context.C2.getTypeDeclType(D2);
974 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
975 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
976 << Base1->getType()
977 << Base1->getSourceRange();
978 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
979 return false;
980 }
981 }
982
983 // Check the fields for consistency.
984 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
985 Field2End = D2->field_end();
986 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
987 Field1End = D1->field_end();
988 Field1 != Field1End;
989 ++Field1, ++Field2) {
990 if (Field2 == Field2End) {
991 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
992 << Context.C2.getTypeDeclType(D2);
993 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
994 << Field1->getDeclName() << Field1->getType();
995 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
996 return false;
997 }
998
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000999 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1000 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001001 }
1002
1003 if (Field2 != Field2End) {
1004 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1005 << Context.C2.getTypeDeclType(D2);
1006 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1007 << Field2->getDeclName() << Field2->getType();
1008 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1009 return false;
1010 }
1011
1012 return true;
1013}
1014
1015/// \brief Determine structural equivalence of two enums.
1016static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1017 EnumDecl *D1, EnumDecl *D2) {
1018 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1019 EC2End = D2->enumerator_end();
1020 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1021 EC1End = D1->enumerator_end();
1022 EC1 != EC1End; ++EC1, ++EC2) {
1023 if (EC2 == EC2End) {
1024 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1025 << Context.C2.getTypeDeclType(D2);
1026 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1027 << EC1->getDeclName()
1028 << EC1->getInitVal().toString(10);
1029 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1030 return false;
1031 }
1032
1033 llvm::APSInt Val1 = EC1->getInitVal();
1034 llvm::APSInt Val2 = EC2->getInitVal();
1035 if (!IsSameValue(Val1, Val2) ||
1036 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1037 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1038 << Context.C2.getTypeDeclType(D2);
1039 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1040 << EC2->getDeclName()
1041 << EC2->getInitVal().toString(10);
1042 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1043 << EC1->getDeclName()
1044 << EC1->getInitVal().toString(10);
1045 return false;
1046 }
1047 }
1048
1049 if (EC2 != EC2End) {
1050 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1051 << Context.C2.getTypeDeclType(D2);
1052 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1053 << EC2->getDeclName()
1054 << EC2->getInitVal().toString(10);
1055 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1056 return false;
1057 }
1058
1059 return true;
1060}
Douglas Gregor040afae2010-11-30 19:14:50 +00001061
1062static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1063 TemplateParameterList *Params1,
1064 TemplateParameterList *Params2) {
1065 if (Params1->size() != Params2->size()) {
1066 Context.Diag2(Params2->getTemplateLoc(),
1067 diag::err_odr_different_num_template_parameters)
1068 << Params1->size() << Params2->size();
1069 Context.Diag1(Params1->getTemplateLoc(),
1070 diag::note_odr_template_parameter_list);
1071 return false;
1072 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001073
Douglas Gregor040afae2010-11-30 19:14:50 +00001074 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1075 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1076 Context.Diag2(Params2->getParam(I)->getLocation(),
1077 diag::err_odr_different_template_parameter_kind);
1078 Context.Diag1(Params1->getParam(I)->getLocation(),
1079 diag::note_odr_template_parameter_here);
1080 return false;
1081 }
1082
1083 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1084 Params2->getParam(I))) {
1085
1086 return false;
1087 }
1088 }
1089
1090 return true;
1091}
1092
1093static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1094 TemplateTypeParmDecl *D1,
1095 TemplateTypeParmDecl *D2) {
1096 if (D1->isParameterPack() != D2->isParameterPack()) {
1097 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1098 << D2->isParameterPack();
1099 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1100 << D1->isParameterPack();
1101 return false;
1102 }
1103
1104 return true;
1105}
1106
1107static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1108 NonTypeTemplateParmDecl *D1,
1109 NonTypeTemplateParmDecl *D2) {
1110 // FIXME: Enable once we have variadic templates.
1111#if 0
1112 if (D1->isParameterPack() != D2->isParameterPack()) {
1113 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1114 << D2->isParameterPack();
1115 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1116 << D1->isParameterPack();
1117 return false;
1118 }
1119#endif
1120
1121 // Check types.
1122 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1123 Context.Diag2(D2->getLocation(),
1124 diag::err_odr_non_type_parameter_type_inconsistent)
1125 << D2->getType() << D1->getType();
1126 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1127 << D1->getType();
1128 return false;
1129 }
1130
1131 return true;
1132}
1133
1134static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1135 TemplateTemplateParmDecl *D1,
1136 TemplateTemplateParmDecl *D2) {
1137 // FIXME: Enable once we have variadic templates.
1138#if 0
1139 if (D1->isParameterPack() != D2->isParameterPack()) {
1140 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1141 << D2->isParameterPack();
1142 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1143 << D1->isParameterPack();
1144 return false;
1145 }
1146#endif
1147
1148 // Check template parameter lists.
1149 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1150 D2->getTemplateParameters());
1151}
1152
1153static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1154 ClassTemplateDecl *D1,
1155 ClassTemplateDecl *D2) {
1156 // Check template parameters.
1157 if (!IsStructurallyEquivalent(Context,
1158 D1->getTemplateParameters(),
1159 D2->getTemplateParameters()))
1160 return false;
1161
1162 // Check the templated declaration.
1163 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1164 D2->getTemplatedDecl());
1165}
1166
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001167/// \brief Determine structural equivalence of two declarations.
1168static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1169 Decl *D1, Decl *D2) {
1170 // FIXME: Check for known structural equivalences via a callback of some sort.
1171
Douglas Gregorea35d112010-02-15 23:54:17 +00001172 // Check whether we already know that these two declarations are not
1173 // structurally equivalent.
1174 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1175 D2->getCanonicalDecl())))
1176 return false;
1177
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001178 // Determine whether we've already produced a tentative equivalence for D1.
1179 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1180 if (EquivToD1)
1181 return EquivToD1 == D2->getCanonicalDecl();
1182
1183 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1184 EquivToD1 = D2->getCanonicalDecl();
1185 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1186 return true;
1187}
1188
1189bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1190 Decl *D2) {
1191 if (!::IsStructurallyEquivalent(*this, D1, D2))
1192 return false;
1193
1194 return !Finish();
1195}
1196
1197bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1198 QualType T2) {
1199 if (!::IsStructurallyEquivalent(*this, T1, T2))
1200 return false;
1201
1202 return !Finish();
1203}
1204
1205bool StructuralEquivalenceContext::Finish() {
1206 while (!DeclsToCheck.empty()) {
1207 // Check the next declaration.
1208 Decl *D1 = DeclsToCheck.front();
1209 DeclsToCheck.pop_front();
1210
1211 Decl *D2 = TentativeEquivalences[D1];
1212 assert(D2 && "Unrecorded tentative equivalence?");
1213
Douglas Gregorea35d112010-02-15 23:54:17 +00001214 bool Equivalent = true;
1215
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001216 // FIXME: Switch on all declaration kinds. For now, we're just going to
1217 // check the obvious ones.
1218 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1219 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1220 // Check for equivalent structure names.
1221 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001222 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1223 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001224 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001225 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1226 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001227 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1228 !::IsStructurallyEquivalent(*this, Record1, Record2))
1229 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001230 } else {
1231 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001232 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001233 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001234 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001235 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1236 // Check for equivalent enum names.
1237 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001238 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1239 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001240 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001241 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1242 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001243 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1244 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1245 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001246 } else {
1247 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001248 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001249 }
Richard Smith162e1c12011-04-15 14:24:37 +00001250 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1251 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001252 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001253 Typedef2->getIdentifier()) ||
1254 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001255 Typedef1->getUnderlyingType(),
1256 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001257 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001258 } else {
1259 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001260 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001261 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001262 } else if (ClassTemplateDecl *ClassTemplate1
1263 = dyn_cast<ClassTemplateDecl>(D1)) {
1264 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1265 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1266 ClassTemplate2->getIdentifier()) ||
1267 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1268 Equivalent = false;
1269 } else {
1270 // Class template/non-class-template mismatch.
1271 Equivalent = false;
1272 }
1273 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1274 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1275 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1276 Equivalent = false;
1277 } else {
1278 // Kind mismatch.
1279 Equivalent = false;
1280 }
1281 } else if (NonTypeTemplateParmDecl *NTTP1
1282 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1283 if (NonTypeTemplateParmDecl *NTTP2
1284 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1285 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1286 Equivalent = false;
1287 } else {
1288 // Kind mismatch.
1289 Equivalent = false;
1290 }
1291 } else if (TemplateTemplateParmDecl *TTP1
1292 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1293 if (TemplateTemplateParmDecl *TTP2
1294 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1295 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1296 Equivalent = false;
1297 } else {
1298 // Kind mismatch.
1299 Equivalent = false;
1300 }
1301 }
1302
Douglas Gregorea35d112010-02-15 23:54:17 +00001303 if (!Equivalent) {
1304 // Note that these two declarations are not equivalent (and we already
1305 // know about it).
1306 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1307 D2->getCanonicalDecl()));
1308 return true;
1309 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001310 // FIXME: Check other declaration kinds!
1311 }
1312
1313 return false;
1314}
1315
1316//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001317// Import Types
1318//----------------------------------------------------------------------------
1319
John McCallf4c73712011-01-19 06:33:43 +00001320QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001321 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1322 << T->getTypeClassName();
1323 return QualType();
1324}
1325
John McCallf4c73712011-01-19 06:33:43 +00001326QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001327 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001328#define SHARED_SINGLETON_TYPE(Expansion)
1329#define BUILTIN_TYPE(Id, SingletonId) \
1330 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1331#include "clang/AST/BuiltinTypes.def"
1332
1333 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1334 // context supports C++.
1335
1336 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1337 // context supports ObjC.
1338
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001339 case BuiltinType::Char_U:
1340 // The context we're importing from has an unsigned 'char'. If we're
1341 // importing into a context with a signed 'char', translate to
1342 // 'unsigned char' instead.
1343 if (Importer.getToContext().getLangOptions().CharIsSigned)
1344 return Importer.getToContext().UnsignedCharTy;
1345
1346 return Importer.getToContext().CharTy;
1347
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001348 case BuiltinType::Char_S:
1349 // The context we're importing from has an unsigned 'char'. If we're
1350 // importing into a context with a signed 'char', translate to
1351 // 'unsigned char' instead.
1352 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1353 return Importer.getToContext().SignedCharTy;
1354
1355 return Importer.getToContext().CharTy;
1356
Chris Lattner3f59c972010-12-25 23:25:43 +00001357 case BuiltinType::WChar_S:
1358 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001359 // FIXME: If not in C++, shall we translate to the C equivalent of
1360 // wchar_t?
1361 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001362 }
David Blaikie30263482012-01-20 21:50:17 +00001363
1364 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001365}
1366
John McCallf4c73712011-01-19 06:33:43 +00001367QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001368 QualType ToElementType = Importer.Import(T->getElementType());
1369 if (ToElementType.isNull())
1370 return QualType();
1371
1372 return Importer.getToContext().getComplexType(ToElementType);
1373}
1374
John McCallf4c73712011-01-19 06:33:43 +00001375QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001376 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1377 if (ToPointeeType.isNull())
1378 return QualType();
1379
1380 return Importer.getToContext().getPointerType(ToPointeeType);
1381}
1382
John McCallf4c73712011-01-19 06:33:43 +00001383QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001384 // FIXME: Check for blocks support in "to" context.
1385 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1386 if (ToPointeeType.isNull())
1387 return QualType();
1388
1389 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1390}
1391
John McCallf4c73712011-01-19 06:33:43 +00001392QualType
1393ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001394 // FIXME: Check for C++ support in "to" context.
1395 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1396 if (ToPointeeType.isNull())
1397 return QualType();
1398
1399 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1400}
1401
John McCallf4c73712011-01-19 06:33:43 +00001402QualType
1403ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001404 // FIXME: Check for C++0x support in "to" context.
1405 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1406 if (ToPointeeType.isNull())
1407 return QualType();
1408
1409 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1410}
1411
John McCallf4c73712011-01-19 06:33:43 +00001412QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001413 // FIXME: Check for C++ support in "to" context.
1414 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1415 if (ToPointeeType.isNull())
1416 return QualType();
1417
1418 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1419 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1420 ClassType.getTypePtr());
1421}
1422
John McCallf4c73712011-01-19 06:33:43 +00001423QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001424 QualType ToElementType = Importer.Import(T->getElementType());
1425 if (ToElementType.isNull())
1426 return QualType();
1427
1428 return Importer.getToContext().getConstantArrayType(ToElementType,
1429 T->getSize(),
1430 T->getSizeModifier(),
1431 T->getIndexTypeCVRQualifiers());
1432}
1433
John McCallf4c73712011-01-19 06:33:43 +00001434QualType
1435ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001436 QualType ToElementType = Importer.Import(T->getElementType());
1437 if (ToElementType.isNull())
1438 return QualType();
1439
1440 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1441 T->getSizeModifier(),
1442 T->getIndexTypeCVRQualifiers());
1443}
1444
John McCallf4c73712011-01-19 06:33:43 +00001445QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001446 QualType ToElementType = Importer.Import(T->getElementType());
1447 if (ToElementType.isNull())
1448 return QualType();
1449
1450 Expr *Size = Importer.Import(T->getSizeExpr());
1451 if (!Size)
1452 return QualType();
1453
1454 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1455 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1456 T->getSizeModifier(),
1457 T->getIndexTypeCVRQualifiers(),
1458 Brackets);
1459}
1460
John McCallf4c73712011-01-19 06:33:43 +00001461QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001462 QualType ToElementType = Importer.Import(T->getElementType());
1463 if (ToElementType.isNull())
1464 return QualType();
1465
1466 return Importer.getToContext().getVectorType(ToElementType,
1467 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001468 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001469}
1470
John McCallf4c73712011-01-19 06:33:43 +00001471QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *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().getExtVectorType(ToElementType,
1477 T->getNumElements());
1478}
1479
John McCallf4c73712011-01-19 06:33:43 +00001480QualType
1481ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001482 // FIXME: What happens if we're importing a function without a prototype
1483 // into C++? Should we make it variadic?
1484 QualType ToResultType = Importer.Import(T->getResultType());
1485 if (ToResultType.isNull())
1486 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001487
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001488 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001489 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490}
1491
John McCallf4c73712011-01-19 06:33:43 +00001492QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001493 QualType ToResultType = Importer.Import(T->getResultType());
1494 if (ToResultType.isNull())
1495 return QualType();
1496
1497 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001498 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001499 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1500 AEnd = T->arg_type_end();
1501 A != AEnd; ++A) {
1502 QualType ArgType = Importer.Import(*A);
1503 if (ArgType.isNull())
1504 return QualType();
1505 ArgTypes.push_back(ArgType);
1506 }
1507
1508 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001509 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001510 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1511 EEnd = T->exception_end();
1512 E != EEnd; ++E) {
1513 QualType ExceptionType = Importer.Import(*E);
1514 if (ExceptionType.isNull())
1515 return QualType();
1516 ExceptionTypes.push_back(ExceptionType);
1517 }
John McCalle23cf432010-12-14 08:05:40 +00001518
1519 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1520 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001521
1522 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001523 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001524}
1525
Sean Callanan0aeb2892011-08-11 16:56:07 +00001526QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1527 QualType ToInnerType = Importer.Import(T->getInnerType());
1528 if (ToInnerType.isNull())
1529 return QualType();
1530
1531 return Importer.getToContext().getParenType(ToInnerType);
1532}
1533
John McCallf4c73712011-01-19 06:33:43 +00001534QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001535 TypedefNameDecl *ToDecl
1536 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001537 if (!ToDecl)
1538 return QualType();
1539
1540 return Importer.getToContext().getTypeDeclType(ToDecl);
1541}
1542
John McCallf4c73712011-01-19 06:33:43 +00001543QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001544 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1545 if (!ToExpr)
1546 return QualType();
1547
1548 return Importer.getToContext().getTypeOfExprType(ToExpr);
1549}
1550
John McCallf4c73712011-01-19 06:33:43 +00001551QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001552 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1553 if (ToUnderlyingType.isNull())
1554 return QualType();
1555
1556 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1557}
1558
John McCallf4c73712011-01-19 06:33:43 +00001559QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001560 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001561 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1562 if (!ToExpr)
1563 return QualType();
1564
1565 return Importer.getToContext().getDecltypeType(ToExpr);
1566}
1567
Sean Huntca63c202011-05-24 22:41:36 +00001568QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1569 QualType ToBaseType = Importer.Import(T->getBaseType());
1570 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1571 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1572 return QualType();
1573
1574 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1575 ToUnderlyingType,
1576 T->getUTTKind());
1577}
1578
Richard Smith34b41d92011-02-20 03:19:35 +00001579QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1580 // FIXME: Make sure that the "to" context supports C++0x!
1581 QualType FromDeduced = T->getDeducedType();
1582 QualType ToDeduced;
1583 if (!FromDeduced.isNull()) {
1584 ToDeduced = Importer.Import(FromDeduced);
1585 if (ToDeduced.isNull())
1586 return QualType();
1587 }
1588
1589 return Importer.getToContext().getAutoType(ToDeduced);
1590}
1591
John McCallf4c73712011-01-19 06:33:43 +00001592QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001593 RecordDecl *ToDecl
1594 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1595 if (!ToDecl)
1596 return QualType();
1597
1598 return Importer.getToContext().getTagDeclType(ToDecl);
1599}
1600
John McCallf4c73712011-01-19 06:33:43 +00001601QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001602 EnumDecl *ToDecl
1603 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1604 if (!ToDecl)
1605 return QualType();
1606
1607 return Importer.getToContext().getTagDeclType(ToDecl);
1608}
1609
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001610QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001611 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001612 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1613 if (ToTemplate.isNull())
1614 return QualType();
1615
Chris Lattner5f9e2722011-07-23 10:55:15 +00001616 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001617 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1618 return QualType();
1619
1620 QualType ToCanonType;
1621 if (!QualType(T, 0).isCanonical()) {
1622 QualType FromCanonType
1623 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1624 ToCanonType =Importer.Import(FromCanonType);
1625 if (ToCanonType.isNull())
1626 return QualType();
1627 }
1628 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1629 ToTemplateArgs.data(),
1630 ToTemplateArgs.size(),
1631 ToCanonType);
1632}
1633
John McCallf4c73712011-01-19 06:33:43 +00001634QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001635 NestedNameSpecifier *ToQualifier = 0;
1636 // Note: the qualifier in an ElaboratedType is optional.
1637 if (T->getQualifier()) {
1638 ToQualifier = Importer.Import(T->getQualifier());
1639 if (!ToQualifier)
1640 return QualType();
1641 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001642
1643 QualType ToNamedType = Importer.Import(T->getNamedType());
1644 if (ToNamedType.isNull())
1645 return QualType();
1646
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001647 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1648 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001649}
1650
John McCallf4c73712011-01-19 06:33:43 +00001651QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001652 ObjCInterfaceDecl *Class
1653 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1654 if (!Class)
1655 return QualType();
1656
John McCallc12c5bb2010-05-15 11:32:37 +00001657 return Importer.getToContext().getObjCInterfaceType(Class);
1658}
1659
John McCallf4c73712011-01-19 06:33:43 +00001660QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001661 QualType ToBaseType = Importer.Import(T->getBaseType());
1662 if (ToBaseType.isNull())
1663 return QualType();
1664
Chris Lattner5f9e2722011-07-23 10:55:15 +00001665 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001666 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001667 PEnd = T->qual_end();
1668 P != PEnd; ++P) {
1669 ObjCProtocolDecl *Protocol
1670 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1671 if (!Protocol)
1672 return QualType();
1673 Protocols.push_back(Protocol);
1674 }
1675
John McCallc12c5bb2010-05-15 11:32:37 +00001676 return Importer.getToContext().getObjCObjectType(ToBaseType,
1677 Protocols.data(),
1678 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001679}
1680
John McCallf4c73712011-01-19 06:33:43 +00001681QualType
1682ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001683 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1684 if (ToPointeeType.isNull())
1685 return QualType();
1686
John McCallc12c5bb2010-05-15 11:32:37 +00001687 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001688}
1689
Douglas Gregor089459a2010-02-08 21:09:39 +00001690//----------------------------------------------------------------------------
1691// Import Declarations
1692//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001693bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1694 DeclContext *&LexicalDC,
1695 DeclarationName &Name,
1696 SourceLocation &Loc) {
1697 // Import the context of this declaration.
1698 DC = Importer.ImportContext(D->getDeclContext());
1699 if (!DC)
1700 return true;
1701
1702 LexicalDC = DC;
1703 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1704 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1705 if (!LexicalDC)
1706 return true;
1707 }
1708
1709 // Import the name of this declaration.
1710 Name = Importer.Import(D->getDeclName());
1711 if (D->getDeclName() && !Name)
1712 return true;
1713
1714 // Import the location of this declaration.
1715 Loc = Importer.Import(D->getLocation());
1716 return false;
1717}
1718
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001719void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1720 if (!FromD)
1721 return;
1722
1723 if (!ToD) {
1724 ToD = Importer.Import(FromD);
1725 if (!ToD)
1726 return;
1727 }
1728
1729 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1730 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1731 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1732 ImportDefinition(FromRecord, ToRecord);
1733 }
1734 }
1735 return;
1736 }
1737
1738 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1739 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1740 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1741 ImportDefinition(FromEnum, ToEnum);
1742 }
1743 }
1744 return;
1745 }
1746}
1747
Abramo Bagnara25777432010-08-11 22:01:17 +00001748void
1749ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1750 DeclarationNameInfo& To) {
1751 // NOTE: To.Name and To.Loc are already imported.
1752 // We only have to import To.LocInfo.
1753 switch (To.getName().getNameKind()) {
1754 case DeclarationName::Identifier:
1755 case DeclarationName::ObjCZeroArgSelector:
1756 case DeclarationName::ObjCOneArgSelector:
1757 case DeclarationName::ObjCMultiArgSelector:
1758 case DeclarationName::CXXUsingDirective:
1759 return;
1760
1761 case DeclarationName::CXXOperatorName: {
1762 SourceRange Range = From.getCXXOperatorNameRange();
1763 To.setCXXOperatorNameRange(Importer.Import(Range));
1764 return;
1765 }
1766 case DeclarationName::CXXLiteralOperatorName: {
1767 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1768 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1769 return;
1770 }
1771 case DeclarationName::CXXConstructorName:
1772 case DeclarationName::CXXDestructorName:
1773 case DeclarationName::CXXConversionFunctionName: {
1774 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1775 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1776 return;
1777 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001778 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001779 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001780}
1781
Douglas Gregord8868a62011-01-18 03:11:38 +00001782void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1783 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001784 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001785 return;
1786 }
1787
Douglas Gregor083a8212010-02-21 18:24:45 +00001788 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1789 FromEnd = FromDC->decls_end();
1790 From != FromEnd;
1791 ++From)
1792 Importer.Import(*From);
1793}
1794
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001795bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1796 bool ForceImport) {
1797 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001798 return false;
1799
1800 To->startDefinition();
1801
1802 // Add base classes.
1803 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1804 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001805
1806 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1807 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1808 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1809 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1810 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1811 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1812 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1813 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1814 ToData.Aggregate = FromData.Aggregate;
1815 ToData.PlainOldData = FromData.PlainOldData;
1816 ToData.Empty = FromData.Empty;
1817 ToData.Polymorphic = FromData.Polymorphic;
1818 ToData.Abstract = FromData.Abstract;
1819 ToData.IsStandardLayout = FromData.IsStandardLayout;
1820 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1821 ToData.HasPrivateFields = FromData.HasPrivateFields;
1822 ToData.HasProtectedFields = FromData.HasProtectedFields;
1823 ToData.HasPublicFields = FromData.HasPublicFields;
1824 ToData.HasMutableFields = FromData.HasMutableFields;
1825 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1826 ToData.HasConstexprNonCopyMoveConstructor
1827 = FromData.HasConstexprNonCopyMoveConstructor;
1828 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1829 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1830 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1831 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1832 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1833 ToData.HasNonLiteralTypeFieldsOrBases
1834 = FromData.HasNonLiteralTypeFieldsOrBases;
1835 ToData.UserProvidedDefaultConstructor
1836 = FromData.UserProvidedDefaultConstructor;
1837 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1838 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1839 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1840 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1841 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1842 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1843 ToData.FailedImplicitMoveConstructor
1844 = FromData.FailedImplicitMoveConstructor;
1845 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001846
Chris Lattner5f9e2722011-07-23 10:55:15 +00001847 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001848 for (CXXRecordDecl::base_class_iterator
1849 Base1 = FromCXX->bases_begin(),
1850 FromBaseEnd = FromCXX->bases_end();
1851 Base1 != FromBaseEnd;
1852 ++Base1) {
1853 QualType T = Importer.Import(Base1->getType());
1854 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001855 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001856
1857 SourceLocation EllipsisLoc;
1858 if (Base1->isPackExpansion())
1859 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001860
1861 // Ensure that we have a definition for the base.
1862 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1863
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001864 Bases.push_back(
1865 new (Importer.getToContext())
1866 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1867 Base1->isVirtual(),
1868 Base1->isBaseOfClass(),
1869 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001870 Importer.Import(Base1->getTypeSourceInfo()),
1871 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001872 }
1873 if (!Bases.empty())
1874 ToCXX->setBases(Bases.data(), Bases.size());
1875 }
1876
Sean Callanan673e7752011-07-19 22:38:25 +00001877 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001878 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001879 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001880}
1881
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001882bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1883 bool ForceImport) {
1884 if (To->getDefinition() || To->isBeingDefined())
1885 return false;
1886
1887 To->startDefinition();
1888
1889 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1890 if (T.isNull())
1891 return true;
1892
1893 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1894 if (ToPromotionType.isNull())
1895 return true;
1896
1897 ImportDeclContext(From, ForceImport);
1898
1899 // FIXME: we might need to merge the number of positive or negative bits
1900 // if the enumerator lists don't match.
1901 To->completeDefinition(T, ToPromotionType,
1902 From->getNumPositiveBits(),
1903 From->getNumNegativeBits());
1904 return false;
1905}
1906
Douglas Gregor040afae2010-11-30 19:14:50 +00001907TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1908 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001909 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001910 ToParams.reserve(Params->size());
1911 for (TemplateParameterList::iterator P = Params->begin(),
1912 PEnd = Params->end();
1913 P != PEnd; ++P) {
1914 Decl *To = Importer.Import(*P);
1915 if (!To)
1916 return 0;
1917
1918 ToParams.push_back(cast<NamedDecl>(To));
1919 }
1920
1921 return TemplateParameterList::Create(Importer.getToContext(),
1922 Importer.Import(Params->getTemplateLoc()),
1923 Importer.Import(Params->getLAngleLoc()),
1924 ToParams.data(), ToParams.size(),
1925 Importer.Import(Params->getRAngleLoc()));
1926}
1927
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001928TemplateArgument
1929ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1930 switch (From.getKind()) {
1931 case TemplateArgument::Null:
1932 return TemplateArgument();
1933
1934 case TemplateArgument::Type: {
1935 QualType ToType = Importer.Import(From.getAsType());
1936 if (ToType.isNull())
1937 return TemplateArgument();
1938 return TemplateArgument(ToType);
1939 }
1940
1941 case TemplateArgument::Integral: {
1942 QualType ToType = Importer.Import(From.getIntegralType());
1943 if (ToType.isNull())
1944 return TemplateArgument();
1945 return TemplateArgument(*From.getAsIntegral(), ToType);
1946 }
1947
1948 case TemplateArgument::Declaration:
1949 if (Decl *To = Importer.Import(From.getAsDecl()))
1950 return TemplateArgument(To);
1951 return TemplateArgument();
1952
1953 case TemplateArgument::Template: {
1954 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1955 if (ToTemplate.isNull())
1956 return TemplateArgument();
1957
1958 return TemplateArgument(ToTemplate);
1959 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001960
1961 case TemplateArgument::TemplateExpansion: {
1962 TemplateName ToTemplate
1963 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1964 if (ToTemplate.isNull())
1965 return TemplateArgument();
1966
Douglas Gregor2be29f42011-01-14 23:41:42 +00001967 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001968 }
1969
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001970 case TemplateArgument::Expression:
1971 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1972 return TemplateArgument(ToExpr);
1973 return TemplateArgument();
1974
1975 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001976 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001977 ToPack.reserve(From.pack_size());
1978 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1979 return TemplateArgument();
1980
1981 TemplateArgument *ToArgs
1982 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1983 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1984 return TemplateArgument(ToArgs, ToPack.size());
1985 }
1986 }
1987
1988 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001989}
1990
1991bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1992 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001993 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001994 for (unsigned I = 0; I != NumFromArgs; ++I) {
1995 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1996 if (To.isNull() && !FromArgs[I].isNull())
1997 return true;
1998
1999 ToArgs.push_back(To);
2000 }
2001
2002 return false;
2003}
2004
Douglas Gregor96a01b42010-02-11 00:48:18 +00002005bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002007 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002008 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002009 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002010 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002011}
2012
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002013bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002014 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002015 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002016 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002017 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002018}
2019
Douglas Gregor040afae2010-11-30 19:14:50 +00002020bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2021 ClassTemplateDecl *To) {
2022 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2023 Importer.getToContext(),
2024 Importer.getNonEquivalentDecls());
2025 return Ctx.IsStructurallyEquivalent(From, To);
2026}
2027
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002028Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002029 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002030 << D->getDeclKindName();
2031 return 0;
2032}
2033
Sean Callananf1b69462011-11-17 23:20:56 +00002034Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2035 TranslationUnitDecl *ToD =
2036 Importer.getToContext().getTranslationUnitDecl();
2037
2038 Importer.Imported(D, ToD);
2039
2040 return ToD;
2041}
2042
Douglas Gregor788c62d2010-02-21 18:26:36 +00002043Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2044 // Import the major distinguishing characteristics of this namespace.
2045 DeclContext *DC, *LexicalDC;
2046 DeclarationName Name;
2047 SourceLocation Loc;
2048 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2049 return 0;
2050
2051 NamespaceDecl *MergeWithNamespace = 0;
2052 if (!Name) {
2053 // This is an anonymous namespace. Adopt an existing anonymous
2054 // namespace if we can.
2055 // FIXME: Not testable.
2056 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2057 MergeWithNamespace = TU->getAnonymousNamespace();
2058 else
2059 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2060 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002061 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002062 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2063 DC->localUncachedLookup(Name, FoundDecls);
2064 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2065 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002066 continue;
2067
Douglas Gregorb75a3452011-10-15 00:10:27 +00002068 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002069 MergeWithNamespace = FoundNS;
2070 ConflictingDecls.clear();
2071 break;
2072 }
2073
Douglas Gregorb75a3452011-10-15 00:10:27 +00002074 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002075 }
2076
2077 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002078 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002079 ConflictingDecls.data(),
2080 ConflictingDecls.size());
2081 }
2082 }
2083
2084 // Create the "to" namespace, if needed.
2085 NamespaceDecl *ToNamespace = MergeWithNamespace;
2086 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002087 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002088 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002089 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002090 Loc, Name.getAsIdentifierInfo(),
2091 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002092 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002093 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002094
2095 // If this is an anonymous namespace, register it as the anonymous
2096 // namespace within its context.
2097 if (!Name) {
2098 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2099 TU->setAnonymousNamespace(ToNamespace);
2100 else
2101 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2102 }
2103 }
2104 Importer.Imported(D, ToNamespace);
2105
2106 ImportDeclContext(D);
2107
2108 return ToNamespace;
2109}
2110
Richard Smith162e1c12011-04-15 14:24:37 +00002111Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002112 // Import the major distinguishing characteristics of this typedef.
2113 DeclContext *DC, *LexicalDC;
2114 DeclarationName Name;
2115 SourceLocation Loc;
2116 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2117 return 0;
2118
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002119 // If this typedef is not in block scope, determine whether we've
2120 // seen a typedef with the same name (that we can merge with) or any
2121 // other entity by that name (which name lookup could conflict with).
2122 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002123 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002124 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002125 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2126 DC->localUncachedLookup(Name, FoundDecls);
2127 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2128 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002129 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002130 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002131 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002132 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2133 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002134 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002135 }
2136
Douglas Gregorb75a3452011-10-15 00:10:27 +00002137 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002138 }
2139
2140 if (!ConflictingDecls.empty()) {
2141 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2142 ConflictingDecls.data(),
2143 ConflictingDecls.size());
2144 if (!Name)
2145 return 0;
2146 }
2147 }
2148
Douglas Gregorea35d112010-02-15 23:54:17 +00002149 // Import the underlying type of this typedef;
2150 QualType T = Importer.Import(D->getUnderlyingType());
2151 if (T.isNull())
2152 return 0;
2153
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002154 // Create the new typedef node.
2155 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002156 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002157 TypedefNameDecl *ToTypedef;
2158 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002159 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2160 StartL, Loc,
2161 Name.getAsIdentifierInfo(),
2162 TInfo);
2163 else
Richard Smith162e1c12011-04-15 14:24:37 +00002164 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2165 StartL, Loc,
2166 Name.getAsIdentifierInfo(),
2167 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002168
Douglas Gregor325bf172010-02-22 17:42:47 +00002169 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002170 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002171 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002172 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002173
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002174 return ToTypedef;
2175}
2176
Richard Smith162e1c12011-04-15 14:24:37 +00002177Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2178 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2179}
2180
2181Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2182 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2183}
2184
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002185Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2186 // Import the major distinguishing characteristics of this enum.
2187 DeclContext *DC, *LexicalDC;
2188 DeclarationName Name;
2189 SourceLocation Loc;
2190 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2191 return 0;
2192
2193 // Figure out what enum name we're looking for.
2194 unsigned IDNS = Decl::IDNS_Tag;
2195 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002196 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2197 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002198 IDNS = Decl::IDNS_Ordinary;
2199 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2200 IDNS |= Decl::IDNS_Ordinary;
2201
2202 // We may already have an enum of the same name; try to find and match it.
2203 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002204 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002205 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2206 DC->localUncachedLookup(SearchName, FoundDecls);
2207 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2208 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002209 continue;
2210
Douglas Gregorb75a3452011-10-15 00:10:27 +00002211 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002212 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002213 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2214 Found = Tag->getDecl();
2215 }
2216
2217 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002218 if (IsStructuralMatch(D, FoundEnum))
2219 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002220 }
2221
Douglas Gregorb75a3452011-10-15 00:10:27 +00002222 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002223 }
2224
2225 if (!ConflictingDecls.empty()) {
2226 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2227 ConflictingDecls.data(),
2228 ConflictingDecls.size());
2229 }
2230 }
2231
2232 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002233 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2234 Importer.Import(D->getLocStart()),
2235 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002236 D->isScoped(), D->isScopedUsingClassTag(),
2237 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002238 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002239 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002240 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002241 D2->setLexicalDeclContext(LexicalDC);
2242 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002243 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002244
2245 // Import the integer type.
2246 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2247 if (ToIntegerType.isNull())
2248 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002249 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002250
2251 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002252 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002253 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002254
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002255 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002256}
2257
Douglas Gregor96a01b42010-02-11 00:48:18 +00002258Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2259 // If this record has a definition in the translation unit we're coming from,
2260 // but this particular declaration is not that definition, import the
2261 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002262 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002263 if (Definition && Definition != D) {
2264 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002265 if (!ImportedDef)
2266 return 0;
2267
2268 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002269 }
2270
2271 // Import the major distinguishing characteristics of this record.
2272 DeclContext *DC, *LexicalDC;
2273 DeclarationName Name;
2274 SourceLocation Loc;
2275 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2276 return 0;
2277
2278 // Figure out what structure name we're looking for.
2279 unsigned IDNS = Decl::IDNS_Tag;
2280 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002281 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2282 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002283 IDNS = Decl::IDNS_Ordinary;
2284 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2285 IDNS |= Decl::IDNS_Ordinary;
2286
2287 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002288 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002289 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002290 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002291 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2292 DC->localUncachedLookup(SearchName, FoundDecls);
2293 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2294 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002295 continue;
2296
Douglas Gregorb75a3452011-10-15 00:10:27 +00002297 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002298 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002299 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2300 Found = Tag->getDecl();
2301 }
2302
2303 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002304 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002305 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002306 // The record types structurally match, or the "from" translation
2307 // unit only had a forward declaration anyway; call it the same
2308 // function.
2309 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002310 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002311 }
2312 } else {
2313 // We have a forward declaration of this type, so adopt that forward
2314 // declaration rather than building a new one.
2315 AdoptDecl = FoundRecord;
2316 continue;
2317 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002318 }
2319
Douglas Gregorb75a3452011-10-15 00:10:27 +00002320 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002321 }
2322
2323 if (!ConflictingDecls.empty()) {
2324 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2325 ConflictingDecls.data(),
2326 ConflictingDecls.size());
2327 }
2328 }
2329
2330 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002332 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002333 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002334 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002335 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002336 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002337 DC, StartLoc, Loc,
2338 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002339 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002340 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002341 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002342 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002343 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002344 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002345
2346 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002347 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002348 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002349 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002350
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002351 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002352
John McCall5e1cdac2011-10-07 06:10:15 +00002353 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002354 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002355
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002356 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002357}
2358
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002359Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2360 // Import the major distinguishing characteristics of this enumerator.
2361 DeclContext *DC, *LexicalDC;
2362 DeclarationName Name;
2363 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002364 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002365 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002366
2367 QualType T = Importer.Import(D->getType());
2368 if (T.isNull())
2369 return 0;
2370
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002371 // Determine whether there are any other declarations with the same name and
2372 // in the same context.
2373 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002374 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002375 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002376 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2377 DC->localUncachedLookup(Name, FoundDecls);
2378 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2379 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002380 continue;
2381
Douglas Gregorb75a3452011-10-15 00:10:27 +00002382 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002383 }
2384
2385 if (!ConflictingDecls.empty()) {
2386 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2387 ConflictingDecls.data(),
2388 ConflictingDecls.size());
2389 if (!Name)
2390 return 0;
2391 }
2392 }
2393
2394 Expr *Init = Importer.Import(D->getInitExpr());
2395 if (D->getInitExpr() && !Init)
2396 return 0;
2397
2398 EnumConstantDecl *ToEnumerator
2399 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2400 Name.getAsIdentifierInfo(), T,
2401 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002402 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002403 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002404 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002405 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002406 return ToEnumerator;
2407}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002408
Douglas Gregora404ea62010-02-10 19:54:31 +00002409Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2410 // Import the major distinguishing characteristics of this function.
2411 DeclContext *DC, *LexicalDC;
2412 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002413 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002414 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002415 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002416
Douglas Gregora404ea62010-02-10 19:54:31 +00002417 // Try to find a function in our own ("to") context with the same name, same
2418 // type, and in the same context as the function we're importing.
2419 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002420 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002421 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002422 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2423 DC->localUncachedLookup(Name, FoundDecls);
2424 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2425 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002426 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002427
Douglas Gregorb75a3452011-10-15 00:10:27 +00002428 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002429 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2430 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002431 if (Importer.IsStructurallyEquivalent(D->getType(),
2432 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002433 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002434 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002435 }
2436
2437 // FIXME: Check for overloading more carefully, e.g., by boosting
2438 // Sema::IsOverload out to the AST library.
2439
2440 // Function overloading is okay in C++.
2441 if (Importer.getToContext().getLangOptions().CPlusPlus)
2442 continue;
2443
2444 // Complain about inconsistent function types.
2445 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002446 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002447 Importer.ToDiag(FoundFunction->getLocation(),
2448 diag::note_odr_value_here)
2449 << FoundFunction->getType();
2450 }
2451 }
2452
Douglas Gregorb75a3452011-10-15 00:10:27 +00002453 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002454 }
2455
2456 if (!ConflictingDecls.empty()) {
2457 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2458 ConflictingDecls.data(),
2459 ConflictingDecls.size());
2460 if (!Name)
2461 return 0;
2462 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002463 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002464
Abramo Bagnara25777432010-08-11 22:01:17 +00002465 DeclarationNameInfo NameInfo(Name, Loc);
2466 // Import additional name location/type info.
2467 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2468
Douglas Gregorea35d112010-02-15 23:54:17 +00002469 // Import the type.
2470 QualType T = Importer.Import(D->getType());
2471 if (T.isNull())
2472 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002473
2474 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002475 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002476 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2477 P != PEnd; ++P) {
2478 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2479 if (!ToP)
2480 return 0;
2481
2482 Parameters.push_back(ToP);
2483 }
2484
2485 // Create the imported function.
2486 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002487 FunctionDecl *ToFunction = 0;
2488 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2489 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2490 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002491 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002492 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002493 FromConstructor->isExplicit(),
2494 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002495 D->isImplicit(),
2496 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002497 } else if (isa<CXXDestructorDecl>(D)) {
2498 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2499 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002500 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002501 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002502 D->isInlineSpecified(),
2503 D->isImplicit());
2504 } else if (CXXConversionDecl *FromConversion
2505 = dyn_cast<CXXConversionDecl>(D)) {
2506 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2507 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002508 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002509 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002510 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002511 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002512 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002513 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002514 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2515 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2516 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002517 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002518 NameInfo, T, TInfo,
2519 Method->isStatic(),
2520 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002521 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002522 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002523 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002524 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002525 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002526 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002527 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002528 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002529 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002530 D->hasWrittenPrototype(),
2531 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002532 }
John McCallb6217662010-03-15 10:12:16 +00002533
2534 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002535 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002536 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002537 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002538 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2539 ToFunction->setTrivial(D->isTrivial());
2540 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002541 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002542
Douglas Gregora404ea62010-02-10 19:54:31 +00002543 // Set the parameters.
2544 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002545 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002546 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002547 }
David Blaikie4278c652011-09-21 18:16:56 +00002548 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002549
2550 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002551
2552 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002553 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002554
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002555 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002556}
2557
Douglas Gregorc144f352010-02-21 18:29:16 +00002558Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2559 return VisitFunctionDecl(D);
2560}
2561
2562Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2563 return VisitCXXMethodDecl(D);
2564}
2565
2566Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2567 return VisitCXXMethodDecl(D);
2568}
2569
2570Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2571 return VisitCXXMethodDecl(D);
2572}
2573
Douglas Gregor96a01b42010-02-11 00:48:18 +00002574Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2575 // Import the major distinguishing characteristics of a variable.
2576 DeclContext *DC, *LexicalDC;
2577 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002578 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002579 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2580 return 0;
2581
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002582 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002583 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2584 DC->localUncachedLookup(Name, FoundDecls);
2585 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2586 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002587 if (Importer.IsStructurallyEquivalent(D->getType(),
2588 FoundField->getType())) {
2589 Importer.Imported(D, FoundField);
2590 return FoundField;
2591 }
2592
2593 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2594 << Name << D->getType() << FoundField->getType();
2595 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2596 << FoundField->getType();
2597 return 0;
2598 }
2599 }
2600
Douglas Gregorea35d112010-02-15 23:54:17 +00002601 // Import the type.
2602 QualType T = Importer.Import(D->getType());
2603 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002604 return 0;
2605
2606 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2607 Expr *BitWidth = Importer.Import(D->getBitWidth());
2608 if (!BitWidth && D->getBitWidth())
2609 return 0;
2610
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002611 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2612 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002613 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002614 T, TInfo, BitWidth, D->isMutable(),
2615 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002616 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002617 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002618 if (ToField->hasInClassInitializer())
2619 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002620 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002621 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002622 return ToField;
2623}
2624
Francois Pichet87c2e122010-11-21 06:08:52 +00002625Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2626 // Import the major distinguishing characteristics of a variable.
2627 DeclContext *DC, *LexicalDC;
2628 DeclarationName Name;
2629 SourceLocation Loc;
2630 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2631 return 0;
2632
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002633 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002634 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2635 DC->localUncachedLookup(Name, FoundDecls);
2636 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002637 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002638 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002639 if (Importer.IsStructurallyEquivalent(D->getType(),
2640 FoundField->getType())) {
2641 Importer.Imported(D, FoundField);
2642 return FoundField;
2643 }
2644
2645 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2646 << Name << D->getType() << FoundField->getType();
2647 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2648 << FoundField->getType();
2649 return 0;
2650 }
2651 }
2652
Francois Pichet87c2e122010-11-21 06:08:52 +00002653 // Import the type.
2654 QualType T = Importer.Import(D->getType());
2655 if (T.isNull())
2656 return 0;
2657
2658 NamedDecl **NamedChain =
2659 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2660
2661 unsigned i = 0;
2662 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2663 PE = D->chain_end(); PI != PE; ++PI) {
2664 Decl* D = Importer.Import(*PI);
2665 if (!D)
2666 return 0;
2667 NamedChain[i++] = cast<NamedDecl>(D);
2668 }
2669
2670 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2671 Importer.getToContext(), DC,
2672 Loc, Name.getAsIdentifierInfo(), T,
2673 NamedChain, D->getChainingSize());
2674 ToIndirectField->setAccess(D->getAccess());
2675 ToIndirectField->setLexicalDeclContext(LexicalDC);
2676 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002677 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002678 return ToIndirectField;
2679}
2680
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002681Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2682 // Import the major distinguishing characteristics of an ivar.
2683 DeclContext *DC, *LexicalDC;
2684 DeclarationName Name;
2685 SourceLocation Loc;
2686 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2687 return 0;
2688
2689 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002690 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2691 DC->localUncachedLookup(Name, FoundDecls);
2692 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2693 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002694 if (Importer.IsStructurallyEquivalent(D->getType(),
2695 FoundIvar->getType())) {
2696 Importer.Imported(D, FoundIvar);
2697 return FoundIvar;
2698 }
2699
2700 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2701 << Name << D->getType() << FoundIvar->getType();
2702 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2703 << FoundIvar->getType();
2704 return 0;
2705 }
2706 }
2707
2708 // Import the type.
2709 QualType T = Importer.Import(D->getType());
2710 if (T.isNull())
2711 return 0;
2712
2713 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2714 Expr *BitWidth = Importer.Import(D->getBitWidth());
2715 if (!BitWidth && D->getBitWidth())
2716 return 0;
2717
Daniel Dunbara0654922010-04-02 20:10:03 +00002718 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2719 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002720 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002721 Loc, Name.getAsIdentifierInfo(),
2722 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002723 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002724 ToIvar->setLexicalDeclContext(LexicalDC);
2725 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002726 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002727 return ToIvar;
2728
2729}
2730
Douglas Gregora404ea62010-02-10 19:54:31 +00002731Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2732 // Import the major distinguishing characteristics of a variable.
2733 DeclContext *DC, *LexicalDC;
2734 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002735 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002736 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002737 return 0;
2738
Douglas Gregor089459a2010-02-08 21:09:39 +00002739 // Try to find a variable in our own ("to") context with the same name and
2740 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002741 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002742 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002743 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002744 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002745 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2746 DC->localUncachedLookup(Name, FoundDecls);
2747 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2748 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002749 continue;
2750
Douglas Gregorb75a3452011-10-15 00:10:27 +00002751 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002752 // We have found a variable that we may need to merge with. Check it.
2753 if (isExternalLinkage(FoundVar->getLinkage()) &&
2754 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002755 if (Importer.IsStructurallyEquivalent(D->getType(),
2756 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002757 MergeWithVar = FoundVar;
2758 break;
2759 }
2760
Douglas Gregord0145422010-02-12 17:23:39 +00002761 const ArrayType *FoundArray
2762 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2763 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002764 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002765 if (FoundArray && TArray) {
2766 if (isa<IncompleteArrayType>(FoundArray) &&
2767 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002768 // Import the type.
2769 QualType T = Importer.Import(D->getType());
2770 if (T.isNull())
2771 return 0;
2772
Douglas Gregord0145422010-02-12 17:23:39 +00002773 FoundVar->setType(T);
2774 MergeWithVar = FoundVar;
2775 break;
2776 } else if (isa<IncompleteArrayType>(TArray) &&
2777 isa<ConstantArrayType>(FoundArray)) {
2778 MergeWithVar = FoundVar;
2779 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002780 }
2781 }
2782
Douglas Gregor089459a2010-02-08 21:09:39 +00002783 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002784 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002785 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2786 << FoundVar->getType();
2787 }
2788 }
2789
Douglas Gregorb75a3452011-10-15 00:10:27 +00002790 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002791 }
2792
2793 if (MergeWithVar) {
2794 // An equivalent variable with external linkage has been found. Link
2795 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002796 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002797
2798 if (VarDecl *DDef = D->getDefinition()) {
2799 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2800 Importer.ToDiag(ExistingDef->getLocation(),
2801 diag::err_odr_variable_multiple_def)
2802 << Name;
2803 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2804 } else {
2805 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002806 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002807 if (DDef->isInitKnownICE()) {
2808 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2809 Eval->CheckedICE = true;
2810 Eval->IsICE = DDef->isInitICE();
2811 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002812 }
2813 }
2814
2815 return MergeWithVar;
2816 }
2817
2818 if (!ConflictingDecls.empty()) {
2819 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2820 ConflictingDecls.data(),
2821 ConflictingDecls.size());
2822 if (!Name)
2823 return 0;
2824 }
2825 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002826
Douglas Gregorea35d112010-02-15 23:54:17 +00002827 // Import the type.
2828 QualType T = Importer.Import(D->getType());
2829 if (T.isNull())
2830 return 0;
2831
Douglas Gregor089459a2010-02-08 21:09:39 +00002832 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002833 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002834 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2835 Importer.Import(D->getInnerLocStart()),
2836 Loc, Name.getAsIdentifierInfo(),
2837 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002838 D->getStorageClass(),
2839 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002840 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002841 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002842 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002843 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002844 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002845
Douglas Gregor089459a2010-02-08 21:09:39 +00002846 // Merge the initializer.
2847 // FIXME: Can we really import any initializer? Alternatively, we could force
2848 // ourselves to import every declaration of a variable and then only use
2849 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002850 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002851
2852 // FIXME: Other bits to merge?
2853
2854 return ToVar;
2855}
2856
Douglas Gregor2cd00932010-02-17 21:22:52 +00002857Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2858 // Parameters are created in the translation unit's context, then moved
2859 // into the function declaration's context afterward.
2860 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2861
2862 // Import the name of this declaration.
2863 DeclarationName Name = Importer.Import(D->getDeclName());
2864 if (D->getDeclName() && !Name)
2865 return 0;
2866
2867 // Import the location of this declaration.
2868 SourceLocation Loc = Importer.Import(D->getLocation());
2869
2870 // Import the parameter's type.
2871 QualType T = Importer.Import(D->getType());
2872 if (T.isNull())
2873 return 0;
2874
2875 // Create the imported parameter.
2876 ImplicitParamDecl *ToParm
2877 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2878 Loc, Name.getAsIdentifierInfo(),
2879 T);
2880 return Importer.Imported(D, ToParm);
2881}
2882
Douglas Gregora404ea62010-02-10 19:54:31 +00002883Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2884 // Parameters are created in the translation unit's context, then moved
2885 // into the function declaration's context afterward.
2886 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2887
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002888 // Import the name of this declaration.
2889 DeclarationName Name = Importer.Import(D->getDeclName());
2890 if (D->getDeclName() && !Name)
2891 return 0;
2892
Douglas Gregora404ea62010-02-10 19:54:31 +00002893 // Import the location of this declaration.
2894 SourceLocation Loc = Importer.Import(D->getLocation());
2895
2896 // Import the parameter's type.
2897 QualType T = Importer.Import(D->getType());
2898 if (T.isNull())
2899 return 0;
2900
2901 // Create the imported parameter.
2902 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2903 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002904 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002905 Loc, Name.getAsIdentifierInfo(),
2906 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002907 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002908 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002909 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002910 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002911}
2912
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002913Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2914 // Import the major distinguishing characteristics of a method.
2915 DeclContext *DC, *LexicalDC;
2916 DeclarationName Name;
2917 SourceLocation Loc;
2918 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2919 return 0;
2920
Douglas Gregorb75a3452011-10-15 00:10:27 +00002921 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2922 DC->localUncachedLookup(Name, FoundDecls);
2923 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2924 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002925 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2926 continue;
2927
2928 // Check return types.
2929 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2930 FoundMethod->getResultType())) {
2931 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2932 << D->isInstanceMethod() << Name
2933 << D->getResultType() << FoundMethod->getResultType();
2934 Importer.ToDiag(FoundMethod->getLocation(),
2935 diag::note_odr_objc_method_here)
2936 << D->isInstanceMethod() << Name;
2937 return 0;
2938 }
2939
2940 // Check the number of parameters.
2941 if (D->param_size() != FoundMethod->param_size()) {
2942 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2943 << D->isInstanceMethod() << Name
2944 << D->param_size() << FoundMethod->param_size();
2945 Importer.ToDiag(FoundMethod->getLocation(),
2946 diag::note_odr_objc_method_here)
2947 << D->isInstanceMethod() << Name;
2948 return 0;
2949 }
2950
2951 // Check parameter types.
2952 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2953 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2954 P != PEnd; ++P, ++FoundP) {
2955 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2956 (*FoundP)->getType())) {
2957 Importer.FromDiag((*P)->getLocation(),
2958 diag::err_odr_objc_method_param_type_inconsistent)
2959 << D->isInstanceMethod() << Name
2960 << (*P)->getType() << (*FoundP)->getType();
2961 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2962 << (*FoundP)->getType();
2963 return 0;
2964 }
2965 }
2966
2967 // Check variadic/non-variadic.
2968 // Check the number of parameters.
2969 if (D->isVariadic() != FoundMethod->isVariadic()) {
2970 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2971 << D->isInstanceMethod() << Name;
2972 Importer.ToDiag(FoundMethod->getLocation(),
2973 diag::note_odr_objc_method_here)
2974 << D->isInstanceMethod() << Name;
2975 return 0;
2976 }
2977
2978 // FIXME: Any other bits we need to merge?
2979 return Importer.Imported(D, FoundMethod);
2980 }
2981 }
2982
2983 // Import the result type.
2984 QualType ResultTy = Importer.Import(D->getResultType());
2985 if (ResultTy.isNull())
2986 return 0;
2987
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002988 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2989
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002990 ObjCMethodDecl *ToMethod
2991 = ObjCMethodDecl::Create(Importer.getToContext(),
2992 Loc,
2993 Importer.Import(D->getLocEnd()),
2994 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002995 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002996 D->isInstanceMethod(),
2997 D->isVariadic(),
2998 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002999 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003000 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00003001 D->getImplementationControl(),
3002 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003003
3004 // FIXME: When we decide to merge method definitions, we'll need to
3005 // deal with implicit parameters.
3006
3007 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003008 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003009 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3010 FromPEnd = D->param_end();
3011 FromP != FromPEnd;
3012 ++FromP) {
3013 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3014 if (!ToP)
3015 return 0;
3016
3017 ToParams.push_back(ToP);
3018 }
3019
3020 // Set the parameters.
3021 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3022 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003023 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003024 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003025 SmallVector<SourceLocation, 12> SelLocs;
3026 D->getSelectorLocs(SelLocs);
3027 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003028
3029 ToMethod->setLexicalDeclContext(LexicalDC);
3030 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003031 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003032 return ToMethod;
3033}
3034
Douglas Gregorb4677b62010-02-18 01:47:50 +00003035Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3036 // Import the major distinguishing characteristics of a category.
3037 DeclContext *DC, *LexicalDC;
3038 DeclarationName Name;
3039 SourceLocation Loc;
3040 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3041 return 0;
3042
3043 ObjCInterfaceDecl *ToInterface
3044 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3045 if (!ToInterface)
3046 return 0;
3047
3048 // Determine if we've already encountered this category.
3049 ObjCCategoryDecl *MergeWithCategory
3050 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3051 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3052 if (!ToCategory) {
3053 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003054 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003055 Loc,
3056 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003057 Name.getAsIdentifierInfo(),
3058 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003059 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003060 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003061 Importer.Imported(D, ToCategory);
3062
Douglas Gregorb4677b62010-02-18 01:47:50 +00003063 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003064 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3065 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003066 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3067 = D->protocol_loc_begin();
3068 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3069 FromProtoEnd = D->protocol_end();
3070 FromProto != FromProtoEnd;
3071 ++FromProto, ++FromProtoLoc) {
3072 ObjCProtocolDecl *ToProto
3073 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3074 if (!ToProto)
3075 return 0;
3076 Protocols.push_back(ToProto);
3077 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3078 }
3079
3080 // FIXME: If we're merging, make sure that the protocol list is the same.
3081 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3082 ProtocolLocs.data(), Importer.getToContext());
3083
3084 } else {
3085 Importer.Imported(D, ToCategory);
3086 }
3087
3088 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003089 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003090
3091 // If we have an implementation, import it as well.
3092 if (D->getImplementation()) {
3093 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003094 = cast_or_null<ObjCCategoryImplDecl>(
3095 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003096 if (!Impl)
3097 return 0;
3098
3099 ToCategory->setImplementation(Impl);
3100 }
3101
3102 return ToCategory;
3103}
3104
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003105bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3106 ObjCProtocolDecl *To,
3107 bool ForceImport) {
3108 if (To->getDefinition()) {
3109 ImportDeclContext(From);
3110 return false;
3111 }
3112
3113 // Start the protocol definition
3114 To->startDefinition();
3115
3116 // Import protocols
3117 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3118 SmallVector<SourceLocation, 4> ProtocolLocs;
3119 ObjCProtocolDecl::protocol_loc_iterator
3120 FromProtoLoc = From->protocol_loc_begin();
3121 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3122 FromProtoEnd = From->protocol_end();
3123 FromProto != FromProtoEnd;
3124 ++FromProto, ++FromProtoLoc) {
3125 ObjCProtocolDecl *ToProto
3126 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3127 if (!ToProto)
3128 return true;
3129 Protocols.push_back(ToProto);
3130 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3131 }
3132
3133 // FIXME: If we're merging, make sure that the protocol list is the same.
3134 To->setProtocolList(Protocols.data(), Protocols.size(),
3135 ProtocolLocs.data(), Importer.getToContext());
3136
3137 // Import all of the members of this protocol.
3138 ImportDeclContext(From);
3139 return false;
3140}
3141
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003142Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003143 // If this protocol has a definition in the translation unit we're coming
3144 // from, but this particular declaration is not that definition, import the
3145 // definition and map to that.
3146 ObjCProtocolDecl *Definition = D->getDefinition();
3147 if (Definition && Definition != D) {
3148 Decl *ImportedDef = Importer.Import(Definition);
3149 if (!ImportedDef)
3150 return 0;
3151
3152 return Importer.Imported(D, ImportedDef);
3153 }
3154
Douglas Gregorb4677b62010-02-18 01:47:50 +00003155 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003156 DeclContext *DC, *LexicalDC;
3157 DeclarationName Name;
3158 SourceLocation Loc;
3159 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3160 return 0;
3161
3162 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003163 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3164 DC->localUncachedLookup(Name, FoundDecls);
3165 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3166 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003167 continue;
3168
Douglas Gregorb75a3452011-10-15 00:10:27 +00003169 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003170 break;
3171 }
3172
3173 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003174 if (!ToProto) {
3175 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3176 Name.getAsIdentifierInfo(), Loc,
3177 Importer.Import(D->getAtStartLoc()),
3178 /*PrevDecl=*/0);
3179 ToProto->setLexicalDeclContext(LexicalDC);
3180 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003181 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003182
3183 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003184
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003185 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3186 return 0;
3187
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003188 return ToProto;
3189}
3190
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003191bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3192 ObjCInterfaceDecl *To,
3193 bool ForceImport) {
3194 if (To->getDefinition()) {
3195 // Check consistency of superclass.
3196 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3197 if (FromSuper) {
3198 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3199 if (!FromSuper)
3200 return true;
3201 }
3202
3203 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3204 if ((bool)FromSuper != (bool)ToSuper ||
3205 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3206 Importer.ToDiag(To->getLocation(),
3207 diag::err_odr_objc_superclass_inconsistent)
3208 << To->getDeclName();
3209 if (ToSuper)
3210 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3211 << To->getSuperClass()->getDeclName();
3212 else
3213 Importer.ToDiag(To->getLocation(),
3214 diag::note_odr_objc_missing_superclass);
3215 if (From->getSuperClass())
3216 Importer.FromDiag(From->getSuperClassLoc(),
3217 diag::note_odr_objc_superclass)
3218 << From->getSuperClass()->getDeclName();
3219 else
3220 Importer.FromDiag(From->getLocation(),
3221 diag::note_odr_objc_missing_superclass);
3222 }
3223
3224 ImportDeclContext(From);
3225 return false;
3226 }
3227
3228 // Start the definition.
3229 To->startDefinition();
3230
3231 // If this class has a superclass, import it.
3232 if (From->getSuperClass()) {
3233 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3234 Importer.Import(From->getSuperClass()));
3235 if (!Super)
3236 return true;
3237
3238 To->setSuperClass(Super);
3239 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3240 }
3241
3242 // Import protocols
3243 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3244 SmallVector<SourceLocation, 4> ProtocolLocs;
3245 ObjCInterfaceDecl::protocol_loc_iterator
3246 FromProtoLoc = From->protocol_loc_begin();
3247
3248 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3249 FromProtoEnd = From->protocol_end();
3250 FromProto != FromProtoEnd;
3251 ++FromProto, ++FromProtoLoc) {
3252 ObjCProtocolDecl *ToProto
3253 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3254 if (!ToProto)
3255 return true;
3256 Protocols.push_back(ToProto);
3257 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3258 }
3259
3260 // FIXME: If we're merging, make sure that the protocol list is the same.
3261 To->setProtocolList(Protocols.data(), Protocols.size(),
3262 ProtocolLocs.data(), Importer.getToContext());
3263
3264 // Import categories. When the categories themselves are imported, they'll
3265 // hook themselves into this interface.
3266 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3267 FromCat = FromCat->getNextClassCategory())
3268 Importer.Import(FromCat);
3269
3270 // If we have an @implementation, import it as well.
3271 if (From->getImplementation()) {
3272 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3273 Importer.Import(From->getImplementation()));
3274 if (!Impl)
3275 return true;
3276
3277 To->setImplementation(Impl);
3278 }
3279
3280 // Import all of the members of this class.
3281 ImportDeclContext(From);
3282
3283 return false;
3284}
3285
Douglas Gregora12d2942010-02-16 01:20:57 +00003286Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003287 // If this class has a definition in the translation unit we're coming from,
3288 // but this particular declaration is not that definition, import the
3289 // definition and map to that.
3290 ObjCInterfaceDecl *Definition = D->getDefinition();
3291 if (Definition && Definition != D) {
3292 Decl *ImportedDef = Importer.Import(Definition);
3293 if (!ImportedDef)
3294 return 0;
3295
3296 return Importer.Imported(D, ImportedDef);
3297 }
3298
Douglas Gregora12d2942010-02-16 01:20:57 +00003299 // Import the major distinguishing characteristics of an @interface.
3300 DeclContext *DC, *LexicalDC;
3301 DeclarationName Name;
3302 SourceLocation Loc;
3303 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3304 return 0;
3305
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003306 // Look for an existing interface with the same name.
Douglas Gregora12d2942010-02-16 01:20:57 +00003307 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003308 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3309 DC->localUncachedLookup(Name, FoundDecls);
3310 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3311 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003312 continue;
3313
Douglas Gregorb75a3452011-10-15 00:10:27 +00003314 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003315 break;
3316 }
3317
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003318 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003319 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003320 if (!ToIface) {
3321 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3322 Importer.Import(D->getAtStartLoc()),
3323 Name.getAsIdentifierInfo(),
3324 /*PrevDecl=*/0,Loc,
3325 D->isImplicitInterfaceDecl());
3326 ToIface->setLexicalDeclContext(LexicalDC);
3327 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003328 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003329 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003330
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003331 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3332 return 0;
Douglas Gregora12d2942010-02-16 01:20:57 +00003333
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003334 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003335}
3336
Douglas Gregor3daef292010-12-07 15:32:12 +00003337Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3338 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3339 Importer.Import(D->getCategoryDecl()));
3340 if (!Category)
3341 return 0;
3342
3343 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3344 if (!ToImpl) {
3345 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3346 if (!DC)
3347 return 0;
3348
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003349 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003350 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003351 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003352 Category->getClassInterface(),
3353 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003354 Importer.Import(D->getAtStartLoc()),
3355 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003356
3357 DeclContext *LexicalDC = DC;
3358 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3359 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3360 if (!LexicalDC)
3361 return 0;
3362
3363 ToImpl->setLexicalDeclContext(LexicalDC);
3364 }
3365
Sean Callanan9faf8102011-10-21 02:57:43 +00003366 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003367 Category->setImplementation(ToImpl);
3368 }
3369
3370 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003371 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003372 return ToImpl;
3373}
3374
Douglas Gregordd182ff2010-12-07 01:26:03 +00003375Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3376 // Find the corresponding interface.
3377 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3378 Importer.Import(D->getClassInterface()));
3379 if (!Iface)
3380 return 0;
3381
3382 // Import the superclass, if any.
3383 ObjCInterfaceDecl *Super = 0;
3384 if (D->getSuperClass()) {
3385 Super = cast_or_null<ObjCInterfaceDecl>(
3386 Importer.Import(D->getSuperClass()));
3387 if (!Super)
3388 return 0;
3389 }
3390
3391 ObjCImplementationDecl *Impl = Iface->getImplementation();
3392 if (!Impl) {
3393 // We haven't imported an implementation yet. Create a new @implementation
3394 // now.
3395 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3396 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003397 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003398 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003399 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003400
3401 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3402 DeclContext *LexicalDC
3403 = Importer.ImportContext(D->getLexicalDeclContext());
3404 if (!LexicalDC)
3405 return 0;
3406 Impl->setLexicalDeclContext(LexicalDC);
3407 }
3408
3409 // Associate the implementation with the class it implements.
3410 Iface->setImplementation(Impl);
3411 Importer.Imported(D, Iface->getImplementation());
3412 } else {
3413 Importer.Imported(D, Iface->getImplementation());
3414
3415 // Verify that the existing @implementation has the same superclass.
3416 if ((Super && !Impl->getSuperClass()) ||
3417 (!Super && Impl->getSuperClass()) ||
3418 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003419 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003420 Importer.ToDiag(Impl->getLocation(),
3421 diag::err_odr_objc_superclass_inconsistent)
3422 << Iface->getDeclName();
3423 // FIXME: It would be nice to have the location of the superclass
3424 // below.
3425 if (Impl->getSuperClass())
3426 Importer.ToDiag(Impl->getLocation(),
3427 diag::note_odr_objc_superclass)
3428 << Impl->getSuperClass()->getDeclName();
3429 else
3430 Importer.ToDiag(Impl->getLocation(),
3431 diag::note_odr_objc_missing_superclass);
3432 if (D->getSuperClass())
3433 Importer.FromDiag(D->getLocation(),
3434 diag::note_odr_objc_superclass)
3435 << D->getSuperClass()->getDeclName();
3436 else
3437 Importer.FromDiag(D->getLocation(),
3438 diag::note_odr_objc_missing_superclass);
3439 return 0;
3440 }
3441 }
3442
3443 // Import all of the members of this @implementation.
3444 ImportDeclContext(D);
3445
3446 return Impl;
3447}
3448
Douglas Gregore3261622010-02-17 18:02:10 +00003449Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3450 // Import the major distinguishing characteristics of an @property.
3451 DeclContext *DC, *LexicalDC;
3452 DeclarationName Name;
3453 SourceLocation Loc;
3454 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3455 return 0;
3456
3457 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003458 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3459 DC->localUncachedLookup(Name, FoundDecls);
3460 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003461 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003462 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003463 // Check property types.
3464 if (!Importer.IsStructurallyEquivalent(D->getType(),
3465 FoundProp->getType())) {
3466 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3467 << Name << D->getType() << FoundProp->getType();
3468 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3469 << FoundProp->getType();
3470 return 0;
3471 }
3472
3473 // FIXME: Check property attributes, getters, setters, etc.?
3474
3475 // Consider these properties to be equivalent.
3476 Importer.Imported(D, FoundProp);
3477 return FoundProp;
3478 }
3479 }
3480
3481 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003482 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3483 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003484 return 0;
3485
3486 // Create the new property.
3487 ObjCPropertyDecl *ToProperty
3488 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3489 Name.getAsIdentifierInfo(),
3490 Importer.Import(D->getAtLoc()),
3491 T,
3492 D->getPropertyImplementation());
3493 Importer.Imported(D, ToProperty);
3494 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003495 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003496
3497 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003498 ToProperty->setPropertyAttributesAsWritten(
3499 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003500 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3501 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3502 ToProperty->setGetterMethodDecl(
3503 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3504 ToProperty->setSetterMethodDecl(
3505 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3506 ToProperty->setPropertyIvarDecl(
3507 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3508 return ToProperty;
3509}
3510
Douglas Gregor954e0c72010-12-07 18:32:03 +00003511Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3512 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3513 Importer.Import(D->getPropertyDecl()));
3514 if (!Property)
3515 return 0;
3516
3517 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3518 if (!DC)
3519 return 0;
3520
3521 // Import the lexical declaration context.
3522 DeclContext *LexicalDC = DC;
3523 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3524 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3525 if (!LexicalDC)
3526 return 0;
3527 }
3528
3529 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3530 if (!InImpl)
3531 return 0;
3532
3533 // Import the ivar (for an @synthesize).
3534 ObjCIvarDecl *Ivar = 0;
3535 if (D->getPropertyIvarDecl()) {
3536 Ivar = cast_or_null<ObjCIvarDecl>(
3537 Importer.Import(D->getPropertyIvarDecl()));
3538 if (!Ivar)
3539 return 0;
3540 }
3541
3542 ObjCPropertyImplDecl *ToImpl
3543 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3544 if (!ToImpl) {
3545 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3546 Importer.Import(D->getLocStart()),
3547 Importer.Import(D->getLocation()),
3548 Property,
3549 D->getPropertyImplementation(),
3550 Ivar,
3551 Importer.Import(D->getPropertyIvarDeclLoc()));
3552 ToImpl->setLexicalDeclContext(LexicalDC);
3553 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003554 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003555 } else {
3556 // Check that we have the same kind of property implementation (@synthesize
3557 // vs. @dynamic).
3558 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3559 Importer.ToDiag(ToImpl->getLocation(),
3560 diag::err_odr_objc_property_impl_kind_inconsistent)
3561 << Property->getDeclName()
3562 << (ToImpl->getPropertyImplementation()
3563 == ObjCPropertyImplDecl::Dynamic);
3564 Importer.FromDiag(D->getLocation(),
3565 diag::note_odr_objc_property_impl_kind)
3566 << D->getPropertyDecl()->getDeclName()
3567 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3568 return 0;
3569 }
3570
3571 // For @synthesize, check that we have the same
3572 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3573 Ivar != ToImpl->getPropertyIvarDecl()) {
3574 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3575 diag::err_odr_objc_synthesize_ivar_inconsistent)
3576 << Property->getDeclName()
3577 << ToImpl->getPropertyIvarDecl()->getDeclName()
3578 << Ivar->getDeclName();
3579 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3580 diag::note_odr_objc_synthesize_ivar_here)
3581 << D->getPropertyIvarDecl()->getDeclName();
3582 return 0;
3583 }
3584
3585 // Merge the existing implementation with the new implementation.
3586 Importer.Imported(D, ToImpl);
3587 }
3588
3589 return ToImpl;
3590}
3591
Douglas Gregor040afae2010-11-30 19:14:50 +00003592Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3593 // For template arguments, we adopt the translation unit as our declaration
3594 // context. This context will be fixed when the actual template declaration
3595 // is created.
3596
3597 // FIXME: Import default argument.
3598 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3599 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003600 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003601 Importer.Import(D->getLocation()),
3602 D->getDepth(),
3603 D->getIndex(),
3604 Importer.Import(D->getIdentifier()),
3605 D->wasDeclaredWithTypename(),
3606 D->isParameterPack());
3607}
3608
3609Decl *
3610ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3611 // Import the name of this declaration.
3612 DeclarationName Name = Importer.Import(D->getDeclName());
3613 if (D->getDeclName() && !Name)
3614 return 0;
3615
3616 // Import the location of this declaration.
3617 SourceLocation Loc = Importer.Import(D->getLocation());
3618
3619 // Import the type of this declaration.
3620 QualType T = Importer.Import(D->getType());
3621 if (T.isNull())
3622 return 0;
3623
3624 // Import type-source information.
3625 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3626 if (D->getTypeSourceInfo() && !TInfo)
3627 return 0;
3628
3629 // FIXME: Import default argument.
3630
3631 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3632 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003633 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003634 Loc, D->getDepth(), D->getPosition(),
3635 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003636 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003637}
3638
3639Decl *
3640ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3641 // Import the name of this declaration.
3642 DeclarationName Name = Importer.Import(D->getDeclName());
3643 if (D->getDeclName() && !Name)
3644 return 0;
3645
3646 // Import the location of this declaration.
3647 SourceLocation Loc = Importer.Import(D->getLocation());
3648
3649 // Import template parameters.
3650 TemplateParameterList *TemplateParams
3651 = ImportTemplateParameterList(D->getTemplateParameters());
3652 if (!TemplateParams)
3653 return 0;
3654
3655 // FIXME: Import default argument.
3656
3657 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3658 Importer.getToContext().getTranslationUnitDecl(),
3659 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003660 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003661 Name.getAsIdentifierInfo(),
3662 TemplateParams);
3663}
3664
3665Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3666 // If this record has a definition in the translation unit we're coming from,
3667 // but this particular declaration is not that definition, import the
3668 // definition and map to that.
3669 CXXRecordDecl *Definition
3670 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3671 if (Definition && Definition != D->getTemplatedDecl()) {
3672 Decl *ImportedDef
3673 = Importer.Import(Definition->getDescribedClassTemplate());
3674 if (!ImportedDef)
3675 return 0;
3676
3677 return Importer.Imported(D, ImportedDef);
3678 }
3679
3680 // Import the major distinguishing characteristics of this class template.
3681 DeclContext *DC, *LexicalDC;
3682 DeclarationName Name;
3683 SourceLocation Loc;
3684 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3685 return 0;
3686
3687 // We may already have a template of the same name; try to find and match it.
3688 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003689 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003690 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3691 DC->localUncachedLookup(Name, FoundDecls);
3692 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3693 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003694 continue;
3695
Douglas Gregorb75a3452011-10-15 00:10:27 +00003696 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003697 if (ClassTemplateDecl *FoundTemplate
3698 = dyn_cast<ClassTemplateDecl>(Found)) {
3699 if (IsStructuralMatch(D, FoundTemplate)) {
3700 // The class templates structurally match; call it the same template.
3701 // FIXME: We may be filling in a forward declaration here. Handle
3702 // this case!
3703 Importer.Imported(D->getTemplatedDecl(),
3704 FoundTemplate->getTemplatedDecl());
3705 return Importer.Imported(D, FoundTemplate);
3706 }
3707 }
3708
Douglas Gregorb75a3452011-10-15 00:10:27 +00003709 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003710 }
3711
3712 if (!ConflictingDecls.empty()) {
3713 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3714 ConflictingDecls.data(),
3715 ConflictingDecls.size());
3716 }
3717
3718 if (!Name)
3719 return 0;
3720 }
3721
3722 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3723
3724 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003725 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3726 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003727 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3728 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003729 DC, StartLoc, IdLoc,
3730 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003731 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003732 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003733 D2Templated->setLexicalDeclContext(LexicalDC);
3734
3735 // Create the class template declaration itself.
3736 TemplateParameterList *TemplateParams
3737 = ImportTemplateParameterList(D->getTemplateParameters());
3738 if (!TemplateParams)
3739 return 0;
3740
3741 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3742 Loc, Name, TemplateParams,
3743 D2Templated,
3744 /*PrevDecl=*/0);
3745 D2Templated->setDescribedClassTemplate(D2);
3746
3747 D2->setAccess(D->getAccess());
3748 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003749 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003750
3751 // Note the relationship between the class templates.
3752 Importer.Imported(D, D2);
3753 Importer.Imported(DTemplated, D2Templated);
3754
John McCall5e1cdac2011-10-07 06:10:15 +00003755 if (DTemplated->isCompleteDefinition() &&
3756 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003757 // FIXME: Import definition!
3758 }
3759
3760 return D2;
3761}
3762
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003763Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3764 ClassTemplateSpecializationDecl *D) {
3765 // If this record has a definition in the translation unit we're coming from,
3766 // but this particular declaration is not that definition, import the
3767 // definition and map to that.
3768 TagDecl *Definition = D->getDefinition();
3769 if (Definition && Definition != D) {
3770 Decl *ImportedDef = Importer.Import(Definition);
3771 if (!ImportedDef)
3772 return 0;
3773
3774 return Importer.Imported(D, ImportedDef);
3775 }
3776
3777 ClassTemplateDecl *ClassTemplate
3778 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3779 D->getSpecializedTemplate()));
3780 if (!ClassTemplate)
3781 return 0;
3782
3783 // Import the context of this declaration.
3784 DeclContext *DC = ClassTemplate->getDeclContext();
3785 if (!DC)
3786 return 0;
3787
3788 DeclContext *LexicalDC = DC;
3789 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3790 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3791 if (!LexicalDC)
3792 return 0;
3793 }
3794
3795 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003796 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3797 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003798
3799 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003800 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003801 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3802 D->getTemplateArgs().size(),
3803 TemplateArgs))
3804 return 0;
3805
3806 // Try to find an existing specialization with these template arguments.
3807 void *InsertPos = 0;
3808 ClassTemplateSpecializationDecl *D2
3809 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3810 TemplateArgs.size(), InsertPos);
3811 if (D2) {
3812 // We already have a class template specialization with these template
3813 // arguments.
3814
3815 // FIXME: Check for specialization vs. instantiation errors.
3816
3817 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003818 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003819 // The record types structurally match, or the "from" translation
3820 // unit only had a forward declaration anyway; call it the same
3821 // function.
3822 return Importer.Imported(D, FoundDef);
3823 }
3824 }
3825 } else {
3826 // Create a new specialization.
3827 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3828 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003829 StartLoc, IdLoc,
3830 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003831 TemplateArgs.data(),
3832 TemplateArgs.size(),
3833 /*PrevDecl=*/0);
3834 D2->setSpecializationKind(D->getSpecializationKind());
3835
3836 // Add this specialization to the class template.
3837 ClassTemplate->AddSpecialization(D2, InsertPos);
3838
3839 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003840 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003841
3842 // Add the specialization to this context.
3843 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003844 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003845 }
3846 Importer.Imported(D, D2);
3847
John McCall5e1cdac2011-10-07 06:10:15 +00003848 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003849 return 0;
3850
3851 return D2;
3852}
3853
Douglas Gregor4800d952010-02-11 19:21:55 +00003854//----------------------------------------------------------------------------
3855// Import Statements
3856//----------------------------------------------------------------------------
3857
3858Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3859 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3860 << S->getStmtClassName();
3861 return 0;
3862}
3863
3864//----------------------------------------------------------------------------
3865// Import Expressions
3866//----------------------------------------------------------------------------
3867Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3868 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3869 << E->getStmtClassName();
3870 return 0;
3871}
3872
Douglas Gregor44080632010-02-19 01:17:02 +00003873Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003874 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3875 if (!ToD)
3876 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003877
3878 NamedDecl *FoundD = 0;
3879 if (E->getDecl() != E->getFoundDecl()) {
3880 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3881 if (!FoundD)
3882 return 0;
3883 }
Douglas Gregor44080632010-02-19 01:17:02 +00003884
3885 QualType T = Importer.Import(E->getType());
3886 if (T.isNull())
3887 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003888
3889 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3890 Importer.Import(E->getQualifierLoc()),
3891 ToD,
3892 Importer.Import(E->getLocation()),
3893 T, E->getValueKind(),
3894 FoundD,
3895 /*FIXME:TemplateArgs=*/0);
3896 if (E->hadMultipleCandidates())
3897 DRE->setHadMultipleCandidates(true);
3898 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003899}
3900
Douglas Gregor4800d952010-02-11 19:21:55 +00003901Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3902 QualType T = Importer.Import(E->getType());
3903 if (T.isNull())
3904 return 0;
3905
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003906 return IntegerLiteral::Create(Importer.getToContext(),
3907 E->getValue(), T,
3908 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003909}
3910
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003911Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3912 QualType T = Importer.Import(E->getType());
3913 if (T.isNull())
3914 return 0;
3915
Douglas Gregor5cee1192011-07-27 05:40:30 +00003916 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3917 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003918 Importer.Import(E->getLocation()));
3919}
3920
Douglas Gregorf638f952010-02-19 01:07:06 +00003921Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3922 Expr *SubExpr = Importer.Import(E->getSubExpr());
3923 if (!SubExpr)
3924 return 0;
3925
3926 return new (Importer.getToContext())
3927 ParenExpr(Importer.Import(E->getLParen()),
3928 Importer.Import(E->getRParen()),
3929 SubExpr);
3930}
3931
3932Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3933 QualType T = Importer.Import(E->getType());
3934 if (T.isNull())
3935 return 0;
3936
3937 Expr *SubExpr = Importer.Import(E->getSubExpr());
3938 if (!SubExpr)
3939 return 0;
3940
3941 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003942 T, E->getValueKind(),
3943 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003944 Importer.Import(E->getOperatorLoc()));
3945}
3946
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003947Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3948 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003949 QualType ResultType = Importer.Import(E->getType());
3950
3951 if (E->isArgumentType()) {
3952 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3953 if (!TInfo)
3954 return 0;
3955
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003956 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3957 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003958 Importer.Import(E->getOperatorLoc()),
3959 Importer.Import(E->getRParenLoc()));
3960 }
3961
3962 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3963 if (!SubExpr)
3964 return 0;
3965
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003966 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3967 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003968 Importer.Import(E->getOperatorLoc()),
3969 Importer.Import(E->getRParenLoc()));
3970}
3971
Douglas Gregorf638f952010-02-19 01:07:06 +00003972Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3973 QualType T = Importer.Import(E->getType());
3974 if (T.isNull())
3975 return 0;
3976
3977 Expr *LHS = Importer.Import(E->getLHS());
3978 if (!LHS)
3979 return 0;
3980
3981 Expr *RHS = Importer.Import(E->getRHS());
3982 if (!RHS)
3983 return 0;
3984
3985 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003986 T, E->getValueKind(),
3987 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003988 Importer.Import(E->getOperatorLoc()));
3989}
3990
3991Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3992 QualType T = Importer.Import(E->getType());
3993 if (T.isNull())
3994 return 0;
3995
3996 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3997 if (CompLHSType.isNull())
3998 return 0;
3999
4000 QualType CompResultType = Importer.Import(E->getComputationResultType());
4001 if (CompResultType.isNull())
4002 return 0;
4003
4004 Expr *LHS = Importer.Import(E->getLHS());
4005 if (!LHS)
4006 return 0;
4007
4008 Expr *RHS = Importer.Import(E->getRHS());
4009 if (!RHS)
4010 return 0;
4011
4012 return new (Importer.getToContext())
4013 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004014 T, E->getValueKind(),
4015 E->getObjectKind(),
4016 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004017 Importer.Import(E->getOperatorLoc()));
4018}
4019
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004020static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004021 if (E->path_empty()) return false;
4022
4023 // TODO: import cast paths
4024 return true;
4025}
4026
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004027Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4028 QualType T = Importer.Import(E->getType());
4029 if (T.isNull())
4030 return 0;
4031
4032 Expr *SubExpr = Importer.Import(E->getSubExpr());
4033 if (!SubExpr)
4034 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004035
4036 CXXCastPath BasePath;
4037 if (ImportCastPath(E, BasePath))
4038 return 0;
4039
4040 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004041 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004042}
4043
Douglas Gregor008847a2010-02-19 01:32:14 +00004044Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4045 QualType T = Importer.Import(E->getType());
4046 if (T.isNull())
4047 return 0;
4048
4049 Expr *SubExpr = Importer.Import(E->getSubExpr());
4050 if (!SubExpr)
4051 return 0;
4052
4053 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4054 if (!TInfo && E->getTypeInfoAsWritten())
4055 return 0;
4056
John McCallf871d0c2010-08-07 06:22:56 +00004057 CXXCastPath BasePath;
4058 if (ImportCastPath(E, BasePath))
4059 return 0;
4060
John McCallf89e55a2010-11-18 06:31:45 +00004061 return CStyleCastExpr::Create(Importer.getToContext(), T,
4062 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004063 SubExpr, &BasePath, TInfo,
4064 Importer.Import(E->getLParenLoc()),
4065 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004066}
4067
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004068ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004069 ASTContext &FromContext, FileManager &FromFileManager,
4070 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004071 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004072 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4073 Minimal(MinimalImport)
4074{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004075 ImportedDecls[FromContext.getTranslationUnitDecl()]
4076 = ToContext.getTranslationUnitDecl();
4077}
4078
4079ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004080
4081QualType ASTImporter::Import(QualType FromT) {
4082 if (FromT.isNull())
4083 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004084
4085 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004086
Douglas Gregor169fba52010-02-08 15:18:58 +00004087 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004088 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4089 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004090 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004091 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004092
Douglas Gregor169fba52010-02-08 15:18:58 +00004093 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004094 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004095 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004096 if (ToT.isNull())
4097 return ToT;
4098
Douglas Gregor169fba52010-02-08 15:18:58 +00004099 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004100 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004101
John McCallf4c73712011-01-19 06:33:43 +00004102 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004103}
4104
Douglas Gregor9bed8792010-02-09 19:21:46 +00004105TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004106 if (!FromTSI)
4107 return FromTSI;
4108
4109 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004110 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004111 QualType T = Import(FromTSI->getType());
4112 if (T.isNull())
4113 return 0;
4114
4115 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004116 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004117}
4118
4119Decl *ASTImporter::Import(Decl *FromD) {
4120 if (!FromD)
4121 return 0;
4122
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004123 ASTNodeImporter Importer(*this);
4124
Douglas Gregor9bed8792010-02-09 19:21:46 +00004125 // Check whether we've already imported this declaration.
4126 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004127 if (Pos != ImportedDecls.end()) {
4128 Decl *ToD = Pos->second;
4129 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4130 return ToD;
4131 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004132
4133 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004134 Decl *ToD = Importer.Visit(FromD);
4135 if (!ToD)
4136 return 0;
4137
4138 // Record the imported declaration.
4139 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004140
4141 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4142 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004143 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004144 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004145 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004146 // When we've finished transforming a typedef, see whether it was the
4147 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004148 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004149 FromTag = AnonTagsWithPendingTypedefs.begin(),
4150 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4151 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004152 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004153 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4154 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004155 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004156 AnonTagsWithPendingTypedefs.erase(FromTag);
4157 break;
4158 }
4159 }
4160 }
4161 }
4162
Douglas Gregor9bed8792010-02-09 19:21:46 +00004163 return ToD;
4164}
4165
4166DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4167 if (!FromDC)
4168 return FromDC;
4169
4170 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4171}
4172
4173Expr *ASTImporter::Import(Expr *FromE) {
4174 if (!FromE)
4175 return 0;
4176
4177 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4178}
4179
4180Stmt *ASTImporter::Import(Stmt *FromS) {
4181 if (!FromS)
4182 return 0;
4183
Douglas Gregor4800d952010-02-11 19:21:55 +00004184 // Check whether we've already imported this declaration.
4185 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4186 if (Pos != ImportedStmts.end())
4187 return Pos->second;
4188
4189 // Import the type
4190 ASTNodeImporter Importer(*this);
4191 Stmt *ToS = Importer.Visit(FromS);
4192 if (!ToS)
4193 return 0;
4194
4195 // Record the imported declaration.
4196 ImportedStmts[FromS] = ToS;
4197 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004198}
4199
4200NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4201 if (!FromNNS)
4202 return 0;
4203
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004204 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4205
4206 switch (FromNNS->getKind()) {
4207 case NestedNameSpecifier::Identifier:
4208 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4209 return NestedNameSpecifier::Create(ToContext, prefix, II);
4210 }
4211 return 0;
4212
4213 case NestedNameSpecifier::Namespace:
4214 if (NamespaceDecl *NS =
4215 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4216 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4217 }
4218 return 0;
4219
4220 case NestedNameSpecifier::NamespaceAlias:
4221 if (NamespaceAliasDecl *NSAD =
4222 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4223 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4224 }
4225 return 0;
4226
4227 case NestedNameSpecifier::Global:
4228 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4229
4230 case NestedNameSpecifier::TypeSpec:
4231 case NestedNameSpecifier::TypeSpecWithTemplate: {
4232 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4233 if (!T.isNull()) {
4234 bool bTemplate = FromNNS->getKind() ==
4235 NestedNameSpecifier::TypeSpecWithTemplate;
4236 return NestedNameSpecifier::Create(ToContext, prefix,
4237 bTemplate, T.getTypePtr());
4238 }
4239 }
4240 return 0;
4241 }
4242
4243 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004244}
4245
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004246NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4247 // FIXME: Implement!
4248 return NestedNameSpecifierLoc();
4249}
4250
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004251TemplateName ASTImporter::Import(TemplateName From) {
4252 switch (From.getKind()) {
4253 case TemplateName::Template:
4254 if (TemplateDecl *ToTemplate
4255 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4256 return TemplateName(ToTemplate);
4257
4258 return TemplateName();
4259
4260 case TemplateName::OverloadedTemplate: {
4261 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4262 UnresolvedSet<2> ToTemplates;
4263 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4264 E = FromStorage->end();
4265 I != E; ++I) {
4266 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4267 ToTemplates.addDecl(To);
4268 else
4269 return TemplateName();
4270 }
4271 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4272 ToTemplates.end());
4273 }
4274
4275 case TemplateName::QualifiedTemplate: {
4276 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4277 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4278 if (!Qualifier)
4279 return TemplateName();
4280
4281 if (TemplateDecl *ToTemplate
4282 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4283 return ToContext.getQualifiedTemplateName(Qualifier,
4284 QTN->hasTemplateKeyword(),
4285 ToTemplate);
4286
4287 return TemplateName();
4288 }
4289
4290 case TemplateName::DependentTemplate: {
4291 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4292 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4293 if (!Qualifier)
4294 return TemplateName();
4295
4296 if (DTN->isIdentifier()) {
4297 return ToContext.getDependentTemplateName(Qualifier,
4298 Import(DTN->getIdentifier()));
4299 }
4300
4301 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4302 }
John McCall14606042011-06-30 08:33:18 +00004303
4304 case TemplateName::SubstTemplateTemplateParm: {
4305 SubstTemplateTemplateParmStorage *subst
4306 = From.getAsSubstTemplateTemplateParm();
4307 TemplateTemplateParmDecl *param
4308 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4309 if (!param)
4310 return TemplateName();
4311
4312 TemplateName replacement = Import(subst->getReplacement());
4313 if (replacement.isNull()) return TemplateName();
4314
4315 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4316 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004317
4318 case TemplateName::SubstTemplateTemplateParmPack: {
4319 SubstTemplateTemplateParmPackStorage *SubstPack
4320 = From.getAsSubstTemplateTemplateParmPack();
4321 TemplateTemplateParmDecl *Param
4322 = cast_or_null<TemplateTemplateParmDecl>(
4323 Import(SubstPack->getParameterPack()));
4324 if (!Param)
4325 return TemplateName();
4326
4327 ASTNodeImporter Importer(*this);
4328 TemplateArgument ArgPack
4329 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4330 if (ArgPack.isNull())
4331 return TemplateName();
4332
4333 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4334 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004335 }
4336
4337 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004338}
4339
Douglas Gregor9bed8792010-02-09 19:21:46 +00004340SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4341 if (FromLoc.isInvalid())
4342 return SourceLocation();
4343
Douglas Gregor88523732010-02-10 00:15:17 +00004344 SourceManager &FromSM = FromContext.getSourceManager();
4345
4346 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004347 // don't have to import macro expansions.
4348 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004349 FromLoc = FromSM.getSpellingLoc(FromLoc);
4350 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4351 SourceManager &ToSM = ToContext.getSourceManager();
4352 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004353 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004354}
4355
4356SourceRange ASTImporter::Import(SourceRange FromRange) {
4357 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4358}
4359
Douglas Gregor88523732010-02-10 00:15:17 +00004360FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004361 llvm::DenseMap<FileID, FileID>::iterator Pos
4362 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004363 if (Pos != ImportedFileIDs.end())
4364 return Pos->second;
4365
4366 SourceManager &FromSM = FromContext.getSourceManager();
4367 SourceManager &ToSM = ToContext.getSourceManager();
4368 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004369 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004370
4371 // Include location of this file.
4372 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4373
4374 // Map the FileID for to the "to" source manager.
4375 FileID ToID;
4376 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004377 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004378 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4379 // disk again
4380 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4381 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004382 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004383 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4384 FromSLoc.getFile().getFileCharacteristic());
4385 } else {
4386 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004387 const llvm::MemoryBuffer *
4388 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004389 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004390 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004391 FromBuf->getBufferIdentifier());
4392 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4393 }
4394
4395
Sebastian Redl535a3e22010-09-30 01:03:06 +00004396 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004397 return ToID;
4398}
4399
Douglas Gregord8868a62011-01-18 03:11:38 +00004400void ASTImporter::ImportDefinition(Decl *From) {
4401 Decl *To = Import(From);
4402 if (!To)
4403 return;
4404
4405 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4406 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004407
4408 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4409 if (!ToRecord->getDefinition()) {
4410 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4411 /*ForceImport=*/true);
4412 return;
4413 }
4414 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004415
4416 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4417 if (!ToEnum->getDefinition()) {
4418 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4419 /*ForceImport=*/true);
4420 return;
4421 }
4422 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004423
4424 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4425 if (!ToIFace->getDefinition()) {
4426 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
4427 /*ForceImport=*/true);
4428 return;
4429 }
4430 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004431
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004432 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4433 if (!ToProto->getDefinition()) {
4434 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
4435 /*ForceImport=*/true);
4436 return;
4437 }
4438 }
4439
Douglas Gregord8868a62011-01-18 03:11:38 +00004440 Importer.ImportDeclContext(FromDC, true);
4441 }
4442}
4443
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004444DeclarationName ASTImporter::Import(DeclarationName FromName) {
4445 if (!FromName)
4446 return DeclarationName();
4447
4448 switch (FromName.getNameKind()) {
4449 case DeclarationName::Identifier:
4450 return Import(FromName.getAsIdentifierInfo());
4451
4452 case DeclarationName::ObjCZeroArgSelector:
4453 case DeclarationName::ObjCOneArgSelector:
4454 case DeclarationName::ObjCMultiArgSelector:
4455 return Import(FromName.getObjCSelector());
4456
4457 case DeclarationName::CXXConstructorName: {
4458 QualType T = Import(FromName.getCXXNameType());
4459 if (T.isNull())
4460 return DeclarationName();
4461
4462 return ToContext.DeclarationNames.getCXXConstructorName(
4463 ToContext.getCanonicalType(T));
4464 }
4465
4466 case DeclarationName::CXXDestructorName: {
4467 QualType T = Import(FromName.getCXXNameType());
4468 if (T.isNull())
4469 return DeclarationName();
4470
4471 return ToContext.DeclarationNames.getCXXDestructorName(
4472 ToContext.getCanonicalType(T));
4473 }
4474
4475 case DeclarationName::CXXConversionFunctionName: {
4476 QualType T = Import(FromName.getCXXNameType());
4477 if (T.isNull())
4478 return DeclarationName();
4479
4480 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4481 ToContext.getCanonicalType(T));
4482 }
4483
4484 case DeclarationName::CXXOperatorName:
4485 return ToContext.DeclarationNames.getCXXOperatorName(
4486 FromName.getCXXOverloadedOperator());
4487
4488 case DeclarationName::CXXLiteralOperatorName:
4489 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4490 Import(FromName.getCXXLiteralIdentifier()));
4491
4492 case DeclarationName::CXXUsingDirective:
4493 // FIXME: STATICS!
4494 return DeclarationName::getUsingDirectiveName();
4495 }
4496
David Blaikie30263482012-01-20 21:50:17 +00004497 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004498}
4499
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004500IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004501 if (!FromId)
4502 return 0;
4503
4504 return &ToContext.Idents.get(FromId->getName());
4505}
Douglas Gregor089459a2010-02-08 21:09:39 +00004506
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004507Selector ASTImporter::Import(Selector FromSel) {
4508 if (FromSel.isNull())
4509 return Selector();
4510
Chris Lattner5f9e2722011-07-23 10:55:15 +00004511 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004512 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4513 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4514 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4515 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4516}
4517
Douglas Gregor089459a2010-02-08 21:09:39 +00004518DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4519 DeclContext *DC,
4520 unsigned IDNS,
4521 NamedDecl **Decls,
4522 unsigned NumDecls) {
4523 return Name;
4524}
4525
4526DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004527 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004528}
4529
4530DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004531 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004532}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004533
4534Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4535 ImportedDecls[From] = To;
4536 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004537}
Douglas Gregorea35d112010-02-15 23:54:17 +00004538
4539bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004540 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004541 = ImportedTypes.find(From.getTypePtr());
4542 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4543 return true;
4544
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004545 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004546 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004547}