blob: 556f97d858258b9c84bc7528e08afd511dc6154b [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
1584 return Importer.getToContext().getDecltypeType(ToExpr);
1585}
1586
Sean Huntca63c202011-05-24 22:41:36 +00001587QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1588 QualType ToBaseType = Importer.Import(T->getBaseType());
1589 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1590 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1591 return QualType();
1592
1593 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1594 ToUnderlyingType,
1595 T->getUTTKind());
1596}
1597
Richard Smith34b41d92011-02-20 03:19:35 +00001598QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1599 // FIXME: Make sure that the "to" context supports C++0x!
1600 QualType FromDeduced = T->getDeducedType();
1601 QualType ToDeduced;
1602 if (!FromDeduced.isNull()) {
1603 ToDeduced = Importer.Import(FromDeduced);
1604 if (ToDeduced.isNull())
1605 return QualType();
1606 }
1607
1608 return Importer.getToContext().getAutoType(ToDeduced);
1609}
1610
John McCallf4c73712011-01-19 06:33:43 +00001611QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001612 RecordDecl *ToDecl
1613 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1614 if (!ToDecl)
1615 return QualType();
1616
1617 return Importer.getToContext().getTagDeclType(ToDecl);
1618}
1619
John McCallf4c73712011-01-19 06:33:43 +00001620QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001621 EnumDecl *ToDecl
1622 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1623 if (!ToDecl)
1624 return QualType();
1625
1626 return Importer.getToContext().getTagDeclType(ToDecl);
1627}
1628
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001629QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001630 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001631 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1632 if (ToTemplate.isNull())
1633 return QualType();
1634
Chris Lattner5f9e2722011-07-23 10:55:15 +00001635 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001636 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1637 return QualType();
1638
1639 QualType ToCanonType;
1640 if (!QualType(T, 0).isCanonical()) {
1641 QualType FromCanonType
1642 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1643 ToCanonType =Importer.Import(FromCanonType);
1644 if (ToCanonType.isNull())
1645 return QualType();
1646 }
1647 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1648 ToTemplateArgs.data(),
1649 ToTemplateArgs.size(),
1650 ToCanonType);
1651}
1652
John McCallf4c73712011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001654 NestedNameSpecifier *ToQualifier = 0;
1655 // Note: the qualifier in an ElaboratedType is optional.
1656 if (T->getQualifier()) {
1657 ToQualifier = Importer.Import(T->getQualifier());
1658 if (!ToQualifier)
1659 return QualType();
1660 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001661
1662 QualType ToNamedType = Importer.Import(T->getNamedType());
1663 if (ToNamedType.isNull())
1664 return QualType();
1665
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001666 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1667 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001668}
1669
John McCallf4c73712011-01-19 06:33:43 +00001670QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001671 ObjCInterfaceDecl *Class
1672 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1673 if (!Class)
1674 return QualType();
1675
John McCallc12c5bb2010-05-15 11:32:37 +00001676 return Importer.getToContext().getObjCInterfaceType(Class);
1677}
1678
John McCallf4c73712011-01-19 06:33:43 +00001679QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001680 QualType ToBaseType = Importer.Import(T->getBaseType());
1681 if (ToBaseType.isNull())
1682 return QualType();
1683
Chris Lattner5f9e2722011-07-23 10:55:15 +00001684 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001685 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001686 PEnd = T->qual_end();
1687 P != PEnd; ++P) {
1688 ObjCProtocolDecl *Protocol
1689 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1690 if (!Protocol)
1691 return QualType();
1692 Protocols.push_back(Protocol);
1693 }
1694
John McCallc12c5bb2010-05-15 11:32:37 +00001695 return Importer.getToContext().getObjCObjectType(ToBaseType,
1696 Protocols.data(),
1697 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001698}
1699
John McCallf4c73712011-01-19 06:33:43 +00001700QualType
1701ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001702 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1703 if (ToPointeeType.isNull())
1704 return QualType();
1705
John McCallc12c5bb2010-05-15 11:32:37 +00001706 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001707}
1708
Douglas Gregor089459a2010-02-08 21:09:39 +00001709//----------------------------------------------------------------------------
1710// Import Declarations
1711//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001712bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1713 DeclContext *&LexicalDC,
1714 DeclarationName &Name,
1715 SourceLocation &Loc) {
1716 // Import the context of this declaration.
1717 DC = Importer.ImportContext(D->getDeclContext());
1718 if (!DC)
1719 return true;
1720
1721 LexicalDC = DC;
1722 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1723 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1724 if (!LexicalDC)
1725 return true;
1726 }
1727
1728 // Import the name of this declaration.
1729 Name = Importer.Import(D->getDeclName());
1730 if (D->getDeclName() && !Name)
1731 return true;
1732
1733 // Import the location of this declaration.
1734 Loc = Importer.Import(D->getLocation());
1735 return false;
1736}
1737
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001738void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1739 if (!FromD)
1740 return;
1741
1742 if (!ToD) {
1743 ToD = Importer.Import(FromD);
1744 if (!ToD)
1745 return;
1746 }
1747
1748 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1749 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1750 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1751 ImportDefinition(FromRecord, ToRecord);
1752 }
1753 }
1754 return;
1755 }
1756
1757 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1758 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1759 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1760 ImportDefinition(FromEnum, ToEnum);
1761 }
1762 }
1763 return;
1764 }
1765}
1766
Abramo Bagnara25777432010-08-11 22:01:17 +00001767void
1768ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1769 DeclarationNameInfo& To) {
1770 // NOTE: To.Name and To.Loc are already imported.
1771 // We only have to import To.LocInfo.
1772 switch (To.getName().getNameKind()) {
1773 case DeclarationName::Identifier:
1774 case DeclarationName::ObjCZeroArgSelector:
1775 case DeclarationName::ObjCOneArgSelector:
1776 case DeclarationName::ObjCMultiArgSelector:
1777 case DeclarationName::CXXUsingDirective:
1778 return;
1779
1780 case DeclarationName::CXXOperatorName: {
1781 SourceRange Range = From.getCXXOperatorNameRange();
1782 To.setCXXOperatorNameRange(Importer.Import(Range));
1783 return;
1784 }
1785 case DeclarationName::CXXLiteralOperatorName: {
1786 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1787 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1788 return;
1789 }
1790 case DeclarationName::CXXConstructorName:
1791 case DeclarationName::CXXDestructorName:
1792 case DeclarationName::CXXConversionFunctionName: {
1793 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1794 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1795 return;
1796 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001797 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001798 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001799}
1800
Douglas Gregorac32ff92012-02-01 21:00:38 +00001801void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregord8868a62011-01-18 03:11:38 +00001802 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001803 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001804 return;
1805 }
1806
Douglas Gregor083a8212010-02-21 18:24:45 +00001807 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1808 FromEnd = FromDC->decls_end();
1809 From != FromEnd;
1810 ++From)
1811 Importer.Import(*From);
1812}
1813
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001814bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001815 ImportDefinitionKind Kind) {
1816 if (To->getDefinition() || To->isBeingDefined()) {
1817 if (Kind == IDK_Everything)
1818 ImportDeclContext(From, /*ForceImport=*/true);
1819
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001820 return false;
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001821 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001822
1823 To->startDefinition();
1824
1825 // Add base classes.
1826 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1827 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001828
1829 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1830 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1831 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1832 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1833 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1834 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1835 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1836 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1837 ToData.Aggregate = FromData.Aggregate;
1838 ToData.PlainOldData = FromData.PlainOldData;
1839 ToData.Empty = FromData.Empty;
1840 ToData.Polymorphic = FromData.Polymorphic;
1841 ToData.Abstract = FromData.Abstract;
1842 ToData.IsStandardLayout = FromData.IsStandardLayout;
1843 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1844 ToData.HasPrivateFields = FromData.HasPrivateFields;
1845 ToData.HasProtectedFields = FromData.HasProtectedFields;
1846 ToData.HasPublicFields = FromData.HasPublicFields;
1847 ToData.HasMutableFields = FromData.HasMutableFields;
1848 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1849 ToData.HasConstexprNonCopyMoveConstructor
1850 = FromData.HasConstexprNonCopyMoveConstructor;
1851 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1852 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1853 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1854 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1855 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1856 ToData.HasNonLiteralTypeFieldsOrBases
1857 = FromData.HasNonLiteralTypeFieldsOrBases;
1858 ToData.UserProvidedDefaultConstructor
1859 = FromData.UserProvidedDefaultConstructor;
1860 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1861 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1862 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1863 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1864 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1865 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1866 ToData.FailedImplicitMoveConstructor
1867 = FromData.FailedImplicitMoveConstructor;
1868 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001869
Chris Lattner5f9e2722011-07-23 10:55:15 +00001870 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001871 for (CXXRecordDecl::base_class_iterator
1872 Base1 = FromCXX->bases_begin(),
1873 FromBaseEnd = FromCXX->bases_end();
1874 Base1 != FromBaseEnd;
1875 ++Base1) {
1876 QualType T = Importer.Import(Base1->getType());
1877 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001878 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001879
1880 SourceLocation EllipsisLoc;
1881 if (Base1->isPackExpansion())
1882 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001883
1884 // Ensure that we have a definition for the base.
1885 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1886
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001887 Bases.push_back(
1888 new (Importer.getToContext())
1889 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1890 Base1->isVirtual(),
1891 Base1->isBaseOfClass(),
1892 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001893 Importer.Import(Base1->getTypeSourceInfo()),
1894 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001895 }
1896 if (!Bases.empty())
1897 ToCXX->setBases(Bases.data(), Bases.size());
1898 }
1899
Douglas Gregorac32ff92012-02-01 21:00:38 +00001900 if (shouldForceImportDeclContext(Kind))
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00001901 ImportDeclContext(From, /*ForceImport=*/true);
1902
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001903 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001904 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001905}
1906
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001907bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00001908 ImportDefinitionKind Kind) {
1909 if (To->getDefinition() || To->isBeingDefined()) {
1910 if (Kind == IDK_Everything)
1911 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001912 return false;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001913 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001914
1915 To->startDefinition();
1916
1917 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1918 if (T.isNull())
1919 return true;
1920
1921 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1922 if (ToPromotionType.isNull())
1923 return true;
Douglas Gregorac32ff92012-02-01 21:00:38 +00001924
1925 if (shouldForceImportDeclContext(Kind))
1926 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001927
1928 // FIXME: we might need to merge the number of positive or negative bits
1929 // if the enumerator lists don't match.
1930 To->completeDefinition(T, ToPromotionType,
1931 From->getNumPositiveBits(),
1932 From->getNumNegativeBits());
1933 return false;
1934}
1935
Douglas Gregor040afae2010-11-30 19:14:50 +00001936TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1937 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001938 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001939 ToParams.reserve(Params->size());
1940 for (TemplateParameterList::iterator P = Params->begin(),
1941 PEnd = Params->end();
1942 P != PEnd; ++P) {
1943 Decl *To = Importer.Import(*P);
1944 if (!To)
1945 return 0;
1946
1947 ToParams.push_back(cast<NamedDecl>(To));
1948 }
1949
1950 return TemplateParameterList::Create(Importer.getToContext(),
1951 Importer.Import(Params->getTemplateLoc()),
1952 Importer.Import(Params->getLAngleLoc()),
1953 ToParams.data(), ToParams.size(),
1954 Importer.Import(Params->getRAngleLoc()));
1955}
1956
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001957TemplateArgument
1958ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1959 switch (From.getKind()) {
1960 case TemplateArgument::Null:
1961 return TemplateArgument();
1962
1963 case TemplateArgument::Type: {
1964 QualType ToType = Importer.Import(From.getAsType());
1965 if (ToType.isNull())
1966 return TemplateArgument();
1967 return TemplateArgument(ToType);
1968 }
1969
1970 case TemplateArgument::Integral: {
1971 QualType ToType = Importer.Import(From.getIntegralType());
1972 if (ToType.isNull())
1973 return TemplateArgument();
1974 return TemplateArgument(*From.getAsIntegral(), ToType);
1975 }
1976
1977 case TemplateArgument::Declaration:
1978 if (Decl *To = Importer.Import(From.getAsDecl()))
1979 return TemplateArgument(To);
1980 return TemplateArgument();
1981
1982 case TemplateArgument::Template: {
1983 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1984 if (ToTemplate.isNull())
1985 return TemplateArgument();
1986
1987 return TemplateArgument(ToTemplate);
1988 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001989
1990 case TemplateArgument::TemplateExpansion: {
1991 TemplateName ToTemplate
1992 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1993 if (ToTemplate.isNull())
1994 return TemplateArgument();
1995
Douglas Gregor2be29f42011-01-14 23:41:42 +00001996 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001997 }
1998
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001999 case TemplateArgument::Expression:
2000 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2001 return TemplateArgument(ToExpr);
2002 return TemplateArgument();
2003
2004 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002005 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002006 ToPack.reserve(From.pack_size());
2007 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2008 return TemplateArgument();
2009
2010 TemplateArgument *ToArgs
2011 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2012 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2013 return TemplateArgument(ToArgs, ToPack.size());
2014 }
2015 }
2016
2017 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002018}
2019
2020bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2021 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002022 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002023 for (unsigned I = 0; I != NumFromArgs; ++I) {
2024 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2025 if (To.isNull() && !FromArgs[I].isNull())
2026 return true;
2027
2028 ToArgs.push_back(To);
2029 }
2030
2031 return false;
2032}
2033
Douglas Gregor96a01b42010-02-11 00:48:18 +00002034bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002035 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002036 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002037 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002038 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002039 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002040}
2041
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002042bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002043 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002044 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002045 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002046 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002047}
2048
Douglas Gregor040afae2010-11-30 19:14:50 +00002049bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2050 ClassTemplateDecl *To) {
2051 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2052 Importer.getToContext(),
2053 Importer.getNonEquivalentDecls());
2054 return Ctx.IsStructurallyEquivalent(From, To);
2055}
2056
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002057Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002058 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002059 << D->getDeclKindName();
2060 return 0;
2061}
2062
Sean Callananf1b69462011-11-17 23:20:56 +00002063Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2064 TranslationUnitDecl *ToD =
2065 Importer.getToContext().getTranslationUnitDecl();
2066
2067 Importer.Imported(D, ToD);
2068
2069 return ToD;
2070}
2071
Douglas Gregor788c62d2010-02-21 18:26:36 +00002072Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2073 // Import the major distinguishing characteristics of this namespace.
2074 DeclContext *DC, *LexicalDC;
2075 DeclarationName Name;
2076 SourceLocation Loc;
2077 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2078 return 0;
2079
2080 NamespaceDecl *MergeWithNamespace = 0;
2081 if (!Name) {
2082 // This is an anonymous namespace. Adopt an existing anonymous
2083 // namespace if we can.
2084 // FIXME: Not testable.
2085 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2086 MergeWithNamespace = TU->getAnonymousNamespace();
2087 else
2088 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2089 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002090 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002091 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2092 DC->localUncachedLookup(Name, FoundDecls);
2093 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2094 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002095 continue;
2096
Douglas Gregorb75a3452011-10-15 00:10:27 +00002097 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002098 MergeWithNamespace = FoundNS;
2099 ConflictingDecls.clear();
2100 break;
2101 }
2102
Douglas Gregorb75a3452011-10-15 00:10:27 +00002103 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002104 }
2105
2106 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002107 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002108 ConflictingDecls.data(),
2109 ConflictingDecls.size());
2110 }
2111 }
2112
2113 // Create the "to" namespace, if needed.
2114 NamespaceDecl *ToNamespace = MergeWithNamespace;
2115 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002116 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002117 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002118 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002119 Loc, Name.getAsIdentifierInfo(),
2120 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002121 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002122 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002123
2124 // If this is an anonymous namespace, register it as the anonymous
2125 // namespace within its context.
2126 if (!Name) {
2127 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2128 TU->setAnonymousNamespace(ToNamespace);
2129 else
2130 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2131 }
2132 }
2133 Importer.Imported(D, ToNamespace);
2134
2135 ImportDeclContext(D);
2136
2137 return ToNamespace;
2138}
2139
Richard Smith162e1c12011-04-15 14:24:37 +00002140Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002141 // Import the major distinguishing characteristics of this typedef.
2142 DeclContext *DC, *LexicalDC;
2143 DeclarationName Name;
2144 SourceLocation Loc;
2145 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2146 return 0;
2147
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002148 // If this typedef is not in block scope, determine whether we've
2149 // seen a typedef with the same name (that we can merge with) or any
2150 // other entity by that name (which name lookup could conflict with).
2151 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002152 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002153 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002154 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2155 DC->localUncachedLookup(Name, FoundDecls);
2156 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2157 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002158 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002159 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002160 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002161 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2162 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002163 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002164 }
2165
Douglas Gregorb75a3452011-10-15 00:10:27 +00002166 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002167 }
2168
2169 if (!ConflictingDecls.empty()) {
2170 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2171 ConflictingDecls.data(),
2172 ConflictingDecls.size());
2173 if (!Name)
2174 return 0;
2175 }
2176 }
2177
Douglas Gregorea35d112010-02-15 23:54:17 +00002178 // Import the underlying type of this typedef;
2179 QualType T = Importer.Import(D->getUnderlyingType());
2180 if (T.isNull())
2181 return 0;
2182
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002183 // Create the new typedef node.
2184 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002185 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002186 TypedefNameDecl *ToTypedef;
2187 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002188 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2189 StartL, Loc,
2190 Name.getAsIdentifierInfo(),
2191 TInfo);
2192 else
Richard Smith162e1c12011-04-15 14:24:37 +00002193 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2194 StartL, Loc,
2195 Name.getAsIdentifierInfo(),
2196 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002197
Douglas Gregor325bf172010-02-22 17:42:47 +00002198 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002199 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002200 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002201 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002202
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002203 return ToTypedef;
2204}
2205
Richard Smith162e1c12011-04-15 14:24:37 +00002206Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2207 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2208}
2209
2210Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2211 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2212}
2213
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002214Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2215 // Import the major distinguishing characteristics of this enum.
2216 DeclContext *DC, *LexicalDC;
2217 DeclarationName Name;
2218 SourceLocation Loc;
2219 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2220 return 0;
2221
2222 // Figure out what enum name we're looking for.
2223 unsigned IDNS = Decl::IDNS_Tag;
2224 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002225 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2226 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002227 IDNS = Decl::IDNS_Ordinary;
2228 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2229 IDNS |= Decl::IDNS_Ordinary;
2230
2231 // We may already have an enum of the same name; try to find and match it.
2232 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002233 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002234 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2235 DC->localUncachedLookup(SearchName, FoundDecls);
2236 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2237 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002238 continue;
2239
Douglas Gregorb75a3452011-10-15 00:10:27 +00002240 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002241 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002242 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2243 Found = Tag->getDecl();
2244 }
2245
2246 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002247 if (IsStructuralMatch(D, FoundEnum))
2248 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002249 }
2250
Douglas Gregorb75a3452011-10-15 00:10:27 +00002251 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002252 }
2253
2254 if (!ConflictingDecls.empty()) {
2255 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2256 ConflictingDecls.data(),
2257 ConflictingDecls.size());
2258 }
2259 }
2260
2261 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002262 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2263 Importer.Import(D->getLocStart()),
2264 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002265 D->isScoped(), D->isScopedUsingClassTag(),
2266 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002267 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002268 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002269 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002270 D2->setLexicalDeclContext(LexicalDC);
2271 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002272 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002273
2274 // Import the integer type.
2275 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2276 if (ToIntegerType.isNull())
2277 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002278 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002279
2280 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002281 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002282 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002283
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002284 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002285}
2286
Douglas Gregor96a01b42010-02-11 00:48:18 +00002287Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2288 // If this record has a definition in the translation unit we're coming from,
2289 // but this particular declaration is not that definition, import the
2290 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002291 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002292 if (Definition && Definition != D) {
2293 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002294 if (!ImportedDef)
2295 return 0;
2296
2297 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002298 }
2299
2300 // Import the major distinguishing characteristics of this record.
2301 DeclContext *DC, *LexicalDC;
2302 DeclarationName Name;
2303 SourceLocation Loc;
2304 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2305 return 0;
2306
2307 // Figure out what structure name we're looking for.
2308 unsigned IDNS = Decl::IDNS_Tag;
2309 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002310 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2311 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002312 IDNS = Decl::IDNS_Ordinary;
2313 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2314 IDNS |= Decl::IDNS_Ordinary;
2315
2316 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002317 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002318 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002319 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002320 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2321 DC->localUncachedLookup(SearchName, FoundDecls);
2322 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2323 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002324 continue;
2325
Douglas Gregorb75a3452011-10-15 00:10:27 +00002326 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002327 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002328 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2329 Found = Tag->getDecl();
2330 }
2331
2332 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002333 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002334 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002335 // The record types structurally match, or the "from" translation
2336 // unit only had a forward declaration anyway; call it the same
2337 // function.
2338 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002339 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002340 }
2341 } else {
2342 // We have a forward declaration of this type, so adopt that forward
2343 // declaration rather than building a new one.
2344 AdoptDecl = FoundRecord;
2345 continue;
2346 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002347 }
2348
Douglas Gregorb75a3452011-10-15 00:10:27 +00002349 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002350 }
2351
2352 if (!ConflictingDecls.empty()) {
2353 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2354 ConflictingDecls.data(),
2355 ConflictingDecls.size());
2356 }
2357 }
2358
2359 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002360 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002361 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002362 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002363 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002364 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002365 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002366 DC, StartLoc, Loc,
2367 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002368 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002369 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002370 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002371 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002372 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002373 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002374
2375 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002376 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002377 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002378 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002379
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002380 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002381
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00002382 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002383 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002384
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002385 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002386}
2387
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002388Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2389 // Import the major distinguishing characteristics of this enumerator.
2390 DeclContext *DC, *LexicalDC;
2391 DeclarationName Name;
2392 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002393 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002394 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002395
2396 QualType T = Importer.Import(D->getType());
2397 if (T.isNull())
2398 return 0;
2399
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002400 // Determine whether there are any other declarations with the same name and
2401 // in the same context.
2402 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002403 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002404 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002405 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2406 DC->localUncachedLookup(Name, FoundDecls);
2407 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2408 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002409 continue;
2410
Douglas Gregorb75a3452011-10-15 00:10:27 +00002411 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002412 }
2413
2414 if (!ConflictingDecls.empty()) {
2415 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2416 ConflictingDecls.data(),
2417 ConflictingDecls.size());
2418 if (!Name)
2419 return 0;
2420 }
2421 }
2422
2423 Expr *Init = Importer.Import(D->getInitExpr());
2424 if (D->getInitExpr() && !Init)
2425 return 0;
2426
2427 EnumConstantDecl *ToEnumerator
2428 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2429 Name.getAsIdentifierInfo(), T,
2430 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002431 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002432 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002433 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002434 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002435 return ToEnumerator;
2436}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002437
Douglas Gregora404ea62010-02-10 19:54:31 +00002438Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2439 // Import the major distinguishing characteristics of this function.
2440 DeclContext *DC, *LexicalDC;
2441 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002442 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002443 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002444 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002445
Douglas Gregora404ea62010-02-10 19:54:31 +00002446 // Try to find a function in our own ("to") context with the same name, same
2447 // type, and in the same context as the function we're importing.
2448 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002449 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002450 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002451 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2452 DC->localUncachedLookup(Name, FoundDecls);
2453 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2454 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002455 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002456
Douglas Gregorb75a3452011-10-15 00:10:27 +00002457 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002458 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2459 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002460 if (Importer.IsStructurallyEquivalent(D->getType(),
2461 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002462 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002463 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002464 }
2465
2466 // FIXME: Check for overloading more carefully, e.g., by boosting
2467 // Sema::IsOverload out to the AST library.
2468
2469 // Function overloading is okay in C++.
2470 if (Importer.getToContext().getLangOptions().CPlusPlus)
2471 continue;
2472
2473 // Complain about inconsistent function types.
2474 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002475 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002476 Importer.ToDiag(FoundFunction->getLocation(),
2477 diag::note_odr_value_here)
2478 << FoundFunction->getType();
2479 }
2480 }
2481
Douglas Gregorb75a3452011-10-15 00:10:27 +00002482 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002483 }
2484
2485 if (!ConflictingDecls.empty()) {
2486 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2487 ConflictingDecls.data(),
2488 ConflictingDecls.size());
2489 if (!Name)
2490 return 0;
2491 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002492 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002493
Abramo Bagnara25777432010-08-11 22:01:17 +00002494 DeclarationNameInfo NameInfo(Name, Loc);
2495 // Import additional name location/type info.
2496 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2497
Douglas Gregorea35d112010-02-15 23:54:17 +00002498 // Import the type.
2499 QualType T = Importer.Import(D->getType());
2500 if (T.isNull())
2501 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002502
2503 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002504 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002505 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2506 P != PEnd; ++P) {
2507 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2508 if (!ToP)
2509 return 0;
2510
2511 Parameters.push_back(ToP);
2512 }
2513
2514 // Create the imported function.
2515 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002516 FunctionDecl *ToFunction = 0;
2517 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2518 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2519 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002520 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002521 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002522 FromConstructor->isExplicit(),
2523 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002524 D->isImplicit(),
2525 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002526 } else if (isa<CXXDestructorDecl>(D)) {
2527 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2528 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002529 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002530 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002531 D->isInlineSpecified(),
2532 D->isImplicit());
2533 } else if (CXXConversionDecl *FromConversion
2534 = dyn_cast<CXXConversionDecl>(D)) {
2535 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2536 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002537 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002538 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002539 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002540 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002541 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002542 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002543 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2544 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2545 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002546 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002547 NameInfo, T, TInfo,
2548 Method->isStatic(),
2549 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002550 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002551 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002552 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002553 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002554 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002555 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002556 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002557 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002558 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002559 D->hasWrittenPrototype(),
2560 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002561 }
John McCallb6217662010-03-15 10:12:16 +00002562
2563 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002564 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002565 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002566 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002567 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2568 ToFunction->setTrivial(D->isTrivial());
2569 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002570 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002571
Douglas Gregora404ea62010-02-10 19:54:31 +00002572 // Set the parameters.
2573 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002574 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002575 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002576 }
David Blaikie4278c652011-09-21 18:16:56 +00002577 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002578
2579 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002580
2581 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002582 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002583
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002584 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002585}
2586
Douglas Gregorc144f352010-02-21 18:29:16 +00002587Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2588 return VisitFunctionDecl(D);
2589}
2590
2591Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2592 return VisitCXXMethodDecl(D);
2593}
2594
2595Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2596 return VisitCXXMethodDecl(D);
2597}
2598
2599Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2600 return VisitCXXMethodDecl(D);
2601}
2602
Douglas Gregor96a01b42010-02-11 00:48:18 +00002603Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2604 // Import the major distinguishing characteristics of a variable.
2605 DeclContext *DC, *LexicalDC;
2606 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002607 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002608 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2609 return 0;
2610
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002611 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002612 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2613 DC->localUncachedLookup(Name, FoundDecls);
2614 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2615 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002616 if (Importer.IsStructurallyEquivalent(D->getType(),
2617 FoundField->getType())) {
2618 Importer.Imported(D, FoundField);
2619 return FoundField;
2620 }
2621
2622 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2623 << Name << D->getType() << FoundField->getType();
2624 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2625 << FoundField->getType();
2626 return 0;
2627 }
2628 }
2629
Douglas Gregorea35d112010-02-15 23:54:17 +00002630 // Import the type.
2631 QualType T = Importer.Import(D->getType());
2632 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002633 return 0;
2634
2635 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2636 Expr *BitWidth = Importer.Import(D->getBitWidth());
2637 if (!BitWidth && D->getBitWidth())
2638 return 0;
2639
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002640 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2641 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002642 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002643 T, TInfo, BitWidth, D->isMutable(),
2644 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002645 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002646 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002647 if (ToField->hasInClassInitializer())
2648 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002649 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002650 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002651 return ToField;
2652}
2653
Francois Pichet87c2e122010-11-21 06:08:52 +00002654Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2655 // Import the major distinguishing characteristics of a variable.
2656 DeclContext *DC, *LexicalDC;
2657 DeclarationName Name;
2658 SourceLocation Loc;
2659 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2660 return 0;
2661
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002662 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002663 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2664 DC->localUncachedLookup(Name, FoundDecls);
2665 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002666 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002667 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002668 if (Importer.IsStructurallyEquivalent(D->getType(),
2669 FoundField->getType())) {
2670 Importer.Imported(D, FoundField);
2671 return FoundField;
2672 }
2673
2674 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2675 << Name << D->getType() << FoundField->getType();
2676 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2677 << FoundField->getType();
2678 return 0;
2679 }
2680 }
2681
Francois Pichet87c2e122010-11-21 06:08:52 +00002682 // Import the type.
2683 QualType T = Importer.Import(D->getType());
2684 if (T.isNull())
2685 return 0;
2686
2687 NamedDecl **NamedChain =
2688 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2689
2690 unsigned i = 0;
2691 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2692 PE = D->chain_end(); PI != PE; ++PI) {
2693 Decl* D = Importer.Import(*PI);
2694 if (!D)
2695 return 0;
2696 NamedChain[i++] = cast<NamedDecl>(D);
2697 }
2698
2699 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2700 Importer.getToContext(), DC,
2701 Loc, Name.getAsIdentifierInfo(), T,
2702 NamedChain, D->getChainingSize());
2703 ToIndirectField->setAccess(D->getAccess());
2704 ToIndirectField->setLexicalDeclContext(LexicalDC);
2705 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002706 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002707 return ToIndirectField;
2708}
2709
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002710Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2711 // Import the major distinguishing characteristics of an ivar.
2712 DeclContext *DC, *LexicalDC;
2713 DeclarationName Name;
2714 SourceLocation Loc;
2715 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2716 return 0;
2717
2718 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002719 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2720 DC->localUncachedLookup(Name, FoundDecls);
2721 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2722 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002723 if (Importer.IsStructurallyEquivalent(D->getType(),
2724 FoundIvar->getType())) {
2725 Importer.Imported(D, FoundIvar);
2726 return FoundIvar;
2727 }
2728
2729 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2730 << Name << D->getType() << FoundIvar->getType();
2731 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2732 << FoundIvar->getType();
2733 return 0;
2734 }
2735 }
2736
2737 // Import the type.
2738 QualType T = Importer.Import(D->getType());
2739 if (T.isNull())
2740 return 0;
2741
2742 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2743 Expr *BitWidth = Importer.Import(D->getBitWidth());
2744 if (!BitWidth && D->getBitWidth())
2745 return 0;
2746
Daniel Dunbara0654922010-04-02 20:10:03 +00002747 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2748 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002749 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002750 Loc, Name.getAsIdentifierInfo(),
2751 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002752 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002753 ToIvar->setLexicalDeclContext(LexicalDC);
2754 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002755 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002756 return ToIvar;
2757
2758}
2759
Douglas Gregora404ea62010-02-10 19:54:31 +00002760Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2761 // Import the major distinguishing characteristics of a variable.
2762 DeclContext *DC, *LexicalDC;
2763 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002764 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002765 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002766 return 0;
2767
Douglas Gregor089459a2010-02-08 21:09:39 +00002768 // Try to find a variable in our own ("to") context with the same name and
2769 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002770 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002771 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002772 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002773 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002774 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2775 DC->localUncachedLookup(Name, FoundDecls);
2776 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2777 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002778 continue;
2779
Douglas Gregorb75a3452011-10-15 00:10:27 +00002780 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002781 // We have found a variable that we may need to merge with. Check it.
2782 if (isExternalLinkage(FoundVar->getLinkage()) &&
2783 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002784 if (Importer.IsStructurallyEquivalent(D->getType(),
2785 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002786 MergeWithVar = FoundVar;
2787 break;
2788 }
2789
Douglas Gregord0145422010-02-12 17:23:39 +00002790 const ArrayType *FoundArray
2791 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2792 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002793 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002794 if (FoundArray && TArray) {
2795 if (isa<IncompleteArrayType>(FoundArray) &&
2796 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002797 // Import the type.
2798 QualType T = Importer.Import(D->getType());
2799 if (T.isNull())
2800 return 0;
2801
Douglas Gregord0145422010-02-12 17:23:39 +00002802 FoundVar->setType(T);
2803 MergeWithVar = FoundVar;
2804 break;
2805 } else if (isa<IncompleteArrayType>(TArray) &&
2806 isa<ConstantArrayType>(FoundArray)) {
2807 MergeWithVar = FoundVar;
2808 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002809 }
2810 }
2811
Douglas Gregor089459a2010-02-08 21:09:39 +00002812 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002813 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002814 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2815 << FoundVar->getType();
2816 }
2817 }
2818
Douglas Gregorb75a3452011-10-15 00:10:27 +00002819 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002820 }
2821
2822 if (MergeWithVar) {
2823 // An equivalent variable with external linkage has been found. Link
2824 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002825 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002826
2827 if (VarDecl *DDef = D->getDefinition()) {
2828 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2829 Importer.ToDiag(ExistingDef->getLocation(),
2830 diag::err_odr_variable_multiple_def)
2831 << Name;
2832 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2833 } else {
2834 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002835 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002836 if (DDef->isInitKnownICE()) {
2837 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2838 Eval->CheckedICE = true;
2839 Eval->IsICE = DDef->isInitICE();
2840 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002841 }
2842 }
2843
2844 return MergeWithVar;
2845 }
2846
2847 if (!ConflictingDecls.empty()) {
2848 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2849 ConflictingDecls.data(),
2850 ConflictingDecls.size());
2851 if (!Name)
2852 return 0;
2853 }
2854 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002855
Douglas Gregorea35d112010-02-15 23:54:17 +00002856 // Import the type.
2857 QualType T = Importer.Import(D->getType());
2858 if (T.isNull())
2859 return 0;
2860
Douglas Gregor089459a2010-02-08 21:09:39 +00002861 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002862 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002863 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2864 Importer.Import(D->getInnerLocStart()),
2865 Loc, Name.getAsIdentifierInfo(),
2866 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002867 D->getStorageClass(),
2868 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002869 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002870 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002871 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002872 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002873 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002874
Douglas Gregor089459a2010-02-08 21:09:39 +00002875 // Merge the initializer.
2876 // FIXME: Can we really import any initializer? Alternatively, we could force
2877 // ourselves to import every declaration of a variable and then only use
2878 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002879 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002880
2881 // FIXME: Other bits to merge?
2882
2883 return ToVar;
2884}
2885
Douglas Gregor2cd00932010-02-17 21:22:52 +00002886Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2887 // Parameters are created in the translation unit's context, then moved
2888 // into the function declaration's context afterward.
2889 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2890
2891 // Import the name of this declaration.
2892 DeclarationName Name = Importer.Import(D->getDeclName());
2893 if (D->getDeclName() && !Name)
2894 return 0;
2895
2896 // Import the location of this declaration.
2897 SourceLocation Loc = Importer.Import(D->getLocation());
2898
2899 // Import the parameter's type.
2900 QualType T = Importer.Import(D->getType());
2901 if (T.isNull())
2902 return 0;
2903
2904 // Create the imported parameter.
2905 ImplicitParamDecl *ToParm
2906 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2907 Loc, Name.getAsIdentifierInfo(),
2908 T);
2909 return Importer.Imported(D, ToParm);
2910}
2911
Douglas Gregora404ea62010-02-10 19:54:31 +00002912Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2913 // Parameters are created in the translation unit's context, then moved
2914 // into the function declaration's context afterward.
2915 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2916
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002917 // Import the name of this declaration.
2918 DeclarationName Name = Importer.Import(D->getDeclName());
2919 if (D->getDeclName() && !Name)
2920 return 0;
2921
Douglas Gregora404ea62010-02-10 19:54:31 +00002922 // Import the location of this declaration.
2923 SourceLocation Loc = Importer.Import(D->getLocation());
2924
2925 // Import the parameter's type.
2926 QualType T = Importer.Import(D->getType());
2927 if (T.isNull())
2928 return 0;
2929
2930 // Create the imported parameter.
2931 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2932 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002933 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002934 Loc, Name.getAsIdentifierInfo(),
2935 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002936 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002937 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002938 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002939 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002940}
2941
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002942Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2943 // Import the major distinguishing characteristics of a method.
2944 DeclContext *DC, *LexicalDC;
2945 DeclarationName Name;
2946 SourceLocation Loc;
2947 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2948 return 0;
2949
Douglas Gregorb75a3452011-10-15 00:10:27 +00002950 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2951 DC->localUncachedLookup(Name, FoundDecls);
2952 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2953 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002954 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2955 continue;
2956
2957 // Check return types.
2958 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2959 FoundMethod->getResultType())) {
2960 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2961 << D->isInstanceMethod() << Name
2962 << D->getResultType() << FoundMethod->getResultType();
2963 Importer.ToDiag(FoundMethod->getLocation(),
2964 diag::note_odr_objc_method_here)
2965 << D->isInstanceMethod() << Name;
2966 return 0;
2967 }
2968
2969 // Check the number of parameters.
2970 if (D->param_size() != FoundMethod->param_size()) {
2971 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2972 << D->isInstanceMethod() << Name
2973 << D->param_size() << FoundMethod->param_size();
2974 Importer.ToDiag(FoundMethod->getLocation(),
2975 diag::note_odr_objc_method_here)
2976 << D->isInstanceMethod() << Name;
2977 return 0;
2978 }
2979
2980 // Check parameter types.
2981 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2982 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2983 P != PEnd; ++P, ++FoundP) {
2984 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2985 (*FoundP)->getType())) {
2986 Importer.FromDiag((*P)->getLocation(),
2987 diag::err_odr_objc_method_param_type_inconsistent)
2988 << D->isInstanceMethod() << Name
2989 << (*P)->getType() << (*FoundP)->getType();
2990 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2991 << (*FoundP)->getType();
2992 return 0;
2993 }
2994 }
2995
2996 // Check variadic/non-variadic.
2997 // Check the number of parameters.
2998 if (D->isVariadic() != FoundMethod->isVariadic()) {
2999 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3000 << D->isInstanceMethod() << Name;
3001 Importer.ToDiag(FoundMethod->getLocation(),
3002 diag::note_odr_objc_method_here)
3003 << D->isInstanceMethod() << Name;
3004 return 0;
3005 }
3006
3007 // FIXME: Any other bits we need to merge?
3008 return Importer.Imported(D, FoundMethod);
3009 }
3010 }
3011
3012 // Import the result type.
3013 QualType ResultTy = Importer.Import(D->getResultType());
3014 if (ResultTy.isNull())
3015 return 0;
3016
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003017 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3018
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003019 ObjCMethodDecl *ToMethod
3020 = ObjCMethodDecl::Create(Importer.getToContext(),
3021 Loc,
3022 Importer.Import(D->getLocEnd()),
3023 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003024 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003025 D->isInstanceMethod(),
3026 D->isVariadic(),
3027 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003028 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003029 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00003030 D->getImplementationControl(),
3031 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003032
3033 // FIXME: When we decide to merge method definitions, we'll need to
3034 // deal with implicit parameters.
3035
3036 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003037 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003038 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3039 FromPEnd = D->param_end();
3040 FromP != FromPEnd;
3041 ++FromP) {
3042 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3043 if (!ToP)
3044 return 0;
3045
3046 ToParams.push_back(ToP);
3047 }
3048
3049 // Set the parameters.
3050 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3051 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003052 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003053 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003054 SmallVector<SourceLocation, 12> SelLocs;
3055 D->getSelectorLocs(SelLocs);
3056 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003057
3058 ToMethod->setLexicalDeclContext(LexicalDC);
3059 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003060 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003061 return ToMethod;
3062}
3063
Douglas Gregorb4677b62010-02-18 01:47:50 +00003064Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3065 // Import the major distinguishing characteristics of a category.
3066 DeclContext *DC, *LexicalDC;
3067 DeclarationName Name;
3068 SourceLocation Loc;
3069 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3070 return 0;
3071
3072 ObjCInterfaceDecl *ToInterface
3073 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3074 if (!ToInterface)
3075 return 0;
3076
3077 // Determine if we've already encountered this category.
3078 ObjCCategoryDecl *MergeWithCategory
3079 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3080 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3081 if (!ToCategory) {
3082 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003083 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003084 Loc,
3085 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003086 Name.getAsIdentifierInfo(),
3087 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003088 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003089 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003090 Importer.Imported(D, ToCategory);
3091
Douglas Gregorb4677b62010-02-18 01:47:50 +00003092 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003093 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3094 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003095 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3096 = D->protocol_loc_begin();
3097 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3098 FromProtoEnd = D->protocol_end();
3099 FromProto != FromProtoEnd;
3100 ++FromProto, ++FromProtoLoc) {
3101 ObjCProtocolDecl *ToProto
3102 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3103 if (!ToProto)
3104 return 0;
3105 Protocols.push_back(ToProto);
3106 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3107 }
3108
3109 // FIXME: If we're merging, make sure that the protocol list is the same.
3110 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3111 ProtocolLocs.data(), Importer.getToContext());
3112
3113 } else {
3114 Importer.Imported(D, ToCategory);
3115 }
3116
3117 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003118 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003119
3120 // If we have an implementation, import it as well.
3121 if (D->getImplementation()) {
3122 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003123 = cast_or_null<ObjCCategoryImplDecl>(
3124 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003125 if (!Impl)
3126 return 0;
3127
3128 ToCategory->setImplementation(Impl);
3129 }
3130
3131 return ToCategory;
3132}
3133
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003134bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3135 ObjCProtocolDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003136 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003137 if (To->getDefinition()) {
Douglas Gregorac32ff92012-02-01 21:00:38 +00003138 if (shouldForceImportDeclContext(Kind))
3139 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003140 return false;
3141 }
3142
3143 // Start the protocol definition
3144 To->startDefinition();
3145
3146 // Import protocols
3147 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3148 SmallVector<SourceLocation, 4> ProtocolLocs;
3149 ObjCProtocolDecl::protocol_loc_iterator
3150 FromProtoLoc = From->protocol_loc_begin();
3151 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3152 FromProtoEnd = From->protocol_end();
3153 FromProto != FromProtoEnd;
3154 ++FromProto, ++FromProtoLoc) {
3155 ObjCProtocolDecl *ToProto
3156 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3157 if (!ToProto)
3158 return true;
3159 Protocols.push_back(ToProto);
3160 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3161 }
3162
3163 // FIXME: If we're merging, make sure that the protocol list is the same.
3164 To->setProtocolList(Protocols.data(), Protocols.size(),
3165 ProtocolLocs.data(), Importer.getToContext());
3166
Douglas Gregorac32ff92012-02-01 21:00:38 +00003167 if (shouldForceImportDeclContext(Kind)) {
3168 // Import all of the members of this protocol.
3169 ImportDeclContext(From, /*ForceImport=*/true);
3170 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003171 return false;
3172}
3173
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003174Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003175 // If this protocol has a definition in the translation unit we're coming
3176 // from, but this particular declaration is not that definition, import the
3177 // definition and map to that.
3178 ObjCProtocolDecl *Definition = D->getDefinition();
3179 if (Definition && Definition != D) {
3180 Decl *ImportedDef = Importer.Import(Definition);
3181 if (!ImportedDef)
3182 return 0;
3183
3184 return Importer.Imported(D, ImportedDef);
3185 }
3186
Douglas Gregorb4677b62010-02-18 01:47:50 +00003187 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003188 DeclContext *DC, *LexicalDC;
3189 DeclarationName Name;
3190 SourceLocation Loc;
3191 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3192 return 0;
3193
3194 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003195 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3196 DC->localUncachedLookup(Name, FoundDecls);
3197 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3198 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003199 continue;
3200
Douglas Gregorb75a3452011-10-15 00:10:27 +00003201 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003202 break;
3203 }
3204
3205 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003206 if (!ToProto) {
3207 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3208 Name.getAsIdentifierInfo(), Loc,
3209 Importer.Import(D->getAtStartLoc()),
3210 /*PrevDecl=*/0);
3211 ToProto->setLexicalDeclContext(LexicalDC);
3212 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003213 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003214
3215 Importer.Imported(D, ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003216
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003217 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3218 return 0;
3219
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003220 return ToProto;
3221}
3222
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003223bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3224 ObjCInterfaceDecl *To,
Douglas Gregorac32ff92012-02-01 21:00:38 +00003225 ImportDefinitionKind Kind) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003226 if (To->getDefinition()) {
3227 // Check consistency of superclass.
3228 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3229 if (FromSuper) {
3230 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3231 if (!FromSuper)
3232 return true;
3233 }
3234
3235 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3236 if ((bool)FromSuper != (bool)ToSuper ||
3237 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3238 Importer.ToDiag(To->getLocation(),
3239 diag::err_odr_objc_superclass_inconsistent)
3240 << To->getDeclName();
3241 if (ToSuper)
3242 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3243 << To->getSuperClass()->getDeclName();
3244 else
3245 Importer.ToDiag(To->getLocation(),
3246 diag::note_odr_objc_missing_superclass);
3247 if (From->getSuperClass())
3248 Importer.FromDiag(From->getSuperClassLoc(),
3249 diag::note_odr_objc_superclass)
3250 << From->getSuperClass()->getDeclName();
3251 else
3252 Importer.FromDiag(From->getLocation(),
3253 diag::note_odr_objc_missing_superclass);
3254 }
3255
Douglas Gregorac32ff92012-02-01 21:00:38 +00003256 if (shouldForceImportDeclContext(Kind))
3257 ImportDeclContext(From);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003258 return false;
3259 }
3260
3261 // Start the definition.
3262 To->startDefinition();
3263
3264 // If this class has a superclass, import it.
3265 if (From->getSuperClass()) {
3266 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3267 Importer.Import(From->getSuperClass()));
3268 if (!Super)
3269 return true;
3270
3271 To->setSuperClass(Super);
3272 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3273 }
3274
3275 // Import protocols
3276 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3277 SmallVector<SourceLocation, 4> ProtocolLocs;
3278 ObjCInterfaceDecl::protocol_loc_iterator
3279 FromProtoLoc = From->protocol_loc_begin();
3280
3281 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3282 FromProtoEnd = From->protocol_end();
3283 FromProto != FromProtoEnd;
3284 ++FromProto, ++FromProtoLoc) {
3285 ObjCProtocolDecl *ToProto
3286 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3287 if (!ToProto)
3288 return true;
3289 Protocols.push_back(ToProto);
3290 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3291 }
3292
3293 // FIXME: If we're merging, make sure that the protocol list is the same.
3294 To->setProtocolList(Protocols.data(), Protocols.size(),
3295 ProtocolLocs.data(), Importer.getToContext());
3296
3297 // Import categories. When the categories themselves are imported, they'll
3298 // hook themselves into this interface.
3299 for (ObjCCategoryDecl *FromCat = From->getCategoryList(); FromCat;
3300 FromCat = FromCat->getNextClassCategory())
3301 Importer.Import(FromCat);
3302
3303 // If we have an @implementation, import it as well.
3304 if (From->getImplementation()) {
3305 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3306 Importer.Import(From->getImplementation()));
3307 if (!Impl)
3308 return true;
3309
3310 To->setImplementation(Impl);
3311 }
3312
Douglas Gregorac32ff92012-02-01 21:00:38 +00003313 if (shouldForceImportDeclContext(Kind)) {
3314 // Import all of the members of this class.
3315 ImportDeclContext(From, /*ForceImport=*/true);
3316 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003317 return false;
3318}
3319
Douglas Gregora12d2942010-02-16 01:20:57 +00003320Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003321 // If this class has a definition in the translation unit we're coming from,
3322 // but this particular declaration is not that definition, import the
3323 // definition and map to that.
3324 ObjCInterfaceDecl *Definition = D->getDefinition();
3325 if (Definition && Definition != D) {
3326 Decl *ImportedDef = Importer.Import(Definition);
3327 if (!ImportedDef)
3328 return 0;
3329
3330 return Importer.Imported(D, ImportedDef);
3331 }
3332
Douglas Gregora12d2942010-02-16 01:20:57 +00003333 // Import the major distinguishing characteristics of an @interface.
3334 DeclContext *DC, *LexicalDC;
3335 DeclarationName Name;
3336 SourceLocation Loc;
3337 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3338 return 0;
3339
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003340 // Look for an existing interface with the same name.
Douglas Gregora12d2942010-02-16 01:20:57 +00003341 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003342 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3343 DC->localUncachedLookup(Name, FoundDecls);
3344 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3345 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003346 continue;
3347
Douglas Gregorb75a3452011-10-15 00:10:27 +00003348 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003349 break;
3350 }
3351
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003352 // Create an interface declaration, if one does not already exist.
Douglas Gregora12d2942010-02-16 01:20:57 +00003353 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003354 if (!ToIface) {
3355 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3356 Importer.Import(D->getAtStartLoc()),
3357 Name.getAsIdentifierInfo(),
3358 /*PrevDecl=*/0,Loc,
3359 D->isImplicitInterfaceDecl());
3360 ToIface->setLexicalDeclContext(LexicalDC);
3361 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003362 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003363 Importer.Imported(D, ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003364
Douglas Gregor5602f7e2012-01-24 17:42:07 +00003365 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3366 return 0;
Douglas Gregora12d2942010-02-16 01:20:57 +00003367
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003368 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003369}
3370
Douglas Gregor3daef292010-12-07 15:32:12 +00003371Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3372 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3373 Importer.Import(D->getCategoryDecl()));
3374 if (!Category)
3375 return 0;
3376
3377 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3378 if (!ToImpl) {
3379 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3380 if (!DC)
3381 return 0;
3382
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003383 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003384 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003385 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003386 Category->getClassInterface(),
3387 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003388 Importer.Import(D->getAtStartLoc()),
3389 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003390
3391 DeclContext *LexicalDC = DC;
3392 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3393 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3394 if (!LexicalDC)
3395 return 0;
3396
3397 ToImpl->setLexicalDeclContext(LexicalDC);
3398 }
3399
Sean Callanan9faf8102011-10-21 02:57:43 +00003400 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003401 Category->setImplementation(ToImpl);
3402 }
3403
3404 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003405 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003406 return ToImpl;
3407}
3408
Douglas Gregordd182ff2010-12-07 01:26:03 +00003409Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3410 // Find the corresponding interface.
3411 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3412 Importer.Import(D->getClassInterface()));
3413 if (!Iface)
3414 return 0;
3415
3416 // Import the superclass, if any.
3417 ObjCInterfaceDecl *Super = 0;
3418 if (D->getSuperClass()) {
3419 Super = cast_or_null<ObjCInterfaceDecl>(
3420 Importer.Import(D->getSuperClass()));
3421 if (!Super)
3422 return 0;
3423 }
3424
3425 ObjCImplementationDecl *Impl = Iface->getImplementation();
3426 if (!Impl) {
3427 // We haven't imported an implementation yet. Create a new @implementation
3428 // now.
3429 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3430 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003431 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003432 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003433 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003434
3435 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3436 DeclContext *LexicalDC
3437 = Importer.ImportContext(D->getLexicalDeclContext());
3438 if (!LexicalDC)
3439 return 0;
3440 Impl->setLexicalDeclContext(LexicalDC);
3441 }
3442
3443 // Associate the implementation with the class it implements.
3444 Iface->setImplementation(Impl);
3445 Importer.Imported(D, Iface->getImplementation());
3446 } else {
3447 Importer.Imported(D, Iface->getImplementation());
3448
3449 // Verify that the existing @implementation has the same superclass.
3450 if ((Super && !Impl->getSuperClass()) ||
3451 (!Super && Impl->getSuperClass()) ||
3452 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003453 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003454 Importer.ToDiag(Impl->getLocation(),
3455 diag::err_odr_objc_superclass_inconsistent)
3456 << Iface->getDeclName();
3457 // FIXME: It would be nice to have the location of the superclass
3458 // below.
3459 if (Impl->getSuperClass())
3460 Importer.ToDiag(Impl->getLocation(),
3461 diag::note_odr_objc_superclass)
3462 << Impl->getSuperClass()->getDeclName();
3463 else
3464 Importer.ToDiag(Impl->getLocation(),
3465 diag::note_odr_objc_missing_superclass);
3466 if (D->getSuperClass())
3467 Importer.FromDiag(D->getLocation(),
3468 diag::note_odr_objc_superclass)
3469 << D->getSuperClass()->getDeclName();
3470 else
3471 Importer.FromDiag(D->getLocation(),
3472 diag::note_odr_objc_missing_superclass);
3473 return 0;
3474 }
3475 }
3476
3477 // Import all of the members of this @implementation.
3478 ImportDeclContext(D);
3479
3480 return Impl;
3481}
3482
Douglas Gregore3261622010-02-17 18:02:10 +00003483Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3484 // Import the major distinguishing characteristics of an @property.
3485 DeclContext *DC, *LexicalDC;
3486 DeclarationName Name;
3487 SourceLocation Loc;
3488 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3489 return 0;
3490
3491 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003492 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3493 DC->localUncachedLookup(Name, FoundDecls);
3494 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003495 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003496 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003497 // Check property types.
3498 if (!Importer.IsStructurallyEquivalent(D->getType(),
3499 FoundProp->getType())) {
3500 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3501 << Name << D->getType() << FoundProp->getType();
3502 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3503 << FoundProp->getType();
3504 return 0;
3505 }
3506
3507 // FIXME: Check property attributes, getters, setters, etc.?
3508
3509 // Consider these properties to be equivalent.
3510 Importer.Imported(D, FoundProp);
3511 return FoundProp;
3512 }
3513 }
3514
3515 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003516 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3517 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003518 return 0;
3519
3520 // Create the new property.
3521 ObjCPropertyDecl *ToProperty
3522 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3523 Name.getAsIdentifierInfo(),
3524 Importer.Import(D->getAtLoc()),
3525 T,
3526 D->getPropertyImplementation());
3527 Importer.Imported(D, ToProperty);
3528 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003529 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003530
3531 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003532 ToProperty->setPropertyAttributesAsWritten(
3533 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003534 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3535 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3536 ToProperty->setGetterMethodDecl(
3537 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3538 ToProperty->setSetterMethodDecl(
3539 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3540 ToProperty->setPropertyIvarDecl(
3541 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3542 return ToProperty;
3543}
3544
Douglas Gregor954e0c72010-12-07 18:32:03 +00003545Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3546 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3547 Importer.Import(D->getPropertyDecl()));
3548 if (!Property)
3549 return 0;
3550
3551 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3552 if (!DC)
3553 return 0;
3554
3555 // Import the lexical declaration context.
3556 DeclContext *LexicalDC = DC;
3557 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3558 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3559 if (!LexicalDC)
3560 return 0;
3561 }
3562
3563 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3564 if (!InImpl)
3565 return 0;
3566
3567 // Import the ivar (for an @synthesize).
3568 ObjCIvarDecl *Ivar = 0;
3569 if (D->getPropertyIvarDecl()) {
3570 Ivar = cast_or_null<ObjCIvarDecl>(
3571 Importer.Import(D->getPropertyIvarDecl()));
3572 if (!Ivar)
3573 return 0;
3574 }
3575
3576 ObjCPropertyImplDecl *ToImpl
3577 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3578 if (!ToImpl) {
3579 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3580 Importer.Import(D->getLocStart()),
3581 Importer.Import(D->getLocation()),
3582 Property,
3583 D->getPropertyImplementation(),
3584 Ivar,
3585 Importer.Import(D->getPropertyIvarDeclLoc()));
3586 ToImpl->setLexicalDeclContext(LexicalDC);
3587 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003588 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003589 } else {
3590 // Check that we have the same kind of property implementation (@synthesize
3591 // vs. @dynamic).
3592 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3593 Importer.ToDiag(ToImpl->getLocation(),
3594 diag::err_odr_objc_property_impl_kind_inconsistent)
3595 << Property->getDeclName()
3596 << (ToImpl->getPropertyImplementation()
3597 == ObjCPropertyImplDecl::Dynamic);
3598 Importer.FromDiag(D->getLocation(),
3599 diag::note_odr_objc_property_impl_kind)
3600 << D->getPropertyDecl()->getDeclName()
3601 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3602 return 0;
3603 }
3604
3605 // For @synthesize, check that we have the same
3606 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3607 Ivar != ToImpl->getPropertyIvarDecl()) {
3608 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3609 diag::err_odr_objc_synthesize_ivar_inconsistent)
3610 << Property->getDeclName()
3611 << ToImpl->getPropertyIvarDecl()->getDeclName()
3612 << Ivar->getDeclName();
3613 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3614 diag::note_odr_objc_synthesize_ivar_here)
3615 << D->getPropertyIvarDecl()->getDeclName();
3616 return 0;
3617 }
3618
3619 // Merge the existing implementation with the new implementation.
3620 Importer.Imported(D, ToImpl);
3621 }
3622
3623 return ToImpl;
3624}
3625
Douglas Gregor040afae2010-11-30 19:14:50 +00003626Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3627 // For template arguments, we adopt the translation unit as our declaration
3628 // context. This context will be fixed when the actual template declaration
3629 // is created.
3630
3631 // FIXME: Import default argument.
3632 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3633 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003634 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003635 Importer.Import(D->getLocation()),
3636 D->getDepth(),
3637 D->getIndex(),
3638 Importer.Import(D->getIdentifier()),
3639 D->wasDeclaredWithTypename(),
3640 D->isParameterPack());
3641}
3642
3643Decl *
3644ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3645 // Import the name of this declaration.
3646 DeclarationName Name = Importer.Import(D->getDeclName());
3647 if (D->getDeclName() && !Name)
3648 return 0;
3649
3650 // Import the location of this declaration.
3651 SourceLocation Loc = Importer.Import(D->getLocation());
3652
3653 // Import the type of this declaration.
3654 QualType T = Importer.Import(D->getType());
3655 if (T.isNull())
3656 return 0;
3657
3658 // Import type-source information.
3659 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3660 if (D->getTypeSourceInfo() && !TInfo)
3661 return 0;
3662
3663 // FIXME: Import default argument.
3664
3665 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3666 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003667 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003668 Loc, D->getDepth(), D->getPosition(),
3669 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003670 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003671}
3672
3673Decl *
3674ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3675 // Import the name of this declaration.
3676 DeclarationName Name = Importer.Import(D->getDeclName());
3677 if (D->getDeclName() && !Name)
3678 return 0;
3679
3680 // Import the location of this declaration.
3681 SourceLocation Loc = Importer.Import(D->getLocation());
3682
3683 // Import template parameters.
3684 TemplateParameterList *TemplateParams
3685 = ImportTemplateParameterList(D->getTemplateParameters());
3686 if (!TemplateParams)
3687 return 0;
3688
3689 // FIXME: Import default argument.
3690
3691 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3692 Importer.getToContext().getTranslationUnitDecl(),
3693 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003694 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003695 Name.getAsIdentifierInfo(),
3696 TemplateParams);
3697}
3698
3699Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3700 // If this record has a definition in the translation unit we're coming from,
3701 // but this particular declaration is not that definition, import the
3702 // definition and map to that.
3703 CXXRecordDecl *Definition
3704 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3705 if (Definition && Definition != D->getTemplatedDecl()) {
3706 Decl *ImportedDef
3707 = Importer.Import(Definition->getDescribedClassTemplate());
3708 if (!ImportedDef)
3709 return 0;
3710
3711 return Importer.Imported(D, ImportedDef);
3712 }
3713
3714 // Import the major distinguishing characteristics of this class template.
3715 DeclContext *DC, *LexicalDC;
3716 DeclarationName Name;
3717 SourceLocation Loc;
3718 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3719 return 0;
3720
3721 // We may already have a template of the same name; try to find and match it.
3722 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003723 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003724 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3725 DC->localUncachedLookup(Name, FoundDecls);
3726 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3727 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003728 continue;
3729
Douglas Gregorb75a3452011-10-15 00:10:27 +00003730 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003731 if (ClassTemplateDecl *FoundTemplate
3732 = dyn_cast<ClassTemplateDecl>(Found)) {
3733 if (IsStructuralMatch(D, FoundTemplate)) {
3734 // The class templates structurally match; call it the same template.
3735 // FIXME: We may be filling in a forward declaration here. Handle
3736 // this case!
3737 Importer.Imported(D->getTemplatedDecl(),
3738 FoundTemplate->getTemplatedDecl());
3739 return Importer.Imported(D, FoundTemplate);
3740 }
3741 }
3742
Douglas Gregorb75a3452011-10-15 00:10:27 +00003743 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003744 }
3745
3746 if (!ConflictingDecls.empty()) {
3747 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3748 ConflictingDecls.data(),
3749 ConflictingDecls.size());
3750 }
3751
3752 if (!Name)
3753 return 0;
3754 }
3755
3756 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3757
3758 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003759 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3760 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003761 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3762 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003763 DC, StartLoc, IdLoc,
3764 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003765 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003766 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003767 D2Templated->setLexicalDeclContext(LexicalDC);
3768
3769 // Create the class template declaration itself.
3770 TemplateParameterList *TemplateParams
3771 = ImportTemplateParameterList(D->getTemplateParameters());
3772 if (!TemplateParams)
3773 return 0;
3774
3775 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3776 Loc, Name, TemplateParams,
3777 D2Templated,
3778 /*PrevDecl=*/0);
3779 D2Templated->setDescribedClassTemplate(D2);
3780
3781 D2->setAccess(D->getAccess());
3782 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003783 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003784
3785 // Note the relationship between the class templates.
3786 Importer.Imported(D, D2);
3787 Importer.Imported(DTemplated, D2Templated);
3788
John McCall5e1cdac2011-10-07 06:10:15 +00003789 if (DTemplated->isCompleteDefinition() &&
3790 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003791 // FIXME: Import definition!
3792 }
3793
3794 return D2;
3795}
3796
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003797Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3798 ClassTemplateSpecializationDecl *D) {
3799 // If this record has a definition in the translation unit we're coming from,
3800 // but this particular declaration is not that definition, import the
3801 // definition and map to that.
3802 TagDecl *Definition = D->getDefinition();
3803 if (Definition && Definition != D) {
3804 Decl *ImportedDef = Importer.Import(Definition);
3805 if (!ImportedDef)
3806 return 0;
3807
3808 return Importer.Imported(D, ImportedDef);
3809 }
3810
3811 ClassTemplateDecl *ClassTemplate
3812 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3813 D->getSpecializedTemplate()));
3814 if (!ClassTemplate)
3815 return 0;
3816
3817 // Import the context of this declaration.
3818 DeclContext *DC = ClassTemplate->getDeclContext();
3819 if (!DC)
3820 return 0;
3821
3822 DeclContext *LexicalDC = DC;
3823 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3824 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3825 if (!LexicalDC)
3826 return 0;
3827 }
3828
3829 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003830 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3831 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003832
3833 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003834 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003835 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3836 D->getTemplateArgs().size(),
3837 TemplateArgs))
3838 return 0;
3839
3840 // Try to find an existing specialization with these template arguments.
3841 void *InsertPos = 0;
3842 ClassTemplateSpecializationDecl *D2
3843 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3844 TemplateArgs.size(), InsertPos);
3845 if (D2) {
3846 // We already have a class template specialization with these template
3847 // arguments.
3848
3849 // FIXME: Check for specialization vs. instantiation errors.
3850
3851 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003852 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003853 // The record types structurally match, or the "from" translation
3854 // unit only had a forward declaration anyway; call it the same
3855 // function.
3856 return Importer.Imported(D, FoundDef);
3857 }
3858 }
3859 } else {
3860 // Create a new specialization.
3861 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3862 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003863 StartLoc, IdLoc,
3864 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003865 TemplateArgs.data(),
3866 TemplateArgs.size(),
3867 /*PrevDecl=*/0);
3868 D2->setSpecializationKind(D->getSpecializationKind());
3869
3870 // Add this specialization to the class template.
3871 ClassTemplate->AddSpecialization(D2, InsertPos);
3872
3873 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003874 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003875
3876 // Add the specialization to this context.
3877 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003878 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003879 }
3880 Importer.Imported(D, D2);
3881
John McCall5e1cdac2011-10-07 06:10:15 +00003882 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003883 return 0;
3884
3885 return D2;
3886}
3887
Douglas Gregor4800d952010-02-11 19:21:55 +00003888//----------------------------------------------------------------------------
3889// Import Statements
3890//----------------------------------------------------------------------------
3891
3892Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3893 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3894 << S->getStmtClassName();
3895 return 0;
3896}
3897
3898//----------------------------------------------------------------------------
3899// Import Expressions
3900//----------------------------------------------------------------------------
3901Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3902 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3903 << E->getStmtClassName();
3904 return 0;
3905}
3906
Douglas Gregor44080632010-02-19 01:17:02 +00003907Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003908 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3909 if (!ToD)
3910 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003911
3912 NamedDecl *FoundD = 0;
3913 if (E->getDecl() != E->getFoundDecl()) {
3914 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3915 if (!FoundD)
3916 return 0;
3917 }
Douglas Gregor44080632010-02-19 01:17:02 +00003918
3919 QualType T = Importer.Import(E->getType());
3920 if (T.isNull())
3921 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003922
3923 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3924 Importer.Import(E->getQualifierLoc()),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00003925 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003926 ToD,
3927 Importer.Import(E->getLocation()),
3928 T, E->getValueKind(),
3929 FoundD,
3930 /*FIXME:TemplateArgs=*/0);
3931 if (E->hadMultipleCandidates())
3932 DRE->setHadMultipleCandidates(true);
3933 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003934}
3935
Douglas Gregor4800d952010-02-11 19:21:55 +00003936Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3937 QualType T = Importer.Import(E->getType());
3938 if (T.isNull())
3939 return 0;
3940
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003941 return IntegerLiteral::Create(Importer.getToContext(),
3942 E->getValue(), T,
3943 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003944}
3945
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003946Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3947 QualType T = Importer.Import(E->getType());
3948 if (T.isNull())
3949 return 0;
3950
Douglas Gregor5cee1192011-07-27 05:40:30 +00003951 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3952 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003953 Importer.Import(E->getLocation()));
3954}
3955
Douglas Gregorf638f952010-02-19 01:07:06 +00003956Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3957 Expr *SubExpr = Importer.Import(E->getSubExpr());
3958 if (!SubExpr)
3959 return 0;
3960
3961 return new (Importer.getToContext())
3962 ParenExpr(Importer.Import(E->getLParen()),
3963 Importer.Import(E->getRParen()),
3964 SubExpr);
3965}
3966
3967Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3968 QualType T = Importer.Import(E->getType());
3969 if (T.isNull())
3970 return 0;
3971
3972 Expr *SubExpr = Importer.Import(E->getSubExpr());
3973 if (!SubExpr)
3974 return 0;
3975
3976 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003977 T, E->getValueKind(),
3978 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003979 Importer.Import(E->getOperatorLoc()));
3980}
3981
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003982Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3983 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003984 QualType ResultType = Importer.Import(E->getType());
3985
3986 if (E->isArgumentType()) {
3987 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3988 if (!TInfo)
3989 return 0;
3990
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003991 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3992 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003993 Importer.Import(E->getOperatorLoc()),
3994 Importer.Import(E->getRParenLoc()));
3995 }
3996
3997 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3998 if (!SubExpr)
3999 return 0;
4000
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004001 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4002 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00004003 Importer.Import(E->getOperatorLoc()),
4004 Importer.Import(E->getRParenLoc()));
4005}
4006
Douglas Gregorf638f952010-02-19 01:07:06 +00004007Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4008 QualType T = Importer.Import(E->getType());
4009 if (T.isNull())
4010 return 0;
4011
4012 Expr *LHS = Importer.Import(E->getLHS());
4013 if (!LHS)
4014 return 0;
4015
4016 Expr *RHS = Importer.Import(E->getRHS());
4017 if (!RHS)
4018 return 0;
4019
4020 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004021 T, E->getValueKind(),
4022 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00004023 Importer.Import(E->getOperatorLoc()));
4024}
4025
4026Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4027 QualType T = Importer.Import(E->getType());
4028 if (T.isNull())
4029 return 0;
4030
4031 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4032 if (CompLHSType.isNull())
4033 return 0;
4034
4035 QualType CompResultType = Importer.Import(E->getComputationResultType());
4036 if (CompResultType.isNull())
4037 return 0;
4038
4039 Expr *LHS = Importer.Import(E->getLHS());
4040 if (!LHS)
4041 return 0;
4042
4043 Expr *RHS = Importer.Import(E->getRHS());
4044 if (!RHS)
4045 return 0;
4046
4047 return new (Importer.getToContext())
4048 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004049 T, E->getValueKind(),
4050 E->getObjectKind(),
4051 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004052 Importer.Import(E->getOperatorLoc()));
4053}
4054
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004055static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004056 if (E->path_empty()) return false;
4057
4058 // TODO: import cast paths
4059 return true;
4060}
4061
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004062Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4063 QualType T = Importer.Import(E->getType());
4064 if (T.isNull())
4065 return 0;
4066
4067 Expr *SubExpr = Importer.Import(E->getSubExpr());
4068 if (!SubExpr)
4069 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004070
4071 CXXCastPath BasePath;
4072 if (ImportCastPath(E, BasePath))
4073 return 0;
4074
4075 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004076 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004077}
4078
Douglas Gregor008847a2010-02-19 01:32:14 +00004079Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4080 QualType T = Importer.Import(E->getType());
4081 if (T.isNull())
4082 return 0;
4083
4084 Expr *SubExpr = Importer.Import(E->getSubExpr());
4085 if (!SubExpr)
4086 return 0;
4087
4088 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4089 if (!TInfo && E->getTypeInfoAsWritten())
4090 return 0;
4091
John McCallf871d0c2010-08-07 06:22:56 +00004092 CXXCastPath BasePath;
4093 if (ImportCastPath(E, BasePath))
4094 return 0;
4095
John McCallf89e55a2010-11-18 06:31:45 +00004096 return CStyleCastExpr::Create(Importer.getToContext(), T,
4097 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004098 SubExpr, &BasePath, TInfo,
4099 Importer.Import(E->getLParenLoc()),
4100 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004101}
4102
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004103ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004104 ASTContext &FromContext, FileManager &FromFileManager,
4105 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004106 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004107 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4108 Minimal(MinimalImport)
4109{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004110 ImportedDecls[FromContext.getTranslationUnitDecl()]
4111 = ToContext.getTranslationUnitDecl();
4112}
4113
4114ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004115
4116QualType ASTImporter::Import(QualType FromT) {
4117 if (FromT.isNull())
4118 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004119
4120 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004121
Douglas Gregor169fba52010-02-08 15:18:58 +00004122 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004123 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4124 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004125 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004126 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004127
Douglas Gregor169fba52010-02-08 15:18:58 +00004128 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004129 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004130 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004131 if (ToT.isNull())
4132 return ToT;
4133
Douglas Gregor169fba52010-02-08 15:18:58 +00004134 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004135 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004136
John McCallf4c73712011-01-19 06:33:43 +00004137 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004138}
4139
Douglas Gregor9bed8792010-02-09 19:21:46 +00004140TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004141 if (!FromTSI)
4142 return FromTSI;
4143
4144 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004145 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004146 QualType T = Import(FromTSI->getType());
4147 if (T.isNull())
4148 return 0;
4149
4150 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004151 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004152}
4153
4154Decl *ASTImporter::Import(Decl *FromD) {
4155 if (!FromD)
4156 return 0;
4157
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004158 ASTNodeImporter Importer(*this);
4159
Douglas Gregor9bed8792010-02-09 19:21:46 +00004160 // Check whether we've already imported this declaration.
4161 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004162 if (Pos != ImportedDecls.end()) {
4163 Decl *ToD = Pos->second;
4164 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4165 return ToD;
4166 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004167
4168 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004169 Decl *ToD = Importer.Visit(FromD);
4170 if (!ToD)
4171 return 0;
4172
4173 // Record the imported declaration.
4174 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004175
4176 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4177 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004178 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004179 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004180 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004181 // When we've finished transforming a typedef, see whether it was the
4182 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004183 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004184 FromTag = AnonTagsWithPendingTypedefs.begin(),
4185 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4186 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004187 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004188 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4189 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004190 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004191 AnonTagsWithPendingTypedefs.erase(FromTag);
4192 break;
4193 }
4194 }
4195 }
4196 }
4197
Douglas Gregor9bed8792010-02-09 19:21:46 +00004198 return ToD;
4199}
4200
4201DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4202 if (!FromDC)
4203 return FromDC;
4204
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004205 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregorac32ff92012-02-01 21:00:38 +00004206 if (!ToDC)
4207 return 0;
4208
4209 // When we're using a record/enum/Objective-C class/protocol as a context, we
4210 // need it to have a definition.
4211 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor568991b2012-01-25 01:13:20 +00004212 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregorac32ff92012-02-01 21:00:38 +00004213 if (ToRecord->isCompleteDefinition()) {
4214 // Do nothing.
4215 } else if (FromRecord->isCompleteDefinition()) {
4216 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4217 ASTNodeImporter::IDK_Basic);
4218 } else {
4219 CompleteDecl(ToRecord);
4220 }
4221 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4222 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4223 if (ToEnum->isCompleteDefinition()) {
4224 // Do nothing.
4225 } else if (FromEnum->isCompleteDefinition()) {
4226 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4227 ASTNodeImporter::IDK_Basic);
4228 } else {
4229 CompleteDecl(ToEnum);
4230 }
4231 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4232 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4233 if (ToClass->getDefinition()) {
4234 // Do nothing.
4235 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4236 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4237 ASTNodeImporter::IDK_Basic);
4238 } else {
4239 CompleteDecl(ToClass);
4240 }
4241 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4242 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4243 if (ToProto->getDefinition()) {
4244 // Do nothing.
4245 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4246 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4247 ASTNodeImporter::IDK_Basic);
4248 } else {
4249 CompleteDecl(ToProto);
4250 }
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004251 }
4252
4253 return ToDC;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004254}
4255
4256Expr *ASTImporter::Import(Expr *FromE) {
4257 if (!FromE)
4258 return 0;
4259
4260 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4261}
4262
4263Stmt *ASTImporter::Import(Stmt *FromS) {
4264 if (!FromS)
4265 return 0;
4266
Douglas Gregor4800d952010-02-11 19:21:55 +00004267 // Check whether we've already imported this declaration.
4268 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4269 if (Pos != ImportedStmts.end())
4270 return Pos->second;
4271
4272 // Import the type
4273 ASTNodeImporter Importer(*this);
4274 Stmt *ToS = Importer.Visit(FromS);
4275 if (!ToS)
4276 return 0;
4277
4278 // Record the imported declaration.
4279 ImportedStmts[FromS] = ToS;
4280 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004281}
4282
4283NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4284 if (!FromNNS)
4285 return 0;
4286
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004287 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4288
4289 switch (FromNNS->getKind()) {
4290 case NestedNameSpecifier::Identifier:
4291 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4292 return NestedNameSpecifier::Create(ToContext, prefix, II);
4293 }
4294 return 0;
4295
4296 case NestedNameSpecifier::Namespace:
4297 if (NamespaceDecl *NS =
4298 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4299 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4300 }
4301 return 0;
4302
4303 case NestedNameSpecifier::NamespaceAlias:
4304 if (NamespaceAliasDecl *NSAD =
4305 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4306 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4307 }
4308 return 0;
4309
4310 case NestedNameSpecifier::Global:
4311 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4312
4313 case NestedNameSpecifier::TypeSpec:
4314 case NestedNameSpecifier::TypeSpecWithTemplate: {
4315 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4316 if (!T.isNull()) {
4317 bool bTemplate = FromNNS->getKind() ==
4318 NestedNameSpecifier::TypeSpecWithTemplate;
4319 return NestedNameSpecifier::Create(ToContext, prefix,
4320 bTemplate, T.getTypePtr());
4321 }
4322 }
4323 return 0;
4324 }
4325
4326 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004327}
4328
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004329NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4330 // FIXME: Implement!
4331 return NestedNameSpecifierLoc();
4332}
4333
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004334TemplateName ASTImporter::Import(TemplateName From) {
4335 switch (From.getKind()) {
4336 case TemplateName::Template:
4337 if (TemplateDecl *ToTemplate
4338 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4339 return TemplateName(ToTemplate);
4340
4341 return TemplateName();
4342
4343 case TemplateName::OverloadedTemplate: {
4344 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4345 UnresolvedSet<2> ToTemplates;
4346 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4347 E = FromStorage->end();
4348 I != E; ++I) {
4349 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4350 ToTemplates.addDecl(To);
4351 else
4352 return TemplateName();
4353 }
4354 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4355 ToTemplates.end());
4356 }
4357
4358 case TemplateName::QualifiedTemplate: {
4359 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4360 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4361 if (!Qualifier)
4362 return TemplateName();
4363
4364 if (TemplateDecl *ToTemplate
4365 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4366 return ToContext.getQualifiedTemplateName(Qualifier,
4367 QTN->hasTemplateKeyword(),
4368 ToTemplate);
4369
4370 return TemplateName();
4371 }
4372
4373 case TemplateName::DependentTemplate: {
4374 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4375 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4376 if (!Qualifier)
4377 return TemplateName();
4378
4379 if (DTN->isIdentifier()) {
4380 return ToContext.getDependentTemplateName(Qualifier,
4381 Import(DTN->getIdentifier()));
4382 }
4383
4384 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4385 }
John McCall14606042011-06-30 08:33:18 +00004386
4387 case TemplateName::SubstTemplateTemplateParm: {
4388 SubstTemplateTemplateParmStorage *subst
4389 = From.getAsSubstTemplateTemplateParm();
4390 TemplateTemplateParmDecl *param
4391 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4392 if (!param)
4393 return TemplateName();
4394
4395 TemplateName replacement = Import(subst->getReplacement());
4396 if (replacement.isNull()) return TemplateName();
4397
4398 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4399 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004400
4401 case TemplateName::SubstTemplateTemplateParmPack: {
4402 SubstTemplateTemplateParmPackStorage *SubstPack
4403 = From.getAsSubstTemplateTemplateParmPack();
4404 TemplateTemplateParmDecl *Param
4405 = cast_or_null<TemplateTemplateParmDecl>(
4406 Import(SubstPack->getParameterPack()));
4407 if (!Param)
4408 return TemplateName();
4409
4410 ASTNodeImporter Importer(*this);
4411 TemplateArgument ArgPack
4412 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4413 if (ArgPack.isNull())
4414 return TemplateName();
4415
4416 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4417 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004418 }
4419
4420 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004421}
4422
Douglas Gregor9bed8792010-02-09 19:21:46 +00004423SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4424 if (FromLoc.isInvalid())
4425 return SourceLocation();
4426
Douglas Gregor88523732010-02-10 00:15:17 +00004427 SourceManager &FromSM = FromContext.getSourceManager();
4428
4429 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004430 // don't have to import macro expansions.
4431 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004432 FromLoc = FromSM.getSpellingLoc(FromLoc);
4433 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4434 SourceManager &ToSM = ToContext.getSourceManager();
4435 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004436 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004437}
4438
4439SourceRange ASTImporter::Import(SourceRange FromRange) {
4440 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4441}
4442
Douglas Gregor88523732010-02-10 00:15:17 +00004443FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004444 llvm::DenseMap<FileID, FileID>::iterator Pos
4445 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004446 if (Pos != ImportedFileIDs.end())
4447 return Pos->second;
4448
4449 SourceManager &FromSM = FromContext.getSourceManager();
4450 SourceManager &ToSM = ToContext.getSourceManager();
4451 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004452 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004453
4454 // Include location of this file.
4455 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4456
4457 // Map the FileID for to the "to" source manager.
4458 FileID ToID;
4459 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004460 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004461 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4462 // disk again
4463 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4464 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004465 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004466 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4467 FromSLoc.getFile().getFileCharacteristic());
4468 } else {
4469 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004470 const llvm::MemoryBuffer *
4471 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004472 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004473 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004474 FromBuf->getBufferIdentifier());
4475 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4476 }
4477
4478
Sebastian Redl535a3e22010-09-30 01:03:06 +00004479 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004480 return ToID;
4481}
4482
Douglas Gregord8868a62011-01-18 03:11:38 +00004483void ASTImporter::ImportDefinition(Decl *From) {
4484 Decl *To = Import(From);
4485 if (!To)
4486 return;
4487
4488 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4489 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004490
4491 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4492 if (!ToRecord->getDefinition()) {
4493 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregorcd0d56a2012-01-24 18:36:04 +00004494 ASTNodeImporter::IDK_Everything);
Sean Callanan673e7752011-07-19 22:38:25 +00004495 return;
4496 }
4497 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004498
4499 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4500 if (!ToEnum->getDefinition()) {
4501 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004502 ASTNodeImporter::IDK_Everything);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004503 return;
4504 }
4505 }
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004506
4507 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4508 if (!ToIFace->getDefinition()) {
4509 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004510 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004511 return;
4512 }
4513 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004514
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004515 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4516 if (!ToProto->getDefinition()) {
4517 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregorac32ff92012-02-01 21:00:38 +00004518 ASTNodeImporter::IDK_Everything);
Douglas Gregor5602f7e2012-01-24 17:42:07 +00004519 return;
4520 }
4521 }
4522
Douglas Gregord8868a62011-01-18 03:11:38 +00004523 Importer.ImportDeclContext(FromDC, true);
4524 }
4525}
4526
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004527DeclarationName ASTImporter::Import(DeclarationName FromName) {
4528 if (!FromName)
4529 return DeclarationName();
4530
4531 switch (FromName.getNameKind()) {
4532 case DeclarationName::Identifier:
4533 return Import(FromName.getAsIdentifierInfo());
4534
4535 case DeclarationName::ObjCZeroArgSelector:
4536 case DeclarationName::ObjCOneArgSelector:
4537 case DeclarationName::ObjCMultiArgSelector:
4538 return Import(FromName.getObjCSelector());
4539
4540 case DeclarationName::CXXConstructorName: {
4541 QualType T = Import(FromName.getCXXNameType());
4542 if (T.isNull())
4543 return DeclarationName();
4544
4545 return ToContext.DeclarationNames.getCXXConstructorName(
4546 ToContext.getCanonicalType(T));
4547 }
4548
4549 case DeclarationName::CXXDestructorName: {
4550 QualType T = Import(FromName.getCXXNameType());
4551 if (T.isNull())
4552 return DeclarationName();
4553
4554 return ToContext.DeclarationNames.getCXXDestructorName(
4555 ToContext.getCanonicalType(T));
4556 }
4557
4558 case DeclarationName::CXXConversionFunctionName: {
4559 QualType T = Import(FromName.getCXXNameType());
4560 if (T.isNull())
4561 return DeclarationName();
4562
4563 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4564 ToContext.getCanonicalType(T));
4565 }
4566
4567 case DeclarationName::CXXOperatorName:
4568 return ToContext.DeclarationNames.getCXXOperatorName(
4569 FromName.getCXXOverloadedOperator());
4570
4571 case DeclarationName::CXXLiteralOperatorName:
4572 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4573 Import(FromName.getCXXLiteralIdentifier()));
4574
4575 case DeclarationName::CXXUsingDirective:
4576 // FIXME: STATICS!
4577 return DeclarationName::getUsingDirectiveName();
4578 }
4579
David Blaikie30263482012-01-20 21:50:17 +00004580 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004581}
4582
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004583IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004584 if (!FromId)
4585 return 0;
4586
4587 return &ToContext.Idents.get(FromId->getName());
4588}
Douglas Gregor089459a2010-02-08 21:09:39 +00004589
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004590Selector ASTImporter::Import(Selector FromSel) {
4591 if (FromSel.isNull())
4592 return Selector();
4593
Chris Lattner5f9e2722011-07-23 10:55:15 +00004594 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004595 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4596 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4597 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4598 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4599}
4600
Douglas Gregor089459a2010-02-08 21:09:39 +00004601DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4602 DeclContext *DC,
4603 unsigned IDNS,
4604 NamedDecl **Decls,
4605 unsigned NumDecls) {
4606 return Name;
4607}
4608
4609DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004610 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004611}
4612
4613DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004614 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004615}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004616
Douglas Gregorac32ff92012-02-01 21:00:38 +00004617void ASTImporter::CompleteDecl (Decl *D) {
4618 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4619 if (!ID->getDefinition())
4620 ID->startDefinition();
4621 }
4622 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4623 if (!PD->getDefinition())
4624 PD->startDefinition();
4625 }
4626 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4627 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4628 TD->startDefinition();
4629 TD->setCompleteDefinition(true);
4630 }
4631 }
4632 else {
4633 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4634 }
4635}
4636
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004637Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4638 ImportedDecls[From] = To;
4639 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004640}
Douglas Gregorea35d112010-02-15 23:54:17 +00004641
4642bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004643 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004644 = ImportedTypes.find(From.getTypePtr());
4645 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4646 return true;
4647
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004648 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004649 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004650}