blob: e15bac0b44766398b33fad46d830fc1e47f1be34 [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);
968 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
969 << Base2->getType()
970 << Base2->getSourceRange();
971 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
972 << 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);
981 Context.Diag2(Base2->getSourceRange().getBegin(),
982 diag::note_odr_virtual_base)
983 << Base2->isVirtual() << Base2->getSourceRange();
984 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
985 << 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();
994 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
995 << 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;
1852 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1853 ToData.HasConstexprNonCopyMoveConstructor
1854 = FromData.HasConstexprNonCopyMoveConstructor;
1855 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1856 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1857 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1858 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1859 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1860 ToData.HasNonLiteralTypeFieldsOrBases
1861 = FromData.HasNonLiteralTypeFieldsOrBases;
1862 ToData.UserProvidedDefaultConstructor
1863 = FromData.UserProvidedDefaultConstructor;
1864 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1865 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1866 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1867 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1868 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1869 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1870 ToData.FailedImplicitMoveConstructor
1871 = FromData.FailedImplicitMoveConstructor;
1872 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001873
Chris Lattner5f9e2722011-07-23 10:55:15 +00001874 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001875 for (CXXRecordDecl::base_class_iterator
1876 Base1 = FromCXX->bases_begin(),
1877 FromBaseEnd = FromCXX->bases_end();
1878 Base1 != FromBaseEnd;
1879 ++Base1) {
1880 QualType T = Importer.Import(Base1->getType());
1881 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001882 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001883
1884 SourceLocation EllipsisLoc;
1885 if (Base1->isPackExpansion())
1886 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001887
1888 // Ensure that we have a definition for the base.
1889 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1890
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001891 Bases.push_back(
1892 new (Importer.getToContext())
1893 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1894 Base1->isVirtual(),
1895 Base1->isBaseOfClass(),
1896 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001897 Importer.Import(Base1->getTypeSourceInfo()),
1898 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001899 }
1900 if (!Bases.empty())
1901 ToCXX->setBases(Bases.data(), Bases.size());
1902 }
1903
Douglas Gregorac32ff92012-02-01 21:00:38 +00001904 if (shouldForceImportDeclContext(Kind))
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001905 ImportDeclContext(From, /*ForceImport=*/true);
1906
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001907 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001908 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001909}
1910
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001911bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00001912 ImportDefinitionKind Kind) {
1913 if (To->getDefinition() || To->isBeingDefined()) {
1914 if (Kind == IDK_Everything)
1915 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001916 return false;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001917 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001918
1919 To->startDefinition();
1920
1921 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1922 if (T.isNull())
1923 return true;
1924
1925 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1926 if (ToPromotionType.isNull())
1927 return true;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001928
1929 if (shouldForceImportDeclContext(Kind))
1930 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001931
1932 // FIXME: we might need to merge the number of positive or negative bits
1933 // if the enumerator lists don't match.
1934 To->completeDefinition(T, ToPromotionType,
1935 From->getNumPositiveBits(),
1936 From->getNumNegativeBits());
1937 return false;
1938}
1939
Douglas Gregor040afae2010-11-30 19:14:50 +00001940TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1941 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001942 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001943 ToParams.reserve(Params->size());
1944 for (TemplateParameterList::iterator P = Params->begin(),
1945 PEnd = Params->end();
1946 P != PEnd; ++P) {
1947 Decl *To = Importer.Import(*P);
1948 if (!To)
1949 return 0;
1950
1951 ToParams.push_back(cast<NamedDecl>(To));
1952 }
1953
1954 return TemplateParameterList::Create(Importer.getToContext(),
1955 Importer.Import(Params->getTemplateLoc()),
1956 Importer.Import(Params->getLAngleLoc()),
1957 ToParams.data(), ToParams.size(),
1958 Importer.Import(Params->getRAngleLoc()));
1959}
1960
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001961TemplateArgument
1962ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1963 switch (From.getKind()) {
1964 case TemplateArgument::Null:
1965 return TemplateArgument();
1966
1967 case TemplateArgument::Type: {
1968 QualType ToType = Importer.Import(From.getAsType());
1969 if (ToType.isNull())
1970 return TemplateArgument();
1971 return TemplateArgument(ToType);
1972 }
1973
1974 case TemplateArgument::Integral: {
1975 QualType ToType = Importer.Import(From.getIntegralType());
1976 if (ToType.isNull())
1977 return TemplateArgument();
1978 return TemplateArgument(*From.getAsIntegral(), ToType);
1979 }
1980
1981 case TemplateArgument::Declaration:
1982 if (Decl *To = Importer.Import(From.getAsDecl()))
1983 return TemplateArgument(To);
1984 return TemplateArgument();
1985
1986 case TemplateArgument::Template: {
1987 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1988 if (ToTemplate.isNull())
1989 return TemplateArgument();
1990
1991 return TemplateArgument(ToTemplate);
1992 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001993
1994 case TemplateArgument::TemplateExpansion: {
1995 TemplateName ToTemplate
1996 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1997 if (ToTemplate.isNull())
1998 return TemplateArgument();
1999
Douglas Gregor2be29f42011-01-14 23:41:42 +00002000 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00002001 }
2002
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002003 case TemplateArgument::Expression:
2004 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2005 return TemplateArgument(ToExpr);
2006 return TemplateArgument();
2007
2008 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002009 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002010 ToPack.reserve(From.pack_size());
2011 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2012 return TemplateArgument();
2013
2014 TemplateArgument *ToArgs
2015 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2016 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2017 return TemplateArgument(ToArgs, ToPack.size());
2018 }
2019 }
2020
2021 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002022}
2023
2024bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2025 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002026 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002027 for (unsigned I = 0; I != NumFromArgs; ++I) {
2028 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2029 if (To.isNull() && !FromArgs[I].isNull())
2030 return true;
2031
2032 ToArgs.push_back(To);
2033 }
2034
2035 return false;
2036}
2037
Douglas Gregor96a01b42010-02-11 00:48:18 +00002038bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002039 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002040 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002041 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002042 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002043 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002044}
2045
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002046bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002047 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002048 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002049 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002050 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002051}
2052
Douglas Gregor040afae2010-11-30 19:14:50 +00002053bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2054 ClassTemplateDecl *To) {
2055 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2056 Importer.getToContext(),
2057 Importer.getNonEquivalentDecls());
2058 return Ctx.IsStructurallyEquivalent(From, To);
2059}
2060
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002061Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002062 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002063 << D->getDeclKindName();
2064 return 0;
2065}
2066
Sean Callananf1b69462011-11-17 23:20:56 +00002067Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2068 TranslationUnitDecl *ToD =
2069 Importer.getToContext().getTranslationUnitDecl();
2070
2071 Importer.Imported(D, ToD);
2072
2073 return ToD;
2074}
2075
Douglas Gregor788c62d2010-02-21 18:26:36 +00002076Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2077 // Import the major distinguishing characteristics of this namespace.
2078 DeclContext *DC, *LexicalDC;
2079 DeclarationName Name;
2080 SourceLocation Loc;
2081 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2082 return 0;
2083
2084 NamespaceDecl *MergeWithNamespace = 0;
2085 if (!Name) {
2086 // This is an anonymous namespace. Adopt an existing anonymous
2087 // namespace if we can.
2088 // FIXME: Not testable.
2089 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2090 MergeWithNamespace = TU->getAnonymousNamespace();
2091 else
2092 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2093 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002094 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002095 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2096 DC->localUncachedLookup(Name, FoundDecls);
2097 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2098 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002099 continue;
2100
Douglas Gregorb75a3452011-10-15 00:10:27 +00002101 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002102 MergeWithNamespace = FoundNS;
2103 ConflictingDecls.clear();
2104 break;
2105 }
2106
Douglas Gregorb75a3452011-10-15 00:10:27 +00002107 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002108 }
2109
2110 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002111 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002112 ConflictingDecls.data(),
2113 ConflictingDecls.size());
2114 }
2115 }
2116
2117 // Create the "to" namespace, if needed.
2118 NamespaceDecl *ToNamespace = MergeWithNamespace;
2119 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002120 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002121 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002122 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002123 Loc, Name.getAsIdentifierInfo(),
2124 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002125 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002126 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002127
2128 // If this is an anonymous namespace, register it as the anonymous
2129 // namespace within its context.
2130 if (!Name) {
2131 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2132 TU->setAnonymousNamespace(ToNamespace);
2133 else
2134 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2135 }
2136 }
2137 Importer.Imported(D, ToNamespace);
2138
2139 ImportDeclContext(D);
2140
2141 return ToNamespace;
2142}
2143
Richard Smith162e1c12011-04-15 14:24:37 +00002144Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002145 // Import the major distinguishing characteristics of this typedef.
2146 DeclContext *DC, *LexicalDC;
2147 DeclarationName Name;
2148 SourceLocation Loc;
2149 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2150 return 0;
2151
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002152 // If this typedef is not in block scope, determine whether we've
2153 // seen a typedef with the same name (that we can merge with) or any
2154 // other entity by that name (which name lookup could conflict with).
2155 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002156 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002157 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002158 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2159 DC->localUncachedLookup(Name, FoundDecls);
2160 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2161 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002162 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002163 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002164 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002165 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2166 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002167 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002168 }
2169
Douglas Gregorb75a3452011-10-15 00:10:27 +00002170 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002171 }
2172
2173 if (!ConflictingDecls.empty()) {
2174 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2175 ConflictingDecls.data(),
2176 ConflictingDecls.size());
2177 if (!Name)
2178 return 0;
2179 }
2180 }
2181
Douglas Gregorea35d112010-02-15 23:54:17 +00002182 // Import the underlying type of this typedef;
2183 QualType T = Importer.Import(D->getUnderlyingType());
2184 if (T.isNull())
2185 return 0;
2186
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002187 // Create the new typedef node.
2188 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002189 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002190 TypedefNameDecl *ToTypedef;
2191 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002192 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2193 StartL, Loc,
2194 Name.getAsIdentifierInfo(),
2195 TInfo);
2196 else
Richard Smith162e1c12011-04-15 14:24:37 +00002197 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2198 StartL, Loc,
2199 Name.getAsIdentifierInfo(),
2200 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002201
Douglas Gregor325bf172010-02-22 17:42:47 +00002202 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002203 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002204 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002205 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002206
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002207 return ToTypedef;
2208}
2209
Richard Smith162e1c12011-04-15 14:24:37 +00002210Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2211 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2212}
2213
2214Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2215 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2216}
2217
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002218Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2219 // Import the major distinguishing characteristics of this enum.
2220 DeclContext *DC, *LexicalDC;
2221 DeclarationName Name;
2222 SourceLocation Loc;
2223 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2224 return 0;
2225
2226 // Figure out what enum name we're looking for.
2227 unsigned IDNS = Decl::IDNS_Tag;
2228 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002229 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2230 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002231 IDNS = Decl::IDNS_Ordinary;
2232 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2233 IDNS |= Decl::IDNS_Ordinary;
2234
2235 // We may already have an enum of the same name; try to find and match it.
2236 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002237 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002238 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2239 DC->localUncachedLookup(SearchName, FoundDecls);
2240 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2241 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002242 continue;
2243
Douglas Gregorb75a3452011-10-15 00:10:27 +00002244 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002245 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002246 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2247 Found = Tag->getDecl();
2248 }
2249
2250 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002251 if (IsStructuralMatch(D, FoundEnum))
2252 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002253 }
2254
Douglas Gregorb75a3452011-10-15 00:10:27 +00002255 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002256 }
2257
2258 if (!ConflictingDecls.empty()) {
2259 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2260 ConflictingDecls.data(),
2261 ConflictingDecls.size());
2262 }
2263 }
2264
2265 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002266 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2267 Importer.Import(D->getLocStart()),
2268 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002269 D->isScoped(), D->isScopedUsingClassTag(),
2270 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002271 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002272 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002273 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002274 D2->setLexicalDeclContext(LexicalDC);
2275 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002276 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002277
2278 // Import the integer type.
2279 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2280 if (ToIntegerType.isNull())
2281 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002282 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002283
2284 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002285 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002286 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002287
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002288 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002289}
2290
Douglas Gregor96a01b42010-02-11 00:48:18 +00002291Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2292 // If this record has a definition in the translation unit we're coming from,
2293 // but this particular declaration is not that definition, import the
2294 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002295 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002296 if (Definition && Definition != D) {
2297 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002298 if (!ImportedDef)
2299 return 0;
2300
2301 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002302 }
2303
2304 // Import the major distinguishing characteristics of this record.
2305 DeclContext *DC, *LexicalDC;
2306 DeclarationName Name;
2307 SourceLocation Loc;
2308 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2309 return 0;
2310
2311 // Figure out what structure name we're looking for.
2312 unsigned IDNS = Decl::IDNS_Tag;
2313 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002314 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2315 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002316 IDNS = Decl::IDNS_Ordinary;
2317 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2318 IDNS |= Decl::IDNS_Ordinary;
2319
2320 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002321 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002322 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002323 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002324 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2325 DC->localUncachedLookup(SearchName, FoundDecls);
2326 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2327 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002328 continue;
2329
Douglas Gregorb75a3452011-10-15 00:10:27 +00002330 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002331 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002332 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2333 Found = Tag->getDecl();
2334 }
2335
2336 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002337 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002338 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002339 // The record types structurally match, or the "from" translation
2340 // unit only had a forward declaration anyway; call it the same
2341 // function.
2342 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002343 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002344 }
2345 } else {
2346 // We have a forward declaration of this type, so adopt that forward
2347 // declaration rather than building a new one.
2348 AdoptDecl = FoundRecord;
2349 continue;
2350 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002351 }
2352
Douglas Gregorb75a3452011-10-15 00:10:27 +00002353 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002354 }
2355
2356 if (!ConflictingDecls.empty()) {
2357 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2358 ConflictingDecls.data(),
2359 ConflictingDecls.size());
2360 }
2361 }
2362
2363 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002364 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002365 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002366 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002367 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002368 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002369 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002370 DC, StartLoc, Loc,
2371 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002372 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002373 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002374 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002375 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002376 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002377 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002378
2379 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002380 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002381 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002382 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002383
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002384 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002385
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002386 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002387 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002388
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002389 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002390}
2391
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002392Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2393 // Import the major distinguishing characteristics of this enumerator.
2394 DeclContext *DC, *LexicalDC;
2395 DeclarationName Name;
2396 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002397 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002398 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002399
2400 QualType T = Importer.Import(D->getType());
2401 if (T.isNull())
2402 return 0;
2403
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002404 // Determine whether there are any other declarations with the same name and
2405 // in the same context.
2406 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002407 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002408 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002409 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2410 DC->localUncachedLookup(Name, FoundDecls);
2411 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2412 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002413 continue;
2414
Douglas Gregorb75a3452011-10-15 00:10:27 +00002415 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002416 }
2417
2418 if (!ConflictingDecls.empty()) {
2419 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2420 ConflictingDecls.data(),
2421 ConflictingDecls.size());
2422 if (!Name)
2423 return 0;
2424 }
2425 }
2426
2427 Expr *Init = Importer.Import(D->getInitExpr());
2428 if (D->getInitExpr() && !Init)
2429 return 0;
2430
2431 EnumConstantDecl *ToEnumerator
2432 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2433 Name.getAsIdentifierInfo(), T,
2434 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002435 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002436 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002437 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002438 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002439 return ToEnumerator;
2440}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002441
Douglas Gregora404ea62010-02-10 19:54:31 +00002442Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2443 // Import the major distinguishing characteristics of this function.
2444 DeclContext *DC, *LexicalDC;
2445 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002446 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002447 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002448 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002449
Douglas Gregora404ea62010-02-10 19:54:31 +00002450 // Try to find a function in our own ("to") context with the same name, same
2451 // type, and in the same context as the function we're importing.
2452 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002453 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002454 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002455 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2456 DC->localUncachedLookup(Name, FoundDecls);
2457 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2458 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002459 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002460
Douglas Gregorb75a3452011-10-15 00:10:27 +00002461 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002462 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2463 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002464 if (Importer.IsStructurallyEquivalent(D->getType(),
2465 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002466 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002467 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002468 }
2469
2470 // FIXME: Check for overloading more carefully, e.g., by boosting
2471 // Sema::IsOverload out to the AST library.
2472
2473 // Function overloading is okay in C++.
2474 if (Importer.getToContext().getLangOptions().CPlusPlus)
2475 continue;
2476
2477 // Complain about inconsistent function types.
2478 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002479 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002480 Importer.ToDiag(FoundFunction->getLocation(),
2481 diag::note_odr_value_here)
2482 << FoundFunction->getType();
2483 }
2484 }
2485
Douglas Gregorb75a3452011-10-15 00:10:27 +00002486 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002487 }
2488
2489 if (!ConflictingDecls.empty()) {
2490 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2491 ConflictingDecls.data(),
2492 ConflictingDecls.size());
2493 if (!Name)
2494 return 0;
2495 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002496 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002497
Abramo Bagnara25777432010-08-11 22:01:17 +00002498 DeclarationNameInfo NameInfo(Name, Loc);
2499 // Import additional name location/type info.
2500 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2501
Douglas Gregorea35d112010-02-15 23:54:17 +00002502 // Import the type.
2503 QualType T = Importer.Import(D->getType());
2504 if (T.isNull())
2505 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002506
2507 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002508 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002509 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2510 P != PEnd; ++P) {
2511 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2512 if (!ToP)
2513 return 0;
2514
2515 Parameters.push_back(ToP);
2516 }
2517
2518 // Create the imported function.
2519 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002520 FunctionDecl *ToFunction = 0;
2521 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2522 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2523 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002524 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002525 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002526 FromConstructor->isExplicit(),
2527 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002528 D->isImplicit(),
2529 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002530 } else if (isa<CXXDestructorDecl>(D)) {
2531 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2532 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002533 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002534 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002535 D->isInlineSpecified(),
2536 D->isImplicit());
2537 } else if (CXXConversionDecl *FromConversion
2538 = dyn_cast<CXXConversionDecl>(D)) {
2539 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2540 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002541 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002542 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002543 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002544 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002545 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002546 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002547 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2548 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2549 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002550 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002551 NameInfo, T, TInfo,
2552 Method->isStatic(),
2553 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002554 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002555 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002556 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002557 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002558 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002559 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002560 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002561 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002562 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002563 D->hasWrittenPrototype(),
2564 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002565 }
John McCallb6217662010-03-15 10:12:16 +00002566
2567 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002568 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002569 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002570 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002571 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2572 ToFunction->setTrivial(D->isTrivial());
2573 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002574 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002575
Douglas Gregora404ea62010-02-10 19:54:31 +00002576 // Set the parameters.
2577 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002578 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002579 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002580 }
David Blaikie4278c652011-09-21 18:16:56 +00002581 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002582
2583 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002584
2585 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002586 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002587
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002588 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002589}
2590
Douglas Gregorc144f352010-02-21 18:29:16 +00002591Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2592 return VisitFunctionDecl(D);
2593}
2594
2595Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2596 return VisitCXXMethodDecl(D);
2597}
2598
2599Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2600 return VisitCXXMethodDecl(D);
2601}
2602
2603Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2604 return VisitCXXMethodDecl(D);
2605}
2606
Douglas Gregor96a01b42010-02-11 00:48:18 +00002607Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2608 // Import the major distinguishing characteristics of a variable.
2609 DeclContext *DC, *LexicalDC;
2610 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002611 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002612 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2613 return 0;
2614
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002615 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002616 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2617 DC->localUncachedLookup(Name, FoundDecls);
2618 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2619 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002620 if (Importer.IsStructurallyEquivalent(D->getType(),
2621 FoundField->getType())) {
2622 Importer.Imported(D, FoundField);
2623 return FoundField;
2624 }
2625
2626 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2627 << Name << D->getType() << FoundField->getType();
2628 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2629 << FoundField->getType();
2630 return 0;
2631 }
2632 }
2633
Douglas Gregorea35d112010-02-15 23:54:17 +00002634 // Import the type.
2635 QualType T = Importer.Import(D->getType());
2636 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002637 return 0;
2638
2639 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2640 Expr *BitWidth = Importer.Import(D->getBitWidth());
2641 if (!BitWidth && D->getBitWidth())
2642 return 0;
2643
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002644 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2645 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002646 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002647 T, TInfo, BitWidth, D->isMutable(),
2648 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002649 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002650 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002651 if (ToField->hasInClassInitializer())
2652 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002653 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002654 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002655 return ToField;
2656}
2657
Francois Pichet87c2e122010-11-21 06:08:52 +00002658Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2659 // Import the major distinguishing characteristics of a variable.
2660 DeclContext *DC, *LexicalDC;
2661 DeclarationName Name;
2662 SourceLocation Loc;
2663 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2664 return 0;
2665
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002666 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002667 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2668 DC->localUncachedLookup(Name, FoundDecls);
2669 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002670 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002671 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002672 if (Importer.IsStructurallyEquivalent(D->getType(),
2673 FoundField->getType())) {
2674 Importer.Imported(D, FoundField);
2675 return FoundField;
2676 }
2677
2678 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2679 << Name << D->getType() << FoundField->getType();
2680 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2681 << FoundField->getType();
2682 return 0;
2683 }
2684 }
2685
Francois Pichet87c2e122010-11-21 06:08:52 +00002686 // Import the type.
2687 QualType T = Importer.Import(D->getType());
2688 if (T.isNull())
2689 return 0;
2690
2691 NamedDecl **NamedChain =
2692 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2693
2694 unsigned i = 0;
2695 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2696 PE = D->chain_end(); PI != PE; ++PI) {
2697 Decl* D = Importer.Import(*PI);
2698 if (!D)
2699 return 0;
2700 NamedChain[i++] = cast<NamedDecl>(D);
2701 }
2702
2703 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2704 Importer.getToContext(), DC,
2705 Loc, Name.getAsIdentifierInfo(), T,
2706 NamedChain, D->getChainingSize());
2707 ToIndirectField->setAccess(D->getAccess());
2708 ToIndirectField->setLexicalDeclContext(LexicalDC);
2709 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002710 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002711 return ToIndirectField;
2712}
2713
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002714Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2715 // Import the major distinguishing characteristics of an ivar.
2716 DeclContext *DC, *LexicalDC;
2717 DeclarationName Name;
2718 SourceLocation Loc;
2719 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2720 return 0;
2721
2722 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002723 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2724 DC->localUncachedLookup(Name, FoundDecls);
2725 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2726 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002727 if (Importer.IsStructurallyEquivalent(D->getType(),
2728 FoundIvar->getType())) {
2729 Importer.Imported(D, FoundIvar);
2730 return FoundIvar;
2731 }
2732
2733 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2734 << Name << D->getType() << FoundIvar->getType();
2735 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2736 << FoundIvar->getType();
2737 return 0;
2738 }
2739 }
2740
2741 // Import the type.
2742 QualType T = Importer.Import(D->getType());
2743 if (T.isNull())
2744 return 0;
2745
2746 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2747 Expr *BitWidth = Importer.Import(D->getBitWidth());
2748 if (!BitWidth && D->getBitWidth())
2749 return 0;
2750
Daniel Dunbara0654922010-04-02 20:10:03 +00002751 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2752 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002753 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002754 Loc, Name.getAsIdentifierInfo(),
2755 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002756 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002757 ToIvar->setLexicalDeclContext(LexicalDC);
2758 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002759 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002760 return ToIvar;
2761
2762}
2763
Douglas Gregora404ea62010-02-10 19:54:31 +00002764Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2765 // Import the major distinguishing characteristics of a variable.
2766 DeclContext *DC, *LexicalDC;
2767 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002768 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002769 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002770 return 0;
2771
Douglas Gregor089459a2010-02-08 21:09:39 +00002772 // Try to find a variable in our own ("to") context with the same name and
2773 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002774 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002775 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002776 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002777 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002778 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2779 DC->localUncachedLookup(Name, FoundDecls);
2780 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2781 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002782 continue;
2783
Douglas Gregorb75a3452011-10-15 00:10:27 +00002784 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002785 // We have found a variable that we may need to merge with. Check it.
2786 if (isExternalLinkage(FoundVar->getLinkage()) &&
2787 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002788 if (Importer.IsStructurallyEquivalent(D->getType(),
2789 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002790 MergeWithVar = FoundVar;
2791 break;
2792 }
2793
Douglas Gregord0145422010-02-12 17:23:39 +00002794 const ArrayType *FoundArray
2795 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2796 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002797 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002798 if (FoundArray && TArray) {
2799 if (isa<IncompleteArrayType>(FoundArray) &&
2800 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002801 // Import the type.
2802 QualType T = Importer.Import(D->getType());
2803 if (T.isNull())
2804 return 0;
2805
Douglas Gregord0145422010-02-12 17:23:39 +00002806 FoundVar->setType(T);
2807 MergeWithVar = FoundVar;
2808 break;
2809 } else if (isa<IncompleteArrayType>(TArray) &&
2810 isa<ConstantArrayType>(FoundArray)) {
2811 MergeWithVar = FoundVar;
2812 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002813 }
2814 }
2815
Douglas Gregor089459a2010-02-08 21:09:39 +00002816 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002817 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002818 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2819 << FoundVar->getType();
2820 }
2821 }
2822
Douglas Gregorb75a3452011-10-15 00:10:27 +00002823 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002824 }
2825
2826 if (MergeWithVar) {
2827 // An equivalent variable with external linkage has been found. Link
2828 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002829 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002830
2831 if (VarDecl *DDef = D->getDefinition()) {
2832 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2833 Importer.ToDiag(ExistingDef->getLocation(),
2834 diag::err_odr_variable_multiple_def)
2835 << Name;
2836 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2837 } else {
2838 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002839 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002840 if (DDef->isInitKnownICE()) {
2841 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2842 Eval->CheckedICE = true;
2843 Eval->IsICE = DDef->isInitICE();
2844 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002845 }
2846 }
2847
2848 return MergeWithVar;
2849 }
2850
2851 if (!ConflictingDecls.empty()) {
2852 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2853 ConflictingDecls.data(),
2854 ConflictingDecls.size());
2855 if (!Name)
2856 return 0;
2857 }
2858 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002859
Douglas Gregorea35d112010-02-15 23:54:17 +00002860 // Import the type.
2861 QualType T = Importer.Import(D->getType());
2862 if (T.isNull())
2863 return 0;
2864
Douglas Gregor089459a2010-02-08 21:09:39 +00002865 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002866 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002867 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2868 Importer.Import(D->getInnerLocStart()),
2869 Loc, Name.getAsIdentifierInfo(),
2870 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002871 D->getStorageClass(),
2872 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002873 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002874 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002875 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002876 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002877 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002878
Douglas Gregor089459a2010-02-08 21:09:39 +00002879 // Merge the initializer.
2880 // FIXME: Can we really import any initializer? Alternatively, we could force
2881 // ourselves to import every declaration of a variable and then only use
2882 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002883 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002884
2885 // FIXME: Other bits to merge?
2886
2887 return ToVar;
2888}
2889
Douglas Gregor2cd00932010-02-17 21:22:52 +00002890Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2891 // Parameters are created in the translation unit's context, then moved
2892 // into the function declaration's context afterward.
2893 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2894
2895 // Import the name of this declaration.
2896 DeclarationName Name = Importer.Import(D->getDeclName());
2897 if (D->getDeclName() && !Name)
2898 return 0;
2899
2900 // Import the location of this declaration.
2901 SourceLocation Loc = Importer.Import(D->getLocation());
2902
2903 // Import the parameter's type.
2904 QualType T = Importer.Import(D->getType());
2905 if (T.isNull())
2906 return 0;
2907
2908 // Create the imported parameter.
2909 ImplicitParamDecl *ToParm
2910 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2911 Loc, Name.getAsIdentifierInfo(),
2912 T);
2913 return Importer.Imported(D, ToParm);
2914}
2915
Douglas Gregora404ea62010-02-10 19:54:31 +00002916Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2917 // Parameters are created in the translation unit's context, then moved
2918 // into the function declaration's context afterward.
2919 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2920
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002921 // Import the name of this declaration.
2922 DeclarationName Name = Importer.Import(D->getDeclName());
2923 if (D->getDeclName() && !Name)
2924 return 0;
2925
Douglas Gregora404ea62010-02-10 19:54:31 +00002926 // Import the location of this declaration.
2927 SourceLocation Loc = Importer.Import(D->getLocation());
2928
2929 // Import the parameter's type.
2930 QualType T = Importer.Import(D->getType());
2931 if (T.isNull())
2932 return 0;
2933
2934 // Create the imported parameter.
2935 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2936 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002937 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002938 Loc, Name.getAsIdentifierInfo(),
2939 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002940 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002941 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002942 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002943 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002944}
2945
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002946Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2947 // Import the major distinguishing characteristics of a method.
2948 DeclContext *DC, *LexicalDC;
2949 DeclarationName Name;
2950 SourceLocation Loc;
2951 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2952 return 0;
2953
Douglas Gregorb75a3452011-10-15 00:10:27 +00002954 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2955 DC->localUncachedLookup(Name, FoundDecls);
2956 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2957 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002958 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2959 continue;
2960
2961 // Check return types.
2962 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2963 FoundMethod->getResultType())) {
2964 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2965 << D->isInstanceMethod() << Name
2966 << D->getResultType() << FoundMethod->getResultType();
2967 Importer.ToDiag(FoundMethod->getLocation(),
2968 diag::note_odr_objc_method_here)
2969 << D->isInstanceMethod() << Name;
2970 return 0;
2971 }
2972
2973 // Check the number of parameters.
2974 if (D->param_size() != FoundMethod->param_size()) {
2975 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2976 << D->isInstanceMethod() << Name
2977 << D->param_size() << FoundMethod->param_size();
2978 Importer.ToDiag(FoundMethod->getLocation(),
2979 diag::note_odr_objc_method_here)
2980 << D->isInstanceMethod() << Name;
2981 return 0;
2982 }
2983
2984 // Check parameter types.
2985 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2986 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2987 P != PEnd; ++P, ++FoundP) {
2988 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2989 (*FoundP)->getType())) {
2990 Importer.FromDiag((*P)->getLocation(),
2991 diag::err_odr_objc_method_param_type_inconsistent)
2992 << D->isInstanceMethod() << Name
2993 << (*P)->getType() << (*FoundP)->getType();
2994 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2995 << (*FoundP)->getType();
2996 return 0;
2997 }
2998 }
2999
3000 // Check variadic/non-variadic.
3001 // Check the number of parameters.
3002 if (D->isVariadic() != FoundMethod->isVariadic()) {
3003 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3004 << D->isInstanceMethod() << Name;
3005 Importer.ToDiag(FoundMethod->getLocation(),
3006 diag::note_odr_objc_method_here)
3007 << D->isInstanceMethod() << Name;
3008 return 0;
3009 }
3010
3011 // FIXME: Any other bits we need to merge?
3012 return Importer.Imported(D, FoundMethod);
3013 }
3014 }
3015
3016 // Import the result type.
3017 QualType ResultTy = Importer.Import(D->getResultType());
3018 if (ResultTy.isNull())
3019 return 0;
3020
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003021 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3022
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003023 ObjCMethodDecl *ToMethod
3024 = ObjCMethodDecl::Create(Importer.getToContext(),
3025 Loc,
3026 Importer.Import(D->getLocEnd()),
3027 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003028 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003029 D->isInstanceMethod(),
3030 D->isVariadic(),
3031 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003032 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003033 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00003034 D->getImplementationControl(),
3035 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003036
3037 // FIXME: When we decide to merge method definitions, we'll need to
3038 // deal with implicit parameters.
3039
3040 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003041 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003042 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3043 FromPEnd = D->param_end();
3044 FromP != FromPEnd;
3045 ++FromP) {
3046 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3047 if (!ToP)
3048 return 0;
3049
3050 ToParams.push_back(ToP);
3051 }
3052
3053 // Set the parameters.
3054 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3055 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003056 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003057 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003058 SmallVector<SourceLocation, 12> SelLocs;
3059 D->getSelectorLocs(SelLocs);
3060 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003061
3062 ToMethod->setLexicalDeclContext(LexicalDC);
3063 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003064 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003065 return ToMethod;
3066}
3067
Douglas Gregorb4677b62010-02-18 01:47:50 +00003068Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3069 // Import the major distinguishing characteristics of a category.
3070 DeclContext *DC, *LexicalDC;
3071 DeclarationName Name;
3072 SourceLocation Loc;
3073 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3074 return 0;
3075
3076 ObjCInterfaceDecl *ToInterface
3077 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3078 if (!ToInterface)
3079 return 0;
3080
3081 // Determine if we've already encountered this category.
3082 ObjCCategoryDecl *MergeWithCategory
3083 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3084 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3085 if (!ToCategory) {
3086 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003087 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003088 Loc,
3089 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003090 Name.getAsIdentifierInfo(),
3091 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003092 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003093 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003094 Importer.Imported(D, ToCategory);
3095
Douglas Gregorb4677b62010-02-18 01:47:50 +00003096 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003097 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3098 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003099 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3100 = D->protocol_loc_begin();
3101 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3102 FromProtoEnd = D->protocol_end();
3103 FromProto != FromProtoEnd;
3104 ++FromProto, ++FromProtoLoc) {
3105 ObjCProtocolDecl *ToProto
3106 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3107 if (!ToProto)
3108 return 0;
3109 Protocols.push_back(ToProto);
3110 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3111 }
3112
3113 // FIXME: If we're merging, make sure that the protocol list is the same.
3114 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3115 ProtocolLocs.data(), Importer.getToContext());
3116
3117 } else {
3118 Importer.Imported(D, ToCategory);
3119 }
3120
3121 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003122 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003123
3124 // If we have an implementation, import it as well.
3125 if (D->getImplementation()) {
3126 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003127 = cast_or_null<ObjCCategoryImplDecl>(
3128 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003129 if (!Impl)
3130 return 0;
3131
3132 ToCategory->setImplementation(Impl);
3133 }
3134
3135 return ToCategory;
3136}
3137
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003138bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3139 ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003140 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003141 if (To->getDefinition()) {
Douglas Gregorac32ff92012-02-01 21:00:38 +00003142 if (shouldForceImportDeclContext(Kind))
3143 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003144 return false;
3145 }
3146
3147 // Start the protocol definition
3148 To->startDefinition();
3149
3150 // Import protocols
3151 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3152 SmallVector<SourceLocation, 4> ProtocolLocs;
3153 ObjCProtocolDecl::protocol_loc_iterator
3154 FromProtoLoc = From->protocol_loc_begin();
3155 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3156 FromProtoEnd = From->protocol_end();
3157 FromProto != FromProtoEnd;
3158 ++FromProto, ++FromProtoLoc) {
3159 ObjCProtocolDecl *ToProto
3160 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3161 if (!ToProto)
3162 return true;
3163 Protocols.push_back(ToProto);
3164 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3165 }
3166
3167 // FIXME: If we're merging, make sure that the protocol list is the same.
3168 To->setProtocolList(Protocols.data(), Protocols.size(),
3169 ProtocolLocs.data(), Importer.getToContext());
3170
Douglas Gregorac32ff92012-02-01 21:00:38 +00003171 if (shouldForceImportDeclContext(Kind)) {
3172 // Import all of the members of this protocol.
3173 ImportDeclContext(From, /*ForceImport=*/true);
3174 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003175 return false;
3176}
3177
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003178Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003179 // If this protocol has a definition in the translation unit we're coming
3180 // from, but this particular declaration is not that definition, import the
3181 // definition and map to that.
3182 ObjCProtocolDecl *Definition = D->getDefinition();
3183 if (Definition && Definition != D) {
3184 Decl *ImportedDef = Importer.Import(Definition);
3185 if (!ImportedDef)
3186 return 0;
3187
3188 return Importer.Imported(D, ImportedDef);
3189 }
3190
Douglas Gregorb4677b62010-02-18 01:47:50 +00003191 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003192 DeclContext *DC, *LexicalDC;
3193 DeclarationName Name;
3194 SourceLocation Loc;
3195 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3196 return 0;
3197
3198 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003199 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3200 DC->localUncachedLookup(Name, FoundDecls);
3201 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3202 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003203 continue;
3204
Douglas Gregorb75a3452011-10-15 00:10:27 +00003205 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003206 break;
3207 }
3208
3209 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003210 if (!ToProto) {
3211 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3212 Name.getAsIdentifierInfo(), Loc,
3213 Importer.Import(D->getAtStartLoc()),
3214 /*PrevDecl=*/0);
3215 ToProto->setLexicalDeclContext(LexicalDC);
3216 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003217 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003218
3219 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003220
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003221 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3222 return 0;
3223
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003224 return ToProto;
3225}
3226
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003227bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3228 ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003229 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003230 if (To->getDefinition()) {
3231 // Check consistency of superclass.
3232 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3233 if (FromSuper) {
3234 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3235 if (!FromSuper)
3236 return true;
3237 }
3238
3239 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3240 if ((bool)FromSuper != (bool)ToSuper ||
3241 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3242 Importer.ToDiag(To->getLocation(),
3243 diag::err_odr_objc_superclass_inconsistent)
3244 << To->getDeclName();
3245 if (ToSuper)
3246 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3247 << To->getSuperClass()->getDeclName();
3248 else
3249 Importer.ToDiag(To->getLocation(),
3250 diag::note_odr_objc_missing_superclass);
3251 if (From->getSuperClass())
3252 Importer.FromDiag(From->getSuperClassLoc(),
3253 diag::note_odr_objc_superclass)
3254 << From->getSuperClass()->getDeclName();
3255 else
3256 Importer.FromDiag(From->getLocation(),
3257 diag::note_odr_objc_missing_superclass);
3258 }
3259
Douglas Gregorac32ff92012-02-01 21:00:38 +00003260 if (shouldForceImportDeclContext(Kind))
3261 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003262 return false;
3263 }
3264
3265 // Start the definition.
3266 To->startDefinition();
3267
3268 // If this class has a superclass, import it.
3269 if (From->getSuperClass()) {
3270 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3271 Importer.Import(From->getSuperClass()));
3272 if (!Super)
3273 return true;
3274
3275 To->setSuperClass(Super);
3276 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3277 }
3278
3279 // Import protocols
3280 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3281 SmallVector<SourceLocation, 4> ProtocolLocs;
3282 ObjCInterfaceDecl::protocol_loc_iterator
3283 FromProtoLoc = From->protocol_loc_begin();
3284
3285 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3286 FromProtoEnd = From->protocol_end();
3287 FromProto != FromProtoEnd;
3288 ++FromProto, ++FromProtoLoc) {
3289 ObjCProtocolDecl *ToProto
3290 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3291 if (!ToProto)
3292 return true;
3293 Protocols.push_back(ToProto);
3294 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3295 }
3296
3297 // FIXME: If we're merging, make sure that the protocol list is the same.
3298 To->setProtocolList(Protocols.data(), Protocols.size(),
3299 ProtocolLocs.data(), Importer.getToContext());
3300
3301 // Import categories. When the categories themselves are imported, they'll
3302 // hook themselves into this interface.
3303 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3304 FromCat = FromCat->getNextClassCategory())
3305 Importer.Import(FromCat);
3306
3307 // If we have an @implementation, import it as well.
3308 if (From->getImplementation()) {
3309 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3310 Importer.Import(From->getImplementation()));
3311 if (!Impl)
3312 return true;
3313
3314 To->setImplementation(Impl);
3315 }
3316
Douglas Gregorac32ff92012-02-01 21:00:38 +00003317 if (shouldForceImportDeclContext(Kind)) {
3318 // Import all of the members of this class.
3319 ImportDeclContext(From, /*ForceImport=*/true);
3320 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003321 return false;
3322}
3323
Douglas Gregora12d2942010-02-16 01:20:57 +00003324Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003325 // If this class has a definition in the translation unit we're coming from,
3326 // but this particular declaration is not that definition, import the
3327 // definition and map to that.
3328 ObjCInterfaceDecl *Definition = D->getDefinition();
3329 if (Definition && Definition != D) {
3330 Decl *ImportedDef = Importer.Import(Definition);
3331 if (!ImportedDef)
3332 return 0;
3333
3334 return Importer.Imported(D, ImportedDef);
3335 }
3336
Douglas Gregora12d2942010-02-16 01:20:57 +00003337 // Import the major distinguishing characteristics of an @interface.
3338 DeclContext *DC, *LexicalDC;
3339 DeclarationName Name;
3340 SourceLocation Loc;
3341 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3342 return 0;
3343
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003344 // Look for an existing interface with the same name.
Douglas Gregora12d2942010-02-16 01:20:57 +00003345 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003346 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3347 DC->localUncachedLookup(Name, FoundDecls);
3348 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3349 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003350 continue;
3351
Douglas Gregorb75a3452011-10-15 00:10:27 +00003352 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003353 break;
3354 }
3355
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003356 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003357 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003358 if (!ToIface) {
3359 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3360 Importer.Import(D->getAtStartLoc()),
3361 Name.getAsIdentifierInfo(),
3362 /*PrevDecl=*/0,Loc,
3363 D->isImplicitInterfaceDecl());
3364 ToIface->setLexicalDeclContext(LexicalDC);
3365 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003366 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003367 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003368
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003369 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3370 return 0;
Douglas Gregora12d2942010-02-16 01:20:57 +00003371
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003372 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003373}
3374
Douglas Gregor3daef292010-12-07 15:32:12 +00003375Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3376 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3377 Importer.Import(D->getCategoryDecl()));
3378 if (!Category)
3379 return 0;
3380
3381 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3382 if (!ToImpl) {
3383 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3384 if (!DC)
3385 return 0;
3386
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003387 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003388 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003389 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003390 Category->getClassInterface(),
3391 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003392 Importer.Import(D->getAtStartLoc()),
3393 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003394
3395 DeclContext *LexicalDC = DC;
3396 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3397 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3398 if (!LexicalDC)
3399 return 0;
3400
3401 ToImpl->setLexicalDeclContext(LexicalDC);
3402 }
3403
Sean Callanan9faf8102011-10-21 02:57:43 +00003404 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003405 Category->setImplementation(ToImpl);
3406 }
3407
3408 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003409 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003410 return ToImpl;
3411}
3412
Douglas Gregordd182ff2010-12-07 01:26:03 +00003413Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3414 // Find the corresponding interface.
3415 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3416 Importer.Import(D->getClassInterface()));
3417 if (!Iface)
3418 return 0;
3419
3420 // Import the superclass, if any.
3421 ObjCInterfaceDecl *Super = 0;
3422 if (D->getSuperClass()) {
3423 Super = cast_or_null<ObjCInterfaceDecl>(
3424 Importer.Import(D->getSuperClass()));
3425 if (!Super)
3426 return 0;
3427 }
3428
3429 ObjCImplementationDecl *Impl = Iface->getImplementation();
3430 if (!Impl) {
3431 // We haven't imported an implementation yet. Create a new @implementation
3432 // now.
3433 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3434 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003435 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003436 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003437 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003438
3439 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3440 DeclContext *LexicalDC
3441 = Importer.ImportContext(D->getLexicalDeclContext());
3442 if (!LexicalDC)
3443 return 0;
3444 Impl->setLexicalDeclContext(LexicalDC);
3445 }
3446
3447 // Associate the implementation with the class it implements.
3448 Iface->setImplementation(Impl);
3449 Importer.Imported(D, Iface->getImplementation());
3450 } else {
3451 Importer.Imported(D, Iface->getImplementation());
3452
3453 // Verify that the existing @implementation has the same superclass.
3454 if ((Super && !Impl->getSuperClass()) ||
3455 (!Super && Impl->getSuperClass()) ||
3456 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003457 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003458 Importer.ToDiag(Impl->getLocation(),
3459 diag::err_odr_objc_superclass_inconsistent)
3460 << Iface->getDeclName();
3461 // FIXME: It would be nice to have the location of the superclass
3462 // below.
3463 if (Impl->getSuperClass())
3464 Importer.ToDiag(Impl->getLocation(),
3465 diag::note_odr_objc_superclass)
3466 << Impl->getSuperClass()->getDeclName();
3467 else
3468 Importer.ToDiag(Impl->getLocation(),
3469 diag::note_odr_objc_missing_superclass);
3470 if (D->getSuperClass())
3471 Importer.FromDiag(D->getLocation(),
3472 diag::note_odr_objc_superclass)
3473 << D->getSuperClass()->getDeclName();
3474 else
3475 Importer.FromDiag(D->getLocation(),
3476 diag::note_odr_objc_missing_superclass);
3477 return 0;
3478 }
3479 }
3480
3481 // Import all of the members of this @implementation.
3482 ImportDeclContext(D);
3483
3484 return Impl;
3485}
3486
Douglas Gregore3261622010-02-17 18:02:10 +00003487Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3488 // Import the major distinguishing characteristics of an @property.
3489 DeclContext *DC, *LexicalDC;
3490 DeclarationName Name;
3491 SourceLocation Loc;
3492 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3493 return 0;
3494
3495 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003496 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3497 DC->localUncachedLookup(Name, FoundDecls);
3498 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003499 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003500 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003501 // Check property types.
3502 if (!Importer.IsStructurallyEquivalent(D->getType(),
3503 FoundProp->getType())) {
3504 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3505 << Name << D->getType() << FoundProp->getType();
3506 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3507 << FoundProp->getType();
3508 return 0;
3509 }
3510
3511 // FIXME: Check property attributes, getters, setters, etc.?
3512
3513 // Consider these properties to be equivalent.
3514 Importer.Imported(D, FoundProp);
3515 return FoundProp;
3516 }
3517 }
3518
3519 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003520 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3521 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003522 return 0;
3523
3524 // Create the new property.
3525 ObjCPropertyDecl *ToProperty
3526 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3527 Name.getAsIdentifierInfo(),
3528 Importer.Import(D->getAtLoc()),
3529 T,
3530 D->getPropertyImplementation());
3531 Importer.Imported(D, ToProperty);
3532 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003533 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003534
3535 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003536 ToProperty->setPropertyAttributesAsWritten(
3537 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003538 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3539 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3540 ToProperty->setGetterMethodDecl(
3541 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3542 ToProperty->setSetterMethodDecl(
3543 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3544 ToProperty->setPropertyIvarDecl(
3545 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3546 return ToProperty;
3547}
3548
Douglas Gregor954e0c72010-12-07 18:32:03 +00003549Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3550 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3551 Importer.Import(D->getPropertyDecl()));
3552 if (!Property)
3553 return 0;
3554
3555 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3556 if (!DC)
3557 return 0;
3558
3559 // Import the lexical declaration context.
3560 DeclContext *LexicalDC = DC;
3561 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3562 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3563 if (!LexicalDC)
3564 return 0;
3565 }
3566
3567 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3568 if (!InImpl)
3569 return 0;
3570
3571 // Import the ivar (for an @synthesize).
3572 ObjCIvarDecl *Ivar = 0;
3573 if (D->getPropertyIvarDecl()) {
3574 Ivar = cast_or_null<ObjCIvarDecl>(
3575 Importer.Import(D->getPropertyIvarDecl()));
3576 if (!Ivar)
3577 return 0;
3578 }
3579
3580 ObjCPropertyImplDecl *ToImpl
3581 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3582 if (!ToImpl) {
3583 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3584 Importer.Import(D->getLocStart()),
3585 Importer.Import(D->getLocation()),
3586 Property,
3587 D->getPropertyImplementation(),
3588 Ivar,
3589 Importer.Import(D->getPropertyIvarDeclLoc()));
3590 ToImpl->setLexicalDeclContext(LexicalDC);
3591 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003592 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003593 } else {
3594 // Check that we have the same kind of property implementation (@synthesize
3595 // vs. @dynamic).
3596 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3597 Importer.ToDiag(ToImpl->getLocation(),
3598 diag::err_odr_objc_property_impl_kind_inconsistent)
3599 << Property->getDeclName()
3600 << (ToImpl->getPropertyImplementation()
3601 == ObjCPropertyImplDecl::Dynamic);
3602 Importer.FromDiag(D->getLocation(),
3603 diag::note_odr_objc_property_impl_kind)
3604 << D->getPropertyDecl()->getDeclName()
3605 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3606 return 0;
3607 }
3608
3609 // For @synthesize, check that we have the same
3610 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3611 Ivar != ToImpl->getPropertyIvarDecl()) {
3612 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3613 diag::err_odr_objc_synthesize_ivar_inconsistent)
3614 << Property->getDeclName()
3615 << ToImpl->getPropertyIvarDecl()->getDeclName()
3616 << Ivar->getDeclName();
3617 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3618 diag::note_odr_objc_synthesize_ivar_here)
3619 << D->getPropertyIvarDecl()->getDeclName();
3620 return 0;
3621 }
3622
3623 // Merge the existing implementation with the new implementation.
3624 Importer.Imported(D, ToImpl);
3625 }
3626
3627 return ToImpl;
3628}
3629
Douglas Gregor040afae2010-11-30 19:14:50 +00003630Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3631 // For template arguments, we adopt the translation unit as our declaration
3632 // context. This context will be fixed when the actual template declaration
3633 // is created.
3634
3635 // FIXME: Import default argument.
3636 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3637 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003638 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003639 Importer.Import(D->getLocation()),
3640 D->getDepth(),
3641 D->getIndex(),
3642 Importer.Import(D->getIdentifier()),
3643 D->wasDeclaredWithTypename(),
3644 D->isParameterPack());
3645}
3646
3647Decl *
3648ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3649 // Import the name of this declaration.
3650 DeclarationName Name = Importer.Import(D->getDeclName());
3651 if (D->getDeclName() && !Name)
3652 return 0;
3653
3654 // Import the location of this declaration.
3655 SourceLocation Loc = Importer.Import(D->getLocation());
3656
3657 // Import the type of this declaration.
3658 QualType T = Importer.Import(D->getType());
3659 if (T.isNull())
3660 return 0;
3661
3662 // Import type-source information.
3663 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3664 if (D->getTypeSourceInfo() && !TInfo)
3665 return 0;
3666
3667 // FIXME: Import default argument.
3668
3669 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3670 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003671 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003672 Loc, D->getDepth(), D->getPosition(),
3673 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003674 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003675}
3676
3677Decl *
3678ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3679 // Import the name of this declaration.
3680 DeclarationName Name = Importer.Import(D->getDeclName());
3681 if (D->getDeclName() && !Name)
3682 return 0;
3683
3684 // Import the location of this declaration.
3685 SourceLocation Loc = Importer.Import(D->getLocation());
3686
3687 // Import template parameters.
3688 TemplateParameterList *TemplateParams
3689 = ImportTemplateParameterList(D->getTemplateParameters());
3690 if (!TemplateParams)
3691 return 0;
3692
3693 // FIXME: Import default argument.
3694
3695 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3696 Importer.getToContext().getTranslationUnitDecl(),
3697 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003698 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003699 Name.getAsIdentifierInfo(),
3700 TemplateParams);
3701}
3702
3703Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3704 // If this record has a definition in the translation unit we're coming from,
3705 // but this particular declaration is not that definition, import the
3706 // definition and map to that.
3707 CXXRecordDecl *Definition
3708 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3709 if (Definition && Definition != D->getTemplatedDecl()) {
3710 Decl *ImportedDef
3711 = Importer.Import(Definition->getDescribedClassTemplate());
3712 if (!ImportedDef)
3713 return 0;
3714
3715 return Importer.Imported(D, ImportedDef);
3716 }
3717
3718 // Import the major distinguishing characteristics of this class template.
3719 DeclContext *DC, *LexicalDC;
3720 DeclarationName Name;
3721 SourceLocation Loc;
3722 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3723 return 0;
3724
3725 // We may already have a template of the same name; try to find and match it.
3726 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003727 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003728 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3729 DC->localUncachedLookup(Name, FoundDecls);
3730 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3731 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003732 continue;
3733
Douglas Gregorb75a3452011-10-15 00:10:27 +00003734 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003735 if (ClassTemplateDecl *FoundTemplate
3736 = dyn_cast<ClassTemplateDecl>(Found)) {
3737 if (IsStructuralMatch(D, FoundTemplate)) {
3738 // The class templates structurally match; call it the same template.
3739 // FIXME: We may be filling in a forward declaration here. Handle
3740 // this case!
3741 Importer.Imported(D->getTemplatedDecl(),
3742 FoundTemplate->getTemplatedDecl());
3743 return Importer.Imported(D, FoundTemplate);
3744 }
3745 }
3746
Douglas Gregorb75a3452011-10-15 00:10:27 +00003747 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003748 }
3749
3750 if (!ConflictingDecls.empty()) {
3751 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3752 ConflictingDecls.data(),
3753 ConflictingDecls.size());
3754 }
3755
3756 if (!Name)
3757 return 0;
3758 }
3759
3760 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3761
3762 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003763 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3764 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003765 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3766 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003767 DC, StartLoc, IdLoc,
3768 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003769 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003770 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003771 D2Templated->setLexicalDeclContext(LexicalDC);
3772
3773 // Create the class template declaration itself.
3774 TemplateParameterList *TemplateParams
3775 = ImportTemplateParameterList(D->getTemplateParameters());
3776 if (!TemplateParams)
3777 return 0;
3778
3779 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3780 Loc, Name, TemplateParams,
3781 D2Templated,
3782 /*PrevDecl=*/0);
3783 D2Templated->setDescribedClassTemplate(D2);
3784
3785 D2->setAccess(D->getAccess());
3786 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003787 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003788
3789 // Note the relationship between the class templates.
3790 Importer.Imported(D, D2);
3791 Importer.Imported(DTemplated, D2Templated);
3792
John McCall5e1cdac2011-10-07 06:10:15 +00003793 if (DTemplated->isCompleteDefinition() &&
3794 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003795 // FIXME: Import definition!
3796 }
3797
3798 return D2;
3799}
3800
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003801Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3802 ClassTemplateSpecializationDecl *D) {
3803 // If this record has a definition in the translation unit we're coming from,
3804 // but this particular declaration is not that definition, import the
3805 // definition and map to that.
3806 TagDecl *Definition = D->getDefinition();
3807 if (Definition && Definition != D) {
3808 Decl *ImportedDef = Importer.Import(Definition);
3809 if (!ImportedDef)
3810 return 0;
3811
3812 return Importer.Imported(D, ImportedDef);
3813 }
3814
3815 ClassTemplateDecl *ClassTemplate
3816 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3817 D->getSpecializedTemplate()));
3818 if (!ClassTemplate)
3819 return 0;
3820
3821 // Import the context of this declaration.
3822 DeclContext *DC = ClassTemplate->getDeclContext();
3823 if (!DC)
3824 return 0;
3825
3826 DeclContext *LexicalDC = DC;
3827 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3828 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3829 if (!LexicalDC)
3830 return 0;
3831 }
3832
3833 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003834 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3835 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003836
3837 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003838 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003839 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3840 D->getTemplateArgs().size(),
3841 TemplateArgs))
3842 return 0;
3843
3844 // Try to find an existing specialization with these template arguments.
3845 void *InsertPos = 0;
3846 ClassTemplateSpecializationDecl *D2
3847 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3848 TemplateArgs.size(), InsertPos);
3849 if (D2) {
3850 // We already have a class template specialization with these template
3851 // arguments.
3852
3853 // FIXME: Check for specialization vs. instantiation errors.
3854
3855 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003856 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003857 // The record types structurally match, or the "from" translation
3858 // unit only had a forward declaration anyway; call it the same
3859 // function.
3860 return Importer.Imported(D, FoundDef);
3861 }
3862 }
3863 } else {
3864 // Create a new specialization.
3865 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3866 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003867 StartLoc, IdLoc,
3868 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003869 TemplateArgs.data(),
3870 TemplateArgs.size(),
3871 /*PrevDecl=*/0);
3872 D2->setSpecializationKind(D->getSpecializationKind());
3873
3874 // Add this specialization to the class template.
3875 ClassTemplate->AddSpecialization(D2, InsertPos);
3876
3877 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003878 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003879
3880 // Add the specialization to this context.
3881 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003882 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003883 }
3884 Importer.Imported(D, D2);
3885
John McCall5e1cdac2011-10-07 06:10:15 +00003886 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003887 return 0;
3888
3889 return D2;
3890}
3891
Douglas Gregor4800d952010-02-11 19:21:55 +00003892//----------------------------------------------------------------------------
3893// Import Statements
3894//----------------------------------------------------------------------------
3895
3896Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3897 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3898 << S->getStmtClassName();
3899 return 0;
3900}
3901
3902//----------------------------------------------------------------------------
3903// Import Expressions
3904//----------------------------------------------------------------------------
3905Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3906 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3907 << E->getStmtClassName();
3908 return 0;
3909}
3910
Douglas Gregor44080632010-02-19 01:17:02 +00003911Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003912 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3913 if (!ToD)
3914 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003915
3916 NamedDecl *FoundD = 0;
3917 if (E->getDecl() != E->getFoundDecl()) {
3918 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3919 if (!FoundD)
3920 return 0;
3921 }
Douglas Gregor44080632010-02-19 01:17:02 +00003922
3923 QualType T = Importer.Import(E->getType());
3924 if (T.isNull())
3925 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003926
3927 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3928 Importer.Import(E->getQualifierLoc()),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003929 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003930 ToD,
3931 Importer.Import(E->getLocation()),
3932 T, E->getValueKind(),
3933 FoundD,
3934 /*FIXME:TemplateArgs=*/0);
3935 if (E->hadMultipleCandidates())
3936 DRE->setHadMultipleCandidates(true);
3937 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003938}
3939
Douglas Gregor4800d952010-02-11 19:21:55 +00003940Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3941 QualType T = Importer.Import(E->getType());
3942 if (T.isNull())
3943 return 0;
3944
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003945 return IntegerLiteral::Create(Importer.getToContext(),
3946 E->getValue(), T,
3947 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003948}
3949
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003950Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3951 QualType T = Importer.Import(E->getType());
3952 if (T.isNull())
3953 return 0;
3954
Douglas Gregor5cee1192011-07-27 05:40:30 +00003955 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3956 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003957 Importer.Import(E->getLocation()));
3958}
3959
Douglas Gregorf638f952010-02-19 01:07:06 +00003960Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3961 Expr *SubExpr = Importer.Import(E->getSubExpr());
3962 if (!SubExpr)
3963 return 0;
3964
3965 return new (Importer.getToContext())
3966 ParenExpr(Importer.Import(E->getLParen()),
3967 Importer.Import(E->getRParen()),
3968 SubExpr);
3969}
3970
3971Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3972 QualType T = Importer.Import(E->getType());
3973 if (T.isNull())
3974 return 0;
3975
3976 Expr *SubExpr = Importer.Import(E->getSubExpr());
3977 if (!SubExpr)
3978 return 0;
3979
3980 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003981 T, E->getValueKind(),
3982 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003983 Importer.Import(E->getOperatorLoc()));
3984}
3985
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003986Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3987 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003988 QualType ResultType = Importer.Import(E->getType());
3989
3990 if (E->isArgumentType()) {
3991 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3992 if (!TInfo)
3993 return 0;
3994
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003995 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3996 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003997 Importer.Import(E->getOperatorLoc()),
3998 Importer.Import(E->getRParenLoc()));
3999 }
4000
4001 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4002 if (!SubExpr)
4003 return 0;
4004
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004005 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4006 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004007 Importer.Import(E->getOperatorLoc()),
4008 Importer.Import(E->getRParenLoc()));
4009}
4010
Douglas Gregorf638f952010-02-19 01:07:06 +00004011Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4012 QualType T = Importer.Import(E->getType());
4013 if (T.isNull())
4014 return 0;
4015
4016 Expr *LHS = Importer.Import(E->getLHS());
4017 if (!LHS)
4018 return 0;
4019
4020 Expr *RHS = Importer.Import(E->getRHS());
4021 if (!RHS)
4022 return 0;
4023
4024 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004025 T, E->getValueKind(),
4026 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004027 Importer.Import(E->getOperatorLoc()));
4028}
4029
4030Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4031 QualType T = Importer.Import(E->getType());
4032 if (T.isNull())
4033 return 0;
4034
4035 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4036 if (CompLHSType.isNull())
4037 return 0;
4038
4039 QualType CompResultType = Importer.Import(E->getComputationResultType());
4040 if (CompResultType.isNull())
4041 return 0;
4042
4043 Expr *LHS = Importer.Import(E->getLHS());
4044 if (!LHS)
4045 return 0;
4046
4047 Expr *RHS = Importer.Import(E->getRHS());
4048 if (!RHS)
4049 return 0;
4050
4051 return new (Importer.getToContext())
4052 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004053 T, E->getValueKind(),
4054 E->getObjectKind(),
4055 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004056 Importer.Import(E->getOperatorLoc()));
4057}
4058
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004059static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004060 if (E->path_empty()) return false;
4061
4062 // TODO: import cast paths
4063 return true;
4064}
4065
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004066Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4067 QualType T = Importer.Import(E->getType());
4068 if (T.isNull())
4069 return 0;
4070
4071 Expr *SubExpr = Importer.Import(E->getSubExpr());
4072 if (!SubExpr)
4073 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004074
4075 CXXCastPath BasePath;
4076 if (ImportCastPath(E, BasePath))
4077 return 0;
4078
4079 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004080 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004081}
4082
Douglas Gregor008847a2010-02-19 01:32:14 +00004083Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4084 QualType T = Importer.Import(E->getType());
4085 if (T.isNull())
4086 return 0;
4087
4088 Expr *SubExpr = Importer.Import(E->getSubExpr());
4089 if (!SubExpr)
4090 return 0;
4091
4092 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4093 if (!TInfo && E->getTypeInfoAsWritten())
4094 return 0;
4095
John McCallf871d0c2010-08-07 06:22:56 +00004096 CXXCastPath BasePath;
4097 if (ImportCastPath(E, BasePath))
4098 return 0;
4099
John McCallf89e55a2010-11-18 06:31:45 +00004100 return CStyleCastExpr::Create(Importer.getToContext(), T,
4101 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004102 SubExpr, &BasePath, TInfo,
4103 Importer.Import(E->getLParenLoc()),
4104 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004105}
4106
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004107ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004108 ASTContext &FromContext, FileManager &FromFileManager,
4109 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004110 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004111 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4112 Minimal(MinimalImport)
4113{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004114 ImportedDecls[FromContext.getTranslationUnitDecl()]
4115 = ToContext.getTranslationUnitDecl();
4116}
4117
4118ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004119
4120QualType ASTImporter::Import(QualType FromT) {
4121 if (FromT.isNull())
4122 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004123
4124 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004125
Douglas Gregor169fba52010-02-08 15:18:58 +00004126 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004127 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4128 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004129 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004130 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004131
Douglas Gregor169fba52010-02-08 15:18:58 +00004132 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004133 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004134 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004135 if (ToT.isNull())
4136 return ToT;
4137
Douglas Gregor169fba52010-02-08 15:18:58 +00004138 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004139 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004140
John McCallf4c73712011-01-19 06:33:43 +00004141 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004142}
4143
Douglas Gregor9bed8792010-02-09 19:21:46 +00004144TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004145 if (!FromTSI)
4146 return FromTSI;
4147
4148 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004149 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004150 QualType T = Import(FromTSI->getType());
4151 if (T.isNull())
4152 return 0;
4153
4154 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004155 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004156}
4157
4158Decl *ASTImporter::Import(Decl *FromD) {
4159 if (!FromD)
4160 return 0;
4161
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004162 ASTNodeImporter Importer(*this);
4163
Douglas Gregor9bed8792010-02-09 19:21:46 +00004164 // Check whether we've already imported this declaration.
4165 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004166 if (Pos != ImportedDecls.end()) {
4167 Decl *ToD = Pos->second;
4168 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4169 return ToD;
4170 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004171
4172 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004173 Decl *ToD = Importer.Visit(FromD);
4174 if (!ToD)
4175 return 0;
4176
4177 // Record the imported declaration.
4178 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004179
4180 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4181 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004182 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004183 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004184 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004185 // When we've finished transforming a typedef, see whether it was the
4186 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004187 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004188 FromTag = AnonTagsWithPendingTypedefs.begin(),
4189 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4190 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004191 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004192 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4193 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004194 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004195 AnonTagsWithPendingTypedefs.erase(FromTag);
4196 break;
4197 }
4198 }
4199 }
4200 }
4201
Douglas Gregor9bed8792010-02-09 19:21:46 +00004202 return ToD;
4203}
4204
4205DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4206 if (!FromDC)
4207 return FromDC;
4208
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004209 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregorac32ff92012-02-01 21:00:38 +00004210 if (!ToDC)
4211 return 0;
4212
4213 // When we're using a record/enum/Objective-C class/protocol as a context, we
4214 // need it to have a definition.
4215 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor568991b2012-01-25 01:13:20 +00004216 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregorac32ff92012-02-01 21:00:38 +00004217 if (ToRecord->isCompleteDefinition()) {
4218 // Do nothing.
4219 } else if (FromRecord->isCompleteDefinition()) {
4220 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4221 ASTNodeImporter::IDK_Basic);
4222 } else {
4223 CompleteDecl(ToRecord);
4224 }
4225 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4226 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4227 if (ToEnum->isCompleteDefinition()) {
4228 // Do nothing.
4229 } else if (FromEnum->isCompleteDefinition()) {
4230 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4231 ASTNodeImporter::IDK_Basic);
4232 } else {
4233 CompleteDecl(ToEnum);
4234 }
4235 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4236 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4237 if (ToClass->getDefinition()) {
4238 // Do nothing.
4239 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4240 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4241 ASTNodeImporter::IDK_Basic);
4242 } else {
4243 CompleteDecl(ToClass);
4244 }
4245 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4246 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4247 if (ToProto->getDefinition()) {
4248 // Do nothing.
4249 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4250 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4251 ASTNodeImporter::IDK_Basic);
4252 } else {
4253 CompleteDecl(ToProto);
4254 }
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004255 }
4256
4257 return ToDC;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004258}
4259
4260Expr *ASTImporter::Import(Expr *FromE) {
4261 if (!FromE)
4262 return 0;
4263
4264 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4265}
4266
4267Stmt *ASTImporter::Import(Stmt *FromS) {
4268 if (!FromS)
4269 return 0;
4270
Douglas Gregor4800d952010-02-11 19:21:55 +00004271 // Check whether we've already imported this declaration.
4272 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4273 if (Pos != ImportedStmts.end())
4274 return Pos->second;
4275
4276 // Import the type
4277 ASTNodeImporter Importer(*this);
4278 Stmt *ToS = Importer.Visit(FromS);
4279 if (!ToS)
4280 return 0;
4281
4282 // Record the imported declaration.
4283 ImportedStmts[FromS] = ToS;
4284 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004285}
4286
4287NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4288 if (!FromNNS)
4289 return 0;
4290
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004291 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4292
4293 switch (FromNNS->getKind()) {
4294 case NestedNameSpecifier::Identifier:
4295 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4296 return NestedNameSpecifier::Create(ToContext, prefix, II);
4297 }
4298 return 0;
4299
4300 case NestedNameSpecifier::Namespace:
4301 if (NamespaceDecl *NS =
4302 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4303 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4304 }
4305 return 0;
4306
4307 case NestedNameSpecifier::NamespaceAlias:
4308 if (NamespaceAliasDecl *NSAD =
4309 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4310 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4311 }
4312 return 0;
4313
4314 case NestedNameSpecifier::Global:
4315 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4316
4317 case NestedNameSpecifier::TypeSpec:
4318 case NestedNameSpecifier::TypeSpecWithTemplate: {
4319 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4320 if (!T.isNull()) {
4321 bool bTemplate = FromNNS->getKind() ==
4322 NestedNameSpecifier::TypeSpecWithTemplate;
4323 return NestedNameSpecifier::Create(ToContext, prefix,
4324 bTemplate, T.getTypePtr());
4325 }
4326 }
4327 return 0;
4328 }
4329
4330 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004331}
4332
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004333NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4334 // FIXME: Implement!
4335 return NestedNameSpecifierLoc();
4336}
4337
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004338TemplateName ASTImporter::Import(TemplateName From) {
4339 switch (From.getKind()) {
4340 case TemplateName::Template:
4341 if (TemplateDecl *ToTemplate
4342 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4343 return TemplateName(ToTemplate);
4344
4345 return TemplateName();
4346
4347 case TemplateName::OverloadedTemplate: {
4348 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4349 UnresolvedSet<2> ToTemplates;
4350 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4351 E = FromStorage->end();
4352 I != E; ++I) {
4353 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4354 ToTemplates.addDecl(To);
4355 else
4356 return TemplateName();
4357 }
4358 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4359 ToTemplates.end());
4360 }
4361
4362 case TemplateName::QualifiedTemplate: {
4363 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4364 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4365 if (!Qualifier)
4366 return TemplateName();
4367
4368 if (TemplateDecl *ToTemplate
4369 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4370 return ToContext.getQualifiedTemplateName(Qualifier,
4371 QTN->hasTemplateKeyword(),
4372 ToTemplate);
4373
4374 return TemplateName();
4375 }
4376
4377 case TemplateName::DependentTemplate: {
4378 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4379 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4380 if (!Qualifier)
4381 return TemplateName();
4382
4383 if (DTN->isIdentifier()) {
4384 return ToContext.getDependentTemplateName(Qualifier,
4385 Import(DTN->getIdentifier()));
4386 }
4387
4388 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4389 }
John McCall14606042011-06-30 08:33:18 +00004390
4391 case TemplateName::SubstTemplateTemplateParm: {
4392 SubstTemplateTemplateParmStorage *subst
4393 = From.getAsSubstTemplateTemplateParm();
4394 TemplateTemplateParmDecl *param
4395 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4396 if (!param)
4397 return TemplateName();
4398
4399 TemplateName replacement = Import(subst->getReplacement());
4400 if (replacement.isNull()) return TemplateName();
4401
4402 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4403 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004404
4405 case TemplateName::SubstTemplateTemplateParmPack: {
4406 SubstTemplateTemplateParmPackStorage *SubstPack
4407 = From.getAsSubstTemplateTemplateParmPack();
4408 TemplateTemplateParmDecl *Param
4409 = cast_or_null<TemplateTemplateParmDecl>(
4410 Import(SubstPack->getParameterPack()));
4411 if (!Param)
4412 return TemplateName();
4413
4414 ASTNodeImporter Importer(*this);
4415 TemplateArgument ArgPack
4416 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4417 if (ArgPack.isNull())
4418 return TemplateName();
4419
4420 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4421 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004422 }
4423
4424 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004425}
4426
Douglas Gregor9bed8792010-02-09 19:21:46 +00004427SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4428 if (FromLoc.isInvalid())
4429 return SourceLocation();
4430
Douglas Gregor88523732010-02-10 00:15:17 +00004431 SourceManager &FromSM = FromContext.getSourceManager();
4432
4433 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004434 // don't have to import macro expansions.
4435 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004436 FromLoc = FromSM.getSpellingLoc(FromLoc);
4437 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4438 SourceManager &ToSM = ToContext.getSourceManager();
4439 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004440 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004441}
4442
4443SourceRange ASTImporter::Import(SourceRange FromRange) {
4444 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4445}
4446
Douglas Gregor88523732010-02-10 00:15:17 +00004447FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004448 llvm::DenseMap<FileID, FileID>::iterator Pos
4449 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004450 if (Pos != ImportedFileIDs.end())
4451 return Pos->second;
4452
4453 SourceManager &FromSM = FromContext.getSourceManager();
4454 SourceManager &ToSM = ToContext.getSourceManager();
4455 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004456 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004457
4458 // Include location of this file.
4459 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4460
4461 // Map the FileID for to the "to" source manager.
4462 FileID ToID;
4463 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004464 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004465 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4466 // disk again
4467 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4468 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004469 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004470 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4471 FromSLoc.getFile().getFileCharacteristic());
4472 } else {
4473 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004474 const llvm::MemoryBuffer *
4475 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004476 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004477 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004478 FromBuf->getBufferIdentifier());
4479 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4480 }
4481
4482
Sebastian Redl535a3e22010-09-30 01:03:06 +00004483 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004484 return ToID;
4485}
4486
Douglas Gregord8868a62011-01-18 03:11:38 +00004487void ASTImporter::ImportDefinition(Decl *From) {
4488 Decl *To = Import(From);
4489 if (!To)
4490 return;
4491
4492 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4493 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004494
4495 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4496 if (!ToRecord->getDefinition()) {
4497 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004498 ASTNodeImporter::IDK_Everything);
Sean Callanan673e7752011-07-19 22:38:25 +00004499 return;
4500 }
4501 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004502
4503 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4504 if (!ToEnum->getDefinition()) {
4505 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004506 ASTNodeImporter::IDK_Everything);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004507 return;
4508 }
4509 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004510
4511 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4512 if (!ToIFace->getDefinition()) {
4513 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004514 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004515 return;
4516 }
4517 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004518
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004519 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4520 if (!ToProto->getDefinition()) {
4521 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004522 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004523 return;
4524 }
4525 }
4526
Douglas Gregord8868a62011-01-18 03:11:38 +00004527 Importer.ImportDeclContext(FromDC, true);
4528 }
4529}
4530
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004531DeclarationName ASTImporter::Import(DeclarationName FromName) {
4532 if (!FromName)
4533 return DeclarationName();
4534
4535 switch (FromName.getNameKind()) {
4536 case DeclarationName::Identifier:
4537 return Import(FromName.getAsIdentifierInfo());
4538
4539 case DeclarationName::ObjCZeroArgSelector:
4540 case DeclarationName::ObjCOneArgSelector:
4541 case DeclarationName::ObjCMultiArgSelector:
4542 return Import(FromName.getObjCSelector());
4543
4544 case DeclarationName::CXXConstructorName: {
4545 QualType T = Import(FromName.getCXXNameType());
4546 if (T.isNull())
4547 return DeclarationName();
4548
4549 return ToContext.DeclarationNames.getCXXConstructorName(
4550 ToContext.getCanonicalType(T));
4551 }
4552
4553 case DeclarationName::CXXDestructorName: {
4554 QualType T = Import(FromName.getCXXNameType());
4555 if (T.isNull())
4556 return DeclarationName();
4557
4558 return ToContext.DeclarationNames.getCXXDestructorName(
4559 ToContext.getCanonicalType(T));
4560 }
4561
4562 case DeclarationName::CXXConversionFunctionName: {
4563 QualType T = Import(FromName.getCXXNameType());
4564 if (T.isNull())
4565 return DeclarationName();
4566
4567 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4568 ToContext.getCanonicalType(T));
4569 }
4570
4571 case DeclarationName::CXXOperatorName:
4572 return ToContext.DeclarationNames.getCXXOperatorName(
4573 FromName.getCXXOverloadedOperator());
4574
4575 case DeclarationName::CXXLiteralOperatorName:
4576 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4577 Import(FromName.getCXXLiteralIdentifier()));
4578
4579 case DeclarationName::CXXUsingDirective:
4580 // FIXME: STATICS!
4581 return DeclarationName::getUsingDirectiveName();
4582 }
4583
David Blaikie30263482012-01-20 21:50:17 +00004584 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004585}
4586
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004587IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004588 if (!FromId)
4589 return 0;
4590
4591 return &ToContext.Idents.get(FromId->getName());
4592}
Douglas Gregor089459a2010-02-08 21:09:39 +00004593
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004594Selector ASTImporter::Import(Selector FromSel) {
4595 if (FromSel.isNull())
4596 return Selector();
4597
Chris Lattner5f9e2722011-07-23 10:55:15 +00004598 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004599 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4600 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4601 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4602 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4603}
4604
Douglas Gregor089459a2010-02-08 21:09:39 +00004605DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4606 DeclContext *DC,
4607 unsigned IDNS,
4608 NamedDecl **Decls,
4609 unsigned NumDecls) {
4610 return Name;
4611}
4612
4613DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004614 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004615}
4616
4617DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004618 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004619}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004620
Douglas Gregorac32ff92012-02-01 21:00:38 +00004621void ASTImporter::CompleteDecl (Decl *D) {
4622 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4623 if (!ID->getDefinition())
4624 ID->startDefinition();
4625 }
4626 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4627 if (!PD->getDefinition())
4628 PD->startDefinition();
4629 }
4630 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4631 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4632 TD->startDefinition();
4633 TD->setCompleteDefinition(true);
4634 }
4635 }
4636 else {
4637 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4638 }
4639}
4640
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004641Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4642 ImportedDecls[From] = To;
4643 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004644}
Douglas Gregorea35d112010-02-15 23:54:17 +00004645
4646bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004647 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004648 = ImportedTypes.find(From.getTypePtr());
4649 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4650 return true;
4651
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004652 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004653 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004654}