blob: cc0213fca235d53c52abd8063efc7e9a7cee8032 [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
Douglas Gregorcd0d56a2012-01-24 18:36:04 +000081 // 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 Gregorac32ff92012-02-01 21:00:38 +000089
Douglas Gregorcd0d56a2012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregorac32ff92012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor1cf038c2011-07-29 23:31:30 +0000110 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000112 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor5602f7e2012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor040afae2010-11-30 19:14:50 +0000116 TemplateParameterList *ImportTemplateParameterList(
117 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000118 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
119 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
120 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000122 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000123 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000124 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000125 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000126 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000127 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000128 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000129 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000130 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000131 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000132 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000133 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000134 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000135 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
136 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
137 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
138 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000139 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000140 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000141 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000142 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000143 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000144 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000145 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000146 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000147 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000148 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000149 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000150 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000151 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000152 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000153 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
154 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
155 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
156 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000157 Decl *VisitClassTemplateSpecializationDecl(
158 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000159
Douglas Gregor4800d952010-02-11 19:21:55 +0000160 // Importing statements
161 Stmt *VisitStmt(Stmt *S);
162
163 // Importing expressions
164 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000165 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000166 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000167 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000168 Expr *VisitParenExpr(ParenExpr *E);
169 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000170 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000171 Expr *VisitBinaryOperator(BinaryOperator *E);
172 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000173 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000174 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000175 };
176}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000177using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000178
179//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180// Structural Equivalence
181//----------------------------------------------------------------------------
182
183namespace {
184 struct StructuralEquivalenceContext {
185 /// \brief AST contexts for which we are checking structural equivalence.
186 ASTContext &C1, &C2;
187
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000188 /// \brief The set of "tentative" equivalences between two canonical
189 /// declarations, mapping from a declaration in the first context to the
190 /// declaration in the second context that we believe to be equivalent.
191 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
192
193 /// \brief Queue of declarations in the first context whose equivalence
194 /// with a declaration in the second context still needs to be verified.
195 std::deque<Decl *> DeclsToCheck;
196
Douglas Gregorea35d112010-02-15 23:54:17 +0000197 /// \brief Declaration (from, to) pairs that are known not to be equivalent
198 /// (which we have already complained about).
199 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
200
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000201 /// \brief Whether we're being strict about the spelling of types when
202 /// unifying two types.
203 bool StrictTypeSpelling;
204
205 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000206 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000207 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000208 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000209 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000210
211 /// \brief Determine whether the two declarations are structurally
212 /// equivalent.
213 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
214
215 /// \brief Determine whether the two types are structurally equivalent.
216 bool IsStructurallyEquivalent(QualType T1, QualType T2);
217
218 private:
219 /// \brief Finish checking all of the structural equivalences.
220 ///
221 /// \returns true if an error occurred, false otherwise.
222 bool Finish();
223
224 public:
225 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000226 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227 }
228
229 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000230 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000231 }
232 };
233}
234
235static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
236 QualType T1, QualType T2);
237static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
238 Decl *D1, Decl *D2);
239
240/// \brief Determine if two APInts have the same value, after zero-extending
241/// one of them (if needed!) to ensure that the bit-widths match.
242static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
243 if (I1.getBitWidth() == I2.getBitWidth())
244 return I1 == I2;
245
246 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000247 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000248
Jay Foad9f71a8f2010-12-07 08:25:34 +0000249 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000250}
251
252/// \brief Determine if two APSInts have the same value, zero- or sign-extending
253/// as needed.
254static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
255 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
256 return I1 == I2;
257
258 // Check for a bit-width mismatch.
259 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000260 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000261 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000262 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000263
264 // We have a signedness mismatch. Turn the signed value into an unsigned
265 // value.
266 if (I1.isSigned()) {
267 if (I1.isNegative())
268 return false;
269
270 return llvm::APSInt(I1, true) == I2;
271 }
272
273 if (I2.isNegative())
274 return false;
275
276 return I1 == llvm::APSInt(I2, true);
277}
278
279/// \brief Determine structural equivalence of two expressions.
280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
281 Expr *E1, Expr *E2) {
282 if (!E1 || !E2)
283 return E1 == E2;
284
285 // FIXME: Actually perform a structural comparison!
286 return true;
287}
288
289/// \brief Determine whether two identifiers are equivalent.
290static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
291 const IdentifierInfo *Name2) {
292 if (!Name1 || !Name2)
293 return Name1 == Name2;
294
295 return Name1->getName() == Name2->getName();
296}
297
298/// \brief Determine whether two nested-name-specifiers are equivalent.
299static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
300 NestedNameSpecifier *NNS1,
301 NestedNameSpecifier *NNS2) {
302 // FIXME: Implement!
303 return true;
304}
305
306/// \brief Determine whether two template arguments are equivalent.
307static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
308 const TemplateArgument &Arg1,
309 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000310 if (Arg1.getKind() != Arg2.getKind())
311 return false;
312
313 switch (Arg1.getKind()) {
314 case TemplateArgument::Null:
315 return true;
316
317 case TemplateArgument::Type:
318 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
319
320 case TemplateArgument::Integral:
321 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
322 Arg2.getIntegralType()))
323 return false;
324
325 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
326
327 case TemplateArgument::Declaration:
328 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
329
330 case TemplateArgument::Template:
331 return IsStructurallyEquivalent(Context,
332 Arg1.getAsTemplate(),
333 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000334
335 case TemplateArgument::TemplateExpansion:
336 return IsStructurallyEquivalent(Context,
337 Arg1.getAsTemplateOrTemplatePattern(),
338 Arg2.getAsTemplateOrTemplatePattern());
339
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000340 case TemplateArgument::Expression:
341 return IsStructurallyEquivalent(Context,
342 Arg1.getAsExpr(), Arg2.getAsExpr());
343
344 case TemplateArgument::Pack:
345 if (Arg1.pack_size() != Arg2.pack_size())
346 return false;
347
348 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
349 if (!IsStructurallyEquivalent(Context,
350 Arg1.pack_begin()[I],
351 Arg2.pack_begin()[I]))
352 return false;
353
354 return true;
355 }
356
357 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000358}
359
360/// \brief Determine structural equivalence for the common part of array
361/// types.
362static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
363 const ArrayType *Array1,
364 const ArrayType *Array2) {
365 if (!IsStructurallyEquivalent(Context,
366 Array1->getElementType(),
367 Array2->getElementType()))
368 return false;
369 if (Array1->getSizeModifier() != Array2->getSizeModifier())
370 return false;
371 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
372 return false;
373
374 return true;
375}
376
377/// \brief Determine structural equivalence of two types.
378static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
379 QualType T1, QualType T2) {
380 if (T1.isNull() || T2.isNull())
381 return T1.isNull() && T2.isNull();
382
383 if (!Context.StrictTypeSpelling) {
384 // We aren't being strict about token-to-token equivalence of types,
385 // so map down to the canonical type.
386 T1 = Context.C1.getCanonicalType(T1);
387 T2 = Context.C2.getCanonicalType(T2);
388 }
389
390 if (T1.getQualifiers() != T2.getQualifiers())
391 return false;
392
Douglas Gregorea35d112010-02-15 23:54:17 +0000393 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000394
Douglas Gregorea35d112010-02-15 23:54:17 +0000395 if (T1->getTypeClass() != T2->getTypeClass()) {
396 // Compare function types with prototypes vs. without prototypes as if
397 // both did not have prototypes.
398 if (T1->getTypeClass() == Type::FunctionProto &&
399 T2->getTypeClass() == Type::FunctionNoProto)
400 TC = Type::FunctionNoProto;
401 else if (T1->getTypeClass() == Type::FunctionNoProto &&
402 T2->getTypeClass() == Type::FunctionProto)
403 TC = Type::FunctionNoProto;
404 else
405 return false;
406 }
407
408 switch (TC) {
409 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000410 // FIXME: Deal with Char_S/Char_U.
411 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
412 return false;
413 break;
414
415 case Type::Complex:
416 if (!IsStructurallyEquivalent(Context,
417 cast<ComplexType>(T1)->getElementType(),
418 cast<ComplexType>(T2)->getElementType()))
419 return false;
420 break;
421
422 case Type::Pointer:
423 if (!IsStructurallyEquivalent(Context,
424 cast<PointerType>(T1)->getPointeeType(),
425 cast<PointerType>(T2)->getPointeeType()))
426 return false;
427 break;
428
429 case Type::BlockPointer:
430 if (!IsStructurallyEquivalent(Context,
431 cast<BlockPointerType>(T1)->getPointeeType(),
432 cast<BlockPointerType>(T2)->getPointeeType()))
433 return false;
434 break;
435
436 case Type::LValueReference:
437 case Type::RValueReference: {
438 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
439 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
440 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
441 return false;
442 if (Ref1->isInnerRef() != Ref2->isInnerRef())
443 return false;
444 if (!IsStructurallyEquivalent(Context,
445 Ref1->getPointeeTypeAsWritten(),
446 Ref2->getPointeeTypeAsWritten()))
447 return false;
448 break;
449 }
450
451 case Type::MemberPointer: {
452 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
453 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
454 if (!IsStructurallyEquivalent(Context,
455 MemPtr1->getPointeeType(),
456 MemPtr2->getPointeeType()))
457 return false;
458 if (!IsStructurallyEquivalent(Context,
459 QualType(MemPtr1->getClass(), 0),
460 QualType(MemPtr2->getClass(), 0)))
461 return false;
462 break;
463 }
464
465 case Type::ConstantArray: {
466 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
467 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
468 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
469 return false;
470
471 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
472 return false;
473 break;
474 }
475
476 case Type::IncompleteArray:
477 if (!IsArrayStructurallyEquivalent(Context,
478 cast<ArrayType>(T1),
479 cast<ArrayType>(T2)))
480 return false;
481 break;
482
483 case Type::VariableArray: {
484 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
485 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
486 if (!IsStructurallyEquivalent(Context,
487 Array1->getSizeExpr(), Array2->getSizeExpr()))
488 return false;
489
490 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
491 return false;
492
493 break;
494 }
495
496 case Type::DependentSizedArray: {
497 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
498 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
499 if (!IsStructurallyEquivalent(Context,
500 Array1->getSizeExpr(), Array2->getSizeExpr()))
501 return false;
502
503 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
504 return false;
505
506 break;
507 }
508
509 case Type::DependentSizedExtVector: {
510 const DependentSizedExtVectorType *Vec1
511 = cast<DependentSizedExtVectorType>(T1);
512 const DependentSizedExtVectorType *Vec2
513 = cast<DependentSizedExtVectorType>(T2);
514 if (!IsStructurallyEquivalent(Context,
515 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
516 return false;
517 if (!IsStructurallyEquivalent(Context,
518 Vec1->getElementType(),
519 Vec2->getElementType()))
520 return false;
521 break;
522 }
523
524 case Type::Vector:
525 case Type::ExtVector: {
526 const VectorType *Vec1 = cast<VectorType>(T1);
527 const VectorType *Vec2 = cast<VectorType>(T2);
528 if (!IsStructurallyEquivalent(Context,
529 Vec1->getElementType(),
530 Vec2->getElementType()))
531 return false;
532 if (Vec1->getNumElements() != Vec2->getNumElements())
533 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000534 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000535 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000536 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000537 }
538
539 case Type::FunctionProto: {
540 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
541 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
542 if (Proto1->getNumArgs() != Proto2->getNumArgs())
543 return false;
544 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
545 if (!IsStructurallyEquivalent(Context,
546 Proto1->getArgType(I),
547 Proto2->getArgType(I)))
548 return false;
549 }
550 if (Proto1->isVariadic() != Proto2->isVariadic())
551 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000552 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000553 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000554 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
555 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
556 return false;
557 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
558 if (!IsStructurallyEquivalent(Context,
559 Proto1->getExceptionType(I),
560 Proto2->getExceptionType(I)))
561 return false;
562 }
563 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000565 Proto1->getNoexceptExpr(),
566 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000567 return false;
568 }
569 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
570 return false;
571
572 // Fall through to check the bits common with FunctionNoProtoType.
573 }
574
575 case Type::FunctionNoProto: {
576 const FunctionType *Function1 = cast<FunctionType>(T1);
577 const FunctionType *Function2 = cast<FunctionType>(T2);
578 if (!IsStructurallyEquivalent(Context,
579 Function1->getResultType(),
580 Function2->getResultType()))
581 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000582 if (Function1->getExtInfo() != Function2->getExtInfo())
583 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000584 break;
585 }
586
587 case Type::UnresolvedUsing:
588 if (!IsStructurallyEquivalent(Context,
589 cast<UnresolvedUsingType>(T1)->getDecl(),
590 cast<UnresolvedUsingType>(T2)->getDecl()))
591 return false;
592
593 break;
John McCall9d156a72011-01-06 01:58:22 +0000594
595 case Type::Attributed:
596 if (!IsStructurallyEquivalent(Context,
597 cast<AttributedType>(T1)->getModifiedType(),
598 cast<AttributedType>(T2)->getModifiedType()))
599 return false;
600 if (!IsStructurallyEquivalent(Context,
601 cast<AttributedType>(T1)->getEquivalentType(),
602 cast<AttributedType>(T2)->getEquivalentType()))
603 return false;
604 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000605
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000606 case Type::Paren:
607 if (!IsStructurallyEquivalent(Context,
608 cast<ParenType>(T1)->getInnerType(),
609 cast<ParenType>(T2)->getInnerType()))
610 return false;
611 break;
612
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000613 case Type::Typedef:
614 if (!IsStructurallyEquivalent(Context,
615 cast<TypedefType>(T1)->getDecl(),
616 cast<TypedefType>(T2)->getDecl()))
617 return false;
618 break;
619
620 case Type::TypeOfExpr:
621 if (!IsStructurallyEquivalent(Context,
622 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
623 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
624 return false;
625 break;
626
627 case Type::TypeOf:
628 if (!IsStructurallyEquivalent(Context,
629 cast<TypeOfType>(T1)->getUnderlyingType(),
630 cast<TypeOfType>(T2)->getUnderlyingType()))
631 return false;
632 break;
Sean Huntca63c202011-05-24 22:41:36 +0000633
634 case Type::UnaryTransform:
635 if (!IsStructurallyEquivalent(Context,
636 cast<UnaryTransformType>(T1)->getUnderlyingType(),
637 cast<UnaryTransformType>(T1)->getUnderlyingType()))
638 return false;
639 break;
640
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000641 case Type::Decltype:
642 if (!IsStructurallyEquivalent(Context,
643 cast<DecltypeType>(T1)->getUnderlyingExpr(),
644 cast<DecltypeType>(T2)->getUnderlyingExpr()))
645 return false;
646 break;
647
Richard Smith34b41d92011-02-20 03:19:35 +0000648 case Type::Auto:
649 if (!IsStructurallyEquivalent(Context,
650 cast<AutoType>(T1)->getDeducedType(),
651 cast<AutoType>(T2)->getDeducedType()))
652 return false;
653 break;
654
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000655 case Type::Record:
656 case Type::Enum:
657 if (!IsStructurallyEquivalent(Context,
658 cast<TagType>(T1)->getDecl(),
659 cast<TagType>(T2)->getDecl()))
660 return false;
661 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000662
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000663 case Type::TemplateTypeParm: {
664 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
665 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
666 if (Parm1->getDepth() != Parm2->getDepth())
667 return false;
668 if (Parm1->getIndex() != Parm2->getIndex())
669 return false;
670 if (Parm1->isParameterPack() != Parm2->isParameterPack())
671 return false;
672
673 // Names of template type parameters are never significant.
674 break;
675 }
676
677 case Type::SubstTemplateTypeParm: {
678 const SubstTemplateTypeParmType *Subst1
679 = cast<SubstTemplateTypeParmType>(T1);
680 const SubstTemplateTypeParmType *Subst2
681 = cast<SubstTemplateTypeParmType>(T2);
682 if (!IsStructurallyEquivalent(Context,
683 QualType(Subst1->getReplacedParameter(), 0),
684 QualType(Subst2->getReplacedParameter(), 0)))
685 return false;
686 if (!IsStructurallyEquivalent(Context,
687 Subst1->getReplacementType(),
688 Subst2->getReplacementType()))
689 return false;
690 break;
691 }
692
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000693 case Type::SubstTemplateTypeParmPack: {
694 const SubstTemplateTypeParmPackType *Subst1
695 = cast<SubstTemplateTypeParmPackType>(T1);
696 const SubstTemplateTypeParmPackType *Subst2
697 = cast<SubstTemplateTypeParmPackType>(T2);
698 if (!IsStructurallyEquivalent(Context,
699 QualType(Subst1->getReplacedParameter(), 0),
700 QualType(Subst2->getReplacedParameter(), 0)))
701 return false;
702 if (!IsStructurallyEquivalent(Context,
703 Subst1->getArgumentPack(),
704 Subst2->getArgumentPack()))
705 return false;
706 break;
707 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000708 case Type::TemplateSpecialization: {
709 const TemplateSpecializationType *Spec1
710 = cast<TemplateSpecializationType>(T1);
711 const TemplateSpecializationType *Spec2
712 = cast<TemplateSpecializationType>(T2);
713 if (!IsStructurallyEquivalent(Context,
714 Spec1->getTemplateName(),
715 Spec2->getTemplateName()))
716 return false;
717 if (Spec1->getNumArgs() != Spec2->getNumArgs())
718 return false;
719 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
720 if (!IsStructurallyEquivalent(Context,
721 Spec1->getArg(I), Spec2->getArg(I)))
722 return false;
723 }
724 break;
725 }
726
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000727 case Type::Elaborated: {
728 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
729 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
730 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
731 if (Elab1->getKeyword() != Elab2->getKeyword())
732 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000733 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000734 Elab1->getQualifier(),
735 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000736 return false;
737 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000738 Elab1->getNamedType(),
739 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000740 return false;
741 break;
742 }
743
John McCall3cb0ebd2010-03-10 03:28:59 +0000744 case Type::InjectedClassName: {
745 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
746 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
747 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000748 Inj1->getInjectedSpecializationType(),
749 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000750 return false;
751 break;
752 }
753
Douglas Gregor4714c122010-03-31 17:34:00 +0000754 case Type::DependentName: {
755 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
756 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000757 if (!IsStructurallyEquivalent(Context,
758 Typename1->getQualifier(),
759 Typename2->getQualifier()))
760 return false;
761 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
762 Typename2->getIdentifier()))
763 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000764
765 break;
766 }
767
John McCall33500952010-06-11 00:33:02 +0000768 case Type::DependentTemplateSpecialization: {
769 const DependentTemplateSpecializationType *Spec1 =
770 cast<DependentTemplateSpecializationType>(T1);
771 const DependentTemplateSpecializationType *Spec2 =
772 cast<DependentTemplateSpecializationType>(T2);
773 if (!IsStructurallyEquivalent(Context,
774 Spec1->getQualifier(),
775 Spec2->getQualifier()))
776 return false;
777 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
778 Spec2->getIdentifier()))
779 return false;
780 if (Spec1->getNumArgs() != Spec2->getNumArgs())
781 return false;
782 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
783 if (!IsStructurallyEquivalent(Context,
784 Spec1->getArg(I), Spec2->getArg(I)))
785 return false;
786 }
787 break;
788 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000789
790 case Type::PackExpansion:
791 if (!IsStructurallyEquivalent(Context,
792 cast<PackExpansionType>(T1)->getPattern(),
793 cast<PackExpansionType>(T2)->getPattern()))
794 return false;
795 break;
796
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000797 case Type::ObjCInterface: {
798 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
799 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
800 if (!IsStructurallyEquivalent(Context,
801 Iface1->getDecl(), Iface2->getDecl()))
802 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000803 break;
804 }
805
806 case Type::ObjCObject: {
807 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
808 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
809 if (!IsStructurallyEquivalent(Context,
810 Obj1->getBaseType(),
811 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000812 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000813 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
814 return false;
815 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000816 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000817 Obj1->getProtocol(I),
818 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000819 return false;
820 }
821 break;
822 }
823
824 case Type::ObjCObjectPointer: {
825 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
826 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
827 if (!IsStructurallyEquivalent(Context,
828 Ptr1->getPointeeType(),
829 Ptr2->getPointeeType()))
830 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000831 break;
832 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000833
834 case Type::Atomic: {
835 if (!IsStructurallyEquivalent(Context,
836 cast<AtomicType>(T1)->getValueType(),
837 cast<AtomicType>(T2)->getValueType()))
838 return false;
839 break;
840 }
841
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000842 } // end switch
843
844 return true;
845}
846
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000847/// \brief Determine structural equivalence of two fields.
848static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
849 FieldDecl *Field1, FieldDecl *Field2) {
850 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
851
852 if (!IsStructurallyEquivalent(Context,
853 Field1->getType(), Field2->getType())) {
854 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
855 << Context.C2.getTypeDeclType(Owner2);
856 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
857 << Field2->getDeclName() << Field2->getType();
858 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
859 << Field1->getDeclName() << Field1->getType();
860 return false;
861 }
862
863 if (Field1->isBitField() != Field2->isBitField()) {
864 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
865 << Context.C2.getTypeDeclType(Owner2);
866 if (Field1->isBitField()) {
867 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
868 << Field1->getDeclName() << Field1->getType()
869 << Field1->getBitWidthValue(Context.C1);
870 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
871 << Field2->getDeclName();
872 } else {
873 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
874 << Field2->getDeclName() << Field2->getType()
875 << Field2->getBitWidthValue(Context.C2);
876 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
877 << Field1->getDeclName();
878 }
879 return false;
880 }
881
882 if (Field1->isBitField()) {
883 // Make sure that the bit-fields are the same length.
884 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
885 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
886
887 if (Bits1 != Bits2) {
888 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
889 << Context.C2.getTypeDeclType(Owner2);
890 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
891 << Field2->getDeclName() << Field2->getType() << Bits2;
892 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
893 << Field1->getDeclName() << Field1->getType() << Bits1;
894 return false;
895 }
896 }
897
898 return true;
899}
900
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000901/// \brief Determine structural equivalence of two records.
902static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
903 RecordDecl *D1, RecordDecl *D2) {
904 if (D1->isUnion() != D2->isUnion()) {
905 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
906 << Context.C2.getTypeDeclType(D2);
907 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
908 << D1->getDeclName() << (unsigned)D1->getTagKind();
909 return false;
910 }
911
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000912 // If both declarations are class template specializations, we know
913 // the ODR applies, so check the template and template arguments.
914 ClassTemplateSpecializationDecl *Spec1
915 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
916 ClassTemplateSpecializationDecl *Spec2
917 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
918 if (Spec1 && Spec2) {
919 // Check that the specialized templates are the same.
920 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
921 Spec2->getSpecializedTemplate()))
922 return false;
923
924 // Check that the template arguments are the same.
925 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
926 return false;
927
928 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
929 if (!IsStructurallyEquivalent(Context,
930 Spec1->getTemplateArgs().get(I),
931 Spec2->getTemplateArgs().get(I)))
932 return false;
933 }
934 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000935 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000936 else if (Spec1 || Spec2)
937 return false;
938
Douglas Gregorea35d112010-02-15 23:54:17 +0000939 // Compare the definitions of these two records. If either or both are
940 // incomplete, we assume that they are equivalent.
941 D1 = D1->getDefinition();
942 D2 = D2->getDefinition();
943 if (!D1 || !D2)
944 return true;
945
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000946 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
947 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
948 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
949 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000950 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000951 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000952 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000953 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000954 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000955 return false;
956 }
957
958 // Check the base classes.
959 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
960 BaseEnd1 = D1CXX->bases_end(),
961 Base2 = D2CXX->bases_begin();
962 Base1 != BaseEnd1;
963 ++Base1, ++Base2) {
964 if (!IsStructurallyEquivalent(Context,
965 Base1->getType(), Base2->getType())) {
966 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
967 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar96a00142012-03-09 18:35:03 +0000968 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000969 << Base2->getType()
970 << Base2->getSourceRange();
Daniel Dunbar96a00142012-03-09 18:35:03 +0000971 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000972 << Base1->getType()
973 << Base1->getSourceRange();
974 return false;
975 }
976
977 // Check virtual vs. non-virtual inheritance mismatch.
978 if (Base1->isVirtual() != Base2->isVirtual()) {
979 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
980 << Context.C2.getTypeDeclType(D2);
Daniel Dunbar96a00142012-03-09 18:35:03 +0000981 Context.Diag2(Base2->getLocStart(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000982 diag::note_odr_virtual_base)
983 << Base2->isVirtual() << Base2->getSourceRange();
Daniel Dunbar96a00142012-03-09 18:35:03 +0000984 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000985 << Base1->isVirtual()
986 << Base1->getSourceRange();
987 return false;
988 }
989 }
990 } else if (D1CXX->getNumBases() > 0) {
991 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
992 << Context.C2.getTypeDeclType(D2);
993 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
Daniel Dunbar96a00142012-03-09 18:35:03 +0000994 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000995 << Base1->getType()
996 << Base1->getSourceRange();
997 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
998 return false;
999 }
1000 }
1001
1002 // Check the fields for consistency.
1003 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
1004 Field2End = D2->field_end();
1005 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
1006 Field1End = D1->field_end();
1007 Field1 != Field1End;
1008 ++Field1, ++Field2) {
1009 if (Field2 == Field2End) {
1010 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1011 << Context.C2.getTypeDeclType(D2);
1012 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1013 << Field1->getDeclName() << Field1->getType();
1014 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1015 return false;
1016 }
1017
Douglas Gregor7c9412c2011-10-14 21:54:42 +00001018 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
1019 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001020 }
1021
1022 if (Field2 != Field2End) {
1023 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1024 << Context.C2.getTypeDeclType(D2);
1025 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1026 << Field2->getDeclName() << Field2->getType();
1027 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1028 return false;
1029 }
1030
1031 return true;
1032}
1033
1034/// \brief Determine structural equivalence of two enums.
1035static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1036 EnumDecl *D1, EnumDecl *D2) {
1037 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1038 EC2End = D2->enumerator_end();
1039 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1040 EC1End = D1->enumerator_end();
1041 EC1 != EC1End; ++EC1, ++EC2) {
1042 if (EC2 == EC2End) {
1043 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1044 << Context.C2.getTypeDeclType(D2);
1045 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1046 << EC1->getDeclName()
1047 << EC1->getInitVal().toString(10);
1048 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1049 return false;
1050 }
1051
1052 llvm::APSInt Val1 = EC1->getInitVal();
1053 llvm::APSInt Val2 = EC2->getInitVal();
1054 if (!IsSameValue(Val1, Val2) ||
1055 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1056 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1057 << Context.C2.getTypeDeclType(D2);
1058 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1059 << EC2->getDeclName()
1060 << EC2->getInitVal().toString(10);
1061 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1062 << EC1->getDeclName()
1063 << EC1->getInitVal().toString(10);
1064 return false;
1065 }
1066 }
1067
1068 if (EC2 != EC2End) {
1069 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1070 << Context.C2.getTypeDeclType(D2);
1071 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1072 << EC2->getDeclName()
1073 << EC2->getInitVal().toString(10);
1074 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1075 return false;
1076 }
1077
1078 return true;
1079}
Douglas Gregor040afae2010-11-30 19:14:50 +00001080
1081static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1082 TemplateParameterList *Params1,
1083 TemplateParameterList *Params2) {
1084 if (Params1->size() != Params2->size()) {
1085 Context.Diag2(Params2->getTemplateLoc(),
1086 diag::err_odr_different_num_template_parameters)
1087 << Params1->size() << Params2->size();
1088 Context.Diag1(Params1->getTemplateLoc(),
1089 diag::note_odr_template_parameter_list);
1090 return false;
1091 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001092
Douglas Gregor040afae2010-11-30 19:14:50 +00001093 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1094 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1095 Context.Diag2(Params2->getParam(I)->getLocation(),
1096 diag::err_odr_different_template_parameter_kind);
1097 Context.Diag1(Params1->getParam(I)->getLocation(),
1098 diag::note_odr_template_parameter_here);
1099 return false;
1100 }
1101
1102 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1103 Params2->getParam(I))) {
1104
1105 return false;
1106 }
1107 }
1108
1109 return true;
1110}
1111
1112static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1113 TemplateTypeParmDecl *D1,
1114 TemplateTypeParmDecl *D2) {
1115 if (D1->isParameterPack() != D2->isParameterPack()) {
1116 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1117 << D2->isParameterPack();
1118 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1119 << D1->isParameterPack();
1120 return false;
1121 }
1122
1123 return true;
1124}
1125
1126static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1127 NonTypeTemplateParmDecl *D1,
1128 NonTypeTemplateParmDecl *D2) {
1129 // FIXME: Enable once we have variadic templates.
1130#if 0
1131 if (D1->isParameterPack() != D2->isParameterPack()) {
1132 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1133 << D2->isParameterPack();
1134 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1135 << D1->isParameterPack();
1136 return false;
1137 }
1138#endif
1139
1140 // Check types.
1141 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1142 Context.Diag2(D2->getLocation(),
1143 diag::err_odr_non_type_parameter_type_inconsistent)
1144 << D2->getType() << D1->getType();
1145 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1146 << D1->getType();
1147 return false;
1148 }
1149
1150 return true;
1151}
1152
1153static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1154 TemplateTemplateParmDecl *D1,
1155 TemplateTemplateParmDecl *D2) {
1156 // FIXME: Enable once we have variadic templates.
1157#if 0
1158 if (D1->isParameterPack() != D2->isParameterPack()) {
1159 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1160 << D2->isParameterPack();
1161 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1162 << D1->isParameterPack();
1163 return false;
1164 }
1165#endif
1166
1167 // Check template parameter lists.
1168 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1169 D2->getTemplateParameters());
1170}
1171
1172static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1173 ClassTemplateDecl *D1,
1174 ClassTemplateDecl *D2) {
1175 // Check template parameters.
1176 if (!IsStructurallyEquivalent(Context,
1177 D1->getTemplateParameters(),
1178 D2->getTemplateParameters()))
1179 return false;
1180
1181 // Check the templated declaration.
1182 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1183 D2->getTemplatedDecl());
1184}
1185
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001186/// \brief Determine structural equivalence of two declarations.
1187static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1188 Decl *D1, Decl *D2) {
1189 // FIXME: Check for known structural equivalences via a callback of some sort.
1190
Douglas Gregorea35d112010-02-15 23:54:17 +00001191 // Check whether we already know that these two declarations are not
1192 // structurally equivalent.
1193 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1194 D2->getCanonicalDecl())))
1195 return false;
1196
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001197 // Determine whether we've already produced a tentative equivalence for D1.
1198 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1199 if (EquivToD1)
1200 return EquivToD1 == D2->getCanonicalDecl();
1201
1202 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1203 EquivToD1 = D2->getCanonicalDecl();
1204 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1205 return true;
1206}
1207
1208bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1209 Decl *D2) {
1210 if (!::IsStructurallyEquivalent(*this, D1, D2))
1211 return false;
1212
1213 return !Finish();
1214}
1215
1216bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1217 QualType T2) {
1218 if (!::IsStructurallyEquivalent(*this, T1, T2))
1219 return false;
1220
1221 return !Finish();
1222}
1223
1224bool StructuralEquivalenceContext::Finish() {
1225 while (!DeclsToCheck.empty()) {
1226 // Check the next declaration.
1227 Decl *D1 = DeclsToCheck.front();
1228 DeclsToCheck.pop_front();
1229
1230 Decl *D2 = TentativeEquivalences[D1];
1231 assert(D2 && "Unrecorded tentative equivalence?");
1232
Douglas Gregorea35d112010-02-15 23:54:17 +00001233 bool Equivalent = true;
1234
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001235 // FIXME: Switch on all declaration kinds. For now, we're just going to
1236 // check the obvious ones.
1237 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1238 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1239 // Check for equivalent structure names.
1240 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001241 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1242 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001243 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001244 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1245 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001246 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1247 !::IsStructurallyEquivalent(*this, Record1, Record2))
1248 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001249 } else {
1250 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001251 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001252 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001253 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001254 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1255 // Check for equivalent enum names.
1256 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001257 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1258 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001259 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001260 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1261 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001262 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1263 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1264 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001265 } else {
1266 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001267 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001268 }
Richard Smith162e1c12011-04-15 14:24:37 +00001269 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1270 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001271 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001272 Typedef2->getIdentifier()) ||
1273 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001274 Typedef1->getUnderlyingType(),
1275 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001276 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001277 } else {
1278 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001279 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001280 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001281 } else if (ClassTemplateDecl *ClassTemplate1
1282 = dyn_cast<ClassTemplateDecl>(D1)) {
1283 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1284 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1285 ClassTemplate2->getIdentifier()) ||
1286 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1287 Equivalent = false;
1288 } else {
1289 // Class template/non-class-template mismatch.
1290 Equivalent = false;
1291 }
1292 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1293 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1294 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1295 Equivalent = false;
1296 } else {
1297 // Kind mismatch.
1298 Equivalent = false;
1299 }
1300 } else if (NonTypeTemplateParmDecl *NTTP1
1301 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1302 if (NonTypeTemplateParmDecl *NTTP2
1303 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1304 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1305 Equivalent = false;
1306 } else {
1307 // Kind mismatch.
1308 Equivalent = false;
1309 }
1310 } else if (TemplateTemplateParmDecl *TTP1
1311 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1312 if (TemplateTemplateParmDecl *TTP2
1313 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1314 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1315 Equivalent = false;
1316 } else {
1317 // Kind mismatch.
1318 Equivalent = false;
1319 }
1320 }
1321
Douglas Gregorea35d112010-02-15 23:54:17 +00001322 if (!Equivalent) {
1323 // Note that these two declarations are not equivalent (and we already
1324 // know about it).
1325 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1326 D2->getCanonicalDecl()));
1327 return true;
1328 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001329 // FIXME: Check other declaration kinds!
1330 }
1331
1332 return false;
1333}
1334
1335//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001336// Import Types
1337//----------------------------------------------------------------------------
1338
John McCallf4c73712011-01-19 06:33:43 +00001339QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001340 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1341 << T->getTypeClassName();
1342 return QualType();
1343}
1344
John McCallf4c73712011-01-19 06:33:43 +00001345QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001346 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001347#define SHARED_SINGLETON_TYPE(Expansion)
1348#define BUILTIN_TYPE(Id, SingletonId) \
1349 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1350#include "clang/AST/BuiltinTypes.def"
1351
1352 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1353 // context supports C++.
1354
1355 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1356 // context supports ObjC.
1357
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001358 case BuiltinType::Char_U:
1359 // The context we're importing from has an unsigned 'char'. If we're
1360 // importing into a context with a signed 'char', translate to
1361 // 'unsigned char' instead.
1362 if (Importer.getToContext().getLangOptions().CharIsSigned)
1363 return Importer.getToContext().UnsignedCharTy;
1364
1365 return Importer.getToContext().CharTy;
1366
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001367 case BuiltinType::Char_S:
1368 // The context we're importing from has an unsigned 'char'. If we're
1369 // importing into a context with a signed 'char', translate to
1370 // 'unsigned char' instead.
1371 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1372 return Importer.getToContext().SignedCharTy;
1373
1374 return Importer.getToContext().CharTy;
1375
Chris Lattner3f59c972010-12-25 23:25:43 +00001376 case BuiltinType::WChar_S:
1377 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001378 // FIXME: If not in C++, shall we translate to the C equivalent of
1379 // wchar_t?
1380 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001381 }
David Blaikie30263482012-01-20 21:50:17 +00001382
1383 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001384}
1385
John McCallf4c73712011-01-19 06:33:43 +00001386QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001387 QualType ToElementType = Importer.Import(T->getElementType());
1388 if (ToElementType.isNull())
1389 return QualType();
1390
1391 return Importer.getToContext().getComplexType(ToElementType);
1392}
1393
John McCallf4c73712011-01-19 06:33:43 +00001394QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001395 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1396 if (ToPointeeType.isNull())
1397 return QualType();
1398
1399 return Importer.getToContext().getPointerType(ToPointeeType);
1400}
1401
John McCallf4c73712011-01-19 06:33:43 +00001402QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001403 // FIXME: Check for blocks support in "to" context.
1404 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1405 if (ToPointeeType.isNull())
1406 return QualType();
1407
1408 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1409}
1410
John McCallf4c73712011-01-19 06:33:43 +00001411QualType
1412ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001413 // FIXME: Check for C++ support in "to" context.
1414 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1415 if (ToPointeeType.isNull())
1416 return QualType();
1417
1418 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1419}
1420
John McCallf4c73712011-01-19 06:33:43 +00001421QualType
1422ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001423 // FIXME: Check for C++0x support in "to" context.
1424 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1425 if (ToPointeeType.isNull())
1426 return QualType();
1427
1428 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1429}
1430
John McCallf4c73712011-01-19 06:33:43 +00001431QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001432 // FIXME: Check for C++ support in "to" context.
1433 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1434 if (ToPointeeType.isNull())
1435 return QualType();
1436
1437 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1438 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1439 ClassType.getTypePtr());
1440}
1441
John McCallf4c73712011-01-19 06:33:43 +00001442QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001443 QualType ToElementType = Importer.Import(T->getElementType());
1444 if (ToElementType.isNull())
1445 return QualType();
1446
1447 return Importer.getToContext().getConstantArrayType(ToElementType,
1448 T->getSize(),
1449 T->getSizeModifier(),
1450 T->getIndexTypeCVRQualifiers());
1451}
1452
John McCallf4c73712011-01-19 06:33:43 +00001453QualType
1454ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001455 QualType ToElementType = Importer.Import(T->getElementType());
1456 if (ToElementType.isNull())
1457 return QualType();
1458
1459 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1460 T->getSizeModifier(),
1461 T->getIndexTypeCVRQualifiers());
1462}
1463
John McCallf4c73712011-01-19 06:33:43 +00001464QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001465 QualType ToElementType = Importer.Import(T->getElementType());
1466 if (ToElementType.isNull())
1467 return QualType();
1468
1469 Expr *Size = Importer.Import(T->getSizeExpr());
1470 if (!Size)
1471 return QualType();
1472
1473 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1474 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1475 T->getSizeModifier(),
1476 T->getIndexTypeCVRQualifiers(),
1477 Brackets);
1478}
1479
John McCallf4c73712011-01-19 06:33:43 +00001480QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001481 QualType ToElementType = Importer.Import(T->getElementType());
1482 if (ToElementType.isNull())
1483 return QualType();
1484
1485 return Importer.getToContext().getVectorType(ToElementType,
1486 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001487 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001488}
1489
John McCallf4c73712011-01-19 06:33:43 +00001490QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001491 QualType ToElementType = Importer.Import(T->getElementType());
1492 if (ToElementType.isNull())
1493 return QualType();
1494
1495 return Importer.getToContext().getExtVectorType(ToElementType,
1496 T->getNumElements());
1497}
1498
John McCallf4c73712011-01-19 06:33:43 +00001499QualType
1500ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001501 // FIXME: What happens if we're importing a function without a prototype
1502 // into C++? Should we make it variadic?
1503 QualType ToResultType = Importer.Import(T->getResultType());
1504 if (ToResultType.isNull())
1505 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001506
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001507 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001508 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001509}
1510
John McCallf4c73712011-01-19 06:33:43 +00001511QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001512 QualType ToResultType = Importer.Import(T->getResultType());
1513 if (ToResultType.isNull())
1514 return QualType();
1515
1516 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001517 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001518 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1519 AEnd = T->arg_type_end();
1520 A != AEnd; ++A) {
1521 QualType ArgType = Importer.Import(*A);
1522 if (ArgType.isNull())
1523 return QualType();
1524 ArgTypes.push_back(ArgType);
1525 }
1526
1527 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001528 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001529 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1530 EEnd = T->exception_end();
1531 E != EEnd; ++E) {
1532 QualType ExceptionType = Importer.Import(*E);
1533 if (ExceptionType.isNull())
1534 return QualType();
1535 ExceptionTypes.push_back(ExceptionType);
1536 }
John McCalle23cf432010-12-14 08:05:40 +00001537
1538 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1539 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001540
1541 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001542 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001543}
1544
Sean Callanan0aeb2892011-08-11 16:56:07 +00001545QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1546 QualType ToInnerType = Importer.Import(T->getInnerType());
1547 if (ToInnerType.isNull())
1548 return QualType();
1549
1550 return Importer.getToContext().getParenType(ToInnerType);
1551}
1552
John McCallf4c73712011-01-19 06:33:43 +00001553QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001554 TypedefNameDecl *ToDecl
1555 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001556 if (!ToDecl)
1557 return QualType();
1558
1559 return Importer.getToContext().getTypeDeclType(ToDecl);
1560}
1561
John McCallf4c73712011-01-19 06:33:43 +00001562QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001563 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1564 if (!ToExpr)
1565 return QualType();
1566
1567 return Importer.getToContext().getTypeOfExprType(ToExpr);
1568}
1569
John McCallf4c73712011-01-19 06:33:43 +00001570QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001571 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1572 if (ToUnderlyingType.isNull())
1573 return QualType();
1574
1575 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1576}
1577
John McCallf4c73712011-01-19 06:33:43 +00001578QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001579 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001580 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1581 if (!ToExpr)
1582 return QualType();
1583
Douglas Gregorf8af9822012-02-12 18:42:33 +00001584 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1585 if (UnderlyingType.isNull())
1586 return QualType();
1587
1588 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589}
1590
Sean Huntca63c202011-05-24 22:41:36 +00001591QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1592 QualType ToBaseType = Importer.Import(T->getBaseType());
1593 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1594 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1595 return QualType();
1596
1597 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1598 ToUnderlyingType,
1599 T->getUTTKind());
1600}
1601
Richard Smith34b41d92011-02-20 03:19:35 +00001602QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1603 // FIXME: Make sure that the "to" context supports C++0x!
1604 QualType FromDeduced = T->getDeducedType();
1605 QualType ToDeduced;
1606 if (!FromDeduced.isNull()) {
1607 ToDeduced = Importer.Import(FromDeduced);
1608 if (ToDeduced.isNull())
1609 return QualType();
1610 }
1611
1612 return Importer.getToContext().getAutoType(ToDeduced);
1613}
1614
John McCallf4c73712011-01-19 06:33:43 +00001615QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001616 RecordDecl *ToDecl
1617 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1618 if (!ToDecl)
1619 return QualType();
1620
1621 return Importer.getToContext().getTagDeclType(ToDecl);
1622}
1623
John McCallf4c73712011-01-19 06:33:43 +00001624QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001625 EnumDecl *ToDecl
1626 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1627 if (!ToDecl)
1628 return QualType();
1629
1630 return Importer.getToContext().getTagDeclType(ToDecl);
1631}
1632
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001633QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001634 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001635 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1636 if (ToTemplate.isNull())
1637 return QualType();
1638
Chris Lattner5f9e2722011-07-23 10:55:15 +00001639 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001640 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1641 return QualType();
1642
1643 QualType ToCanonType;
1644 if (!QualType(T, 0).isCanonical()) {
1645 QualType FromCanonType
1646 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1647 ToCanonType =Importer.Import(FromCanonType);
1648 if (ToCanonType.isNull())
1649 return QualType();
1650 }
1651 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1652 ToTemplateArgs.data(),
1653 ToTemplateArgs.size(),
1654 ToCanonType);
1655}
1656
John McCallf4c73712011-01-19 06:33:43 +00001657QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001658 NestedNameSpecifier *ToQualifier = 0;
1659 // Note: the qualifier in an ElaboratedType is optional.
1660 if (T->getQualifier()) {
1661 ToQualifier = Importer.Import(T->getQualifier());
1662 if (!ToQualifier)
1663 return QualType();
1664 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001665
1666 QualType ToNamedType = Importer.Import(T->getNamedType());
1667 if (ToNamedType.isNull())
1668 return QualType();
1669
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001670 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1671 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001672}
1673
John McCallf4c73712011-01-19 06:33:43 +00001674QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001675 ObjCInterfaceDecl *Class
1676 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1677 if (!Class)
1678 return QualType();
1679
John McCallc12c5bb2010-05-15 11:32:37 +00001680 return Importer.getToContext().getObjCInterfaceType(Class);
1681}
1682
John McCallf4c73712011-01-19 06:33:43 +00001683QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001684 QualType ToBaseType = Importer.Import(T->getBaseType());
1685 if (ToBaseType.isNull())
1686 return QualType();
1687
Chris Lattner5f9e2722011-07-23 10:55:15 +00001688 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001689 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001690 PEnd = T->qual_end();
1691 P != PEnd; ++P) {
1692 ObjCProtocolDecl *Protocol
1693 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1694 if (!Protocol)
1695 return QualType();
1696 Protocols.push_back(Protocol);
1697 }
1698
John McCallc12c5bb2010-05-15 11:32:37 +00001699 return Importer.getToContext().getObjCObjectType(ToBaseType,
1700 Protocols.data(),
1701 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001702}
1703
John McCallf4c73712011-01-19 06:33:43 +00001704QualType
1705ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001706 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1707 if (ToPointeeType.isNull())
1708 return QualType();
1709
John McCallc12c5bb2010-05-15 11:32:37 +00001710 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001711}
1712
Douglas Gregor089459a2010-02-08 21:09:39 +00001713//----------------------------------------------------------------------------
1714// Import Declarations
1715//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001716bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1717 DeclContext *&LexicalDC,
1718 DeclarationName &Name,
1719 SourceLocation &Loc) {
1720 // Import the context of this declaration.
1721 DC = Importer.ImportContext(D->getDeclContext());
1722 if (!DC)
1723 return true;
1724
1725 LexicalDC = DC;
1726 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1727 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1728 if (!LexicalDC)
1729 return true;
1730 }
1731
1732 // Import the name of this declaration.
1733 Name = Importer.Import(D->getDeclName());
1734 if (D->getDeclName() && !Name)
1735 return true;
1736
1737 // Import the location of this declaration.
1738 Loc = Importer.Import(D->getLocation());
1739 return false;
1740}
1741
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001742void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1743 if (!FromD)
1744 return;
1745
1746 if (!ToD) {
1747 ToD = Importer.Import(FromD);
1748 if (!ToD)
1749 return;
1750 }
1751
1752 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1753 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1754 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1755 ImportDefinition(FromRecord, ToRecord);
1756 }
1757 }
1758 return;
1759 }
1760
1761 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1762 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1763 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1764 ImportDefinition(FromEnum, ToEnum);
1765 }
1766 }
1767 return;
1768 }
1769}
1770
Abramo Bagnara25777432010-08-11 22:01:17 +00001771void
1772ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1773 DeclarationNameInfo& To) {
1774 // NOTE: To.Name and To.Loc are already imported.
1775 // We only have to import To.LocInfo.
1776 switch (To.getName().getNameKind()) {
1777 case DeclarationName::Identifier:
1778 case DeclarationName::ObjCZeroArgSelector:
1779 case DeclarationName::ObjCOneArgSelector:
1780 case DeclarationName::ObjCMultiArgSelector:
1781 case DeclarationName::CXXUsingDirective:
1782 return;
1783
1784 case DeclarationName::CXXOperatorName: {
1785 SourceRange Range = From.getCXXOperatorNameRange();
1786 To.setCXXOperatorNameRange(Importer.Import(Range));
1787 return;
1788 }
1789 case DeclarationName::CXXLiteralOperatorName: {
1790 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1791 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1792 return;
1793 }
1794 case DeclarationName::CXXConstructorName:
1795 case DeclarationName::CXXDestructorName:
1796 case DeclarationName::CXXConversionFunctionName: {
1797 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1798 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1799 return;
1800 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001801 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001802 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001803}
1804
Douglas Gregorac32ff92012-02-01 21:00:38 +00001805void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregord8868a62011-01-18 03:11:38 +00001806 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001807 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001808 return;
1809 }
1810
Douglas Gregor083a8212010-02-21 18:24:45 +00001811 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1812 FromEnd = FromDC->decls_end();
1813 From != FromEnd;
1814 ++From)
1815 Importer.Import(*From);
1816}
1817
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001818bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001819 ImportDefinitionKind Kind) {
1820 if (To->getDefinition() || To->isBeingDefined()) {
1821 if (Kind == IDK_Everything)
1822 ImportDeclContext(From, /*ForceImport=*/true);
1823
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001824 return false;
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001825 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001826
1827 To->startDefinition();
1828
1829 // Add base classes.
1830 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1831 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001832
1833 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1834 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1835 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1836 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1837 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1838 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1839 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1840 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1841 ToData.Aggregate = FromData.Aggregate;
1842 ToData.PlainOldData = FromData.PlainOldData;
1843 ToData.Empty = FromData.Empty;
1844 ToData.Polymorphic = FromData.Polymorphic;
1845 ToData.Abstract = FromData.Abstract;
1846 ToData.IsStandardLayout = FromData.IsStandardLayout;
1847 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1848 ToData.HasPrivateFields = FromData.HasPrivateFields;
1849 ToData.HasProtectedFields = FromData.HasProtectedFields;
1850 ToData.HasPublicFields = FromData.HasPublicFields;
1851 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithdfefb842012-02-25 07:33:38 +00001852 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001853 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1854 ToData.HasConstexprNonCopyMoveConstructor
1855 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smithdfefb842012-02-25 07:33:38 +00001856 ToData.DefaultedDefaultConstructorIsConstexpr
1857 = FromData.DefaultedDefaultConstructorIsConstexpr;
1858 ToData.DefaultedCopyConstructorIsConstexpr
1859 = FromData.DefaultedCopyConstructorIsConstexpr;
1860 ToData.DefaultedMoveConstructorIsConstexpr
1861 = FromData.DefaultedMoveConstructorIsConstexpr;
1862 ToData.HasConstexprDefaultConstructor
1863 = FromData.HasConstexprDefaultConstructor;
1864 ToData.HasConstexprCopyConstructor = FromData.HasConstexprCopyConstructor;
1865 ToData.HasConstexprMoveConstructor = FromData.HasConstexprMoveConstructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001866 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1867 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1868 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1869 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1870 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
Richard Smithdfefb842012-02-25 07:33:38 +00001871 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor27c72d82011-11-03 18:07:07 +00001872 ToData.HasNonLiteralTypeFieldsOrBases
1873 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smithdfefb842012-02-25 07:33:38 +00001874 // ComputedVisibleConversions not imported.
Douglas Gregor27c72d82011-11-03 18:07:07 +00001875 ToData.UserProvidedDefaultConstructor
1876 = FromData.UserProvidedDefaultConstructor;
1877 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1878 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1879 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1880 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1881 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1882 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1883 ToData.FailedImplicitMoveConstructor
1884 = FromData.FailedImplicitMoveConstructor;
1885 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smithdfefb842012-02-25 07:33:38 +00001886 ToData.IsLambda = FromData.IsLambda;
1887
Chris Lattner5f9e2722011-07-23 10:55:15 +00001888 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001889 for (CXXRecordDecl::base_class_iterator
1890 Base1 = FromCXX->bases_begin(),
1891 FromBaseEnd = FromCXX->bases_end();
1892 Base1 != FromBaseEnd;
1893 ++Base1) {
1894 QualType T = Importer.Import(Base1->getType());
1895 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001896 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001897
1898 SourceLocation EllipsisLoc;
1899 if (Base1->isPackExpansion())
1900 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001901
1902 // Ensure that we have a definition for the base.
1903 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1904
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001905 Bases.push_back(
1906 new (Importer.getToContext())
1907 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1908 Base1->isVirtual(),
1909 Base1->isBaseOfClass(),
1910 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001911 Importer.Import(Base1->getTypeSourceInfo()),
1912 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001913 }
1914 if (!Bases.empty())
1915 ToCXX->setBases(Bases.data(), Bases.size());
1916 }
1917
Douglas Gregorac32ff92012-02-01 21:00:38 +00001918 if (shouldForceImportDeclContext(Kind))
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001919 ImportDeclContext(From, /*ForceImport=*/true);
1920
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001921 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001922 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001923}
1924
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001925bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00001926 ImportDefinitionKind Kind) {
1927 if (To->getDefinition() || To->isBeingDefined()) {
1928 if (Kind == IDK_Everything)
1929 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001930 return false;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001931 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001932
1933 To->startDefinition();
1934
1935 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1936 if (T.isNull())
1937 return true;
1938
1939 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1940 if (ToPromotionType.isNull())
1941 return true;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001942
1943 if (shouldForceImportDeclContext(Kind))
1944 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001945
1946 // FIXME: we might need to merge the number of positive or negative bits
1947 // if the enumerator lists don't match.
1948 To->completeDefinition(T, ToPromotionType,
1949 From->getNumPositiveBits(),
1950 From->getNumNegativeBits());
1951 return false;
1952}
1953
Douglas Gregor040afae2010-11-30 19:14:50 +00001954TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1955 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001956 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001957 ToParams.reserve(Params->size());
1958 for (TemplateParameterList::iterator P = Params->begin(),
1959 PEnd = Params->end();
1960 P != PEnd; ++P) {
1961 Decl *To = Importer.Import(*P);
1962 if (!To)
1963 return 0;
1964
1965 ToParams.push_back(cast<NamedDecl>(To));
1966 }
1967
1968 return TemplateParameterList::Create(Importer.getToContext(),
1969 Importer.Import(Params->getTemplateLoc()),
1970 Importer.Import(Params->getLAngleLoc()),
1971 ToParams.data(), ToParams.size(),
1972 Importer.Import(Params->getRAngleLoc()));
1973}
1974
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001975TemplateArgument
1976ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1977 switch (From.getKind()) {
1978 case TemplateArgument::Null:
1979 return TemplateArgument();
1980
1981 case TemplateArgument::Type: {
1982 QualType ToType = Importer.Import(From.getAsType());
1983 if (ToType.isNull())
1984 return TemplateArgument();
1985 return TemplateArgument(ToType);
1986 }
1987
1988 case TemplateArgument::Integral: {
1989 QualType ToType = Importer.Import(From.getIntegralType());
1990 if (ToType.isNull())
1991 return TemplateArgument();
1992 return TemplateArgument(*From.getAsIntegral(), ToType);
1993 }
1994
1995 case TemplateArgument::Declaration:
1996 if (Decl *To = Importer.Import(From.getAsDecl()))
1997 return TemplateArgument(To);
1998 return TemplateArgument();
1999
2000 case TemplateArgument::Template: {
2001 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2002 if (ToTemplate.isNull())
2003 return TemplateArgument();
2004
2005 return TemplateArgument(ToTemplate);
2006 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00002007
2008 case TemplateArgument::TemplateExpansion: {
2009 TemplateName ToTemplate
2010 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2011 if (ToTemplate.isNull())
2012 return TemplateArgument();
2013
Douglas Gregor2be29f42011-01-14 23:41:42 +00002014 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00002015 }
2016
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002017 case TemplateArgument::Expression:
2018 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2019 return TemplateArgument(ToExpr);
2020 return TemplateArgument();
2021
2022 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002023 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002024 ToPack.reserve(From.pack_size());
2025 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2026 return TemplateArgument();
2027
2028 TemplateArgument *ToArgs
2029 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2030 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2031 return TemplateArgument(ToArgs, ToPack.size());
2032 }
2033 }
2034
2035 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002036}
2037
2038bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2039 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002040 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002041 for (unsigned I = 0; I != NumFromArgs; ++I) {
2042 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2043 if (To.isNull() && !FromArgs[I].isNull())
2044 return true;
2045
2046 ToArgs.push_back(To);
2047 }
2048
2049 return false;
2050}
2051
Douglas Gregor96a01b42010-02-11 00:48:18 +00002052bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002053 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002054 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002055 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002056 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002057 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002058}
2059
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002060bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002061 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002062 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002063 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002064 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002065}
2066
Douglas Gregor040afae2010-11-30 19:14:50 +00002067bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2068 ClassTemplateDecl *To) {
2069 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2070 Importer.getToContext(),
2071 Importer.getNonEquivalentDecls());
2072 return Ctx.IsStructurallyEquivalent(From, To);
2073}
2074
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002075Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002076 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002077 << D->getDeclKindName();
2078 return 0;
2079}
2080
Sean Callananf1b69462011-11-17 23:20:56 +00002081Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2082 TranslationUnitDecl *ToD =
2083 Importer.getToContext().getTranslationUnitDecl();
2084
2085 Importer.Imported(D, ToD);
2086
2087 return ToD;
2088}
2089
Douglas Gregor788c62d2010-02-21 18:26:36 +00002090Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2091 // Import the major distinguishing characteristics of this namespace.
2092 DeclContext *DC, *LexicalDC;
2093 DeclarationName Name;
2094 SourceLocation Loc;
2095 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2096 return 0;
2097
2098 NamespaceDecl *MergeWithNamespace = 0;
2099 if (!Name) {
2100 // This is an anonymous namespace. Adopt an existing anonymous
2101 // namespace if we can.
2102 // FIXME: Not testable.
2103 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2104 MergeWithNamespace = TU->getAnonymousNamespace();
2105 else
2106 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2107 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002108 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002109 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2110 DC->localUncachedLookup(Name, FoundDecls);
2111 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2112 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002113 continue;
2114
Douglas Gregorb75a3452011-10-15 00:10:27 +00002115 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002116 MergeWithNamespace = FoundNS;
2117 ConflictingDecls.clear();
2118 break;
2119 }
2120
Douglas Gregorb75a3452011-10-15 00:10:27 +00002121 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002122 }
2123
2124 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002125 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002126 ConflictingDecls.data(),
2127 ConflictingDecls.size());
2128 }
2129 }
2130
2131 // Create the "to" namespace, if needed.
2132 NamespaceDecl *ToNamespace = MergeWithNamespace;
2133 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002134 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002135 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002136 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002137 Loc, Name.getAsIdentifierInfo(),
2138 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002139 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002140 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002141
2142 // If this is an anonymous namespace, register it as the anonymous
2143 // namespace within its context.
2144 if (!Name) {
2145 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2146 TU->setAnonymousNamespace(ToNamespace);
2147 else
2148 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2149 }
2150 }
2151 Importer.Imported(D, ToNamespace);
2152
2153 ImportDeclContext(D);
2154
2155 return ToNamespace;
2156}
2157
Richard Smith162e1c12011-04-15 14:24:37 +00002158Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002159 // Import the major distinguishing characteristics of this typedef.
2160 DeclContext *DC, *LexicalDC;
2161 DeclarationName Name;
2162 SourceLocation Loc;
2163 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2164 return 0;
2165
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002166 // If this typedef is not in block scope, determine whether we've
2167 // seen a typedef with the same name (that we can merge with) or any
2168 // other entity by that name (which name lookup could conflict with).
2169 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002170 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002171 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002172 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2173 DC->localUncachedLookup(Name, FoundDecls);
2174 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2175 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002176 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002177 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002178 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002179 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2180 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002181 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002182 }
2183
Douglas Gregorb75a3452011-10-15 00:10:27 +00002184 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002185 }
2186
2187 if (!ConflictingDecls.empty()) {
2188 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2189 ConflictingDecls.data(),
2190 ConflictingDecls.size());
2191 if (!Name)
2192 return 0;
2193 }
2194 }
2195
Douglas Gregorea35d112010-02-15 23:54:17 +00002196 // Import the underlying type of this typedef;
2197 QualType T = Importer.Import(D->getUnderlyingType());
2198 if (T.isNull())
2199 return 0;
2200
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002201 // Create the new typedef node.
2202 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002203 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002204 TypedefNameDecl *ToTypedef;
2205 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002206 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2207 StartL, Loc,
2208 Name.getAsIdentifierInfo(),
2209 TInfo);
2210 else
Richard Smith162e1c12011-04-15 14:24:37 +00002211 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2212 StartL, Loc,
2213 Name.getAsIdentifierInfo(),
2214 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002215
Douglas Gregor325bf172010-02-22 17:42:47 +00002216 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002217 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002218 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002219 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002220
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002221 return ToTypedef;
2222}
2223
Richard Smith162e1c12011-04-15 14:24:37 +00002224Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2225 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2226}
2227
2228Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2229 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2230}
2231
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002232Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2233 // Import the major distinguishing characteristics of this enum.
2234 DeclContext *DC, *LexicalDC;
2235 DeclarationName Name;
2236 SourceLocation Loc;
2237 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2238 return 0;
2239
2240 // Figure out what enum name we're looking for.
2241 unsigned IDNS = Decl::IDNS_Tag;
2242 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002243 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2244 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002245 IDNS = Decl::IDNS_Ordinary;
2246 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2247 IDNS |= Decl::IDNS_Ordinary;
2248
2249 // We may already have an enum of the same name; try to find and match it.
2250 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002251 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002252 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2253 DC->localUncachedLookup(SearchName, FoundDecls);
2254 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2255 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002256 continue;
2257
Douglas Gregorb75a3452011-10-15 00:10:27 +00002258 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002259 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002260 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2261 Found = Tag->getDecl();
2262 }
2263
2264 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002265 if (IsStructuralMatch(D, FoundEnum))
2266 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002267 }
2268
Douglas Gregorb75a3452011-10-15 00:10:27 +00002269 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002270 }
2271
2272 if (!ConflictingDecls.empty()) {
2273 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2274 ConflictingDecls.data(),
2275 ConflictingDecls.size());
2276 }
2277 }
2278
2279 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002280 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2281 Importer.Import(D->getLocStart()),
2282 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002283 D->isScoped(), D->isScopedUsingClassTag(),
2284 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002285 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002286 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002287 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002288 D2->setLexicalDeclContext(LexicalDC);
2289 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002290 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002291
2292 // Import the integer type.
2293 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2294 if (ToIntegerType.isNull())
2295 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002296 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002297
2298 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002299 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002300 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002301
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002302 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002303}
2304
Douglas Gregor96a01b42010-02-11 00:48:18 +00002305Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2306 // If this record has a definition in the translation unit we're coming from,
2307 // but this particular declaration is not that definition, import the
2308 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002309 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002310 if (Definition && Definition != D) {
2311 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002312 if (!ImportedDef)
2313 return 0;
2314
2315 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002316 }
2317
2318 // Import the major distinguishing characteristics of this record.
2319 DeclContext *DC, *LexicalDC;
2320 DeclarationName Name;
2321 SourceLocation Loc;
2322 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2323 return 0;
2324
2325 // Figure out what structure name we're looking for.
2326 unsigned IDNS = Decl::IDNS_Tag;
2327 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002328 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2329 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002330 IDNS = Decl::IDNS_Ordinary;
2331 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2332 IDNS |= Decl::IDNS_Ordinary;
2333
2334 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002335 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002336 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002337 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002338 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2339 DC->localUncachedLookup(SearchName, FoundDecls);
2340 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2341 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002342 continue;
2343
Douglas Gregorb75a3452011-10-15 00:10:27 +00002344 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002345 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002346 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2347 Found = Tag->getDecl();
2348 }
2349
2350 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002351 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002352 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002353 // The record types structurally match, or the "from" translation
2354 // unit only had a forward declaration anyway; call it the same
2355 // function.
2356 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002357 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002358 }
2359 } else {
2360 // We have a forward declaration of this type, so adopt that forward
2361 // declaration rather than building a new one.
2362 AdoptDecl = FoundRecord;
2363 continue;
2364 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002365 }
2366
Douglas Gregorb75a3452011-10-15 00:10:27 +00002367 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002368 }
2369
2370 if (!ConflictingDecls.empty()) {
2371 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2372 ConflictingDecls.data(),
2373 ConflictingDecls.size());
2374 }
2375 }
2376
2377 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002378 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002379 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002380 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002381 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002382 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002383 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002384 DC, StartLoc, Loc,
2385 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002386 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002387 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002388 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002389 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002390 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002391 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002392
2393 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002394 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002395 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002396 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002397
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002398 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002399
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002400 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002401 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002402
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002403 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002404}
2405
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002406Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2407 // Import the major distinguishing characteristics of this enumerator.
2408 DeclContext *DC, *LexicalDC;
2409 DeclarationName Name;
2410 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002411 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002412 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002413
2414 QualType T = Importer.Import(D->getType());
2415 if (T.isNull())
2416 return 0;
2417
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002418 // Determine whether there are any other declarations with the same name and
2419 // in the same context.
2420 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002421 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002422 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002423 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2424 DC->localUncachedLookup(Name, FoundDecls);
2425 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2426 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002427 continue;
2428
Douglas Gregorb75a3452011-10-15 00:10:27 +00002429 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002430 }
2431
2432 if (!ConflictingDecls.empty()) {
2433 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2434 ConflictingDecls.data(),
2435 ConflictingDecls.size());
2436 if (!Name)
2437 return 0;
2438 }
2439 }
2440
2441 Expr *Init = Importer.Import(D->getInitExpr());
2442 if (D->getInitExpr() && !Init)
2443 return 0;
2444
2445 EnumConstantDecl *ToEnumerator
2446 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2447 Name.getAsIdentifierInfo(), T,
2448 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002449 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002450 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002451 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002452 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002453 return ToEnumerator;
2454}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002455
Douglas Gregora404ea62010-02-10 19:54:31 +00002456Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2457 // Import the major distinguishing characteristics of this function.
2458 DeclContext *DC, *LexicalDC;
2459 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002460 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002461 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002462 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002463
Douglas Gregora404ea62010-02-10 19:54:31 +00002464 // Try to find a function in our own ("to") context with the same name, same
2465 // type, and in the same context as the function we're importing.
2466 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002467 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002468 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002469 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2470 DC->localUncachedLookup(Name, FoundDecls);
2471 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2472 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002473 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002474
Douglas Gregorb75a3452011-10-15 00:10:27 +00002475 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002476 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2477 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002478 if (Importer.IsStructurallyEquivalent(D->getType(),
2479 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002480 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002481 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002482 }
2483
2484 // FIXME: Check for overloading more carefully, e.g., by boosting
2485 // Sema::IsOverload out to the AST library.
2486
2487 // Function overloading is okay in C++.
2488 if (Importer.getToContext().getLangOptions().CPlusPlus)
2489 continue;
2490
2491 // Complain about inconsistent function types.
2492 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002493 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002494 Importer.ToDiag(FoundFunction->getLocation(),
2495 diag::note_odr_value_here)
2496 << FoundFunction->getType();
2497 }
2498 }
2499
Douglas Gregorb75a3452011-10-15 00:10:27 +00002500 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002501 }
2502
2503 if (!ConflictingDecls.empty()) {
2504 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2505 ConflictingDecls.data(),
2506 ConflictingDecls.size());
2507 if (!Name)
2508 return 0;
2509 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002510 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002511
Abramo Bagnara25777432010-08-11 22:01:17 +00002512 DeclarationNameInfo NameInfo(Name, Loc);
2513 // Import additional name location/type info.
2514 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2515
Douglas Gregorea35d112010-02-15 23:54:17 +00002516 // Import the type.
2517 QualType T = Importer.Import(D->getType());
2518 if (T.isNull())
2519 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002520
2521 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002522 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002523 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2524 P != PEnd; ++P) {
2525 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2526 if (!ToP)
2527 return 0;
2528
2529 Parameters.push_back(ToP);
2530 }
2531
2532 // Create the imported function.
2533 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002534 FunctionDecl *ToFunction = 0;
2535 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2536 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2537 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002538 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002539 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002540 FromConstructor->isExplicit(),
2541 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002542 D->isImplicit(),
2543 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002544 } else if (isa<CXXDestructorDecl>(D)) {
2545 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2546 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002547 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002548 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002549 D->isInlineSpecified(),
2550 D->isImplicit());
2551 } else if (CXXConversionDecl *FromConversion
2552 = dyn_cast<CXXConversionDecl>(D)) {
2553 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2554 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002555 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002556 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002557 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002558 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002559 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002560 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002561 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2562 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2563 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002564 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002565 NameInfo, T, TInfo,
2566 Method->isStatic(),
2567 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002568 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002569 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002570 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002571 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002572 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002573 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002574 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002575 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002576 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002577 D->hasWrittenPrototype(),
2578 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002579 }
John McCallb6217662010-03-15 10:12:16 +00002580
2581 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002582 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002583 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002584 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002585 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2586 ToFunction->setTrivial(D->isTrivial());
2587 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002588 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002589
Douglas Gregora404ea62010-02-10 19:54:31 +00002590 // Set the parameters.
2591 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002592 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002593 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002594 }
David Blaikie4278c652011-09-21 18:16:56 +00002595 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002596
2597 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002598
2599 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002600 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002601
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002602 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002603}
2604
Douglas Gregorc144f352010-02-21 18:29:16 +00002605Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2606 return VisitFunctionDecl(D);
2607}
2608
2609Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2610 return VisitCXXMethodDecl(D);
2611}
2612
2613Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2614 return VisitCXXMethodDecl(D);
2615}
2616
2617Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2618 return VisitCXXMethodDecl(D);
2619}
2620
Douglas Gregor96a01b42010-02-11 00:48:18 +00002621Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2622 // Import the major distinguishing characteristics of a variable.
2623 DeclContext *DC, *LexicalDC;
2624 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002625 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002626 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2627 return 0;
2628
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002629 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002630 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2631 DC->localUncachedLookup(Name, FoundDecls);
2632 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2633 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002634 if (Importer.IsStructurallyEquivalent(D->getType(),
2635 FoundField->getType())) {
2636 Importer.Imported(D, FoundField);
2637 return FoundField;
2638 }
2639
2640 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2641 << Name << D->getType() << FoundField->getType();
2642 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2643 << FoundField->getType();
2644 return 0;
2645 }
2646 }
2647
Douglas Gregorea35d112010-02-15 23:54:17 +00002648 // Import the type.
2649 QualType T = Importer.Import(D->getType());
2650 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002651 return 0;
2652
2653 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2654 Expr *BitWidth = Importer.Import(D->getBitWidth());
2655 if (!BitWidth && D->getBitWidth())
2656 return 0;
2657
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002658 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2659 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002660 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002661 T, TInfo, BitWidth, D->isMutable(),
2662 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002663 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002664 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002665 if (ToField->hasInClassInitializer())
2666 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002667 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002668 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002669 return ToField;
2670}
2671
Francois Pichet87c2e122010-11-21 06:08:52 +00002672Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2673 // Import the major distinguishing characteristics of a variable.
2674 DeclContext *DC, *LexicalDC;
2675 DeclarationName Name;
2676 SourceLocation Loc;
2677 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2678 return 0;
2679
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002680 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002681 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2682 DC->localUncachedLookup(Name, FoundDecls);
2683 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002684 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002685 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002686 if (Importer.IsStructurallyEquivalent(D->getType(),
2687 FoundField->getType())) {
2688 Importer.Imported(D, FoundField);
2689 return FoundField;
2690 }
2691
2692 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2693 << Name << D->getType() << FoundField->getType();
2694 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2695 << FoundField->getType();
2696 return 0;
2697 }
2698 }
2699
Francois Pichet87c2e122010-11-21 06:08:52 +00002700 // Import the type.
2701 QualType T = Importer.Import(D->getType());
2702 if (T.isNull())
2703 return 0;
2704
2705 NamedDecl **NamedChain =
2706 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2707
2708 unsigned i = 0;
2709 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2710 PE = D->chain_end(); PI != PE; ++PI) {
2711 Decl* D = Importer.Import(*PI);
2712 if (!D)
2713 return 0;
2714 NamedChain[i++] = cast<NamedDecl>(D);
2715 }
2716
2717 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2718 Importer.getToContext(), DC,
2719 Loc, Name.getAsIdentifierInfo(), T,
2720 NamedChain, D->getChainingSize());
2721 ToIndirectField->setAccess(D->getAccess());
2722 ToIndirectField->setLexicalDeclContext(LexicalDC);
2723 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002724 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002725 return ToIndirectField;
2726}
2727
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002728Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2729 // Import the major distinguishing characteristics of an ivar.
2730 DeclContext *DC, *LexicalDC;
2731 DeclarationName Name;
2732 SourceLocation Loc;
2733 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2734 return 0;
2735
2736 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002737 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2738 DC->localUncachedLookup(Name, FoundDecls);
2739 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2740 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002741 if (Importer.IsStructurallyEquivalent(D->getType(),
2742 FoundIvar->getType())) {
2743 Importer.Imported(D, FoundIvar);
2744 return FoundIvar;
2745 }
2746
2747 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2748 << Name << D->getType() << FoundIvar->getType();
2749 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2750 << FoundIvar->getType();
2751 return 0;
2752 }
2753 }
2754
2755 // Import the type.
2756 QualType T = Importer.Import(D->getType());
2757 if (T.isNull())
2758 return 0;
2759
2760 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2761 Expr *BitWidth = Importer.Import(D->getBitWidth());
2762 if (!BitWidth && D->getBitWidth())
2763 return 0;
2764
Daniel Dunbara0654922010-04-02 20:10:03 +00002765 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2766 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002767 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002768 Loc, Name.getAsIdentifierInfo(),
2769 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002770 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002771 ToIvar->setLexicalDeclContext(LexicalDC);
2772 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002773 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002774 return ToIvar;
2775
2776}
2777
Douglas Gregora404ea62010-02-10 19:54:31 +00002778Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2779 // Import the major distinguishing characteristics of a variable.
2780 DeclContext *DC, *LexicalDC;
2781 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002782 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002783 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002784 return 0;
2785
Douglas Gregor089459a2010-02-08 21:09:39 +00002786 // Try to find a variable in our own ("to") context with the same name and
2787 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002788 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002789 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002790 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002791 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002792 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2793 DC->localUncachedLookup(Name, FoundDecls);
2794 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2795 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002796 continue;
2797
Douglas Gregorb75a3452011-10-15 00:10:27 +00002798 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002799 // We have found a variable that we may need to merge with. Check it.
2800 if (isExternalLinkage(FoundVar->getLinkage()) &&
2801 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002802 if (Importer.IsStructurallyEquivalent(D->getType(),
2803 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002804 MergeWithVar = FoundVar;
2805 break;
2806 }
2807
Douglas Gregord0145422010-02-12 17:23:39 +00002808 const ArrayType *FoundArray
2809 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2810 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002811 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002812 if (FoundArray && TArray) {
2813 if (isa<IncompleteArrayType>(FoundArray) &&
2814 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002815 // Import the type.
2816 QualType T = Importer.Import(D->getType());
2817 if (T.isNull())
2818 return 0;
2819
Douglas Gregord0145422010-02-12 17:23:39 +00002820 FoundVar->setType(T);
2821 MergeWithVar = FoundVar;
2822 break;
2823 } else if (isa<IncompleteArrayType>(TArray) &&
2824 isa<ConstantArrayType>(FoundArray)) {
2825 MergeWithVar = FoundVar;
2826 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002827 }
2828 }
2829
Douglas Gregor089459a2010-02-08 21:09:39 +00002830 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002831 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002832 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2833 << FoundVar->getType();
2834 }
2835 }
2836
Douglas Gregorb75a3452011-10-15 00:10:27 +00002837 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002838 }
2839
2840 if (MergeWithVar) {
2841 // An equivalent variable with external linkage has been found. Link
2842 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002843 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002844
2845 if (VarDecl *DDef = D->getDefinition()) {
2846 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2847 Importer.ToDiag(ExistingDef->getLocation(),
2848 diag::err_odr_variable_multiple_def)
2849 << Name;
2850 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2851 } else {
2852 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002853 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002854 if (DDef->isInitKnownICE()) {
2855 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2856 Eval->CheckedICE = true;
2857 Eval->IsICE = DDef->isInitICE();
2858 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002859 }
2860 }
2861
2862 return MergeWithVar;
2863 }
2864
2865 if (!ConflictingDecls.empty()) {
2866 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2867 ConflictingDecls.data(),
2868 ConflictingDecls.size());
2869 if (!Name)
2870 return 0;
2871 }
2872 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002873
Douglas Gregorea35d112010-02-15 23:54:17 +00002874 // Import the type.
2875 QualType T = Importer.Import(D->getType());
2876 if (T.isNull())
2877 return 0;
2878
Douglas Gregor089459a2010-02-08 21:09:39 +00002879 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002880 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002881 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2882 Importer.Import(D->getInnerLocStart()),
2883 Loc, Name.getAsIdentifierInfo(),
2884 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002885 D->getStorageClass(),
2886 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002887 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002888 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002889 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002890 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002891 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002892
Douglas Gregor089459a2010-02-08 21:09:39 +00002893 // Merge the initializer.
2894 // FIXME: Can we really import any initializer? Alternatively, we could force
2895 // ourselves to import every declaration of a variable and then only use
2896 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002897 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002898
2899 // FIXME: Other bits to merge?
2900
2901 return ToVar;
2902}
2903
Douglas Gregor2cd00932010-02-17 21:22:52 +00002904Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2905 // Parameters are created in the translation unit's context, then moved
2906 // into the function declaration's context afterward.
2907 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2908
2909 // Import the name of this declaration.
2910 DeclarationName Name = Importer.Import(D->getDeclName());
2911 if (D->getDeclName() && !Name)
2912 return 0;
2913
2914 // Import the location of this declaration.
2915 SourceLocation Loc = Importer.Import(D->getLocation());
2916
2917 // Import the parameter's type.
2918 QualType T = Importer.Import(D->getType());
2919 if (T.isNull())
2920 return 0;
2921
2922 // Create the imported parameter.
2923 ImplicitParamDecl *ToParm
2924 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2925 Loc, Name.getAsIdentifierInfo(),
2926 T);
2927 return Importer.Imported(D, ToParm);
2928}
2929
Douglas Gregora404ea62010-02-10 19:54:31 +00002930Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2931 // Parameters are created in the translation unit's context, then moved
2932 // into the function declaration's context afterward.
2933 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2934
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002935 // Import the name of this declaration.
2936 DeclarationName Name = Importer.Import(D->getDeclName());
2937 if (D->getDeclName() && !Name)
2938 return 0;
2939
Douglas Gregora404ea62010-02-10 19:54:31 +00002940 // Import the location of this declaration.
2941 SourceLocation Loc = Importer.Import(D->getLocation());
2942
2943 // Import the parameter's type.
2944 QualType T = Importer.Import(D->getType());
2945 if (T.isNull())
2946 return 0;
2947
2948 // Create the imported parameter.
2949 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2950 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002951 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002952 Loc, Name.getAsIdentifierInfo(),
2953 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002954 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002955 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002956 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002957 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002958}
2959
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002960Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2961 // Import the major distinguishing characteristics of a method.
2962 DeclContext *DC, *LexicalDC;
2963 DeclarationName Name;
2964 SourceLocation Loc;
2965 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2966 return 0;
2967
Douglas Gregorb75a3452011-10-15 00:10:27 +00002968 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2969 DC->localUncachedLookup(Name, FoundDecls);
2970 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2971 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002972 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2973 continue;
2974
2975 // Check return types.
2976 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2977 FoundMethod->getResultType())) {
2978 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2979 << D->isInstanceMethod() << Name
2980 << D->getResultType() << FoundMethod->getResultType();
2981 Importer.ToDiag(FoundMethod->getLocation(),
2982 diag::note_odr_objc_method_here)
2983 << D->isInstanceMethod() << Name;
2984 return 0;
2985 }
2986
2987 // Check the number of parameters.
2988 if (D->param_size() != FoundMethod->param_size()) {
2989 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2990 << D->isInstanceMethod() << Name
2991 << D->param_size() << FoundMethod->param_size();
2992 Importer.ToDiag(FoundMethod->getLocation(),
2993 diag::note_odr_objc_method_here)
2994 << D->isInstanceMethod() << Name;
2995 return 0;
2996 }
2997
2998 // Check parameter types.
2999 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3000 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3001 P != PEnd; ++P, ++FoundP) {
3002 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3003 (*FoundP)->getType())) {
3004 Importer.FromDiag((*P)->getLocation(),
3005 diag::err_odr_objc_method_param_type_inconsistent)
3006 << D->isInstanceMethod() << Name
3007 << (*P)->getType() << (*FoundP)->getType();
3008 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3009 << (*FoundP)->getType();
3010 return 0;
3011 }
3012 }
3013
3014 // Check variadic/non-variadic.
3015 // Check the number of parameters.
3016 if (D->isVariadic() != FoundMethod->isVariadic()) {
3017 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3018 << D->isInstanceMethod() << Name;
3019 Importer.ToDiag(FoundMethod->getLocation(),
3020 diag::note_odr_objc_method_here)
3021 << D->isInstanceMethod() << Name;
3022 return 0;
3023 }
3024
3025 // FIXME: Any other bits we need to merge?
3026 return Importer.Imported(D, FoundMethod);
3027 }
3028 }
3029
3030 // Import the result type.
3031 QualType ResultTy = Importer.Import(D->getResultType());
3032 if (ResultTy.isNull())
3033 return 0;
3034
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003035 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3036
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003037 ObjCMethodDecl *ToMethod
3038 = ObjCMethodDecl::Create(Importer.getToContext(),
3039 Loc,
3040 Importer.Import(D->getLocEnd()),
3041 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003042 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003043 D->isInstanceMethod(),
3044 D->isVariadic(),
3045 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003046 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003047 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00003048 D->getImplementationControl(),
3049 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003050
3051 // FIXME: When we decide to merge method definitions, we'll need to
3052 // deal with implicit parameters.
3053
3054 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003055 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003056 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3057 FromPEnd = D->param_end();
3058 FromP != FromPEnd;
3059 ++FromP) {
3060 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3061 if (!ToP)
3062 return 0;
3063
3064 ToParams.push_back(ToP);
3065 }
3066
3067 // Set the parameters.
3068 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3069 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003070 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003071 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003072 SmallVector<SourceLocation, 12> SelLocs;
3073 D->getSelectorLocs(SelLocs);
3074 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003075
3076 ToMethod->setLexicalDeclContext(LexicalDC);
3077 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003078 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003079 return ToMethod;
3080}
3081
Douglas Gregorb4677b62010-02-18 01:47:50 +00003082Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3083 // Import the major distinguishing characteristics of a category.
3084 DeclContext *DC, *LexicalDC;
3085 DeclarationName Name;
3086 SourceLocation Loc;
3087 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3088 return 0;
3089
3090 ObjCInterfaceDecl *ToInterface
3091 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3092 if (!ToInterface)
3093 return 0;
3094
3095 // Determine if we've already encountered this category.
3096 ObjCCategoryDecl *MergeWithCategory
3097 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3098 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3099 if (!ToCategory) {
3100 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003101 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003102 Loc,
3103 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003104 Name.getAsIdentifierInfo(),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003105 ToInterface,
3106 Importer.Import(D->getIvarLBraceLoc()),
3107 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003108 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003109 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003110 Importer.Imported(D, ToCategory);
3111
Douglas Gregorb4677b62010-02-18 01:47:50 +00003112 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003113 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3114 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003115 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3116 = D->protocol_loc_begin();
3117 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3118 FromProtoEnd = D->protocol_end();
3119 FromProto != FromProtoEnd;
3120 ++FromProto, ++FromProtoLoc) {
3121 ObjCProtocolDecl *ToProto
3122 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3123 if (!ToProto)
3124 return 0;
3125 Protocols.push_back(ToProto);
3126 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3127 }
3128
3129 // FIXME: If we're merging, make sure that the protocol list is the same.
3130 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3131 ProtocolLocs.data(), Importer.getToContext());
3132
3133 } else {
3134 Importer.Imported(D, ToCategory);
3135 }
3136
3137 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003138 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003139
3140 // If we have an implementation, import it as well.
3141 if (D->getImplementation()) {
3142 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003143 = cast_or_null<ObjCCategoryImplDecl>(
3144 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003145 if (!Impl)
3146 return 0;
3147
3148 ToCategory->setImplementation(Impl);
3149 }
3150
3151 return ToCategory;
3152}
3153
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003154bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3155 ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003156 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003157 if (To->getDefinition()) {
Douglas Gregorac32ff92012-02-01 21:00:38 +00003158 if (shouldForceImportDeclContext(Kind))
3159 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003160 return false;
3161 }
3162
3163 // Start the protocol definition
3164 To->startDefinition();
3165
3166 // Import protocols
3167 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3168 SmallVector<SourceLocation, 4> ProtocolLocs;
3169 ObjCProtocolDecl::protocol_loc_iterator
3170 FromProtoLoc = From->protocol_loc_begin();
3171 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3172 FromProtoEnd = From->protocol_end();
3173 FromProto != FromProtoEnd;
3174 ++FromProto, ++FromProtoLoc) {
3175 ObjCProtocolDecl *ToProto
3176 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3177 if (!ToProto)
3178 return true;
3179 Protocols.push_back(ToProto);
3180 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3181 }
3182
3183 // FIXME: If we're merging, make sure that the protocol list is the same.
3184 To->setProtocolList(Protocols.data(), Protocols.size(),
3185 ProtocolLocs.data(), Importer.getToContext());
3186
Douglas Gregorac32ff92012-02-01 21:00:38 +00003187 if (shouldForceImportDeclContext(Kind)) {
3188 // Import all of the members of this protocol.
3189 ImportDeclContext(From, /*ForceImport=*/true);
3190 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003191 return false;
3192}
3193
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003194Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003195 // If this protocol has a definition in the translation unit we're coming
3196 // from, but this particular declaration is not that definition, import the
3197 // definition and map to that.
3198 ObjCProtocolDecl *Definition = D->getDefinition();
3199 if (Definition && Definition != D) {
3200 Decl *ImportedDef = Importer.Import(Definition);
3201 if (!ImportedDef)
3202 return 0;
3203
3204 return Importer.Imported(D, ImportedDef);
3205 }
3206
Douglas Gregorb4677b62010-02-18 01:47:50 +00003207 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003208 DeclContext *DC, *LexicalDC;
3209 DeclarationName Name;
3210 SourceLocation Loc;
3211 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3212 return 0;
3213
3214 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003215 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3216 DC->localUncachedLookup(Name, FoundDecls);
3217 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3218 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003219 continue;
3220
Douglas Gregorb75a3452011-10-15 00:10:27 +00003221 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003222 break;
3223 }
3224
3225 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003226 if (!ToProto) {
3227 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3228 Name.getAsIdentifierInfo(), Loc,
3229 Importer.Import(D->getAtStartLoc()),
3230 /*PrevDecl=*/0);
3231 ToProto->setLexicalDeclContext(LexicalDC);
3232 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003233 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003234
3235 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003236
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003237 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3238 return 0;
3239
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003240 return ToProto;
3241}
3242
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003243bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3244 ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003245 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003246 if (To->getDefinition()) {
3247 // Check consistency of superclass.
3248 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3249 if (FromSuper) {
3250 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3251 if (!FromSuper)
3252 return true;
3253 }
3254
3255 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3256 if ((bool)FromSuper != (bool)ToSuper ||
3257 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3258 Importer.ToDiag(To->getLocation(),
3259 diag::err_odr_objc_superclass_inconsistent)
3260 << To->getDeclName();
3261 if (ToSuper)
3262 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3263 << To->getSuperClass()->getDeclName();
3264 else
3265 Importer.ToDiag(To->getLocation(),
3266 diag::note_odr_objc_missing_superclass);
3267 if (From->getSuperClass())
3268 Importer.FromDiag(From->getSuperClassLoc(),
3269 diag::note_odr_objc_superclass)
3270 << From->getSuperClass()->getDeclName();
3271 else
3272 Importer.FromDiag(From->getLocation(),
3273 diag::note_odr_objc_missing_superclass);
3274 }
3275
Douglas Gregorac32ff92012-02-01 21:00:38 +00003276 if (shouldForceImportDeclContext(Kind))
3277 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003278 return false;
3279 }
3280
3281 // Start the definition.
3282 To->startDefinition();
3283
3284 // If this class has a superclass, import it.
3285 if (From->getSuperClass()) {
3286 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3287 Importer.Import(From->getSuperClass()));
3288 if (!Super)
3289 return true;
3290
3291 To->setSuperClass(Super);
3292 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3293 }
3294
3295 // Import protocols
3296 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3297 SmallVector<SourceLocation, 4> ProtocolLocs;
3298 ObjCInterfaceDecl::protocol_loc_iterator
3299 FromProtoLoc = From->protocol_loc_begin();
3300
3301 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3302 FromProtoEnd = From->protocol_end();
3303 FromProto != FromProtoEnd;
3304 ++FromProto, ++FromProtoLoc) {
3305 ObjCProtocolDecl *ToProto
3306 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3307 if (!ToProto)
3308 return true;
3309 Protocols.push_back(ToProto);
3310 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3311 }
3312
3313 // FIXME: If we're merging, make sure that the protocol list is the same.
3314 To->setProtocolList(Protocols.data(), Protocols.size(),
3315 ProtocolLocs.data(), Importer.getToContext());
3316
3317 // Import categories. When the categories themselves are imported, they'll
3318 // hook themselves into this interface.
3319 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3320 FromCat = FromCat->getNextClassCategory())
3321 Importer.Import(FromCat);
3322
3323 // If we have an @implementation, import it as well.
3324 if (From->getImplementation()) {
3325 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3326 Importer.Import(From->getImplementation()));
3327 if (!Impl)
3328 return true;
3329
3330 To->setImplementation(Impl);
3331 }
3332
Douglas Gregorac32ff92012-02-01 21:00:38 +00003333 if (shouldForceImportDeclContext(Kind)) {
3334 // Import all of the members of this class.
3335 ImportDeclContext(From, /*ForceImport=*/true);
3336 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003337 return false;
3338}
3339
Douglas Gregora12d2942010-02-16 01:20:57 +00003340Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003341 // If this class has a definition in the translation unit we're coming from,
3342 // but this particular declaration is not that definition, import the
3343 // definition and map to that.
3344 ObjCInterfaceDecl *Definition = D->getDefinition();
3345 if (Definition && Definition != D) {
3346 Decl *ImportedDef = Importer.Import(Definition);
3347 if (!ImportedDef)
3348 return 0;
3349
3350 return Importer.Imported(D, ImportedDef);
3351 }
3352
Douglas Gregora12d2942010-02-16 01:20:57 +00003353 // Import the major distinguishing characteristics of an @interface.
3354 DeclContext *DC, *LexicalDC;
3355 DeclarationName Name;
3356 SourceLocation Loc;
3357 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3358 return 0;
3359
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003360 // Look for an existing interface with the same name.
Douglas Gregora12d2942010-02-16 01:20:57 +00003361 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003362 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3363 DC->localUncachedLookup(Name, FoundDecls);
3364 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3365 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003366 continue;
3367
Douglas Gregorb75a3452011-10-15 00:10:27 +00003368 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003369 break;
3370 }
3371
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003372 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003373 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003374 if (!ToIface) {
3375 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3376 Importer.Import(D->getAtStartLoc()),
3377 Name.getAsIdentifierInfo(),
3378 /*PrevDecl=*/0,Loc,
3379 D->isImplicitInterfaceDecl());
3380 ToIface->setLexicalDeclContext(LexicalDC);
3381 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003382 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003383 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003384
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003385 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3386 return 0;
Douglas Gregora12d2942010-02-16 01:20:57 +00003387
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003388 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003389}
3390
Douglas Gregor3daef292010-12-07 15:32:12 +00003391Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3392 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3393 Importer.Import(D->getCategoryDecl()));
3394 if (!Category)
3395 return 0;
3396
3397 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3398 if (!ToImpl) {
3399 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3400 if (!DC)
3401 return 0;
3402
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003403 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003404 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003405 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003406 Category->getClassInterface(),
3407 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003408 Importer.Import(D->getAtStartLoc()),
3409 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003410
3411 DeclContext *LexicalDC = DC;
3412 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3413 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3414 if (!LexicalDC)
3415 return 0;
3416
3417 ToImpl->setLexicalDeclContext(LexicalDC);
3418 }
3419
Sean Callanan9faf8102011-10-21 02:57:43 +00003420 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003421 Category->setImplementation(ToImpl);
3422 }
3423
3424 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003425 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003426 return ToImpl;
3427}
3428
Douglas Gregordd182ff2010-12-07 01:26:03 +00003429Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3430 // Find the corresponding interface.
3431 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3432 Importer.Import(D->getClassInterface()));
3433 if (!Iface)
3434 return 0;
3435
3436 // Import the superclass, if any.
3437 ObjCInterfaceDecl *Super = 0;
3438 if (D->getSuperClass()) {
3439 Super = cast_or_null<ObjCInterfaceDecl>(
3440 Importer.Import(D->getSuperClass()));
3441 if (!Super)
3442 return 0;
3443 }
3444
3445 ObjCImplementationDecl *Impl = Iface->getImplementation();
3446 if (!Impl) {
3447 // We haven't imported an implementation yet. Create a new @implementation
3448 // now.
3449 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3450 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003451 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003452 Importer.Import(D->getLocation()),
Fariborz Jahanianaf300292012-02-20 20:09:20 +00003453 Importer.Import(D->getAtStartLoc()),
3454 Importer.Import(D->getIvarLBraceLoc()),
3455 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003456
3457 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3458 DeclContext *LexicalDC
3459 = Importer.ImportContext(D->getLexicalDeclContext());
3460 if (!LexicalDC)
3461 return 0;
3462 Impl->setLexicalDeclContext(LexicalDC);
3463 }
3464
3465 // Associate the implementation with the class it implements.
3466 Iface->setImplementation(Impl);
3467 Importer.Imported(D, Iface->getImplementation());
3468 } else {
3469 Importer.Imported(D, Iface->getImplementation());
3470
3471 // Verify that the existing @implementation has the same superclass.
3472 if ((Super && !Impl->getSuperClass()) ||
3473 (!Super && Impl->getSuperClass()) ||
3474 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003475 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003476 Importer.ToDiag(Impl->getLocation(),
3477 diag::err_odr_objc_superclass_inconsistent)
3478 << Iface->getDeclName();
3479 // FIXME: It would be nice to have the location of the superclass
3480 // below.
3481 if (Impl->getSuperClass())
3482 Importer.ToDiag(Impl->getLocation(),
3483 diag::note_odr_objc_superclass)
3484 << Impl->getSuperClass()->getDeclName();
3485 else
3486 Importer.ToDiag(Impl->getLocation(),
3487 diag::note_odr_objc_missing_superclass);
3488 if (D->getSuperClass())
3489 Importer.FromDiag(D->getLocation(),
3490 diag::note_odr_objc_superclass)
3491 << D->getSuperClass()->getDeclName();
3492 else
3493 Importer.FromDiag(D->getLocation(),
3494 diag::note_odr_objc_missing_superclass);
3495 return 0;
3496 }
3497 }
3498
3499 // Import all of the members of this @implementation.
3500 ImportDeclContext(D);
3501
3502 return Impl;
3503}
3504
Douglas Gregore3261622010-02-17 18:02:10 +00003505Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3506 // Import the major distinguishing characteristics of an @property.
3507 DeclContext *DC, *LexicalDC;
3508 DeclarationName Name;
3509 SourceLocation Loc;
3510 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3511 return 0;
3512
3513 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003514 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3515 DC->localUncachedLookup(Name, FoundDecls);
3516 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003517 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003518 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003519 // Check property types.
3520 if (!Importer.IsStructurallyEquivalent(D->getType(),
3521 FoundProp->getType())) {
3522 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3523 << Name << D->getType() << FoundProp->getType();
3524 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3525 << FoundProp->getType();
3526 return 0;
3527 }
3528
3529 // FIXME: Check property attributes, getters, setters, etc.?
3530
3531 // Consider these properties to be equivalent.
3532 Importer.Imported(D, FoundProp);
3533 return FoundProp;
3534 }
3535 }
3536
3537 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003538 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3539 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003540 return 0;
3541
3542 // Create the new property.
3543 ObjCPropertyDecl *ToProperty
3544 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3545 Name.getAsIdentifierInfo(),
3546 Importer.Import(D->getAtLoc()),
Fariborz Jahanian77bfb8b2012-02-29 22:18:55 +00003547 Importer.Import(D->getLParenLoc()),
Douglas Gregore3261622010-02-17 18:02:10 +00003548 T,
3549 D->getPropertyImplementation());
3550 Importer.Imported(D, ToProperty);
3551 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003552 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003553
3554 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003555 ToProperty->setPropertyAttributesAsWritten(
3556 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003557 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3558 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3559 ToProperty->setGetterMethodDecl(
3560 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3561 ToProperty->setSetterMethodDecl(
3562 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3563 ToProperty->setPropertyIvarDecl(
3564 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3565 return ToProperty;
3566}
3567
Douglas Gregor954e0c72010-12-07 18:32:03 +00003568Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3569 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3570 Importer.Import(D->getPropertyDecl()));
3571 if (!Property)
3572 return 0;
3573
3574 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3575 if (!DC)
3576 return 0;
3577
3578 // Import the lexical declaration context.
3579 DeclContext *LexicalDC = DC;
3580 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3581 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3582 if (!LexicalDC)
3583 return 0;
3584 }
3585
3586 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3587 if (!InImpl)
3588 return 0;
3589
3590 // Import the ivar (for an @synthesize).
3591 ObjCIvarDecl *Ivar = 0;
3592 if (D->getPropertyIvarDecl()) {
3593 Ivar = cast_or_null<ObjCIvarDecl>(
3594 Importer.Import(D->getPropertyIvarDecl()));
3595 if (!Ivar)
3596 return 0;
3597 }
3598
3599 ObjCPropertyImplDecl *ToImpl
3600 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3601 if (!ToImpl) {
3602 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3603 Importer.Import(D->getLocStart()),
3604 Importer.Import(D->getLocation()),
3605 Property,
3606 D->getPropertyImplementation(),
3607 Ivar,
3608 Importer.Import(D->getPropertyIvarDeclLoc()));
3609 ToImpl->setLexicalDeclContext(LexicalDC);
3610 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003611 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003612 } else {
3613 // Check that we have the same kind of property implementation (@synthesize
3614 // vs. @dynamic).
3615 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3616 Importer.ToDiag(ToImpl->getLocation(),
3617 diag::err_odr_objc_property_impl_kind_inconsistent)
3618 << Property->getDeclName()
3619 << (ToImpl->getPropertyImplementation()
3620 == ObjCPropertyImplDecl::Dynamic);
3621 Importer.FromDiag(D->getLocation(),
3622 diag::note_odr_objc_property_impl_kind)
3623 << D->getPropertyDecl()->getDeclName()
3624 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3625 return 0;
3626 }
3627
3628 // For @synthesize, check that we have the same
3629 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3630 Ivar != ToImpl->getPropertyIvarDecl()) {
3631 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3632 diag::err_odr_objc_synthesize_ivar_inconsistent)
3633 << Property->getDeclName()
3634 << ToImpl->getPropertyIvarDecl()->getDeclName()
3635 << Ivar->getDeclName();
3636 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3637 diag::note_odr_objc_synthesize_ivar_here)
3638 << D->getPropertyIvarDecl()->getDeclName();
3639 return 0;
3640 }
3641
3642 // Merge the existing implementation with the new implementation.
3643 Importer.Imported(D, ToImpl);
3644 }
3645
3646 return ToImpl;
3647}
3648
Douglas Gregor040afae2010-11-30 19:14:50 +00003649Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3650 // For template arguments, we adopt the translation unit as our declaration
3651 // context. This context will be fixed when the actual template declaration
3652 // is created.
3653
3654 // FIXME: Import default argument.
3655 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3656 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003657 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003658 Importer.Import(D->getLocation()),
3659 D->getDepth(),
3660 D->getIndex(),
3661 Importer.Import(D->getIdentifier()),
3662 D->wasDeclaredWithTypename(),
3663 D->isParameterPack());
3664}
3665
3666Decl *
3667ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3668 // Import the name of this declaration.
3669 DeclarationName Name = Importer.Import(D->getDeclName());
3670 if (D->getDeclName() && !Name)
3671 return 0;
3672
3673 // Import the location of this declaration.
3674 SourceLocation Loc = Importer.Import(D->getLocation());
3675
3676 // Import the type of this declaration.
3677 QualType T = Importer.Import(D->getType());
3678 if (T.isNull())
3679 return 0;
3680
3681 // Import type-source information.
3682 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3683 if (D->getTypeSourceInfo() && !TInfo)
3684 return 0;
3685
3686 // FIXME: Import default argument.
3687
3688 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3689 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003690 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003691 Loc, D->getDepth(), D->getPosition(),
3692 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003693 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003694}
3695
3696Decl *
3697ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3698 // Import the name of this declaration.
3699 DeclarationName Name = Importer.Import(D->getDeclName());
3700 if (D->getDeclName() && !Name)
3701 return 0;
3702
3703 // Import the location of this declaration.
3704 SourceLocation Loc = Importer.Import(D->getLocation());
3705
3706 // Import template parameters.
3707 TemplateParameterList *TemplateParams
3708 = ImportTemplateParameterList(D->getTemplateParameters());
3709 if (!TemplateParams)
3710 return 0;
3711
3712 // FIXME: Import default argument.
3713
3714 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3715 Importer.getToContext().getTranslationUnitDecl(),
3716 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003717 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003718 Name.getAsIdentifierInfo(),
3719 TemplateParams);
3720}
3721
3722Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3723 // If this record has a definition in the translation unit we're coming from,
3724 // but this particular declaration is not that definition, import the
3725 // definition and map to that.
3726 CXXRecordDecl *Definition
3727 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3728 if (Definition && Definition != D->getTemplatedDecl()) {
3729 Decl *ImportedDef
3730 = Importer.Import(Definition->getDescribedClassTemplate());
3731 if (!ImportedDef)
3732 return 0;
3733
3734 return Importer.Imported(D, ImportedDef);
3735 }
3736
3737 // Import the major distinguishing characteristics of this class template.
3738 DeclContext *DC, *LexicalDC;
3739 DeclarationName Name;
3740 SourceLocation Loc;
3741 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3742 return 0;
3743
3744 // We may already have a template of the same name; try to find and match it.
3745 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003746 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003747 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3748 DC->localUncachedLookup(Name, FoundDecls);
3749 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3750 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003751 continue;
3752
Douglas Gregorb75a3452011-10-15 00:10:27 +00003753 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003754 if (ClassTemplateDecl *FoundTemplate
3755 = dyn_cast<ClassTemplateDecl>(Found)) {
3756 if (IsStructuralMatch(D, FoundTemplate)) {
3757 // The class templates structurally match; call it the same template.
3758 // FIXME: We may be filling in a forward declaration here. Handle
3759 // this case!
3760 Importer.Imported(D->getTemplatedDecl(),
3761 FoundTemplate->getTemplatedDecl());
3762 return Importer.Imported(D, FoundTemplate);
3763 }
3764 }
3765
Douglas Gregorb75a3452011-10-15 00:10:27 +00003766 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003767 }
3768
3769 if (!ConflictingDecls.empty()) {
3770 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3771 ConflictingDecls.data(),
3772 ConflictingDecls.size());
3773 }
3774
3775 if (!Name)
3776 return 0;
3777 }
3778
3779 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3780
3781 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003782 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3783 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003784 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3785 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003786 DC, StartLoc, IdLoc,
3787 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003788 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003789 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003790 D2Templated->setLexicalDeclContext(LexicalDC);
3791
3792 // Create the class template declaration itself.
3793 TemplateParameterList *TemplateParams
3794 = ImportTemplateParameterList(D->getTemplateParameters());
3795 if (!TemplateParams)
3796 return 0;
3797
3798 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3799 Loc, Name, TemplateParams,
3800 D2Templated,
3801 /*PrevDecl=*/0);
3802 D2Templated->setDescribedClassTemplate(D2);
3803
3804 D2->setAccess(D->getAccess());
3805 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003806 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003807
3808 // Note the relationship between the class templates.
3809 Importer.Imported(D, D2);
3810 Importer.Imported(DTemplated, D2Templated);
3811
John McCall5e1cdac2011-10-07 06:10:15 +00003812 if (DTemplated->isCompleteDefinition() &&
3813 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003814 // FIXME: Import definition!
3815 }
3816
3817 return D2;
3818}
3819
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003820Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3821 ClassTemplateSpecializationDecl *D) {
3822 // If this record has a definition in the translation unit we're coming from,
3823 // but this particular declaration is not that definition, import the
3824 // definition and map to that.
3825 TagDecl *Definition = D->getDefinition();
3826 if (Definition && Definition != D) {
3827 Decl *ImportedDef = Importer.Import(Definition);
3828 if (!ImportedDef)
3829 return 0;
3830
3831 return Importer.Imported(D, ImportedDef);
3832 }
3833
3834 ClassTemplateDecl *ClassTemplate
3835 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3836 D->getSpecializedTemplate()));
3837 if (!ClassTemplate)
3838 return 0;
3839
3840 // Import the context of this declaration.
3841 DeclContext *DC = ClassTemplate->getDeclContext();
3842 if (!DC)
3843 return 0;
3844
3845 DeclContext *LexicalDC = DC;
3846 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3847 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3848 if (!LexicalDC)
3849 return 0;
3850 }
3851
3852 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003853 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3854 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003855
3856 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003857 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003858 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3859 D->getTemplateArgs().size(),
3860 TemplateArgs))
3861 return 0;
3862
3863 // Try to find an existing specialization with these template arguments.
3864 void *InsertPos = 0;
3865 ClassTemplateSpecializationDecl *D2
3866 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3867 TemplateArgs.size(), InsertPos);
3868 if (D2) {
3869 // We already have a class template specialization with these template
3870 // arguments.
3871
3872 // FIXME: Check for specialization vs. instantiation errors.
3873
3874 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003875 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003876 // The record types structurally match, or the "from" translation
3877 // unit only had a forward declaration anyway; call it the same
3878 // function.
3879 return Importer.Imported(D, FoundDef);
3880 }
3881 }
3882 } else {
3883 // Create a new specialization.
3884 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3885 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003886 StartLoc, IdLoc,
3887 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003888 TemplateArgs.data(),
3889 TemplateArgs.size(),
3890 /*PrevDecl=*/0);
3891 D2->setSpecializationKind(D->getSpecializationKind());
3892
3893 // Add this specialization to the class template.
3894 ClassTemplate->AddSpecialization(D2, InsertPos);
3895
3896 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003897 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003898
3899 // Add the specialization to this context.
3900 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003901 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003902 }
3903 Importer.Imported(D, D2);
3904
John McCall5e1cdac2011-10-07 06:10:15 +00003905 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003906 return 0;
3907
3908 return D2;
3909}
3910
Douglas Gregor4800d952010-02-11 19:21:55 +00003911//----------------------------------------------------------------------------
3912// Import Statements
3913//----------------------------------------------------------------------------
3914
3915Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3916 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3917 << S->getStmtClassName();
3918 return 0;
3919}
3920
3921//----------------------------------------------------------------------------
3922// Import Expressions
3923//----------------------------------------------------------------------------
3924Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3925 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3926 << E->getStmtClassName();
3927 return 0;
3928}
3929
Douglas Gregor44080632010-02-19 01:17:02 +00003930Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003931 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3932 if (!ToD)
3933 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003934
3935 NamedDecl *FoundD = 0;
3936 if (E->getDecl() != E->getFoundDecl()) {
3937 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3938 if (!FoundD)
3939 return 0;
3940 }
Douglas Gregor44080632010-02-19 01:17:02 +00003941
3942 QualType T = Importer.Import(E->getType());
3943 if (T.isNull())
3944 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003945
3946 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3947 Importer.Import(E->getQualifierLoc()),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003948 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003949 ToD,
3950 Importer.Import(E->getLocation()),
3951 T, E->getValueKind(),
3952 FoundD,
3953 /*FIXME:TemplateArgs=*/0);
3954 if (E->hadMultipleCandidates())
3955 DRE->setHadMultipleCandidates(true);
3956 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003957}
3958
Douglas Gregor4800d952010-02-11 19:21:55 +00003959Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3960 QualType T = Importer.Import(E->getType());
3961 if (T.isNull())
3962 return 0;
3963
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003964 return IntegerLiteral::Create(Importer.getToContext(),
3965 E->getValue(), T,
3966 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003967}
3968
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003969Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3970 QualType T = Importer.Import(E->getType());
3971 if (T.isNull())
3972 return 0;
3973
Douglas Gregor5cee1192011-07-27 05:40:30 +00003974 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3975 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003976 Importer.Import(E->getLocation()));
3977}
3978
Douglas Gregorf638f952010-02-19 01:07:06 +00003979Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3980 Expr *SubExpr = Importer.Import(E->getSubExpr());
3981 if (!SubExpr)
3982 return 0;
3983
3984 return new (Importer.getToContext())
3985 ParenExpr(Importer.Import(E->getLParen()),
3986 Importer.Import(E->getRParen()),
3987 SubExpr);
3988}
3989
3990Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3991 QualType T = Importer.Import(E->getType());
3992 if (T.isNull())
3993 return 0;
3994
3995 Expr *SubExpr = Importer.Import(E->getSubExpr());
3996 if (!SubExpr)
3997 return 0;
3998
3999 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004000 T, E->getValueKind(),
4001 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004002 Importer.Import(E->getOperatorLoc()));
4003}
4004
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004005Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4006 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00004007 QualType ResultType = Importer.Import(E->getType());
4008
4009 if (E->isArgumentType()) {
4010 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4011 if (!TInfo)
4012 return 0;
4013
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004014 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4015 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004016 Importer.Import(E->getOperatorLoc()),
4017 Importer.Import(E->getRParenLoc()));
4018 }
4019
4020 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4021 if (!SubExpr)
4022 return 0;
4023
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004024 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4025 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004026 Importer.Import(E->getOperatorLoc()),
4027 Importer.Import(E->getRParenLoc()));
4028}
4029
Douglas Gregorf638f952010-02-19 01:07:06 +00004030Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4031 QualType T = Importer.Import(E->getType());
4032 if (T.isNull())
4033 return 0;
4034
4035 Expr *LHS = Importer.Import(E->getLHS());
4036 if (!LHS)
4037 return 0;
4038
4039 Expr *RHS = Importer.Import(E->getRHS());
4040 if (!RHS)
4041 return 0;
4042
4043 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004044 T, E->getValueKind(),
4045 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004046 Importer.Import(E->getOperatorLoc()));
4047}
4048
4049Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4050 QualType T = Importer.Import(E->getType());
4051 if (T.isNull())
4052 return 0;
4053
4054 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4055 if (CompLHSType.isNull())
4056 return 0;
4057
4058 QualType CompResultType = Importer.Import(E->getComputationResultType());
4059 if (CompResultType.isNull())
4060 return 0;
4061
4062 Expr *LHS = Importer.Import(E->getLHS());
4063 if (!LHS)
4064 return 0;
4065
4066 Expr *RHS = Importer.Import(E->getRHS());
4067 if (!RHS)
4068 return 0;
4069
4070 return new (Importer.getToContext())
4071 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004072 T, E->getValueKind(),
4073 E->getObjectKind(),
4074 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004075 Importer.Import(E->getOperatorLoc()));
4076}
4077
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004078static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004079 if (E->path_empty()) return false;
4080
4081 // TODO: import cast paths
4082 return true;
4083}
4084
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004085Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4086 QualType T = Importer.Import(E->getType());
4087 if (T.isNull())
4088 return 0;
4089
4090 Expr *SubExpr = Importer.Import(E->getSubExpr());
4091 if (!SubExpr)
4092 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004093
4094 CXXCastPath BasePath;
4095 if (ImportCastPath(E, BasePath))
4096 return 0;
4097
4098 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004099 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004100}
4101
Douglas Gregor008847a2010-02-19 01:32:14 +00004102Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4103 QualType T = Importer.Import(E->getType());
4104 if (T.isNull())
4105 return 0;
4106
4107 Expr *SubExpr = Importer.Import(E->getSubExpr());
4108 if (!SubExpr)
4109 return 0;
4110
4111 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4112 if (!TInfo && E->getTypeInfoAsWritten())
4113 return 0;
4114
John McCallf871d0c2010-08-07 06:22:56 +00004115 CXXCastPath BasePath;
4116 if (ImportCastPath(E, BasePath))
4117 return 0;
4118
John McCallf89e55a2010-11-18 06:31:45 +00004119 return CStyleCastExpr::Create(Importer.getToContext(), T,
4120 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004121 SubExpr, &BasePath, TInfo,
4122 Importer.Import(E->getLParenLoc()),
4123 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004124}
4125
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004126ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004127 ASTContext &FromContext, FileManager &FromFileManager,
4128 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004129 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004130 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4131 Minimal(MinimalImport)
4132{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004133 ImportedDecls[FromContext.getTranslationUnitDecl()]
4134 = ToContext.getTranslationUnitDecl();
4135}
4136
4137ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004138
4139QualType ASTImporter::Import(QualType FromT) {
4140 if (FromT.isNull())
4141 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004142
4143 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004144
Douglas Gregor169fba52010-02-08 15:18:58 +00004145 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004146 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4147 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004148 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004149 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004150
Douglas Gregor169fba52010-02-08 15:18:58 +00004151 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004152 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004153 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004154 if (ToT.isNull())
4155 return ToT;
4156
Douglas Gregor169fba52010-02-08 15:18:58 +00004157 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004158 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004159
John McCallf4c73712011-01-19 06:33:43 +00004160 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004161}
4162
Douglas Gregor9bed8792010-02-09 19:21:46 +00004163TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004164 if (!FromTSI)
4165 return FromTSI;
4166
4167 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004168 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004169 QualType T = Import(FromTSI->getType());
4170 if (T.isNull())
4171 return 0;
4172
4173 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar96a00142012-03-09 18:35:03 +00004174 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004175}
4176
4177Decl *ASTImporter::Import(Decl *FromD) {
4178 if (!FromD)
4179 return 0;
4180
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004181 ASTNodeImporter Importer(*this);
4182
Douglas Gregor9bed8792010-02-09 19:21:46 +00004183 // Check whether we've already imported this declaration.
4184 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004185 if (Pos != ImportedDecls.end()) {
4186 Decl *ToD = Pos->second;
4187 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4188 return ToD;
4189 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004190
4191 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004192 Decl *ToD = Importer.Visit(FromD);
4193 if (!ToD)
4194 return 0;
4195
4196 // Record the imported declaration.
4197 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004198
4199 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4200 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004201 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004202 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004203 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004204 // When we've finished transforming a typedef, see whether it was the
4205 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004206 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004207 FromTag = AnonTagsWithPendingTypedefs.begin(),
4208 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4209 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004210 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004211 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4212 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004213 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004214 AnonTagsWithPendingTypedefs.erase(FromTag);
4215 break;
4216 }
4217 }
4218 }
4219 }
4220
Douglas Gregor9bed8792010-02-09 19:21:46 +00004221 return ToD;
4222}
4223
4224DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4225 if (!FromDC)
4226 return FromDC;
4227
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004228 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregorac32ff92012-02-01 21:00:38 +00004229 if (!ToDC)
4230 return 0;
4231
4232 // When we're using a record/enum/Objective-C class/protocol as a context, we
4233 // need it to have a definition.
4234 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor568991b2012-01-25 01:13:20 +00004235 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregorac32ff92012-02-01 21:00:38 +00004236 if (ToRecord->isCompleteDefinition()) {
4237 // Do nothing.
4238 } else if (FromRecord->isCompleteDefinition()) {
4239 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4240 ASTNodeImporter::IDK_Basic);
4241 } else {
4242 CompleteDecl(ToRecord);
4243 }
4244 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4245 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4246 if (ToEnum->isCompleteDefinition()) {
4247 // Do nothing.
4248 } else if (FromEnum->isCompleteDefinition()) {
4249 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4250 ASTNodeImporter::IDK_Basic);
4251 } else {
4252 CompleteDecl(ToEnum);
4253 }
4254 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4255 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4256 if (ToClass->getDefinition()) {
4257 // Do nothing.
4258 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4259 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4260 ASTNodeImporter::IDK_Basic);
4261 } else {
4262 CompleteDecl(ToClass);
4263 }
4264 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4265 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4266 if (ToProto->getDefinition()) {
4267 // Do nothing.
4268 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4269 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4270 ASTNodeImporter::IDK_Basic);
4271 } else {
4272 CompleteDecl(ToProto);
4273 }
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004274 }
4275
4276 return ToDC;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004277}
4278
4279Expr *ASTImporter::Import(Expr *FromE) {
4280 if (!FromE)
4281 return 0;
4282
4283 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4284}
4285
4286Stmt *ASTImporter::Import(Stmt *FromS) {
4287 if (!FromS)
4288 return 0;
4289
Douglas Gregor4800d952010-02-11 19:21:55 +00004290 // Check whether we've already imported this declaration.
4291 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4292 if (Pos != ImportedStmts.end())
4293 return Pos->second;
4294
4295 // Import the type
4296 ASTNodeImporter Importer(*this);
4297 Stmt *ToS = Importer.Visit(FromS);
4298 if (!ToS)
4299 return 0;
4300
4301 // Record the imported declaration.
4302 ImportedStmts[FromS] = ToS;
4303 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004304}
4305
4306NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4307 if (!FromNNS)
4308 return 0;
4309
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004310 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4311
4312 switch (FromNNS->getKind()) {
4313 case NestedNameSpecifier::Identifier:
4314 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4315 return NestedNameSpecifier::Create(ToContext, prefix, II);
4316 }
4317 return 0;
4318
4319 case NestedNameSpecifier::Namespace:
4320 if (NamespaceDecl *NS =
4321 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4322 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4323 }
4324 return 0;
4325
4326 case NestedNameSpecifier::NamespaceAlias:
4327 if (NamespaceAliasDecl *NSAD =
4328 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4329 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4330 }
4331 return 0;
4332
4333 case NestedNameSpecifier::Global:
4334 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4335
4336 case NestedNameSpecifier::TypeSpec:
4337 case NestedNameSpecifier::TypeSpecWithTemplate: {
4338 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4339 if (!T.isNull()) {
4340 bool bTemplate = FromNNS->getKind() ==
4341 NestedNameSpecifier::TypeSpecWithTemplate;
4342 return NestedNameSpecifier::Create(ToContext, prefix,
4343 bTemplate, T.getTypePtr());
4344 }
4345 }
4346 return 0;
4347 }
4348
4349 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004350}
4351
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004352NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4353 // FIXME: Implement!
4354 return NestedNameSpecifierLoc();
4355}
4356
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004357TemplateName ASTImporter::Import(TemplateName From) {
4358 switch (From.getKind()) {
4359 case TemplateName::Template:
4360 if (TemplateDecl *ToTemplate
4361 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4362 return TemplateName(ToTemplate);
4363
4364 return TemplateName();
4365
4366 case TemplateName::OverloadedTemplate: {
4367 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4368 UnresolvedSet<2> ToTemplates;
4369 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4370 E = FromStorage->end();
4371 I != E; ++I) {
4372 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4373 ToTemplates.addDecl(To);
4374 else
4375 return TemplateName();
4376 }
4377 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4378 ToTemplates.end());
4379 }
4380
4381 case TemplateName::QualifiedTemplate: {
4382 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4383 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4384 if (!Qualifier)
4385 return TemplateName();
4386
4387 if (TemplateDecl *ToTemplate
4388 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4389 return ToContext.getQualifiedTemplateName(Qualifier,
4390 QTN->hasTemplateKeyword(),
4391 ToTemplate);
4392
4393 return TemplateName();
4394 }
4395
4396 case TemplateName::DependentTemplate: {
4397 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4398 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4399 if (!Qualifier)
4400 return TemplateName();
4401
4402 if (DTN->isIdentifier()) {
4403 return ToContext.getDependentTemplateName(Qualifier,
4404 Import(DTN->getIdentifier()));
4405 }
4406
4407 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4408 }
John McCall14606042011-06-30 08:33:18 +00004409
4410 case TemplateName::SubstTemplateTemplateParm: {
4411 SubstTemplateTemplateParmStorage *subst
4412 = From.getAsSubstTemplateTemplateParm();
4413 TemplateTemplateParmDecl *param
4414 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4415 if (!param)
4416 return TemplateName();
4417
4418 TemplateName replacement = Import(subst->getReplacement());
4419 if (replacement.isNull()) return TemplateName();
4420
4421 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4422 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004423
4424 case TemplateName::SubstTemplateTemplateParmPack: {
4425 SubstTemplateTemplateParmPackStorage *SubstPack
4426 = From.getAsSubstTemplateTemplateParmPack();
4427 TemplateTemplateParmDecl *Param
4428 = cast_or_null<TemplateTemplateParmDecl>(
4429 Import(SubstPack->getParameterPack()));
4430 if (!Param)
4431 return TemplateName();
4432
4433 ASTNodeImporter Importer(*this);
4434 TemplateArgument ArgPack
4435 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4436 if (ArgPack.isNull())
4437 return TemplateName();
4438
4439 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4440 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004441 }
4442
4443 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004444}
4445
Douglas Gregor9bed8792010-02-09 19:21:46 +00004446SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4447 if (FromLoc.isInvalid())
4448 return SourceLocation();
4449
Douglas Gregor88523732010-02-10 00:15:17 +00004450 SourceManager &FromSM = FromContext.getSourceManager();
4451
4452 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004453 // don't have to import macro expansions.
4454 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004455 FromLoc = FromSM.getSpellingLoc(FromLoc);
4456 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4457 SourceManager &ToSM = ToContext.getSourceManager();
4458 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004459 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004460}
4461
4462SourceRange ASTImporter::Import(SourceRange FromRange) {
4463 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4464}
4465
Douglas Gregor88523732010-02-10 00:15:17 +00004466FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004467 llvm::DenseMap<FileID, FileID>::iterator Pos
4468 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004469 if (Pos != ImportedFileIDs.end())
4470 return Pos->second;
4471
4472 SourceManager &FromSM = FromContext.getSourceManager();
4473 SourceManager &ToSM = ToContext.getSourceManager();
4474 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004475 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004476
4477 // Include location of this file.
4478 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4479
4480 // Map the FileID for to the "to" source manager.
4481 FileID ToID;
4482 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004483 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004484 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4485 // disk again
4486 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4487 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004488 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004489 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4490 FromSLoc.getFile().getFileCharacteristic());
4491 } else {
4492 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004493 const llvm::MemoryBuffer *
4494 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004495 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004496 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004497 FromBuf->getBufferIdentifier());
4498 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4499 }
4500
4501
Sebastian Redl535a3e22010-09-30 01:03:06 +00004502 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004503 return ToID;
4504}
4505
Douglas Gregord8868a62011-01-18 03:11:38 +00004506void ASTImporter::ImportDefinition(Decl *From) {
4507 Decl *To = Import(From);
4508 if (!To)
4509 return;
4510
4511 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4512 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004513
4514 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4515 if (!ToRecord->getDefinition()) {
4516 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004517 ASTNodeImporter::IDK_Everything);
Sean Callanan673e7752011-07-19 22:38:25 +00004518 return;
4519 }
4520 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004521
4522 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4523 if (!ToEnum->getDefinition()) {
4524 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004525 ASTNodeImporter::IDK_Everything);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004526 return;
4527 }
4528 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004529
4530 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4531 if (!ToIFace->getDefinition()) {
4532 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004533 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004534 return;
4535 }
4536 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004537
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004538 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4539 if (!ToProto->getDefinition()) {
4540 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004541 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004542 return;
4543 }
4544 }
4545
Douglas Gregord8868a62011-01-18 03:11:38 +00004546 Importer.ImportDeclContext(FromDC, true);
4547 }
4548}
4549
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004550DeclarationName ASTImporter::Import(DeclarationName FromName) {
4551 if (!FromName)
4552 return DeclarationName();
4553
4554 switch (FromName.getNameKind()) {
4555 case DeclarationName::Identifier:
4556 return Import(FromName.getAsIdentifierInfo());
4557
4558 case DeclarationName::ObjCZeroArgSelector:
4559 case DeclarationName::ObjCOneArgSelector:
4560 case DeclarationName::ObjCMultiArgSelector:
4561 return Import(FromName.getObjCSelector());
4562
4563 case DeclarationName::CXXConstructorName: {
4564 QualType T = Import(FromName.getCXXNameType());
4565 if (T.isNull())
4566 return DeclarationName();
4567
4568 return ToContext.DeclarationNames.getCXXConstructorName(
4569 ToContext.getCanonicalType(T));
4570 }
4571
4572 case DeclarationName::CXXDestructorName: {
4573 QualType T = Import(FromName.getCXXNameType());
4574 if (T.isNull())
4575 return DeclarationName();
4576
4577 return ToContext.DeclarationNames.getCXXDestructorName(
4578 ToContext.getCanonicalType(T));
4579 }
4580
4581 case DeclarationName::CXXConversionFunctionName: {
4582 QualType T = Import(FromName.getCXXNameType());
4583 if (T.isNull())
4584 return DeclarationName();
4585
4586 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4587 ToContext.getCanonicalType(T));
4588 }
4589
4590 case DeclarationName::CXXOperatorName:
4591 return ToContext.DeclarationNames.getCXXOperatorName(
4592 FromName.getCXXOverloadedOperator());
4593
4594 case DeclarationName::CXXLiteralOperatorName:
4595 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4596 Import(FromName.getCXXLiteralIdentifier()));
4597
4598 case DeclarationName::CXXUsingDirective:
4599 // FIXME: STATICS!
4600 return DeclarationName::getUsingDirectiveName();
4601 }
4602
David Blaikie30263482012-01-20 21:50:17 +00004603 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004604}
4605
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004606IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004607 if (!FromId)
4608 return 0;
4609
4610 return &ToContext.Idents.get(FromId->getName());
4611}
Douglas Gregor089459a2010-02-08 21:09:39 +00004612
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004613Selector ASTImporter::Import(Selector FromSel) {
4614 if (FromSel.isNull())
4615 return Selector();
4616
Chris Lattner5f9e2722011-07-23 10:55:15 +00004617 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004618 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4619 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4620 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4621 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4622}
4623
Douglas Gregor089459a2010-02-08 21:09:39 +00004624DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4625 DeclContext *DC,
4626 unsigned IDNS,
4627 NamedDecl **Decls,
4628 unsigned NumDecls) {
4629 return Name;
4630}
4631
4632DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004633 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004634}
4635
4636DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004637 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004638}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004639
Douglas Gregorac32ff92012-02-01 21:00:38 +00004640void ASTImporter::CompleteDecl (Decl *D) {
4641 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4642 if (!ID->getDefinition())
4643 ID->startDefinition();
4644 }
4645 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4646 if (!PD->getDefinition())
4647 PD->startDefinition();
4648 }
4649 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4650 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4651 TD->startDefinition();
4652 TD->setCompleteDefinition(true);
4653 }
4654 }
4655 else {
4656 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4657 }
4658}
4659
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004660Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4661 ImportedDecls[From] = To;
4662 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004663}
Douglas Gregorea35d112010-02-15 23:54:17 +00004664
4665bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004666 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004667 = ImportedTypes.find(From.getTypePtr());
4668 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4669 return true;
4670
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004671 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004672 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004673}