blob: abf214aa1a15ce7841beca3aa3b6f5c3728cd2b7 [file] [log] [blame]
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
Douglas Gregor27c72d82011-11-03 18:07:07 +000028namespace clang {
Douglas Gregor089459a2010-02-08 21:09:39 +000029 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000030 public DeclVisitor<ASTNodeImporter, Decl *>,
31 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000032 ASTImporter &Importer;
33
34 public:
35 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
36
37 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000038 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000039 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000040
41 // Importing types
John McCallf4c73712011-01-19 06:33:43 +000042 QualType VisitType(const Type *T);
43 QualType VisitBuiltinType(const BuiltinType *T);
44 QualType VisitComplexType(const ComplexType *T);
45 QualType VisitPointerType(const PointerType *T);
46 QualType VisitBlockPointerType(const BlockPointerType *T);
47 QualType VisitLValueReferenceType(const LValueReferenceType *T);
48 QualType VisitRValueReferenceType(const RValueReferenceType *T);
49 QualType VisitMemberPointerType(const MemberPointerType *T);
50 QualType VisitConstantArrayType(const ConstantArrayType *T);
51 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
52 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000053 // FIXME: DependentSizedArrayType
54 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000055 QualType VisitVectorType(const VectorType *T);
56 QualType VisitExtVectorType(const ExtVectorType *T);
57 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
58 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000059 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000060 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000061 QualType VisitTypedefType(const TypedefType *T);
62 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000063 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000064 QualType VisitTypeOfType(const TypeOfType *T);
65 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000066 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000089 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
90 bool ForceImport = false);
91 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
92 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000093 TemplateParameterList *ImportTemplateParameterList(
94 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000095 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
96 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
97 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +000099 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000100 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000101 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000102 Decl *VisitDecl(Decl *D);
Sean Callananf1b69462011-11-17 23:20:56 +0000103 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000104 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000105 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000106 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000107 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000108 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000109 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000110 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000111 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000112 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
113 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
114 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
115 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000116 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000117 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000118 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000119 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000120 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000121 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000122 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000123 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000124 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000125 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000126 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000127 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000128 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000129 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000130 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
131 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
132 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
133 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000134 Decl *VisitClassTemplateSpecializationDecl(
135 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000136
Douglas Gregor4800d952010-02-11 19:21:55 +0000137 // Importing statements
138 Stmt *VisitStmt(Stmt *S);
139
140 // Importing expressions
141 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000142 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000143 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000144 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000145 Expr *VisitParenExpr(ParenExpr *E);
146 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000147 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitBinaryOperator(BinaryOperator *E);
149 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000150 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000151 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000152 };
153}
Douglas Gregor27c72d82011-11-03 18:07:07 +0000154using namespace clang;
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155
156//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000157// Structural Equivalence
158//----------------------------------------------------------------------------
159
160namespace {
161 struct StructuralEquivalenceContext {
162 /// \brief AST contexts for which we are checking structural equivalence.
163 ASTContext &C1, &C2;
164
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000165 /// \brief The set of "tentative" equivalences between two canonical
166 /// declarations, mapping from a declaration in the first context to the
167 /// declaration in the second context that we believe to be equivalent.
168 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
169
170 /// \brief Queue of declarations in the first context whose equivalence
171 /// with a declaration in the second context still needs to be verified.
172 std::deque<Decl *> DeclsToCheck;
173
Douglas Gregorea35d112010-02-15 23:54:17 +0000174 /// \brief Declaration (from, to) pairs that are known not to be equivalent
175 /// (which we have already complained about).
176 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
177
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000178 /// \brief Whether we're being strict about the spelling of types when
179 /// unifying two types.
180 bool StrictTypeSpelling;
181
182 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000183 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000184 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000185 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000186 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000187
188 /// \brief Determine whether the two declarations are structurally
189 /// equivalent.
190 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
191
192 /// \brief Determine whether the two types are structurally equivalent.
193 bool IsStructurallyEquivalent(QualType T1, QualType T2);
194
195 private:
196 /// \brief Finish checking all of the structural equivalences.
197 ///
198 /// \returns true if an error occurred, false otherwise.
199 bool Finish();
200
201 public:
202 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000203 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000204 }
205
206 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000207 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000208 }
209 };
210}
211
212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
213 QualType T1, QualType T2);
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 Decl *D1, Decl *D2);
216
217/// \brief Determine if two APInts have the same value, after zero-extending
218/// one of them (if needed!) to ensure that the bit-widths match.
219static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
220 if (I1.getBitWidth() == I2.getBitWidth())
221 return I1 == I2;
222
223 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000224 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000225
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227}
228
229/// \brief Determine if two APSInts have the same value, zero- or sign-extending
230/// as needed.
231static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
232 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
233 return I1 == I2;
234
235 // Check for a bit-width mismatch.
236 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000237 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000238 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240
241 // We have a signedness mismatch. Turn the signed value into an unsigned
242 // value.
243 if (I1.isSigned()) {
244 if (I1.isNegative())
245 return false;
246
247 return llvm::APSInt(I1, true) == I2;
248 }
249
250 if (I2.isNegative())
251 return false;
252
253 return I1 == llvm::APSInt(I2, true);
254}
255
256/// \brief Determine structural equivalence of two expressions.
257static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
258 Expr *E1, Expr *E2) {
259 if (!E1 || !E2)
260 return E1 == E2;
261
262 // FIXME: Actually perform a structural comparison!
263 return true;
264}
265
266/// \brief Determine whether two identifiers are equivalent.
267static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
268 const IdentifierInfo *Name2) {
269 if (!Name1 || !Name2)
270 return Name1 == Name2;
271
272 return Name1->getName() == Name2->getName();
273}
274
275/// \brief Determine whether two nested-name-specifiers are equivalent.
276static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
277 NestedNameSpecifier *NNS1,
278 NestedNameSpecifier *NNS2) {
279 // FIXME: Implement!
280 return true;
281}
282
283/// \brief Determine whether two template arguments are equivalent.
284static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
285 const TemplateArgument &Arg1,
286 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000287 if (Arg1.getKind() != Arg2.getKind())
288 return false;
289
290 switch (Arg1.getKind()) {
291 case TemplateArgument::Null:
292 return true;
293
294 case TemplateArgument::Type:
295 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
296
297 case TemplateArgument::Integral:
298 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
299 Arg2.getIntegralType()))
300 return false;
301
302 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
303
304 case TemplateArgument::Declaration:
305 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
306
307 case TemplateArgument::Template:
308 return IsStructurallyEquivalent(Context,
309 Arg1.getAsTemplate(),
310 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000311
312 case TemplateArgument::TemplateExpansion:
313 return IsStructurallyEquivalent(Context,
314 Arg1.getAsTemplateOrTemplatePattern(),
315 Arg2.getAsTemplateOrTemplatePattern());
316
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000317 case TemplateArgument::Expression:
318 return IsStructurallyEquivalent(Context,
319 Arg1.getAsExpr(), Arg2.getAsExpr());
320
321 case TemplateArgument::Pack:
322 if (Arg1.pack_size() != Arg2.pack_size())
323 return false;
324
325 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
326 if (!IsStructurallyEquivalent(Context,
327 Arg1.pack_begin()[I],
328 Arg2.pack_begin()[I]))
329 return false;
330
331 return true;
332 }
333
334 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000335}
336
337/// \brief Determine structural equivalence for the common part of array
338/// types.
339static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
340 const ArrayType *Array1,
341 const ArrayType *Array2) {
342 if (!IsStructurallyEquivalent(Context,
343 Array1->getElementType(),
344 Array2->getElementType()))
345 return false;
346 if (Array1->getSizeModifier() != Array2->getSizeModifier())
347 return false;
348 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
349 return false;
350
351 return true;
352}
353
354/// \brief Determine structural equivalence of two types.
355static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
356 QualType T1, QualType T2) {
357 if (T1.isNull() || T2.isNull())
358 return T1.isNull() && T2.isNull();
359
360 if (!Context.StrictTypeSpelling) {
361 // We aren't being strict about token-to-token equivalence of types,
362 // so map down to the canonical type.
363 T1 = Context.C1.getCanonicalType(T1);
364 T2 = Context.C2.getCanonicalType(T2);
365 }
366
367 if (T1.getQualifiers() != T2.getQualifiers())
368 return false;
369
Douglas Gregorea35d112010-02-15 23:54:17 +0000370 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000371
Douglas Gregorea35d112010-02-15 23:54:17 +0000372 if (T1->getTypeClass() != T2->getTypeClass()) {
373 // Compare function types with prototypes vs. without prototypes as if
374 // both did not have prototypes.
375 if (T1->getTypeClass() == Type::FunctionProto &&
376 T2->getTypeClass() == Type::FunctionNoProto)
377 TC = Type::FunctionNoProto;
378 else if (T1->getTypeClass() == Type::FunctionNoProto &&
379 T2->getTypeClass() == Type::FunctionProto)
380 TC = Type::FunctionNoProto;
381 else
382 return false;
383 }
384
385 switch (TC) {
386 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000387 // FIXME: Deal with Char_S/Char_U.
388 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
389 return false;
390 break;
391
392 case Type::Complex:
393 if (!IsStructurallyEquivalent(Context,
394 cast<ComplexType>(T1)->getElementType(),
395 cast<ComplexType>(T2)->getElementType()))
396 return false;
397 break;
398
399 case Type::Pointer:
400 if (!IsStructurallyEquivalent(Context,
401 cast<PointerType>(T1)->getPointeeType(),
402 cast<PointerType>(T2)->getPointeeType()))
403 return false;
404 break;
405
406 case Type::BlockPointer:
407 if (!IsStructurallyEquivalent(Context,
408 cast<BlockPointerType>(T1)->getPointeeType(),
409 cast<BlockPointerType>(T2)->getPointeeType()))
410 return false;
411 break;
412
413 case Type::LValueReference:
414 case Type::RValueReference: {
415 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
416 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
417 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
418 return false;
419 if (Ref1->isInnerRef() != Ref2->isInnerRef())
420 return false;
421 if (!IsStructurallyEquivalent(Context,
422 Ref1->getPointeeTypeAsWritten(),
423 Ref2->getPointeeTypeAsWritten()))
424 return false;
425 break;
426 }
427
428 case Type::MemberPointer: {
429 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
430 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
431 if (!IsStructurallyEquivalent(Context,
432 MemPtr1->getPointeeType(),
433 MemPtr2->getPointeeType()))
434 return false;
435 if (!IsStructurallyEquivalent(Context,
436 QualType(MemPtr1->getClass(), 0),
437 QualType(MemPtr2->getClass(), 0)))
438 return false;
439 break;
440 }
441
442 case Type::ConstantArray: {
443 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
444 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
445 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
446 return false;
447
448 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
449 return false;
450 break;
451 }
452
453 case Type::IncompleteArray:
454 if (!IsArrayStructurallyEquivalent(Context,
455 cast<ArrayType>(T1),
456 cast<ArrayType>(T2)))
457 return false;
458 break;
459
460 case Type::VariableArray: {
461 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
462 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
463 if (!IsStructurallyEquivalent(Context,
464 Array1->getSizeExpr(), Array2->getSizeExpr()))
465 return false;
466
467 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
468 return false;
469
470 break;
471 }
472
473 case Type::DependentSizedArray: {
474 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
475 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
476 if (!IsStructurallyEquivalent(Context,
477 Array1->getSizeExpr(), Array2->getSizeExpr()))
478 return false;
479
480 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
481 return false;
482
483 break;
484 }
485
486 case Type::DependentSizedExtVector: {
487 const DependentSizedExtVectorType *Vec1
488 = cast<DependentSizedExtVectorType>(T1);
489 const DependentSizedExtVectorType *Vec2
490 = cast<DependentSizedExtVectorType>(T2);
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
493 return false;
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getElementType(),
496 Vec2->getElementType()))
497 return false;
498 break;
499 }
500
501 case Type::Vector:
502 case Type::ExtVector: {
503 const VectorType *Vec1 = cast<VectorType>(T1);
504 const VectorType *Vec2 = cast<VectorType>(T2);
505 if (!IsStructurallyEquivalent(Context,
506 Vec1->getElementType(),
507 Vec2->getElementType()))
508 return false;
509 if (Vec1->getNumElements() != Vec2->getNumElements())
510 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000511 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000512 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000513 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000514 }
515
516 case Type::FunctionProto: {
517 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
518 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
519 if (Proto1->getNumArgs() != Proto2->getNumArgs())
520 return false;
521 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
522 if (!IsStructurallyEquivalent(Context,
523 Proto1->getArgType(I),
524 Proto2->getArgType(I)))
525 return false;
526 }
527 if (Proto1->isVariadic() != Proto2->isVariadic())
528 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000529 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000530 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000531 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
532 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
533 return false;
534 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
535 if (!IsStructurallyEquivalent(Context,
536 Proto1->getExceptionType(I),
537 Proto2->getExceptionType(I)))
538 return false;
539 }
540 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000541 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000542 Proto1->getNoexceptExpr(),
543 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000544 return false;
545 }
546 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
547 return false;
548
549 // Fall through to check the bits common with FunctionNoProtoType.
550 }
551
552 case Type::FunctionNoProto: {
553 const FunctionType *Function1 = cast<FunctionType>(T1);
554 const FunctionType *Function2 = cast<FunctionType>(T2);
555 if (!IsStructurallyEquivalent(Context,
556 Function1->getResultType(),
557 Function2->getResultType()))
558 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000559 if (Function1->getExtInfo() != Function2->getExtInfo())
560 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000561 break;
562 }
563
564 case Type::UnresolvedUsing:
565 if (!IsStructurallyEquivalent(Context,
566 cast<UnresolvedUsingType>(T1)->getDecl(),
567 cast<UnresolvedUsingType>(T2)->getDecl()))
568 return false;
569
570 break;
John McCall9d156a72011-01-06 01:58:22 +0000571
572 case Type::Attributed:
573 if (!IsStructurallyEquivalent(Context,
574 cast<AttributedType>(T1)->getModifiedType(),
575 cast<AttributedType>(T2)->getModifiedType()))
576 return false;
577 if (!IsStructurallyEquivalent(Context,
578 cast<AttributedType>(T1)->getEquivalentType(),
579 cast<AttributedType>(T2)->getEquivalentType()))
580 return false;
581 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000582
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000583 case Type::Paren:
584 if (!IsStructurallyEquivalent(Context,
585 cast<ParenType>(T1)->getInnerType(),
586 cast<ParenType>(T2)->getInnerType()))
587 return false;
588 break;
589
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000590 case Type::Typedef:
591 if (!IsStructurallyEquivalent(Context,
592 cast<TypedefType>(T1)->getDecl(),
593 cast<TypedefType>(T2)->getDecl()))
594 return false;
595 break;
596
597 case Type::TypeOfExpr:
598 if (!IsStructurallyEquivalent(Context,
599 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
600 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
601 return false;
602 break;
603
604 case Type::TypeOf:
605 if (!IsStructurallyEquivalent(Context,
606 cast<TypeOfType>(T1)->getUnderlyingType(),
607 cast<TypeOfType>(T2)->getUnderlyingType()))
608 return false;
609 break;
Sean Huntca63c202011-05-24 22:41:36 +0000610
611 case Type::UnaryTransform:
612 if (!IsStructurallyEquivalent(Context,
613 cast<UnaryTransformType>(T1)->getUnderlyingType(),
614 cast<UnaryTransformType>(T1)->getUnderlyingType()))
615 return false;
616 break;
617
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000618 case Type::Decltype:
619 if (!IsStructurallyEquivalent(Context,
620 cast<DecltypeType>(T1)->getUnderlyingExpr(),
621 cast<DecltypeType>(T2)->getUnderlyingExpr()))
622 return false;
623 break;
624
Richard Smith34b41d92011-02-20 03:19:35 +0000625 case Type::Auto:
626 if (!IsStructurallyEquivalent(Context,
627 cast<AutoType>(T1)->getDeducedType(),
628 cast<AutoType>(T2)->getDeducedType()))
629 return false;
630 break;
631
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000632 case Type::Record:
633 case Type::Enum:
634 if (!IsStructurallyEquivalent(Context,
635 cast<TagType>(T1)->getDecl(),
636 cast<TagType>(T2)->getDecl()))
637 return false;
638 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000639
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000640 case Type::TemplateTypeParm: {
641 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
642 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
643 if (Parm1->getDepth() != Parm2->getDepth())
644 return false;
645 if (Parm1->getIndex() != Parm2->getIndex())
646 return false;
647 if (Parm1->isParameterPack() != Parm2->isParameterPack())
648 return false;
649
650 // Names of template type parameters are never significant.
651 break;
652 }
653
654 case Type::SubstTemplateTypeParm: {
655 const SubstTemplateTypeParmType *Subst1
656 = cast<SubstTemplateTypeParmType>(T1);
657 const SubstTemplateTypeParmType *Subst2
658 = cast<SubstTemplateTypeParmType>(T2);
659 if (!IsStructurallyEquivalent(Context,
660 QualType(Subst1->getReplacedParameter(), 0),
661 QualType(Subst2->getReplacedParameter(), 0)))
662 return false;
663 if (!IsStructurallyEquivalent(Context,
664 Subst1->getReplacementType(),
665 Subst2->getReplacementType()))
666 return false;
667 break;
668 }
669
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000670 case Type::SubstTemplateTypeParmPack: {
671 const SubstTemplateTypeParmPackType *Subst1
672 = cast<SubstTemplateTypeParmPackType>(T1);
673 const SubstTemplateTypeParmPackType *Subst2
674 = cast<SubstTemplateTypeParmPackType>(T2);
675 if (!IsStructurallyEquivalent(Context,
676 QualType(Subst1->getReplacedParameter(), 0),
677 QualType(Subst2->getReplacedParameter(), 0)))
678 return false;
679 if (!IsStructurallyEquivalent(Context,
680 Subst1->getArgumentPack(),
681 Subst2->getArgumentPack()))
682 return false;
683 break;
684 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000685 case Type::TemplateSpecialization: {
686 const TemplateSpecializationType *Spec1
687 = cast<TemplateSpecializationType>(T1);
688 const TemplateSpecializationType *Spec2
689 = cast<TemplateSpecializationType>(T2);
690 if (!IsStructurallyEquivalent(Context,
691 Spec1->getTemplateName(),
692 Spec2->getTemplateName()))
693 return false;
694 if (Spec1->getNumArgs() != Spec2->getNumArgs())
695 return false;
696 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
697 if (!IsStructurallyEquivalent(Context,
698 Spec1->getArg(I), Spec2->getArg(I)))
699 return false;
700 }
701 break;
702 }
703
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000704 case Type::Elaborated: {
705 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
706 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
707 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
708 if (Elab1->getKeyword() != Elab2->getKeyword())
709 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000710 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000711 Elab1->getQualifier(),
712 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000713 return false;
714 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000715 Elab1->getNamedType(),
716 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000717 return false;
718 break;
719 }
720
John McCall3cb0ebd2010-03-10 03:28:59 +0000721 case Type::InjectedClassName: {
722 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
723 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
724 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000725 Inj1->getInjectedSpecializationType(),
726 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000727 return false;
728 break;
729 }
730
Douglas Gregor4714c122010-03-31 17:34:00 +0000731 case Type::DependentName: {
732 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
733 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000734 if (!IsStructurallyEquivalent(Context,
735 Typename1->getQualifier(),
736 Typename2->getQualifier()))
737 return false;
738 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
739 Typename2->getIdentifier()))
740 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000741
742 break;
743 }
744
John McCall33500952010-06-11 00:33:02 +0000745 case Type::DependentTemplateSpecialization: {
746 const DependentTemplateSpecializationType *Spec1 =
747 cast<DependentTemplateSpecializationType>(T1);
748 const DependentTemplateSpecializationType *Spec2 =
749 cast<DependentTemplateSpecializationType>(T2);
750 if (!IsStructurallyEquivalent(Context,
751 Spec1->getQualifier(),
752 Spec2->getQualifier()))
753 return false;
754 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
755 Spec2->getIdentifier()))
756 return false;
757 if (Spec1->getNumArgs() != Spec2->getNumArgs())
758 return false;
759 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
760 if (!IsStructurallyEquivalent(Context,
761 Spec1->getArg(I), Spec2->getArg(I)))
762 return false;
763 }
764 break;
765 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000766
767 case Type::PackExpansion:
768 if (!IsStructurallyEquivalent(Context,
769 cast<PackExpansionType>(T1)->getPattern(),
770 cast<PackExpansionType>(T2)->getPattern()))
771 return false;
772 break;
773
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000774 case Type::ObjCInterface: {
775 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
776 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
777 if (!IsStructurallyEquivalent(Context,
778 Iface1->getDecl(), Iface2->getDecl()))
779 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000780 break;
781 }
782
783 case Type::ObjCObject: {
784 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
785 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
786 if (!IsStructurallyEquivalent(Context,
787 Obj1->getBaseType(),
788 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000789 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000790 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
791 return false;
792 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000793 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000794 Obj1->getProtocol(I),
795 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 return false;
797 }
798 break;
799 }
800
801 case Type::ObjCObjectPointer: {
802 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
803 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
804 if (!IsStructurallyEquivalent(Context,
805 Ptr1->getPointeeType(),
806 Ptr2->getPointeeType()))
807 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000808 break;
809 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000810
811 case Type::Atomic: {
812 if (!IsStructurallyEquivalent(Context,
813 cast<AtomicType>(T1)->getValueType(),
814 cast<AtomicType>(T2)->getValueType()))
815 return false;
816 break;
817 }
818
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000819 } // end switch
820
821 return true;
822}
823
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000824/// \brief Determine structural equivalence of two fields.
825static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
826 FieldDecl *Field1, FieldDecl *Field2) {
827 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
828
829 if (!IsStructurallyEquivalent(Context,
830 Field1->getType(), Field2->getType())) {
831 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
832 << Context.C2.getTypeDeclType(Owner2);
833 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
834 << Field2->getDeclName() << Field2->getType();
835 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
836 << Field1->getDeclName() << Field1->getType();
837 return false;
838 }
839
840 if (Field1->isBitField() != Field2->isBitField()) {
841 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
842 << Context.C2.getTypeDeclType(Owner2);
843 if (Field1->isBitField()) {
844 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
845 << Field1->getDeclName() << Field1->getType()
846 << Field1->getBitWidthValue(Context.C1);
847 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
848 << Field2->getDeclName();
849 } else {
850 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
851 << Field2->getDeclName() << Field2->getType()
852 << Field2->getBitWidthValue(Context.C2);
853 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
854 << Field1->getDeclName();
855 }
856 return false;
857 }
858
859 if (Field1->isBitField()) {
860 // Make sure that the bit-fields are the same length.
861 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
862 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
863
864 if (Bits1 != Bits2) {
865 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(Owner2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
868 << Field2->getDeclName() << Field2->getType() << Bits2;
869 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
870 << Field1->getDeclName() << Field1->getType() << Bits1;
871 return false;
872 }
873 }
874
875 return true;
876}
877
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000878/// \brief Determine structural equivalence of two records.
879static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
880 RecordDecl *D1, RecordDecl *D2) {
881 if (D1->isUnion() != D2->isUnion()) {
882 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
883 << Context.C2.getTypeDeclType(D2);
884 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
885 << D1->getDeclName() << (unsigned)D1->getTagKind();
886 return false;
887 }
888
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000889 // If both declarations are class template specializations, we know
890 // the ODR applies, so check the template and template arguments.
891 ClassTemplateSpecializationDecl *Spec1
892 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
893 ClassTemplateSpecializationDecl *Spec2
894 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
895 if (Spec1 && Spec2) {
896 // Check that the specialized templates are the same.
897 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
898 Spec2->getSpecializedTemplate()))
899 return false;
900
901 // Check that the template arguments are the same.
902 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
903 return false;
904
905 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
906 if (!IsStructurallyEquivalent(Context,
907 Spec1->getTemplateArgs().get(I),
908 Spec2->getTemplateArgs().get(I)))
909 return false;
910 }
911 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000912 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000913 else if (Spec1 || Spec2)
914 return false;
915
Douglas Gregorea35d112010-02-15 23:54:17 +0000916 // Compare the definitions of these two records. If either or both are
917 // incomplete, we assume that they are equivalent.
918 D1 = D1->getDefinition();
919 D2 = D2->getDefinition();
920 if (!D1 || !D2)
921 return true;
922
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000923 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
924 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
925 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
926 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000927 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000928 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000929 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000930 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000931 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000932 return false;
933 }
934
935 // Check the base classes.
936 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
937 BaseEnd1 = D1CXX->bases_end(),
938 Base2 = D2CXX->bases_begin();
939 Base1 != BaseEnd1;
940 ++Base1, ++Base2) {
941 if (!IsStructurallyEquivalent(Context,
942 Base1->getType(), Base2->getType())) {
943 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
944 << Context.C2.getTypeDeclType(D2);
945 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
946 << Base2->getType()
947 << Base2->getSourceRange();
948 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
949 << Base1->getType()
950 << Base1->getSourceRange();
951 return false;
952 }
953
954 // Check virtual vs. non-virtual inheritance mismatch.
955 if (Base1->isVirtual() != Base2->isVirtual()) {
956 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
957 << Context.C2.getTypeDeclType(D2);
958 Context.Diag2(Base2->getSourceRange().getBegin(),
959 diag::note_odr_virtual_base)
960 << Base2->isVirtual() << Base2->getSourceRange();
961 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
962 << Base1->isVirtual()
963 << Base1->getSourceRange();
964 return false;
965 }
966 }
967 } else if (D1CXX->getNumBases() > 0) {
968 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
969 << Context.C2.getTypeDeclType(D2);
970 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
971 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
972 << Base1->getType()
973 << Base1->getSourceRange();
974 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
975 return false;
976 }
977 }
978
979 // Check the fields for consistency.
980 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
981 Field2End = D2->field_end();
982 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
983 Field1End = D1->field_end();
984 Field1 != Field1End;
985 ++Field1, ++Field2) {
986 if (Field2 == Field2End) {
987 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
988 << Context.C2.getTypeDeclType(D2);
989 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
990 << Field1->getDeclName() << Field1->getType();
991 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
992 return false;
993 }
994
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000995 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
996 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000997 }
998
999 if (Field2 != Field2End) {
1000 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1001 << Context.C2.getTypeDeclType(D2);
1002 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1003 << Field2->getDeclName() << Field2->getType();
1004 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1005 return false;
1006 }
1007
1008 return true;
1009}
1010
1011/// \brief Determine structural equivalence of two enums.
1012static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1013 EnumDecl *D1, EnumDecl *D2) {
1014 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1015 EC2End = D2->enumerator_end();
1016 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1017 EC1End = D1->enumerator_end();
1018 EC1 != EC1End; ++EC1, ++EC2) {
1019 if (EC2 == EC2End) {
1020 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1021 << Context.C2.getTypeDeclType(D2);
1022 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1023 << EC1->getDeclName()
1024 << EC1->getInitVal().toString(10);
1025 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1026 return false;
1027 }
1028
1029 llvm::APSInt Val1 = EC1->getInitVal();
1030 llvm::APSInt Val2 = EC2->getInitVal();
1031 if (!IsSameValue(Val1, Val2) ||
1032 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1033 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1034 << Context.C2.getTypeDeclType(D2);
1035 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1036 << EC2->getDeclName()
1037 << EC2->getInitVal().toString(10);
1038 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1039 << EC1->getDeclName()
1040 << EC1->getInitVal().toString(10);
1041 return false;
1042 }
1043 }
1044
1045 if (EC2 != EC2End) {
1046 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1047 << Context.C2.getTypeDeclType(D2);
1048 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1049 << EC2->getDeclName()
1050 << EC2->getInitVal().toString(10);
1051 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1052 return false;
1053 }
1054
1055 return true;
1056}
Douglas Gregor040afae2010-11-30 19:14:50 +00001057
1058static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1059 TemplateParameterList *Params1,
1060 TemplateParameterList *Params2) {
1061 if (Params1->size() != Params2->size()) {
1062 Context.Diag2(Params2->getTemplateLoc(),
1063 diag::err_odr_different_num_template_parameters)
1064 << Params1->size() << Params2->size();
1065 Context.Diag1(Params1->getTemplateLoc(),
1066 diag::note_odr_template_parameter_list);
1067 return false;
1068 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001069
Douglas Gregor040afae2010-11-30 19:14:50 +00001070 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1071 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1072 Context.Diag2(Params2->getParam(I)->getLocation(),
1073 diag::err_odr_different_template_parameter_kind);
1074 Context.Diag1(Params1->getParam(I)->getLocation(),
1075 diag::note_odr_template_parameter_here);
1076 return false;
1077 }
1078
1079 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1080 Params2->getParam(I))) {
1081
1082 return false;
1083 }
1084 }
1085
1086 return true;
1087}
1088
1089static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1090 TemplateTypeParmDecl *D1,
1091 TemplateTypeParmDecl *D2) {
1092 if (D1->isParameterPack() != D2->isParameterPack()) {
1093 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1094 << D2->isParameterPack();
1095 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1096 << D1->isParameterPack();
1097 return false;
1098 }
1099
1100 return true;
1101}
1102
1103static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1104 NonTypeTemplateParmDecl *D1,
1105 NonTypeTemplateParmDecl *D2) {
1106 // FIXME: Enable once we have variadic templates.
1107#if 0
1108 if (D1->isParameterPack() != D2->isParameterPack()) {
1109 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1110 << D2->isParameterPack();
1111 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1112 << D1->isParameterPack();
1113 return false;
1114 }
1115#endif
1116
1117 // Check types.
1118 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1119 Context.Diag2(D2->getLocation(),
1120 diag::err_odr_non_type_parameter_type_inconsistent)
1121 << D2->getType() << D1->getType();
1122 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1123 << D1->getType();
1124 return false;
1125 }
1126
1127 return true;
1128}
1129
1130static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1131 TemplateTemplateParmDecl *D1,
1132 TemplateTemplateParmDecl *D2) {
1133 // FIXME: Enable once we have variadic templates.
1134#if 0
1135 if (D1->isParameterPack() != D2->isParameterPack()) {
1136 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1137 << D2->isParameterPack();
1138 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1139 << D1->isParameterPack();
1140 return false;
1141 }
1142#endif
1143
1144 // Check template parameter lists.
1145 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1146 D2->getTemplateParameters());
1147}
1148
1149static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1150 ClassTemplateDecl *D1,
1151 ClassTemplateDecl *D2) {
1152 // Check template parameters.
1153 if (!IsStructurallyEquivalent(Context,
1154 D1->getTemplateParameters(),
1155 D2->getTemplateParameters()))
1156 return false;
1157
1158 // Check the templated declaration.
1159 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1160 D2->getTemplatedDecl());
1161}
1162
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001163/// \brief Determine structural equivalence of two declarations.
1164static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1165 Decl *D1, Decl *D2) {
1166 // FIXME: Check for known structural equivalences via a callback of some sort.
1167
Douglas Gregorea35d112010-02-15 23:54:17 +00001168 // Check whether we already know that these two declarations are not
1169 // structurally equivalent.
1170 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1171 D2->getCanonicalDecl())))
1172 return false;
1173
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001174 // Determine whether we've already produced a tentative equivalence for D1.
1175 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1176 if (EquivToD1)
1177 return EquivToD1 == D2->getCanonicalDecl();
1178
1179 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1180 EquivToD1 = D2->getCanonicalDecl();
1181 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1182 return true;
1183}
1184
1185bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1186 Decl *D2) {
1187 if (!::IsStructurallyEquivalent(*this, D1, D2))
1188 return false;
1189
1190 return !Finish();
1191}
1192
1193bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1194 QualType T2) {
1195 if (!::IsStructurallyEquivalent(*this, T1, T2))
1196 return false;
1197
1198 return !Finish();
1199}
1200
1201bool StructuralEquivalenceContext::Finish() {
1202 while (!DeclsToCheck.empty()) {
1203 // Check the next declaration.
1204 Decl *D1 = DeclsToCheck.front();
1205 DeclsToCheck.pop_front();
1206
1207 Decl *D2 = TentativeEquivalences[D1];
1208 assert(D2 && "Unrecorded tentative equivalence?");
1209
Douglas Gregorea35d112010-02-15 23:54:17 +00001210 bool Equivalent = true;
1211
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001212 // FIXME: Switch on all declaration kinds. For now, we're just going to
1213 // check the obvious ones.
1214 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1215 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1216 // Check for equivalent structure names.
1217 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001218 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1219 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001220 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001221 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1222 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001223 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1224 !::IsStructurallyEquivalent(*this, Record1, Record2))
1225 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001226 } else {
1227 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001228 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001229 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001230 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001231 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1232 // Check for equivalent enum names.
1233 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001234 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1235 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001236 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001237 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1238 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001239 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1240 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1241 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001242 } else {
1243 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001244 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001245 }
Richard Smith162e1c12011-04-15 14:24:37 +00001246 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1247 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001248 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001249 Typedef2->getIdentifier()) ||
1250 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001251 Typedef1->getUnderlyingType(),
1252 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001253 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001254 } else {
1255 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001256 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001257 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001258 } else if (ClassTemplateDecl *ClassTemplate1
1259 = dyn_cast<ClassTemplateDecl>(D1)) {
1260 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1261 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1262 ClassTemplate2->getIdentifier()) ||
1263 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1264 Equivalent = false;
1265 } else {
1266 // Class template/non-class-template mismatch.
1267 Equivalent = false;
1268 }
1269 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1270 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1271 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1272 Equivalent = false;
1273 } else {
1274 // Kind mismatch.
1275 Equivalent = false;
1276 }
1277 } else if (NonTypeTemplateParmDecl *NTTP1
1278 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1279 if (NonTypeTemplateParmDecl *NTTP2
1280 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1281 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1282 Equivalent = false;
1283 } else {
1284 // Kind mismatch.
1285 Equivalent = false;
1286 }
1287 } else if (TemplateTemplateParmDecl *TTP1
1288 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1289 if (TemplateTemplateParmDecl *TTP2
1290 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1291 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1292 Equivalent = false;
1293 } else {
1294 // Kind mismatch.
1295 Equivalent = false;
1296 }
1297 }
1298
Douglas Gregorea35d112010-02-15 23:54:17 +00001299 if (!Equivalent) {
1300 // Note that these two declarations are not equivalent (and we already
1301 // know about it).
1302 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1303 D2->getCanonicalDecl()));
1304 return true;
1305 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001306 // FIXME: Check other declaration kinds!
1307 }
1308
1309 return false;
1310}
1311
1312//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001313// Import Types
1314//----------------------------------------------------------------------------
1315
John McCallf4c73712011-01-19 06:33:43 +00001316QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001317 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1318 << T->getTypeClassName();
1319 return QualType();
1320}
1321
John McCallf4c73712011-01-19 06:33:43 +00001322QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001323 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001324#define SHARED_SINGLETON_TYPE(Expansion)
1325#define BUILTIN_TYPE(Id, SingletonId) \
1326 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1327#include "clang/AST/BuiltinTypes.def"
1328
1329 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1330 // context supports C++.
1331
1332 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1333 // context supports ObjC.
1334
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001335 case BuiltinType::Char_U:
1336 // The context we're importing from has an unsigned 'char'. If we're
1337 // importing into a context with a signed 'char', translate to
1338 // 'unsigned char' instead.
1339 if (Importer.getToContext().getLangOptions().CharIsSigned)
1340 return Importer.getToContext().UnsignedCharTy;
1341
1342 return Importer.getToContext().CharTy;
1343
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001344 case BuiltinType::Char_S:
1345 // The context we're importing from has an unsigned 'char'. If we're
1346 // importing into a context with a signed 'char', translate to
1347 // 'unsigned char' instead.
1348 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1349 return Importer.getToContext().SignedCharTy;
1350
1351 return Importer.getToContext().CharTy;
1352
Chris Lattner3f59c972010-12-25 23:25:43 +00001353 case BuiltinType::WChar_S:
1354 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001355 // FIXME: If not in C++, shall we translate to the C equivalent of
1356 // wchar_t?
1357 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001358 }
David Blaikie30263482012-01-20 21:50:17 +00001359
1360 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001361}
1362
John McCallf4c73712011-01-19 06:33:43 +00001363QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001364 QualType ToElementType = Importer.Import(T->getElementType());
1365 if (ToElementType.isNull())
1366 return QualType();
1367
1368 return Importer.getToContext().getComplexType(ToElementType);
1369}
1370
John McCallf4c73712011-01-19 06:33:43 +00001371QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001372 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1373 if (ToPointeeType.isNull())
1374 return QualType();
1375
1376 return Importer.getToContext().getPointerType(ToPointeeType);
1377}
1378
John McCallf4c73712011-01-19 06:33:43 +00001379QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001380 // FIXME: Check for blocks support in "to" context.
1381 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1382 if (ToPointeeType.isNull())
1383 return QualType();
1384
1385 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1386}
1387
John McCallf4c73712011-01-19 06:33:43 +00001388QualType
1389ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001390 // FIXME: Check for C++ support in "to" context.
1391 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1392 if (ToPointeeType.isNull())
1393 return QualType();
1394
1395 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1396}
1397
John McCallf4c73712011-01-19 06:33:43 +00001398QualType
1399ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001400 // FIXME: Check for C++0x support in "to" context.
1401 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1402 if (ToPointeeType.isNull())
1403 return QualType();
1404
1405 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1406}
1407
John McCallf4c73712011-01-19 06:33:43 +00001408QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001409 // FIXME: Check for C++ support in "to" context.
1410 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1411 if (ToPointeeType.isNull())
1412 return QualType();
1413
1414 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1415 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1416 ClassType.getTypePtr());
1417}
1418
John McCallf4c73712011-01-19 06:33:43 +00001419QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001420 QualType ToElementType = Importer.Import(T->getElementType());
1421 if (ToElementType.isNull())
1422 return QualType();
1423
1424 return Importer.getToContext().getConstantArrayType(ToElementType,
1425 T->getSize(),
1426 T->getSizeModifier(),
1427 T->getIndexTypeCVRQualifiers());
1428}
1429
John McCallf4c73712011-01-19 06:33:43 +00001430QualType
1431ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001432 QualType ToElementType = Importer.Import(T->getElementType());
1433 if (ToElementType.isNull())
1434 return QualType();
1435
1436 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1437 T->getSizeModifier(),
1438 T->getIndexTypeCVRQualifiers());
1439}
1440
John McCallf4c73712011-01-19 06:33:43 +00001441QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001442 QualType ToElementType = Importer.Import(T->getElementType());
1443 if (ToElementType.isNull())
1444 return QualType();
1445
1446 Expr *Size = Importer.Import(T->getSizeExpr());
1447 if (!Size)
1448 return QualType();
1449
1450 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1451 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1452 T->getSizeModifier(),
1453 T->getIndexTypeCVRQualifiers(),
1454 Brackets);
1455}
1456
John McCallf4c73712011-01-19 06:33:43 +00001457QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001458 QualType ToElementType = Importer.Import(T->getElementType());
1459 if (ToElementType.isNull())
1460 return QualType();
1461
1462 return Importer.getToContext().getVectorType(ToElementType,
1463 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001464 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001465}
1466
John McCallf4c73712011-01-19 06:33:43 +00001467QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001468 QualType ToElementType = Importer.Import(T->getElementType());
1469 if (ToElementType.isNull())
1470 return QualType();
1471
1472 return Importer.getToContext().getExtVectorType(ToElementType,
1473 T->getNumElements());
1474}
1475
John McCallf4c73712011-01-19 06:33:43 +00001476QualType
1477ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001478 // FIXME: What happens if we're importing a function without a prototype
1479 // into C++? Should we make it variadic?
1480 QualType ToResultType = Importer.Import(T->getResultType());
1481 if (ToResultType.isNull())
1482 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001483
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001484 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001485 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001486}
1487
John McCallf4c73712011-01-19 06:33:43 +00001488QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001489 QualType ToResultType = Importer.Import(T->getResultType());
1490 if (ToResultType.isNull())
1491 return QualType();
1492
1493 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001494 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001495 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1496 AEnd = T->arg_type_end();
1497 A != AEnd; ++A) {
1498 QualType ArgType = Importer.Import(*A);
1499 if (ArgType.isNull())
1500 return QualType();
1501 ArgTypes.push_back(ArgType);
1502 }
1503
1504 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001505 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001506 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1507 EEnd = T->exception_end();
1508 E != EEnd; ++E) {
1509 QualType ExceptionType = Importer.Import(*E);
1510 if (ExceptionType.isNull())
1511 return QualType();
1512 ExceptionTypes.push_back(ExceptionType);
1513 }
John McCalle23cf432010-12-14 08:05:40 +00001514
1515 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1516 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001517
1518 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001519 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001520}
1521
Sean Callanan0aeb2892011-08-11 16:56:07 +00001522QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1523 QualType ToInnerType = Importer.Import(T->getInnerType());
1524 if (ToInnerType.isNull())
1525 return QualType();
1526
1527 return Importer.getToContext().getParenType(ToInnerType);
1528}
1529
John McCallf4c73712011-01-19 06:33:43 +00001530QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001531 TypedefNameDecl *ToDecl
1532 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001533 if (!ToDecl)
1534 return QualType();
1535
1536 return Importer.getToContext().getTypeDeclType(ToDecl);
1537}
1538
John McCallf4c73712011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001540 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1541 if (!ToExpr)
1542 return QualType();
1543
1544 return Importer.getToContext().getTypeOfExprType(ToExpr);
1545}
1546
John McCallf4c73712011-01-19 06:33:43 +00001547QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001548 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1549 if (ToUnderlyingType.isNull())
1550 return QualType();
1551
1552 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1553}
1554
John McCallf4c73712011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001556 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001557 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1558 if (!ToExpr)
1559 return QualType();
1560
1561 return Importer.getToContext().getDecltypeType(ToExpr);
1562}
1563
Sean Huntca63c202011-05-24 22:41:36 +00001564QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1565 QualType ToBaseType = Importer.Import(T->getBaseType());
1566 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1567 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1568 return QualType();
1569
1570 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1571 ToUnderlyingType,
1572 T->getUTTKind());
1573}
1574
Richard Smith34b41d92011-02-20 03:19:35 +00001575QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1576 // FIXME: Make sure that the "to" context supports C++0x!
1577 QualType FromDeduced = T->getDeducedType();
1578 QualType ToDeduced;
1579 if (!FromDeduced.isNull()) {
1580 ToDeduced = Importer.Import(FromDeduced);
1581 if (ToDeduced.isNull())
1582 return QualType();
1583 }
1584
1585 return Importer.getToContext().getAutoType(ToDeduced);
1586}
1587
John McCallf4c73712011-01-19 06:33:43 +00001588QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 RecordDecl *ToDecl
1590 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1591 if (!ToDecl)
1592 return QualType();
1593
1594 return Importer.getToContext().getTagDeclType(ToDecl);
1595}
1596
John McCallf4c73712011-01-19 06:33:43 +00001597QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001598 EnumDecl *ToDecl
1599 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1600 if (!ToDecl)
1601 return QualType();
1602
1603 return Importer.getToContext().getTagDeclType(ToDecl);
1604}
1605
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001606QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001607 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001608 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1609 if (ToTemplate.isNull())
1610 return QualType();
1611
Chris Lattner5f9e2722011-07-23 10:55:15 +00001612 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001613 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1614 return QualType();
1615
1616 QualType ToCanonType;
1617 if (!QualType(T, 0).isCanonical()) {
1618 QualType FromCanonType
1619 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1620 ToCanonType =Importer.Import(FromCanonType);
1621 if (ToCanonType.isNull())
1622 return QualType();
1623 }
1624 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1625 ToTemplateArgs.data(),
1626 ToTemplateArgs.size(),
1627 ToCanonType);
1628}
1629
John McCallf4c73712011-01-19 06:33:43 +00001630QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001631 NestedNameSpecifier *ToQualifier = 0;
1632 // Note: the qualifier in an ElaboratedType is optional.
1633 if (T->getQualifier()) {
1634 ToQualifier = Importer.Import(T->getQualifier());
1635 if (!ToQualifier)
1636 return QualType();
1637 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001638
1639 QualType ToNamedType = Importer.Import(T->getNamedType());
1640 if (ToNamedType.isNull())
1641 return QualType();
1642
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001643 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1644 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001645}
1646
John McCallf4c73712011-01-19 06:33:43 +00001647QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001648 ObjCInterfaceDecl *Class
1649 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1650 if (!Class)
1651 return QualType();
1652
John McCallc12c5bb2010-05-15 11:32:37 +00001653 return Importer.getToContext().getObjCInterfaceType(Class);
1654}
1655
John McCallf4c73712011-01-19 06:33:43 +00001656QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001657 QualType ToBaseType = Importer.Import(T->getBaseType());
1658 if (ToBaseType.isNull())
1659 return QualType();
1660
Chris Lattner5f9e2722011-07-23 10:55:15 +00001661 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001662 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001663 PEnd = T->qual_end();
1664 P != PEnd; ++P) {
1665 ObjCProtocolDecl *Protocol
1666 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1667 if (!Protocol)
1668 return QualType();
1669 Protocols.push_back(Protocol);
1670 }
1671
John McCallc12c5bb2010-05-15 11:32:37 +00001672 return Importer.getToContext().getObjCObjectType(ToBaseType,
1673 Protocols.data(),
1674 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001675}
1676
John McCallf4c73712011-01-19 06:33:43 +00001677QualType
1678ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001679 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1680 if (ToPointeeType.isNull())
1681 return QualType();
1682
John McCallc12c5bb2010-05-15 11:32:37 +00001683 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001684}
1685
Douglas Gregor089459a2010-02-08 21:09:39 +00001686//----------------------------------------------------------------------------
1687// Import Declarations
1688//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001689bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1690 DeclContext *&LexicalDC,
1691 DeclarationName &Name,
1692 SourceLocation &Loc) {
1693 // Import the context of this declaration.
1694 DC = Importer.ImportContext(D->getDeclContext());
1695 if (!DC)
1696 return true;
1697
1698 LexicalDC = DC;
1699 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1700 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1701 if (!LexicalDC)
1702 return true;
1703 }
1704
1705 // Import the name of this declaration.
1706 Name = Importer.Import(D->getDeclName());
1707 if (D->getDeclName() && !Name)
1708 return true;
1709
1710 // Import the location of this declaration.
1711 Loc = Importer.Import(D->getLocation());
1712 return false;
1713}
1714
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001715void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1716 if (!FromD)
1717 return;
1718
1719 if (!ToD) {
1720 ToD = Importer.Import(FromD);
1721 if (!ToD)
1722 return;
1723 }
1724
1725 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1726 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1727 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1728 ImportDefinition(FromRecord, ToRecord);
1729 }
1730 }
1731 return;
1732 }
1733
1734 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1735 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1736 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1737 ImportDefinition(FromEnum, ToEnum);
1738 }
1739 }
1740 return;
1741 }
1742}
1743
Abramo Bagnara25777432010-08-11 22:01:17 +00001744void
1745ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1746 DeclarationNameInfo& To) {
1747 // NOTE: To.Name and To.Loc are already imported.
1748 // We only have to import To.LocInfo.
1749 switch (To.getName().getNameKind()) {
1750 case DeclarationName::Identifier:
1751 case DeclarationName::ObjCZeroArgSelector:
1752 case DeclarationName::ObjCOneArgSelector:
1753 case DeclarationName::ObjCMultiArgSelector:
1754 case DeclarationName::CXXUsingDirective:
1755 return;
1756
1757 case DeclarationName::CXXOperatorName: {
1758 SourceRange Range = From.getCXXOperatorNameRange();
1759 To.setCXXOperatorNameRange(Importer.Import(Range));
1760 return;
1761 }
1762 case DeclarationName::CXXLiteralOperatorName: {
1763 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1764 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1765 return;
1766 }
1767 case DeclarationName::CXXConstructorName:
1768 case DeclarationName::CXXDestructorName:
1769 case DeclarationName::CXXConversionFunctionName: {
1770 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1771 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1772 return;
1773 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001774 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001775 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001776}
1777
Douglas Gregord8868a62011-01-18 03:11:38 +00001778void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1779 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001780 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001781 return;
1782 }
1783
Douglas Gregor083a8212010-02-21 18:24:45 +00001784 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1785 FromEnd = FromDC->decls_end();
1786 From != FromEnd;
1787 ++From)
1788 Importer.Import(*From);
1789}
1790
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001791bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1792 bool ForceImport) {
1793 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001794 return false;
1795
1796 To->startDefinition();
1797
1798 // Add base classes.
1799 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1800 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001801
1802 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1803 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1804 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1805 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1806 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1807 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1808 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1809 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1810 ToData.Aggregate = FromData.Aggregate;
1811 ToData.PlainOldData = FromData.PlainOldData;
1812 ToData.Empty = FromData.Empty;
1813 ToData.Polymorphic = FromData.Polymorphic;
1814 ToData.Abstract = FromData.Abstract;
1815 ToData.IsStandardLayout = FromData.IsStandardLayout;
1816 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1817 ToData.HasPrivateFields = FromData.HasPrivateFields;
1818 ToData.HasProtectedFields = FromData.HasProtectedFields;
1819 ToData.HasPublicFields = FromData.HasPublicFields;
1820 ToData.HasMutableFields = FromData.HasMutableFields;
1821 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1822 ToData.HasConstexprNonCopyMoveConstructor
1823 = FromData.HasConstexprNonCopyMoveConstructor;
1824 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1825 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1826 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1827 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1828 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1829 ToData.HasNonLiteralTypeFieldsOrBases
1830 = FromData.HasNonLiteralTypeFieldsOrBases;
1831 ToData.UserProvidedDefaultConstructor
1832 = FromData.UserProvidedDefaultConstructor;
1833 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1834 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1835 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1836 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1837 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1838 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1839 ToData.FailedImplicitMoveConstructor
1840 = FromData.FailedImplicitMoveConstructor;
1841 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001842
Chris Lattner5f9e2722011-07-23 10:55:15 +00001843 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001844 for (CXXRecordDecl::base_class_iterator
1845 Base1 = FromCXX->bases_begin(),
1846 FromBaseEnd = FromCXX->bases_end();
1847 Base1 != FromBaseEnd;
1848 ++Base1) {
1849 QualType T = Importer.Import(Base1->getType());
1850 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001851 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001852
1853 SourceLocation EllipsisLoc;
1854 if (Base1->isPackExpansion())
1855 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001856
1857 // Ensure that we have a definition for the base.
1858 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1859
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001860 Bases.push_back(
1861 new (Importer.getToContext())
1862 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1863 Base1->isVirtual(),
1864 Base1->isBaseOfClass(),
1865 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001866 Importer.Import(Base1->getTypeSourceInfo()),
1867 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001868 }
1869 if (!Bases.empty())
1870 ToCXX->setBases(Bases.data(), Bases.size());
1871 }
1872
Sean Callanan673e7752011-07-19 22:38:25 +00001873 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001874 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001875 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001876}
1877
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001878bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1879 bool ForceImport) {
1880 if (To->getDefinition() || To->isBeingDefined())
1881 return false;
1882
1883 To->startDefinition();
1884
1885 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1886 if (T.isNull())
1887 return true;
1888
1889 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1890 if (ToPromotionType.isNull())
1891 return true;
1892
1893 ImportDeclContext(From, ForceImport);
1894
1895 // FIXME: we might need to merge the number of positive or negative bits
1896 // if the enumerator lists don't match.
1897 To->completeDefinition(T, ToPromotionType,
1898 From->getNumPositiveBits(),
1899 From->getNumNegativeBits());
1900 return false;
1901}
1902
Douglas Gregor040afae2010-11-30 19:14:50 +00001903TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1904 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001905 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001906 ToParams.reserve(Params->size());
1907 for (TemplateParameterList::iterator P = Params->begin(),
1908 PEnd = Params->end();
1909 P != PEnd; ++P) {
1910 Decl *To = Importer.Import(*P);
1911 if (!To)
1912 return 0;
1913
1914 ToParams.push_back(cast<NamedDecl>(To));
1915 }
1916
1917 return TemplateParameterList::Create(Importer.getToContext(),
1918 Importer.Import(Params->getTemplateLoc()),
1919 Importer.Import(Params->getLAngleLoc()),
1920 ToParams.data(), ToParams.size(),
1921 Importer.Import(Params->getRAngleLoc()));
1922}
1923
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001924TemplateArgument
1925ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1926 switch (From.getKind()) {
1927 case TemplateArgument::Null:
1928 return TemplateArgument();
1929
1930 case TemplateArgument::Type: {
1931 QualType ToType = Importer.Import(From.getAsType());
1932 if (ToType.isNull())
1933 return TemplateArgument();
1934 return TemplateArgument(ToType);
1935 }
1936
1937 case TemplateArgument::Integral: {
1938 QualType ToType = Importer.Import(From.getIntegralType());
1939 if (ToType.isNull())
1940 return TemplateArgument();
1941 return TemplateArgument(*From.getAsIntegral(), ToType);
1942 }
1943
1944 case TemplateArgument::Declaration:
1945 if (Decl *To = Importer.Import(From.getAsDecl()))
1946 return TemplateArgument(To);
1947 return TemplateArgument();
1948
1949 case TemplateArgument::Template: {
1950 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1951 if (ToTemplate.isNull())
1952 return TemplateArgument();
1953
1954 return TemplateArgument(ToTemplate);
1955 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001956
1957 case TemplateArgument::TemplateExpansion: {
1958 TemplateName ToTemplate
1959 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1960 if (ToTemplate.isNull())
1961 return TemplateArgument();
1962
Douglas Gregor2be29f42011-01-14 23:41:42 +00001963 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001964 }
1965
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001966 case TemplateArgument::Expression:
1967 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1968 return TemplateArgument(ToExpr);
1969 return TemplateArgument();
1970
1971 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001972 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001973 ToPack.reserve(From.pack_size());
1974 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1975 return TemplateArgument();
1976
1977 TemplateArgument *ToArgs
1978 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1979 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1980 return TemplateArgument(ToArgs, ToPack.size());
1981 }
1982 }
1983
1984 llvm_unreachable("Invalid template argument kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001985}
1986
1987bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1988 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001989 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001990 for (unsigned I = 0; I != NumFromArgs; ++I) {
1991 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1992 if (To.isNull() && !FromArgs[I].isNull())
1993 return true;
1994
1995 ToArgs.push_back(To);
1996 }
1997
1998 return false;
1999}
2000
Douglas Gregor96a01b42010-02-11 00:48:18 +00002001bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002002 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002003 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002004 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002005 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002006 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002007}
2008
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002009bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002010 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002011 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002012 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002013 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002014}
2015
Douglas Gregor040afae2010-11-30 19:14:50 +00002016bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2017 ClassTemplateDecl *To) {
2018 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2019 Importer.getToContext(),
2020 Importer.getNonEquivalentDecls());
2021 return Ctx.IsStructurallyEquivalent(From, To);
2022}
2023
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002024Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002025 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002026 << D->getDeclKindName();
2027 return 0;
2028}
2029
Sean Callananf1b69462011-11-17 23:20:56 +00002030Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2031 TranslationUnitDecl *ToD =
2032 Importer.getToContext().getTranslationUnitDecl();
2033
2034 Importer.Imported(D, ToD);
2035
2036 return ToD;
2037}
2038
Douglas Gregor788c62d2010-02-21 18:26:36 +00002039Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2040 // Import the major distinguishing characteristics of this namespace.
2041 DeclContext *DC, *LexicalDC;
2042 DeclarationName Name;
2043 SourceLocation Loc;
2044 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2045 return 0;
2046
2047 NamespaceDecl *MergeWithNamespace = 0;
2048 if (!Name) {
2049 // This is an anonymous namespace. Adopt an existing anonymous
2050 // namespace if we can.
2051 // FIXME: Not testable.
2052 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2053 MergeWithNamespace = TU->getAnonymousNamespace();
2054 else
2055 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2056 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002057 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002058 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2059 DC->localUncachedLookup(Name, FoundDecls);
2060 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2061 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002062 continue;
2063
Douglas Gregorb75a3452011-10-15 00:10:27 +00002064 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002065 MergeWithNamespace = FoundNS;
2066 ConflictingDecls.clear();
2067 break;
2068 }
2069
Douglas Gregorb75a3452011-10-15 00:10:27 +00002070 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002071 }
2072
2073 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002074 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002075 ConflictingDecls.data(),
2076 ConflictingDecls.size());
2077 }
2078 }
2079
2080 // Create the "to" namespace, if needed.
2081 NamespaceDecl *ToNamespace = MergeWithNamespace;
2082 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002083 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002084 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002085 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002086 Loc, Name.getAsIdentifierInfo(),
2087 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002088 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002089 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002090
2091 // If this is an anonymous namespace, register it as the anonymous
2092 // namespace within its context.
2093 if (!Name) {
2094 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2095 TU->setAnonymousNamespace(ToNamespace);
2096 else
2097 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2098 }
2099 }
2100 Importer.Imported(D, ToNamespace);
2101
2102 ImportDeclContext(D);
2103
2104 return ToNamespace;
2105}
2106
Richard Smith162e1c12011-04-15 14:24:37 +00002107Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002108 // Import the major distinguishing characteristics of this typedef.
2109 DeclContext *DC, *LexicalDC;
2110 DeclarationName Name;
2111 SourceLocation Loc;
2112 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2113 return 0;
2114
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002115 // If this typedef is not in block scope, determine whether we've
2116 // seen a typedef with the same name (that we can merge with) or any
2117 // other entity by that name (which name lookup could conflict with).
2118 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002119 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002120 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002121 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2122 DC->localUncachedLookup(Name, FoundDecls);
2123 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2124 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002125 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002126 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002127 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002128 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2129 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002130 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002131 }
2132
Douglas Gregorb75a3452011-10-15 00:10:27 +00002133 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002134 }
2135
2136 if (!ConflictingDecls.empty()) {
2137 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2138 ConflictingDecls.data(),
2139 ConflictingDecls.size());
2140 if (!Name)
2141 return 0;
2142 }
2143 }
2144
Douglas Gregorea35d112010-02-15 23:54:17 +00002145 // Import the underlying type of this typedef;
2146 QualType T = Importer.Import(D->getUnderlyingType());
2147 if (T.isNull())
2148 return 0;
2149
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002150 // Create the new typedef node.
2151 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002152 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002153 TypedefNameDecl *ToTypedef;
2154 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002155 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2156 StartL, Loc,
2157 Name.getAsIdentifierInfo(),
2158 TInfo);
2159 else
Richard Smith162e1c12011-04-15 14:24:37 +00002160 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2161 StartL, Loc,
2162 Name.getAsIdentifierInfo(),
2163 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002164
Douglas Gregor325bf172010-02-22 17:42:47 +00002165 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002166 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002167 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002168 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002169
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002170 return ToTypedef;
2171}
2172
Richard Smith162e1c12011-04-15 14:24:37 +00002173Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2174 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2175}
2176
2177Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2178 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2179}
2180
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002181Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2182 // Import the major distinguishing characteristics of this enum.
2183 DeclContext *DC, *LexicalDC;
2184 DeclarationName Name;
2185 SourceLocation Loc;
2186 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2187 return 0;
2188
2189 // Figure out what enum name we're looking for.
2190 unsigned IDNS = Decl::IDNS_Tag;
2191 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002192 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2193 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002194 IDNS = Decl::IDNS_Ordinary;
2195 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2196 IDNS |= Decl::IDNS_Ordinary;
2197
2198 // We may already have an enum of the same name; try to find and match it.
2199 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002200 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002201 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2202 DC->localUncachedLookup(SearchName, FoundDecls);
2203 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2204 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002205 continue;
2206
Douglas Gregorb75a3452011-10-15 00:10:27 +00002207 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002208 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002209 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2210 Found = Tag->getDecl();
2211 }
2212
2213 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002214 if (IsStructuralMatch(D, FoundEnum))
2215 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002216 }
2217
Douglas Gregorb75a3452011-10-15 00:10:27 +00002218 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002219 }
2220
2221 if (!ConflictingDecls.empty()) {
2222 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2223 ConflictingDecls.data(),
2224 ConflictingDecls.size());
2225 }
2226 }
2227
2228 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002229 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2230 Importer.Import(D->getLocStart()),
2231 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002232 D->isScoped(), D->isScopedUsingClassTag(),
2233 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002234 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002235 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002236 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002237 D2->setLexicalDeclContext(LexicalDC);
2238 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002239 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002240
2241 // Import the integer type.
2242 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2243 if (ToIntegerType.isNull())
2244 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002245 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002246
2247 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002248 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002249 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002250
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002251 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002252}
2253
Douglas Gregor96a01b42010-02-11 00:48:18 +00002254Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2255 // If this record has a definition in the translation unit we're coming from,
2256 // but this particular declaration is not that definition, import the
2257 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002258 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002259 if (Definition && Definition != D) {
2260 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002261 if (!ImportedDef)
2262 return 0;
2263
2264 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002265 }
2266
2267 // Import the major distinguishing characteristics of this record.
2268 DeclContext *DC, *LexicalDC;
2269 DeclarationName Name;
2270 SourceLocation Loc;
2271 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2272 return 0;
2273
2274 // Figure out what structure name we're looking for.
2275 unsigned IDNS = Decl::IDNS_Tag;
2276 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002277 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2278 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002279 IDNS = Decl::IDNS_Ordinary;
2280 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2281 IDNS |= Decl::IDNS_Ordinary;
2282
2283 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002284 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002285 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002286 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002287 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2288 DC->localUncachedLookup(SearchName, FoundDecls);
2289 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2290 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002291 continue;
2292
Douglas Gregorb75a3452011-10-15 00:10:27 +00002293 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002294 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002295 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2296 Found = Tag->getDecl();
2297 }
2298
2299 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002300 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002301 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002302 // The record types structurally match, or the "from" translation
2303 // unit only had a forward declaration anyway; call it the same
2304 // function.
2305 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002306 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002307 }
2308 } else {
2309 // We have a forward declaration of this type, so adopt that forward
2310 // declaration rather than building a new one.
2311 AdoptDecl = FoundRecord;
2312 continue;
2313 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002314 }
2315
Douglas Gregorb75a3452011-10-15 00:10:27 +00002316 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002317 }
2318
2319 if (!ConflictingDecls.empty()) {
2320 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2321 ConflictingDecls.data(),
2322 ConflictingDecls.size());
2323 }
2324 }
2325
2326 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002327 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002328 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002329 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002330 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002332 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002333 DC, StartLoc, Loc,
2334 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002335 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002336 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002337 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002338 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002339 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002340 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002341
2342 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002343 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002344 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002345 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002346
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002347 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002348
John McCall5e1cdac2011-10-07 06:10:15 +00002349 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002350 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002351
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002352 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002353}
2354
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002355Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2356 // Import the major distinguishing characteristics of this enumerator.
2357 DeclContext *DC, *LexicalDC;
2358 DeclarationName Name;
2359 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002360 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002361 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002362
2363 QualType T = Importer.Import(D->getType());
2364 if (T.isNull())
2365 return 0;
2366
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002367 // Determine whether there are any other declarations with the same name and
2368 // in the same context.
2369 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002370 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002371 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002372 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2373 DC->localUncachedLookup(Name, FoundDecls);
2374 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2375 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002376 continue;
2377
Douglas Gregorb75a3452011-10-15 00:10:27 +00002378 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002379 }
2380
2381 if (!ConflictingDecls.empty()) {
2382 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2383 ConflictingDecls.data(),
2384 ConflictingDecls.size());
2385 if (!Name)
2386 return 0;
2387 }
2388 }
2389
2390 Expr *Init = Importer.Import(D->getInitExpr());
2391 if (D->getInitExpr() && !Init)
2392 return 0;
2393
2394 EnumConstantDecl *ToEnumerator
2395 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2396 Name.getAsIdentifierInfo(), T,
2397 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002398 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002399 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002400 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002401 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002402 return ToEnumerator;
2403}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002404
Douglas Gregora404ea62010-02-10 19:54:31 +00002405Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2406 // Import the major distinguishing characteristics of this function.
2407 DeclContext *DC, *LexicalDC;
2408 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002409 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002410 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002411 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002412
Douglas Gregora404ea62010-02-10 19:54:31 +00002413 // Try to find a function in our own ("to") context with the same name, same
2414 // type, and in the same context as the function we're importing.
2415 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002416 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002417 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002418 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2419 DC->localUncachedLookup(Name, FoundDecls);
2420 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2421 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002422 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002423
Douglas Gregorb75a3452011-10-15 00:10:27 +00002424 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002425 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2426 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002427 if (Importer.IsStructurallyEquivalent(D->getType(),
2428 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002429 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002430 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002431 }
2432
2433 // FIXME: Check for overloading more carefully, e.g., by boosting
2434 // Sema::IsOverload out to the AST library.
2435
2436 // Function overloading is okay in C++.
2437 if (Importer.getToContext().getLangOptions().CPlusPlus)
2438 continue;
2439
2440 // Complain about inconsistent function types.
2441 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002442 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002443 Importer.ToDiag(FoundFunction->getLocation(),
2444 diag::note_odr_value_here)
2445 << FoundFunction->getType();
2446 }
2447 }
2448
Douglas Gregorb75a3452011-10-15 00:10:27 +00002449 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002450 }
2451
2452 if (!ConflictingDecls.empty()) {
2453 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2454 ConflictingDecls.data(),
2455 ConflictingDecls.size());
2456 if (!Name)
2457 return 0;
2458 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002459 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002460
Abramo Bagnara25777432010-08-11 22:01:17 +00002461 DeclarationNameInfo NameInfo(Name, Loc);
2462 // Import additional name location/type info.
2463 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2464
Douglas Gregorea35d112010-02-15 23:54:17 +00002465 // Import the type.
2466 QualType T = Importer.Import(D->getType());
2467 if (T.isNull())
2468 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002469
2470 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002471 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002472 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2473 P != PEnd; ++P) {
2474 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2475 if (!ToP)
2476 return 0;
2477
2478 Parameters.push_back(ToP);
2479 }
2480
2481 // Create the imported function.
2482 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002483 FunctionDecl *ToFunction = 0;
2484 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2485 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2486 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002487 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002488 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002489 FromConstructor->isExplicit(),
2490 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002491 D->isImplicit(),
2492 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002493 } else if (isa<CXXDestructorDecl>(D)) {
2494 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2495 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002496 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002497 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002498 D->isInlineSpecified(),
2499 D->isImplicit());
2500 } else if (CXXConversionDecl *FromConversion
2501 = dyn_cast<CXXConversionDecl>(D)) {
2502 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2503 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002504 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002505 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002506 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002507 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002508 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002509 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002510 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2511 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2512 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002513 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002514 NameInfo, T, TInfo,
2515 Method->isStatic(),
2516 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002517 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002518 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002519 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002520 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002521 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002522 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002523 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002524 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002525 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002526 D->hasWrittenPrototype(),
2527 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002528 }
John McCallb6217662010-03-15 10:12:16 +00002529
2530 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002531 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002532 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002533 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002534 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2535 ToFunction->setTrivial(D->isTrivial());
2536 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002537 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002538
Douglas Gregora404ea62010-02-10 19:54:31 +00002539 // Set the parameters.
2540 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002541 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002542 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002543 }
David Blaikie4278c652011-09-21 18:16:56 +00002544 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002545
2546 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002547
2548 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002549 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002550
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002551 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002552}
2553
Douglas Gregorc144f352010-02-21 18:29:16 +00002554Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2555 return VisitFunctionDecl(D);
2556}
2557
2558Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2559 return VisitCXXMethodDecl(D);
2560}
2561
2562Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2563 return VisitCXXMethodDecl(D);
2564}
2565
2566Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2567 return VisitCXXMethodDecl(D);
2568}
2569
Douglas Gregor96a01b42010-02-11 00:48:18 +00002570Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2571 // Import the major distinguishing characteristics of a variable.
2572 DeclContext *DC, *LexicalDC;
2573 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002574 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002575 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2576 return 0;
2577
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002578 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002579 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2580 DC->localUncachedLookup(Name, FoundDecls);
2581 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2582 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002583 if (Importer.IsStructurallyEquivalent(D->getType(),
2584 FoundField->getType())) {
2585 Importer.Imported(D, FoundField);
2586 return FoundField;
2587 }
2588
2589 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2590 << Name << D->getType() << FoundField->getType();
2591 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2592 << FoundField->getType();
2593 return 0;
2594 }
2595 }
2596
Douglas Gregorea35d112010-02-15 23:54:17 +00002597 // Import the type.
2598 QualType T = Importer.Import(D->getType());
2599 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002600 return 0;
2601
2602 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2603 Expr *BitWidth = Importer.Import(D->getBitWidth());
2604 if (!BitWidth && D->getBitWidth())
2605 return 0;
2606
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002607 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2608 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002609 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002610 T, TInfo, BitWidth, D->isMutable(),
2611 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002612 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002613 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002614 if (ToField->hasInClassInitializer())
2615 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002616 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002617 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002618 return ToField;
2619}
2620
Francois Pichet87c2e122010-11-21 06:08:52 +00002621Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2622 // Import the major distinguishing characteristics of a variable.
2623 DeclContext *DC, *LexicalDC;
2624 DeclarationName Name;
2625 SourceLocation Loc;
2626 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2627 return 0;
2628
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002629 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002630 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2631 DC->localUncachedLookup(Name, FoundDecls);
2632 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002633 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002634 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002635 if (Importer.IsStructurallyEquivalent(D->getType(),
2636 FoundField->getType())) {
2637 Importer.Imported(D, FoundField);
2638 return FoundField;
2639 }
2640
2641 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2642 << Name << D->getType() << FoundField->getType();
2643 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2644 << FoundField->getType();
2645 return 0;
2646 }
2647 }
2648
Francois Pichet87c2e122010-11-21 06:08:52 +00002649 // Import the type.
2650 QualType T = Importer.Import(D->getType());
2651 if (T.isNull())
2652 return 0;
2653
2654 NamedDecl **NamedChain =
2655 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2656
2657 unsigned i = 0;
2658 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2659 PE = D->chain_end(); PI != PE; ++PI) {
2660 Decl* D = Importer.Import(*PI);
2661 if (!D)
2662 return 0;
2663 NamedChain[i++] = cast<NamedDecl>(D);
2664 }
2665
2666 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2667 Importer.getToContext(), DC,
2668 Loc, Name.getAsIdentifierInfo(), T,
2669 NamedChain, D->getChainingSize());
2670 ToIndirectField->setAccess(D->getAccess());
2671 ToIndirectField->setLexicalDeclContext(LexicalDC);
2672 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002673 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002674 return ToIndirectField;
2675}
2676
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002677Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2678 // Import the major distinguishing characteristics of an ivar.
2679 DeclContext *DC, *LexicalDC;
2680 DeclarationName Name;
2681 SourceLocation Loc;
2682 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2683 return 0;
2684
2685 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002686 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2687 DC->localUncachedLookup(Name, FoundDecls);
2688 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2689 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002690 if (Importer.IsStructurallyEquivalent(D->getType(),
2691 FoundIvar->getType())) {
2692 Importer.Imported(D, FoundIvar);
2693 return FoundIvar;
2694 }
2695
2696 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2697 << Name << D->getType() << FoundIvar->getType();
2698 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2699 << FoundIvar->getType();
2700 return 0;
2701 }
2702 }
2703
2704 // Import the type.
2705 QualType T = Importer.Import(D->getType());
2706 if (T.isNull())
2707 return 0;
2708
2709 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2710 Expr *BitWidth = Importer.Import(D->getBitWidth());
2711 if (!BitWidth && D->getBitWidth())
2712 return 0;
2713
Daniel Dunbara0654922010-04-02 20:10:03 +00002714 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2715 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002716 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002717 Loc, Name.getAsIdentifierInfo(),
2718 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002719 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002720 ToIvar->setLexicalDeclContext(LexicalDC);
2721 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002722 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002723 return ToIvar;
2724
2725}
2726
Douglas Gregora404ea62010-02-10 19:54:31 +00002727Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2728 // Import the major distinguishing characteristics of a variable.
2729 DeclContext *DC, *LexicalDC;
2730 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002731 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002732 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002733 return 0;
2734
Douglas Gregor089459a2010-02-08 21:09:39 +00002735 // Try to find a variable in our own ("to") context with the same name and
2736 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002737 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002738 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002739 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002740 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002741 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2742 DC->localUncachedLookup(Name, FoundDecls);
2743 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2744 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002745 continue;
2746
Douglas Gregorb75a3452011-10-15 00:10:27 +00002747 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002748 // We have found a variable that we may need to merge with. Check it.
2749 if (isExternalLinkage(FoundVar->getLinkage()) &&
2750 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002751 if (Importer.IsStructurallyEquivalent(D->getType(),
2752 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002753 MergeWithVar = FoundVar;
2754 break;
2755 }
2756
Douglas Gregord0145422010-02-12 17:23:39 +00002757 const ArrayType *FoundArray
2758 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2759 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002760 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002761 if (FoundArray && TArray) {
2762 if (isa<IncompleteArrayType>(FoundArray) &&
2763 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002764 // Import the type.
2765 QualType T = Importer.Import(D->getType());
2766 if (T.isNull())
2767 return 0;
2768
Douglas Gregord0145422010-02-12 17:23:39 +00002769 FoundVar->setType(T);
2770 MergeWithVar = FoundVar;
2771 break;
2772 } else if (isa<IncompleteArrayType>(TArray) &&
2773 isa<ConstantArrayType>(FoundArray)) {
2774 MergeWithVar = FoundVar;
2775 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002776 }
2777 }
2778
Douglas Gregor089459a2010-02-08 21:09:39 +00002779 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002780 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002781 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2782 << FoundVar->getType();
2783 }
2784 }
2785
Douglas Gregorb75a3452011-10-15 00:10:27 +00002786 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002787 }
2788
2789 if (MergeWithVar) {
2790 // An equivalent variable with external linkage has been found. Link
2791 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002792 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002793
2794 if (VarDecl *DDef = D->getDefinition()) {
2795 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2796 Importer.ToDiag(ExistingDef->getLocation(),
2797 diag::err_odr_variable_multiple_def)
2798 << Name;
2799 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2800 } else {
2801 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002802 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002803 if (DDef->isInitKnownICE()) {
2804 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2805 Eval->CheckedICE = true;
2806 Eval->IsICE = DDef->isInitICE();
2807 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002808 }
2809 }
2810
2811 return MergeWithVar;
2812 }
2813
2814 if (!ConflictingDecls.empty()) {
2815 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2816 ConflictingDecls.data(),
2817 ConflictingDecls.size());
2818 if (!Name)
2819 return 0;
2820 }
2821 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002822
Douglas Gregorea35d112010-02-15 23:54:17 +00002823 // Import the type.
2824 QualType T = Importer.Import(D->getType());
2825 if (T.isNull())
2826 return 0;
2827
Douglas Gregor089459a2010-02-08 21:09:39 +00002828 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002829 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002830 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2831 Importer.Import(D->getInnerLocStart()),
2832 Loc, Name.getAsIdentifierInfo(),
2833 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002834 D->getStorageClass(),
2835 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002836 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002837 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002838 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002839 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002840 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002841
Douglas Gregor089459a2010-02-08 21:09:39 +00002842 // Merge the initializer.
2843 // FIXME: Can we really import any initializer? Alternatively, we could force
2844 // ourselves to import every declaration of a variable and then only use
2845 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002846 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002847
2848 // FIXME: Other bits to merge?
2849
2850 return ToVar;
2851}
2852
Douglas Gregor2cd00932010-02-17 21:22:52 +00002853Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2854 // Parameters are created in the translation unit's context, then moved
2855 // into the function declaration's context afterward.
2856 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2857
2858 // Import the name of this declaration.
2859 DeclarationName Name = Importer.Import(D->getDeclName());
2860 if (D->getDeclName() && !Name)
2861 return 0;
2862
2863 // Import the location of this declaration.
2864 SourceLocation Loc = Importer.Import(D->getLocation());
2865
2866 // Import the parameter's type.
2867 QualType T = Importer.Import(D->getType());
2868 if (T.isNull())
2869 return 0;
2870
2871 // Create the imported parameter.
2872 ImplicitParamDecl *ToParm
2873 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2874 Loc, Name.getAsIdentifierInfo(),
2875 T);
2876 return Importer.Imported(D, ToParm);
2877}
2878
Douglas Gregora404ea62010-02-10 19:54:31 +00002879Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2880 // Parameters are created in the translation unit's context, then moved
2881 // into the function declaration's context afterward.
2882 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2883
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002884 // Import the name of this declaration.
2885 DeclarationName Name = Importer.Import(D->getDeclName());
2886 if (D->getDeclName() && !Name)
2887 return 0;
2888
Douglas Gregora404ea62010-02-10 19:54:31 +00002889 // Import the location of this declaration.
2890 SourceLocation Loc = Importer.Import(D->getLocation());
2891
2892 // Import the parameter's type.
2893 QualType T = Importer.Import(D->getType());
2894 if (T.isNull())
2895 return 0;
2896
2897 // Create the imported parameter.
2898 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2899 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002900 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002901 Loc, Name.getAsIdentifierInfo(),
2902 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002903 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002904 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002905 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002906 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002907}
2908
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002909Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2910 // Import the major distinguishing characteristics of a method.
2911 DeclContext *DC, *LexicalDC;
2912 DeclarationName Name;
2913 SourceLocation Loc;
2914 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2915 return 0;
2916
Douglas Gregorb75a3452011-10-15 00:10:27 +00002917 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2918 DC->localUncachedLookup(Name, FoundDecls);
2919 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2920 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002921 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2922 continue;
2923
2924 // Check return types.
2925 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2926 FoundMethod->getResultType())) {
2927 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2928 << D->isInstanceMethod() << Name
2929 << D->getResultType() << FoundMethod->getResultType();
2930 Importer.ToDiag(FoundMethod->getLocation(),
2931 diag::note_odr_objc_method_here)
2932 << D->isInstanceMethod() << Name;
2933 return 0;
2934 }
2935
2936 // Check the number of parameters.
2937 if (D->param_size() != FoundMethod->param_size()) {
2938 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2939 << D->isInstanceMethod() << Name
2940 << D->param_size() << FoundMethod->param_size();
2941 Importer.ToDiag(FoundMethod->getLocation(),
2942 diag::note_odr_objc_method_here)
2943 << D->isInstanceMethod() << Name;
2944 return 0;
2945 }
2946
2947 // Check parameter types.
2948 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2949 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2950 P != PEnd; ++P, ++FoundP) {
2951 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2952 (*FoundP)->getType())) {
2953 Importer.FromDiag((*P)->getLocation(),
2954 diag::err_odr_objc_method_param_type_inconsistent)
2955 << D->isInstanceMethod() << Name
2956 << (*P)->getType() << (*FoundP)->getType();
2957 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2958 << (*FoundP)->getType();
2959 return 0;
2960 }
2961 }
2962
2963 // Check variadic/non-variadic.
2964 // Check the number of parameters.
2965 if (D->isVariadic() != FoundMethod->isVariadic()) {
2966 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2967 << D->isInstanceMethod() << Name;
2968 Importer.ToDiag(FoundMethod->getLocation(),
2969 diag::note_odr_objc_method_here)
2970 << D->isInstanceMethod() << Name;
2971 return 0;
2972 }
2973
2974 // FIXME: Any other bits we need to merge?
2975 return Importer.Imported(D, FoundMethod);
2976 }
2977 }
2978
2979 // Import the result type.
2980 QualType ResultTy = Importer.Import(D->getResultType());
2981 if (ResultTy.isNull())
2982 return 0;
2983
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002984 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2985
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002986 ObjCMethodDecl *ToMethod
2987 = ObjCMethodDecl::Create(Importer.getToContext(),
2988 Loc,
2989 Importer.Import(D->getLocEnd()),
2990 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002991 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002992 D->isInstanceMethod(),
2993 D->isVariadic(),
2994 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002995 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002996 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002997 D->getImplementationControl(),
2998 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002999
3000 // FIXME: When we decide to merge method definitions, we'll need to
3001 // deal with implicit parameters.
3002
3003 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003004 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003005 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3006 FromPEnd = D->param_end();
3007 FromP != FromPEnd;
3008 ++FromP) {
3009 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3010 if (!ToP)
3011 return 0;
3012
3013 ToParams.push_back(ToP);
3014 }
3015
3016 // Set the parameters.
3017 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3018 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003019 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003020 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003021 SmallVector<SourceLocation, 12> SelLocs;
3022 D->getSelectorLocs(SelLocs);
3023 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003024
3025 ToMethod->setLexicalDeclContext(LexicalDC);
3026 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003027 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003028 return ToMethod;
3029}
3030
Douglas Gregorb4677b62010-02-18 01:47:50 +00003031Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3032 // Import the major distinguishing characteristics of a category.
3033 DeclContext *DC, *LexicalDC;
3034 DeclarationName Name;
3035 SourceLocation Loc;
3036 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3037 return 0;
3038
3039 ObjCInterfaceDecl *ToInterface
3040 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3041 if (!ToInterface)
3042 return 0;
3043
3044 // Determine if we've already encountered this category.
3045 ObjCCategoryDecl *MergeWithCategory
3046 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3047 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3048 if (!ToCategory) {
3049 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003050 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003051 Loc,
3052 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003053 Name.getAsIdentifierInfo(),
3054 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003055 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003056 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003057 Importer.Imported(D, ToCategory);
3058
Douglas Gregorb4677b62010-02-18 01:47:50 +00003059 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003060 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3061 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003062 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3063 = D->protocol_loc_begin();
3064 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3065 FromProtoEnd = D->protocol_end();
3066 FromProto != FromProtoEnd;
3067 ++FromProto, ++FromProtoLoc) {
3068 ObjCProtocolDecl *ToProto
3069 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3070 if (!ToProto)
3071 return 0;
3072 Protocols.push_back(ToProto);
3073 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3074 }
3075
3076 // FIXME: If we're merging, make sure that the protocol list is the same.
3077 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3078 ProtocolLocs.data(), Importer.getToContext());
3079
3080 } else {
3081 Importer.Imported(D, ToCategory);
3082 }
3083
3084 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003085 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003086
3087 // If we have an implementation, import it as well.
3088 if (D->getImplementation()) {
3089 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003090 = cast_or_null<ObjCCategoryImplDecl>(
3091 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003092 if (!Impl)
3093 return 0;
3094
3095 ToCategory->setImplementation(Impl);
3096 }
3097
3098 return ToCategory;
3099}
3100
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003101Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003102 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003103 DeclContext *DC, *LexicalDC;
3104 DeclarationName Name;
3105 SourceLocation Loc;
3106 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3107 return 0;
3108
3109 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003110 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3111 DC->localUncachedLookup(Name, FoundDecls);
3112 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3113 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003114 continue;
3115
Douglas Gregorb75a3452011-10-15 00:10:27 +00003116 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003117 break;
3118 }
3119
3120 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003121 if (!ToProto || !ToProto->hasDefinition()) {
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003122 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003123 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3124 Name.getAsIdentifierInfo(), Loc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003125 Importer.Import(D->getAtStartLoc()),
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00003126 /*PrevDecl=*/0);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003127 ToProto->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003128 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003129 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003130 if (!ToProto->hasDefinition())
3131 ToProto->startDefinition();
3132
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003133 Importer.Imported(D, ToProto);
3134
3135 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003136 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3137 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003138 ObjCProtocolDecl::protocol_loc_iterator
3139 FromProtoLoc = D->protocol_loc_begin();
3140 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3141 FromProtoEnd = D->protocol_end();
3142 FromProto != FromProtoEnd;
3143 ++FromProto, ++FromProtoLoc) {
3144 ObjCProtocolDecl *ToProto
3145 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3146 if (!ToProto)
3147 return 0;
3148 Protocols.push_back(ToProto);
3149 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3150 }
3151
3152 // FIXME: If we're merging, make sure that the protocol list is the same.
3153 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3154 ProtocolLocs.data(), Importer.getToContext());
3155 } else {
3156 Importer.Imported(D, ToProto);
3157 }
3158
Douglas Gregorb4677b62010-02-18 01:47:50 +00003159 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003160 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003161
3162 return ToProto;
3163}
3164
Douglas Gregora12d2942010-02-16 01:20:57 +00003165Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3166 // Import the major distinguishing characteristics of an @interface.
3167 DeclContext *DC, *LexicalDC;
3168 DeclarationName Name;
3169 SourceLocation Loc;
3170 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3171 return 0;
3172
3173 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003174 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3175 DC->localUncachedLookup(Name, FoundDecls);
3176 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3177 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003178 continue;
3179
Douglas Gregorb75a3452011-10-15 00:10:27 +00003180 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003181 break;
3182 }
3183
3184 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003185 if (!ToIface || !ToIface->hasDefinition()) {
Douglas Gregora12d2942010-02-16 01:20:57 +00003186 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003187 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3188 Importer.Import(D->getAtStartLoc()),
Douglas Gregor0af55012011-12-16 03:12:41 +00003189 Name.getAsIdentifierInfo(),
3190 /*PrevDecl=*/0,Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003191 D->isImplicitInterfaceDecl());
3192 ToIface->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003193 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003194 }
3195 Importer.Imported(D, ToIface);
3196
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003197 if (D->hasDefinition()) {
3198 if (!ToIface->hasDefinition())
3199 ToIface->startDefinition();
Douglas Gregora12d2942010-02-16 01:20:57 +00003200
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003201 if (D->getSuperClass()) {
3202 ObjCInterfaceDecl *Super
3203 = cast_or_null<ObjCInterfaceDecl>(
3204 Importer.Import(D->getSuperClass()));
3205 if (!Super)
3206 return 0;
3207
3208 ToIface->setSuperClass(Super);
3209 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3210 }
3211
3212 // Import protocols
3213 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3214 SmallVector<SourceLocation, 4> ProtocolLocs;
3215 ObjCInterfaceDecl::protocol_loc_iterator
3216 FromProtoLoc = D->protocol_loc_begin();
3217
3218 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3219 FromProtoEnd = D->protocol_end();
3220 FromProto != FromProtoEnd;
3221 ++FromProto, ++FromProtoLoc) {
3222 ObjCProtocolDecl *ToProto
3223 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3224 if (!ToProto)
3225 return 0;
3226 Protocols.push_back(ToProto);
3227 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3228 }
3229
3230 // FIXME: If we're merging, make sure that the protocol list is the same.
3231 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3232 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregora12d2942010-02-16 01:20:57 +00003233 }
3234
Douglas Gregora12d2942010-02-16 01:20:57 +00003235 // Import @end range
3236 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3237 } else {
3238 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003239
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003240 if (D->hasDefinition()) {
3241 // Check for consistency of superclasses.
3242 DeclarationName FromSuperName, ToSuperName;
3243
3244 // If the superclass hasn't been imported yet, do so before checking.
3245 ObjCInterfaceDecl *DSuperClass = D->getSuperClass();
3246 ObjCInterfaceDecl *ToIfaceSuperClass = ToIface->getSuperClass();
3247
3248 if (DSuperClass && !ToIfaceSuperClass) {
3249 Decl *ImportedSuperClass = Importer.Import(DSuperClass);
3250 ObjCInterfaceDecl *ImportedSuperIface
3251 = cast<ObjCInterfaceDecl>(ImportedSuperClass);
3252
3253 ToIface->setSuperClass(ImportedSuperIface);
3254 }
Sean Callananb1ce7302011-11-11 17:39:52 +00003255
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003256 if (D->getSuperClass())
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003257 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3258 if (ToIface->getSuperClass())
3259 ToSuperName = ToIface->getSuperClass()->getDeclName();
3260 if (FromSuperName != ToSuperName) {
3261 Importer.ToDiag(ToIface->getLocation(),
3262 diag::err_odr_objc_superclass_inconsistent)
3263 << ToIface->getDeclName();
3264 if (ToIface->getSuperClass())
3265 Importer.ToDiag(ToIface->getSuperClassLoc(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003266 diag::note_odr_objc_superclass)
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003267 << ToIface->getSuperClass()->getDeclName();
3268 else
3269 Importer.ToDiag(ToIface->getLocation(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003270 diag::note_odr_objc_missing_superclass);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003271 if (D->getSuperClass())
3272 Importer.FromDiag(D->getSuperClassLoc(),
3273 diag::note_odr_objc_superclass)
3274 << D->getSuperClass()->getDeclName();
3275 else
3276 Importer.FromDiag(D->getLocation(),
3277 diag::note_odr_objc_missing_superclass);
3278 return 0;
3279 }
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003280 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003281 }
3282
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003283 if (!D->hasDefinition())
3284 return ToIface;
3285
Douglas Gregorb4677b62010-02-18 01:47:50 +00003286 // Import categories. When the categories themselves are imported, they'll
3287 // hook themselves into this interface.
3288 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3289 FromCat = FromCat->getNextClassCategory())
3290 Importer.Import(FromCat);
3291
Douglas Gregora12d2942010-02-16 01:20:57 +00003292 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003293 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003294
3295 // If we have an @implementation, import it as well.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003296 if ( D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003297 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3298 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003299 if (!Impl)
3300 return 0;
3301
3302 ToIface->setImplementation(Impl);
3303 }
3304
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003305 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003306}
3307
Douglas Gregor3daef292010-12-07 15:32:12 +00003308Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3309 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3310 Importer.Import(D->getCategoryDecl()));
3311 if (!Category)
3312 return 0;
3313
3314 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3315 if (!ToImpl) {
3316 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3317 if (!DC)
3318 return 0;
3319
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003320 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003321 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003322 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003323 Category->getClassInterface(),
3324 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003325 Importer.Import(D->getAtStartLoc()),
3326 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003327
3328 DeclContext *LexicalDC = DC;
3329 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3330 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3331 if (!LexicalDC)
3332 return 0;
3333
3334 ToImpl->setLexicalDeclContext(LexicalDC);
3335 }
3336
Sean Callanan9faf8102011-10-21 02:57:43 +00003337 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003338 Category->setImplementation(ToImpl);
3339 }
3340
3341 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003342 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003343 return ToImpl;
3344}
3345
Douglas Gregordd182ff2010-12-07 01:26:03 +00003346Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3347 // Find the corresponding interface.
3348 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3349 Importer.Import(D->getClassInterface()));
3350 if (!Iface)
3351 return 0;
3352
3353 // Import the superclass, if any.
3354 ObjCInterfaceDecl *Super = 0;
3355 if (D->getSuperClass()) {
3356 Super = cast_or_null<ObjCInterfaceDecl>(
3357 Importer.Import(D->getSuperClass()));
3358 if (!Super)
3359 return 0;
3360 }
3361
3362 ObjCImplementationDecl *Impl = Iface->getImplementation();
3363 if (!Impl) {
3364 // We haven't imported an implementation yet. Create a new @implementation
3365 // now.
3366 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3367 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003368 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003369 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003370 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003371
3372 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3373 DeclContext *LexicalDC
3374 = Importer.ImportContext(D->getLexicalDeclContext());
3375 if (!LexicalDC)
3376 return 0;
3377 Impl->setLexicalDeclContext(LexicalDC);
3378 }
3379
3380 // Associate the implementation with the class it implements.
3381 Iface->setImplementation(Impl);
3382 Importer.Imported(D, Iface->getImplementation());
3383 } else {
3384 Importer.Imported(D, Iface->getImplementation());
3385
3386 // Verify that the existing @implementation has the same superclass.
3387 if ((Super && !Impl->getSuperClass()) ||
3388 (!Super && Impl->getSuperClass()) ||
3389 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003390 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003391 Importer.ToDiag(Impl->getLocation(),
3392 diag::err_odr_objc_superclass_inconsistent)
3393 << Iface->getDeclName();
3394 // FIXME: It would be nice to have the location of the superclass
3395 // below.
3396 if (Impl->getSuperClass())
3397 Importer.ToDiag(Impl->getLocation(),
3398 diag::note_odr_objc_superclass)
3399 << Impl->getSuperClass()->getDeclName();
3400 else
3401 Importer.ToDiag(Impl->getLocation(),
3402 diag::note_odr_objc_missing_superclass);
3403 if (D->getSuperClass())
3404 Importer.FromDiag(D->getLocation(),
3405 diag::note_odr_objc_superclass)
3406 << D->getSuperClass()->getDeclName();
3407 else
3408 Importer.FromDiag(D->getLocation(),
3409 diag::note_odr_objc_missing_superclass);
3410 return 0;
3411 }
3412 }
3413
3414 // Import all of the members of this @implementation.
3415 ImportDeclContext(D);
3416
3417 return Impl;
3418}
3419
Douglas Gregore3261622010-02-17 18:02:10 +00003420Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3421 // Import the major distinguishing characteristics of an @property.
3422 DeclContext *DC, *LexicalDC;
3423 DeclarationName Name;
3424 SourceLocation Loc;
3425 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3426 return 0;
3427
3428 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003429 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3430 DC->localUncachedLookup(Name, FoundDecls);
3431 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003432 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003433 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003434 // Check property types.
3435 if (!Importer.IsStructurallyEquivalent(D->getType(),
3436 FoundProp->getType())) {
3437 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3438 << Name << D->getType() << FoundProp->getType();
3439 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3440 << FoundProp->getType();
3441 return 0;
3442 }
3443
3444 // FIXME: Check property attributes, getters, setters, etc.?
3445
3446 // Consider these properties to be equivalent.
3447 Importer.Imported(D, FoundProp);
3448 return FoundProp;
3449 }
3450 }
3451
3452 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003453 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3454 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003455 return 0;
3456
3457 // Create the new property.
3458 ObjCPropertyDecl *ToProperty
3459 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3460 Name.getAsIdentifierInfo(),
3461 Importer.Import(D->getAtLoc()),
3462 T,
3463 D->getPropertyImplementation());
3464 Importer.Imported(D, ToProperty);
3465 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003466 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003467
3468 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003469 ToProperty->setPropertyAttributesAsWritten(
3470 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003471 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3472 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3473 ToProperty->setGetterMethodDecl(
3474 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3475 ToProperty->setSetterMethodDecl(
3476 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3477 ToProperty->setPropertyIvarDecl(
3478 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3479 return ToProperty;
3480}
3481
Douglas Gregor954e0c72010-12-07 18:32:03 +00003482Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3483 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3484 Importer.Import(D->getPropertyDecl()));
3485 if (!Property)
3486 return 0;
3487
3488 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3489 if (!DC)
3490 return 0;
3491
3492 // Import the lexical declaration context.
3493 DeclContext *LexicalDC = DC;
3494 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3495 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3496 if (!LexicalDC)
3497 return 0;
3498 }
3499
3500 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3501 if (!InImpl)
3502 return 0;
3503
3504 // Import the ivar (for an @synthesize).
3505 ObjCIvarDecl *Ivar = 0;
3506 if (D->getPropertyIvarDecl()) {
3507 Ivar = cast_or_null<ObjCIvarDecl>(
3508 Importer.Import(D->getPropertyIvarDecl()));
3509 if (!Ivar)
3510 return 0;
3511 }
3512
3513 ObjCPropertyImplDecl *ToImpl
3514 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3515 if (!ToImpl) {
3516 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3517 Importer.Import(D->getLocStart()),
3518 Importer.Import(D->getLocation()),
3519 Property,
3520 D->getPropertyImplementation(),
3521 Ivar,
3522 Importer.Import(D->getPropertyIvarDeclLoc()));
3523 ToImpl->setLexicalDeclContext(LexicalDC);
3524 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003525 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003526 } else {
3527 // Check that we have the same kind of property implementation (@synthesize
3528 // vs. @dynamic).
3529 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3530 Importer.ToDiag(ToImpl->getLocation(),
3531 diag::err_odr_objc_property_impl_kind_inconsistent)
3532 << Property->getDeclName()
3533 << (ToImpl->getPropertyImplementation()
3534 == ObjCPropertyImplDecl::Dynamic);
3535 Importer.FromDiag(D->getLocation(),
3536 diag::note_odr_objc_property_impl_kind)
3537 << D->getPropertyDecl()->getDeclName()
3538 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3539 return 0;
3540 }
3541
3542 // For @synthesize, check that we have the same
3543 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3544 Ivar != ToImpl->getPropertyIvarDecl()) {
3545 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3546 diag::err_odr_objc_synthesize_ivar_inconsistent)
3547 << Property->getDeclName()
3548 << ToImpl->getPropertyIvarDecl()->getDeclName()
3549 << Ivar->getDeclName();
3550 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3551 diag::note_odr_objc_synthesize_ivar_here)
3552 << D->getPropertyIvarDecl()->getDeclName();
3553 return 0;
3554 }
3555
3556 // Merge the existing implementation with the new implementation.
3557 Importer.Imported(D, ToImpl);
3558 }
3559
3560 return ToImpl;
3561}
3562
Douglas Gregor040afae2010-11-30 19:14:50 +00003563Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3564 // For template arguments, we adopt the translation unit as our declaration
3565 // context. This context will be fixed when the actual template declaration
3566 // is created.
3567
3568 // FIXME: Import default argument.
3569 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3570 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003571 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003572 Importer.Import(D->getLocation()),
3573 D->getDepth(),
3574 D->getIndex(),
3575 Importer.Import(D->getIdentifier()),
3576 D->wasDeclaredWithTypename(),
3577 D->isParameterPack());
3578}
3579
3580Decl *
3581ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3582 // Import the name of this declaration.
3583 DeclarationName Name = Importer.Import(D->getDeclName());
3584 if (D->getDeclName() && !Name)
3585 return 0;
3586
3587 // Import the location of this declaration.
3588 SourceLocation Loc = Importer.Import(D->getLocation());
3589
3590 // Import the type of this declaration.
3591 QualType T = Importer.Import(D->getType());
3592 if (T.isNull())
3593 return 0;
3594
3595 // Import type-source information.
3596 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3597 if (D->getTypeSourceInfo() && !TInfo)
3598 return 0;
3599
3600 // FIXME: Import default argument.
3601
3602 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3603 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003604 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003605 Loc, D->getDepth(), D->getPosition(),
3606 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003607 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003608}
3609
3610Decl *
3611ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3612 // Import the name of this declaration.
3613 DeclarationName Name = Importer.Import(D->getDeclName());
3614 if (D->getDeclName() && !Name)
3615 return 0;
3616
3617 // Import the location of this declaration.
3618 SourceLocation Loc = Importer.Import(D->getLocation());
3619
3620 // Import template parameters.
3621 TemplateParameterList *TemplateParams
3622 = ImportTemplateParameterList(D->getTemplateParameters());
3623 if (!TemplateParams)
3624 return 0;
3625
3626 // FIXME: Import default argument.
3627
3628 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3629 Importer.getToContext().getTranslationUnitDecl(),
3630 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003631 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003632 Name.getAsIdentifierInfo(),
3633 TemplateParams);
3634}
3635
3636Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3637 // If this record has a definition in the translation unit we're coming from,
3638 // but this particular declaration is not that definition, import the
3639 // definition and map to that.
3640 CXXRecordDecl *Definition
3641 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3642 if (Definition && Definition != D->getTemplatedDecl()) {
3643 Decl *ImportedDef
3644 = Importer.Import(Definition->getDescribedClassTemplate());
3645 if (!ImportedDef)
3646 return 0;
3647
3648 return Importer.Imported(D, ImportedDef);
3649 }
3650
3651 // Import the major distinguishing characteristics of this class template.
3652 DeclContext *DC, *LexicalDC;
3653 DeclarationName Name;
3654 SourceLocation Loc;
3655 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3656 return 0;
3657
3658 // We may already have a template of the same name; try to find and match it.
3659 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003660 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003661 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3662 DC->localUncachedLookup(Name, FoundDecls);
3663 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3664 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003665 continue;
3666
Douglas Gregorb75a3452011-10-15 00:10:27 +00003667 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003668 if (ClassTemplateDecl *FoundTemplate
3669 = dyn_cast<ClassTemplateDecl>(Found)) {
3670 if (IsStructuralMatch(D, FoundTemplate)) {
3671 // The class templates structurally match; call it the same template.
3672 // FIXME: We may be filling in a forward declaration here. Handle
3673 // this case!
3674 Importer.Imported(D->getTemplatedDecl(),
3675 FoundTemplate->getTemplatedDecl());
3676 return Importer.Imported(D, FoundTemplate);
3677 }
3678 }
3679
Douglas Gregorb75a3452011-10-15 00:10:27 +00003680 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003681 }
3682
3683 if (!ConflictingDecls.empty()) {
3684 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3685 ConflictingDecls.data(),
3686 ConflictingDecls.size());
3687 }
3688
3689 if (!Name)
3690 return 0;
3691 }
3692
3693 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3694
3695 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003696 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3697 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003698 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3699 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003700 DC, StartLoc, IdLoc,
3701 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003702 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003703 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003704 D2Templated->setLexicalDeclContext(LexicalDC);
3705
3706 // Create the class template declaration itself.
3707 TemplateParameterList *TemplateParams
3708 = ImportTemplateParameterList(D->getTemplateParameters());
3709 if (!TemplateParams)
3710 return 0;
3711
3712 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3713 Loc, Name, TemplateParams,
3714 D2Templated,
3715 /*PrevDecl=*/0);
3716 D2Templated->setDescribedClassTemplate(D2);
3717
3718 D2->setAccess(D->getAccess());
3719 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003720 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003721
3722 // Note the relationship between the class templates.
3723 Importer.Imported(D, D2);
3724 Importer.Imported(DTemplated, D2Templated);
3725
John McCall5e1cdac2011-10-07 06:10:15 +00003726 if (DTemplated->isCompleteDefinition() &&
3727 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003728 // FIXME: Import definition!
3729 }
3730
3731 return D2;
3732}
3733
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003734Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3735 ClassTemplateSpecializationDecl *D) {
3736 // If this record has a definition in the translation unit we're coming from,
3737 // but this particular declaration is not that definition, import the
3738 // definition and map to that.
3739 TagDecl *Definition = D->getDefinition();
3740 if (Definition && Definition != D) {
3741 Decl *ImportedDef = Importer.Import(Definition);
3742 if (!ImportedDef)
3743 return 0;
3744
3745 return Importer.Imported(D, ImportedDef);
3746 }
3747
3748 ClassTemplateDecl *ClassTemplate
3749 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3750 D->getSpecializedTemplate()));
3751 if (!ClassTemplate)
3752 return 0;
3753
3754 // Import the context of this declaration.
3755 DeclContext *DC = ClassTemplate->getDeclContext();
3756 if (!DC)
3757 return 0;
3758
3759 DeclContext *LexicalDC = DC;
3760 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3761 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3762 if (!LexicalDC)
3763 return 0;
3764 }
3765
3766 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003767 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3768 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003769
3770 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003771 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003772 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3773 D->getTemplateArgs().size(),
3774 TemplateArgs))
3775 return 0;
3776
3777 // Try to find an existing specialization with these template arguments.
3778 void *InsertPos = 0;
3779 ClassTemplateSpecializationDecl *D2
3780 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3781 TemplateArgs.size(), InsertPos);
3782 if (D2) {
3783 // We already have a class template specialization with these template
3784 // arguments.
3785
3786 // FIXME: Check for specialization vs. instantiation errors.
3787
3788 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003789 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003790 // The record types structurally match, or the "from" translation
3791 // unit only had a forward declaration anyway; call it the same
3792 // function.
3793 return Importer.Imported(D, FoundDef);
3794 }
3795 }
3796 } else {
3797 // Create a new specialization.
3798 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3799 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003800 StartLoc, IdLoc,
3801 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003802 TemplateArgs.data(),
3803 TemplateArgs.size(),
3804 /*PrevDecl=*/0);
3805 D2->setSpecializationKind(D->getSpecializationKind());
3806
3807 // Add this specialization to the class template.
3808 ClassTemplate->AddSpecialization(D2, InsertPos);
3809
3810 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003811 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003812
3813 // Add the specialization to this context.
3814 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003815 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003816 }
3817 Importer.Imported(D, D2);
3818
John McCall5e1cdac2011-10-07 06:10:15 +00003819 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003820 return 0;
3821
3822 return D2;
3823}
3824
Douglas Gregor4800d952010-02-11 19:21:55 +00003825//----------------------------------------------------------------------------
3826// Import Statements
3827//----------------------------------------------------------------------------
3828
3829Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3830 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3831 << S->getStmtClassName();
3832 return 0;
3833}
3834
3835//----------------------------------------------------------------------------
3836// Import Expressions
3837//----------------------------------------------------------------------------
3838Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3839 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3840 << E->getStmtClassName();
3841 return 0;
3842}
3843
Douglas Gregor44080632010-02-19 01:17:02 +00003844Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003845 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3846 if (!ToD)
3847 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003848
3849 NamedDecl *FoundD = 0;
3850 if (E->getDecl() != E->getFoundDecl()) {
3851 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3852 if (!FoundD)
3853 return 0;
3854 }
Douglas Gregor44080632010-02-19 01:17:02 +00003855
3856 QualType T = Importer.Import(E->getType());
3857 if (T.isNull())
3858 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003859
3860 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3861 Importer.Import(E->getQualifierLoc()),
3862 ToD,
3863 Importer.Import(E->getLocation()),
3864 T, E->getValueKind(),
3865 FoundD,
3866 /*FIXME:TemplateArgs=*/0);
3867 if (E->hadMultipleCandidates())
3868 DRE->setHadMultipleCandidates(true);
3869 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003870}
3871
Douglas Gregor4800d952010-02-11 19:21:55 +00003872Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3873 QualType T = Importer.Import(E->getType());
3874 if (T.isNull())
3875 return 0;
3876
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003877 return IntegerLiteral::Create(Importer.getToContext(),
3878 E->getValue(), T,
3879 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003880}
3881
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003882Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3883 QualType T = Importer.Import(E->getType());
3884 if (T.isNull())
3885 return 0;
3886
Douglas Gregor5cee1192011-07-27 05:40:30 +00003887 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3888 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003889 Importer.Import(E->getLocation()));
3890}
3891
Douglas Gregorf638f952010-02-19 01:07:06 +00003892Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3893 Expr *SubExpr = Importer.Import(E->getSubExpr());
3894 if (!SubExpr)
3895 return 0;
3896
3897 return new (Importer.getToContext())
3898 ParenExpr(Importer.Import(E->getLParen()),
3899 Importer.Import(E->getRParen()),
3900 SubExpr);
3901}
3902
3903Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3904 QualType T = Importer.Import(E->getType());
3905 if (T.isNull())
3906 return 0;
3907
3908 Expr *SubExpr = Importer.Import(E->getSubExpr());
3909 if (!SubExpr)
3910 return 0;
3911
3912 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003913 T, E->getValueKind(),
3914 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003915 Importer.Import(E->getOperatorLoc()));
3916}
3917
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003918Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3919 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003920 QualType ResultType = Importer.Import(E->getType());
3921
3922 if (E->isArgumentType()) {
3923 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3924 if (!TInfo)
3925 return 0;
3926
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003927 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3928 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003929 Importer.Import(E->getOperatorLoc()),
3930 Importer.Import(E->getRParenLoc()));
3931 }
3932
3933 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3934 if (!SubExpr)
3935 return 0;
3936
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003937 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3938 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003939 Importer.Import(E->getOperatorLoc()),
3940 Importer.Import(E->getRParenLoc()));
3941}
3942
Douglas Gregorf638f952010-02-19 01:07:06 +00003943Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3944 QualType T = Importer.Import(E->getType());
3945 if (T.isNull())
3946 return 0;
3947
3948 Expr *LHS = Importer.Import(E->getLHS());
3949 if (!LHS)
3950 return 0;
3951
3952 Expr *RHS = Importer.Import(E->getRHS());
3953 if (!RHS)
3954 return 0;
3955
3956 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003957 T, E->getValueKind(),
3958 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003959 Importer.Import(E->getOperatorLoc()));
3960}
3961
3962Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3963 QualType T = Importer.Import(E->getType());
3964 if (T.isNull())
3965 return 0;
3966
3967 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3968 if (CompLHSType.isNull())
3969 return 0;
3970
3971 QualType CompResultType = Importer.Import(E->getComputationResultType());
3972 if (CompResultType.isNull())
3973 return 0;
3974
3975 Expr *LHS = Importer.Import(E->getLHS());
3976 if (!LHS)
3977 return 0;
3978
3979 Expr *RHS = Importer.Import(E->getRHS());
3980 if (!RHS)
3981 return 0;
3982
3983 return new (Importer.getToContext())
3984 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003985 T, E->getValueKind(),
3986 E->getObjectKind(),
3987 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003988 Importer.Import(E->getOperatorLoc()));
3989}
3990
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003991static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003992 if (E->path_empty()) return false;
3993
3994 // TODO: import cast paths
3995 return true;
3996}
3997
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003998Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3999 QualType T = Importer.Import(E->getType());
4000 if (T.isNull())
4001 return 0;
4002
4003 Expr *SubExpr = Importer.Import(E->getSubExpr());
4004 if (!SubExpr)
4005 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004006
4007 CXXCastPath BasePath;
4008 if (ImportCastPath(E, BasePath))
4009 return 0;
4010
4011 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004012 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004013}
4014
Douglas Gregor008847a2010-02-19 01:32:14 +00004015Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4016 QualType T = Importer.Import(E->getType());
4017 if (T.isNull())
4018 return 0;
4019
4020 Expr *SubExpr = Importer.Import(E->getSubExpr());
4021 if (!SubExpr)
4022 return 0;
4023
4024 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4025 if (!TInfo && E->getTypeInfoAsWritten())
4026 return 0;
4027
John McCallf871d0c2010-08-07 06:22:56 +00004028 CXXCastPath BasePath;
4029 if (ImportCastPath(E, BasePath))
4030 return 0;
4031
John McCallf89e55a2010-11-18 06:31:45 +00004032 return CStyleCastExpr::Create(Importer.getToContext(), T,
4033 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004034 SubExpr, &BasePath, TInfo,
4035 Importer.Import(E->getLParenLoc()),
4036 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004037}
4038
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004039ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004040 ASTContext &FromContext, FileManager &FromFileManager,
4041 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004042 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004043 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4044 Minimal(MinimalImport)
4045{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004046 ImportedDecls[FromContext.getTranslationUnitDecl()]
4047 = ToContext.getTranslationUnitDecl();
4048}
4049
4050ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004051
4052QualType ASTImporter::Import(QualType FromT) {
4053 if (FromT.isNull())
4054 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004055
4056 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004057
Douglas Gregor169fba52010-02-08 15:18:58 +00004058 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004059 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4060 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004061 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004062 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004063
Douglas Gregor169fba52010-02-08 15:18:58 +00004064 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004065 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004066 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004067 if (ToT.isNull())
4068 return ToT;
4069
Douglas Gregor169fba52010-02-08 15:18:58 +00004070 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004071 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004072
John McCallf4c73712011-01-19 06:33:43 +00004073 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004074}
4075
Douglas Gregor9bed8792010-02-09 19:21:46 +00004076TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004077 if (!FromTSI)
4078 return FromTSI;
4079
4080 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004081 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004082 QualType T = Import(FromTSI->getType());
4083 if (T.isNull())
4084 return 0;
4085
4086 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004087 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004088}
4089
4090Decl *ASTImporter::Import(Decl *FromD) {
4091 if (!FromD)
4092 return 0;
4093
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004094 ASTNodeImporter Importer(*this);
4095
Douglas Gregor9bed8792010-02-09 19:21:46 +00004096 // Check whether we've already imported this declaration.
4097 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004098 if (Pos != ImportedDecls.end()) {
4099 Decl *ToD = Pos->second;
4100 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4101 return ToD;
4102 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004103
4104 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004105 Decl *ToD = Importer.Visit(FromD);
4106 if (!ToD)
4107 return 0;
4108
4109 // Record the imported declaration.
4110 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004111
4112 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4113 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004114 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004115 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004116 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004117 // When we've finished transforming a typedef, see whether it was the
4118 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004119 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004120 FromTag = AnonTagsWithPendingTypedefs.begin(),
4121 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4122 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004123 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004124 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4125 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004126 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004127 AnonTagsWithPendingTypedefs.erase(FromTag);
4128 break;
4129 }
4130 }
4131 }
4132 }
4133
Douglas Gregor9bed8792010-02-09 19:21:46 +00004134 return ToD;
4135}
4136
4137DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4138 if (!FromDC)
4139 return FromDC;
4140
4141 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4142}
4143
4144Expr *ASTImporter::Import(Expr *FromE) {
4145 if (!FromE)
4146 return 0;
4147
4148 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4149}
4150
4151Stmt *ASTImporter::Import(Stmt *FromS) {
4152 if (!FromS)
4153 return 0;
4154
Douglas Gregor4800d952010-02-11 19:21:55 +00004155 // Check whether we've already imported this declaration.
4156 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4157 if (Pos != ImportedStmts.end())
4158 return Pos->second;
4159
4160 // Import the type
4161 ASTNodeImporter Importer(*this);
4162 Stmt *ToS = Importer.Visit(FromS);
4163 if (!ToS)
4164 return 0;
4165
4166 // Record the imported declaration.
4167 ImportedStmts[FromS] = ToS;
4168 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004169}
4170
4171NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4172 if (!FromNNS)
4173 return 0;
4174
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004175 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4176
4177 switch (FromNNS->getKind()) {
4178 case NestedNameSpecifier::Identifier:
4179 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4180 return NestedNameSpecifier::Create(ToContext, prefix, II);
4181 }
4182 return 0;
4183
4184 case NestedNameSpecifier::Namespace:
4185 if (NamespaceDecl *NS =
4186 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4187 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4188 }
4189 return 0;
4190
4191 case NestedNameSpecifier::NamespaceAlias:
4192 if (NamespaceAliasDecl *NSAD =
4193 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4194 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4195 }
4196 return 0;
4197
4198 case NestedNameSpecifier::Global:
4199 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4200
4201 case NestedNameSpecifier::TypeSpec:
4202 case NestedNameSpecifier::TypeSpecWithTemplate: {
4203 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4204 if (!T.isNull()) {
4205 bool bTemplate = FromNNS->getKind() ==
4206 NestedNameSpecifier::TypeSpecWithTemplate;
4207 return NestedNameSpecifier::Create(ToContext, prefix,
4208 bTemplate, T.getTypePtr());
4209 }
4210 }
4211 return 0;
4212 }
4213
4214 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004215}
4216
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004217NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4218 // FIXME: Implement!
4219 return NestedNameSpecifierLoc();
4220}
4221
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004222TemplateName ASTImporter::Import(TemplateName From) {
4223 switch (From.getKind()) {
4224 case TemplateName::Template:
4225 if (TemplateDecl *ToTemplate
4226 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4227 return TemplateName(ToTemplate);
4228
4229 return TemplateName();
4230
4231 case TemplateName::OverloadedTemplate: {
4232 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4233 UnresolvedSet<2> ToTemplates;
4234 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4235 E = FromStorage->end();
4236 I != E; ++I) {
4237 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4238 ToTemplates.addDecl(To);
4239 else
4240 return TemplateName();
4241 }
4242 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4243 ToTemplates.end());
4244 }
4245
4246 case TemplateName::QualifiedTemplate: {
4247 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4248 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4249 if (!Qualifier)
4250 return TemplateName();
4251
4252 if (TemplateDecl *ToTemplate
4253 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4254 return ToContext.getQualifiedTemplateName(Qualifier,
4255 QTN->hasTemplateKeyword(),
4256 ToTemplate);
4257
4258 return TemplateName();
4259 }
4260
4261 case TemplateName::DependentTemplate: {
4262 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4263 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4264 if (!Qualifier)
4265 return TemplateName();
4266
4267 if (DTN->isIdentifier()) {
4268 return ToContext.getDependentTemplateName(Qualifier,
4269 Import(DTN->getIdentifier()));
4270 }
4271
4272 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4273 }
John McCall14606042011-06-30 08:33:18 +00004274
4275 case TemplateName::SubstTemplateTemplateParm: {
4276 SubstTemplateTemplateParmStorage *subst
4277 = From.getAsSubstTemplateTemplateParm();
4278 TemplateTemplateParmDecl *param
4279 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4280 if (!param)
4281 return TemplateName();
4282
4283 TemplateName replacement = Import(subst->getReplacement());
4284 if (replacement.isNull()) return TemplateName();
4285
4286 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4287 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004288
4289 case TemplateName::SubstTemplateTemplateParmPack: {
4290 SubstTemplateTemplateParmPackStorage *SubstPack
4291 = From.getAsSubstTemplateTemplateParmPack();
4292 TemplateTemplateParmDecl *Param
4293 = cast_or_null<TemplateTemplateParmDecl>(
4294 Import(SubstPack->getParameterPack()));
4295 if (!Param)
4296 return TemplateName();
4297
4298 ASTNodeImporter Importer(*this);
4299 TemplateArgument ArgPack
4300 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4301 if (ArgPack.isNull())
4302 return TemplateName();
4303
4304 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4305 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004306 }
4307
4308 llvm_unreachable("Invalid template name kind");
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004309}
4310
Douglas Gregor9bed8792010-02-09 19:21:46 +00004311SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4312 if (FromLoc.isInvalid())
4313 return SourceLocation();
4314
Douglas Gregor88523732010-02-10 00:15:17 +00004315 SourceManager &FromSM = FromContext.getSourceManager();
4316
4317 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004318 // don't have to import macro expansions.
4319 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004320 FromLoc = FromSM.getSpellingLoc(FromLoc);
4321 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4322 SourceManager &ToSM = ToContext.getSourceManager();
4323 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004324 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004325}
4326
4327SourceRange ASTImporter::Import(SourceRange FromRange) {
4328 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4329}
4330
Douglas Gregor88523732010-02-10 00:15:17 +00004331FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004332 llvm::DenseMap<FileID, FileID>::iterator Pos
4333 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004334 if (Pos != ImportedFileIDs.end())
4335 return Pos->second;
4336
4337 SourceManager &FromSM = FromContext.getSourceManager();
4338 SourceManager &ToSM = ToContext.getSourceManager();
4339 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004340 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004341
4342 // Include location of this file.
4343 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4344
4345 // Map the FileID for to the "to" source manager.
4346 FileID ToID;
4347 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004348 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004349 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4350 // disk again
4351 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4352 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004353 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004354 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4355 FromSLoc.getFile().getFileCharacteristic());
4356 } else {
4357 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004358 const llvm::MemoryBuffer *
4359 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004360 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004361 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004362 FromBuf->getBufferIdentifier());
4363 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4364 }
4365
4366
Sebastian Redl535a3e22010-09-30 01:03:06 +00004367 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004368 return ToID;
4369}
4370
Douglas Gregord8868a62011-01-18 03:11:38 +00004371void ASTImporter::ImportDefinition(Decl *From) {
4372 Decl *To = Import(From);
4373 if (!To)
4374 return;
4375
4376 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4377 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004378
4379 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4380 if (!ToRecord->getDefinition()) {
4381 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4382 /*ForceImport=*/true);
4383 return;
4384 }
4385 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004386
4387 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4388 if (!ToEnum->getDefinition()) {
4389 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4390 /*ForceImport=*/true);
4391 return;
4392 }
4393 }
4394
Douglas Gregord8868a62011-01-18 03:11:38 +00004395 Importer.ImportDeclContext(FromDC, true);
4396 }
4397}
4398
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004399DeclarationName ASTImporter::Import(DeclarationName FromName) {
4400 if (!FromName)
4401 return DeclarationName();
4402
4403 switch (FromName.getNameKind()) {
4404 case DeclarationName::Identifier:
4405 return Import(FromName.getAsIdentifierInfo());
4406
4407 case DeclarationName::ObjCZeroArgSelector:
4408 case DeclarationName::ObjCOneArgSelector:
4409 case DeclarationName::ObjCMultiArgSelector:
4410 return Import(FromName.getObjCSelector());
4411
4412 case DeclarationName::CXXConstructorName: {
4413 QualType T = Import(FromName.getCXXNameType());
4414 if (T.isNull())
4415 return DeclarationName();
4416
4417 return ToContext.DeclarationNames.getCXXConstructorName(
4418 ToContext.getCanonicalType(T));
4419 }
4420
4421 case DeclarationName::CXXDestructorName: {
4422 QualType T = Import(FromName.getCXXNameType());
4423 if (T.isNull())
4424 return DeclarationName();
4425
4426 return ToContext.DeclarationNames.getCXXDestructorName(
4427 ToContext.getCanonicalType(T));
4428 }
4429
4430 case DeclarationName::CXXConversionFunctionName: {
4431 QualType T = Import(FromName.getCXXNameType());
4432 if (T.isNull())
4433 return DeclarationName();
4434
4435 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4436 ToContext.getCanonicalType(T));
4437 }
4438
4439 case DeclarationName::CXXOperatorName:
4440 return ToContext.DeclarationNames.getCXXOperatorName(
4441 FromName.getCXXOverloadedOperator());
4442
4443 case DeclarationName::CXXLiteralOperatorName:
4444 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4445 Import(FromName.getCXXLiteralIdentifier()));
4446
4447 case DeclarationName::CXXUsingDirective:
4448 // FIXME: STATICS!
4449 return DeclarationName::getUsingDirectiveName();
4450 }
4451
David Blaikie30263482012-01-20 21:50:17 +00004452 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004453}
4454
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004455IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004456 if (!FromId)
4457 return 0;
4458
4459 return &ToContext.Idents.get(FromId->getName());
4460}
Douglas Gregor089459a2010-02-08 21:09:39 +00004461
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004462Selector ASTImporter::Import(Selector FromSel) {
4463 if (FromSel.isNull())
4464 return Selector();
4465
Chris Lattner5f9e2722011-07-23 10:55:15 +00004466 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004467 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4468 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4469 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4470 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4471}
4472
Douglas Gregor089459a2010-02-08 21:09:39 +00004473DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4474 DeclContext *DC,
4475 unsigned IDNS,
4476 NamedDecl **Decls,
4477 unsigned NumDecls) {
4478 return Name;
4479}
4480
4481DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004482 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004483}
4484
4485DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004486 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004487}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004488
4489Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4490 ImportedDecls[From] = To;
4491 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004492}
Douglas Gregorea35d112010-02-15 23:54:17 +00004493
4494bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004495 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004496 = ImportedTypes.find(From.getTypePtr());
4497 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4498 return true;
4499
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004500 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004501 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004502}