blob: 10f5a206d5b2fbaad79147feb4f51a97cb98e049 [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 return true;
336}
337
338/// \brief Determine structural equivalence for the common part of array
339/// types.
340static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
341 const ArrayType *Array1,
342 const ArrayType *Array2) {
343 if (!IsStructurallyEquivalent(Context,
344 Array1->getElementType(),
345 Array2->getElementType()))
346 return false;
347 if (Array1->getSizeModifier() != Array2->getSizeModifier())
348 return false;
349 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
350 return false;
351
352 return true;
353}
354
355/// \brief Determine structural equivalence of two types.
356static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
357 QualType T1, QualType T2) {
358 if (T1.isNull() || T2.isNull())
359 return T1.isNull() && T2.isNull();
360
361 if (!Context.StrictTypeSpelling) {
362 // We aren't being strict about token-to-token equivalence of types,
363 // so map down to the canonical type.
364 T1 = Context.C1.getCanonicalType(T1);
365 T2 = Context.C2.getCanonicalType(T2);
366 }
367
368 if (T1.getQualifiers() != T2.getQualifiers())
369 return false;
370
Douglas Gregorea35d112010-02-15 23:54:17 +0000371 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 if (T1->getTypeClass() != T2->getTypeClass()) {
374 // Compare function types with prototypes vs. without prototypes as if
375 // both did not have prototypes.
376 if (T1->getTypeClass() == Type::FunctionProto &&
377 T2->getTypeClass() == Type::FunctionNoProto)
378 TC = Type::FunctionNoProto;
379 else if (T1->getTypeClass() == Type::FunctionNoProto &&
380 T2->getTypeClass() == Type::FunctionProto)
381 TC = Type::FunctionNoProto;
382 else
383 return false;
384 }
385
386 switch (TC) {
387 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000388 // FIXME: Deal with Char_S/Char_U.
389 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
390 return false;
391 break;
392
393 case Type::Complex:
394 if (!IsStructurallyEquivalent(Context,
395 cast<ComplexType>(T1)->getElementType(),
396 cast<ComplexType>(T2)->getElementType()))
397 return false;
398 break;
399
400 case Type::Pointer:
401 if (!IsStructurallyEquivalent(Context,
402 cast<PointerType>(T1)->getPointeeType(),
403 cast<PointerType>(T2)->getPointeeType()))
404 return false;
405 break;
406
407 case Type::BlockPointer:
408 if (!IsStructurallyEquivalent(Context,
409 cast<BlockPointerType>(T1)->getPointeeType(),
410 cast<BlockPointerType>(T2)->getPointeeType()))
411 return false;
412 break;
413
414 case Type::LValueReference:
415 case Type::RValueReference: {
416 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
417 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
418 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
419 return false;
420 if (Ref1->isInnerRef() != Ref2->isInnerRef())
421 return false;
422 if (!IsStructurallyEquivalent(Context,
423 Ref1->getPointeeTypeAsWritten(),
424 Ref2->getPointeeTypeAsWritten()))
425 return false;
426 break;
427 }
428
429 case Type::MemberPointer: {
430 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
431 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
432 if (!IsStructurallyEquivalent(Context,
433 MemPtr1->getPointeeType(),
434 MemPtr2->getPointeeType()))
435 return false;
436 if (!IsStructurallyEquivalent(Context,
437 QualType(MemPtr1->getClass(), 0),
438 QualType(MemPtr2->getClass(), 0)))
439 return false;
440 break;
441 }
442
443 case Type::ConstantArray: {
444 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
445 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
446 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
447 return false;
448
449 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
450 return false;
451 break;
452 }
453
454 case Type::IncompleteArray:
455 if (!IsArrayStructurallyEquivalent(Context,
456 cast<ArrayType>(T1),
457 cast<ArrayType>(T2)))
458 return false;
459 break;
460
461 case Type::VariableArray: {
462 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
463 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
464 if (!IsStructurallyEquivalent(Context,
465 Array1->getSizeExpr(), Array2->getSizeExpr()))
466 return false;
467
468 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
469 return false;
470
471 break;
472 }
473
474 case Type::DependentSizedArray: {
475 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
476 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
477 if (!IsStructurallyEquivalent(Context,
478 Array1->getSizeExpr(), Array2->getSizeExpr()))
479 return false;
480
481 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
482 return false;
483
484 break;
485 }
486
487 case Type::DependentSizedExtVector: {
488 const DependentSizedExtVectorType *Vec1
489 = cast<DependentSizedExtVectorType>(T1);
490 const DependentSizedExtVectorType *Vec2
491 = cast<DependentSizedExtVectorType>(T2);
492 if (!IsStructurallyEquivalent(Context,
493 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
494 return false;
495 if (!IsStructurallyEquivalent(Context,
496 Vec1->getElementType(),
497 Vec2->getElementType()))
498 return false;
499 break;
500 }
501
502 case Type::Vector:
503 case Type::ExtVector: {
504 const VectorType *Vec1 = cast<VectorType>(T1);
505 const VectorType *Vec2 = cast<VectorType>(T2);
506 if (!IsStructurallyEquivalent(Context,
507 Vec1->getElementType(),
508 Vec2->getElementType()))
509 return false;
510 if (Vec1->getNumElements() != Vec2->getNumElements())
511 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000512 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000513 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000514 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 }
516
517 case Type::FunctionProto: {
518 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
519 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
520 if (Proto1->getNumArgs() != Proto2->getNumArgs())
521 return false;
522 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
523 if (!IsStructurallyEquivalent(Context,
524 Proto1->getArgType(I),
525 Proto2->getArgType(I)))
526 return false;
527 }
528 if (Proto1->isVariadic() != Proto2->isVariadic())
529 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000530 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
533 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
534 return false;
535 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
536 if (!IsStructurallyEquivalent(Context,
537 Proto1->getExceptionType(I),
538 Proto2->getExceptionType(I)))
539 return false;
540 }
541 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000542 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000543 Proto1->getNoexceptExpr(),
544 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000545 return false;
546 }
547 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
548 return false;
549
550 // Fall through to check the bits common with FunctionNoProtoType.
551 }
552
553 case Type::FunctionNoProto: {
554 const FunctionType *Function1 = cast<FunctionType>(T1);
555 const FunctionType *Function2 = cast<FunctionType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 Function1->getResultType(),
558 Function2->getResultType()))
559 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000560 if (Function1->getExtInfo() != Function2->getExtInfo())
561 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000562 break;
563 }
564
565 case Type::UnresolvedUsing:
566 if (!IsStructurallyEquivalent(Context,
567 cast<UnresolvedUsingType>(T1)->getDecl(),
568 cast<UnresolvedUsingType>(T2)->getDecl()))
569 return false;
570
571 break;
John McCall9d156a72011-01-06 01:58:22 +0000572
573 case Type::Attributed:
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getModifiedType(),
576 cast<AttributedType>(T2)->getModifiedType()))
577 return false;
578 if (!IsStructurallyEquivalent(Context,
579 cast<AttributedType>(T1)->getEquivalentType(),
580 cast<AttributedType>(T2)->getEquivalentType()))
581 return false;
582 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000583
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000584 case Type::Paren:
585 if (!IsStructurallyEquivalent(Context,
586 cast<ParenType>(T1)->getInnerType(),
587 cast<ParenType>(T2)->getInnerType()))
588 return false;
589 break;
590
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000591 case Type::Typedef:
592 if (!IsStructurallyEquivalent(Context,
593 cast<TypedefType>(T1)->getDecl(),
594 cast<TypedefType>(T2)->getDecl()))
595 return false;
596 break;
597
598 case Type::TypeOfExpr:
599 if (!IsStructurallyEquivalent(Context,
600 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
601 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
602 return false;
603 break;
604
605 case Type::TypeOf:
606 if (!IsStructurallyEquivalent(Context,
607 cast<TypeOfType>(T1)->getUnderlyingType(),
608 cast<TypeOfType>(T2)->getUnderlyingType()))
609 return false;
610 break;
Sean Huntca63c202011-05-24 22:41:36 +0000611
612 case Type::UnaryTransform:
613 if (!IsStructurallyEquivalent(Context,
614 cast<UnaryTransformType>(T1)->getUnderlyingType(),
615 cast<UnaryTransformType>(T1)->getUnderlyingType()))
616 return false;
617 break;
618
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000619 case Type::Decltype:
620 if (!IsStructurallyEquivalent(Context,
621 cast<DecltypeType>(T1)->getUnderlyingExpr(),
622 cast<DecltypeType>(T2)->getUnderlyingExpr()))
623 return false;
624 break;
625
Richard Smith34b41d92011-02-20 03:19:35 +0000626 case Type::Auto:
627 if (!IsStructurallyEquivalent(Context,
628 cast<AutoType>(T1)->getDeducedType(),
629 cast<AutoType>(T2)->getDeducedType()))
630 return false;
631 break;
632
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000633 case Type::Record:
634 case Type::Enum:
635 if (!IsStructurallyEquivalent(Context,
636 cast<TagType>(T1)->getDecl(),
637 cast<TagType>(T2)->getDecl()))
638 return false;
639 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000640
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000641 case Type::TemplateTypeParm: {
642 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
643 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
644 if (Parm1->getDepth() != Parm2->getDepth())
645 return false;
646 if (Parm1->getIndex() != Parm2->getIndex())
647 return false;
648 if (Parm1->isParameterPack() != Parm2->isParameterPack())
649 return false;
650
651 // Names of template type parameters are never significant.
652 break;
653 }
654
655 case Type::SubstTemplateTypeParm: {
656 const SubstTemplateTypeParmType *Subst1
657 = cast<SubstTemplateTypeParmType>(T1);
658 const SubstTemplateTypeParmType *Subst2
659 = cast<SubstTemplateTypeParmType>(T2);
660 if (!IsStructurallyEquivalent(Context,
661 QualType(Subst1->getReplacedParameter(), 0),
662 QualType(Subst2->getReplacedParameter(), 0)))
663 return false;
664 if (!IsStructurallyEquivalent(Context,
665 Subst1->getReplacementType(),
666 Subst2->getReplacementType()))
667 return false;
668 break;
669 }
670
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000671 case Type::SubstTemplateTypeParmPack: {
672 const SubstTemplateTypeParmPackType *Subst1
673 = cast<SubstTemplateTypeParmPackType>(T1);
674 const SubstTemplateTypeParmPackType *Subst2
675 = cast<SubstTemplateTypeParmPackType>(T2);
676 if (!IsStructurallyEquivalent(Context,
677 QualType(Subst1->getReplacedParameter(), 0),
678 QualType(Subst2->getReplacedParameter(), 0)))
679 return false;
680 if (!IsStructurallyEquivalent(Context,
681 Subst1->getArgumentPack(),
682 Subst2->getArgumentPack()))
683 return false;
684 break;
685 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000686 case Type::TemplateSpecialization: {
687 const TemplateSpecializationType *Spec1
688 = cast<TemplateSpecializationType>(T1);
689 const TemplateSpecializationType *Spec2
690 = cast<TemplateSpecializationType>(T2);
691 if (!IsStructurallyEquivalent(Context,
692 Spec1->getTemplateName(),
693 Spec2->getTemplateName()))
694 return false;
695 if (Spec1->getNumArgs() != Spec2->getNumArgs())
696 return false;
697 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
698 if (!IsStructurallyEquivalent(Context,
699 Spec1->getArg(I), Spec2->getArg(I)))
700 return false;
701 }
702 break;
703 }
704
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000705 case Type::Elaborated: {
706 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
707 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
708 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
709 if (Elab1->getKeyword() != Elab2->getKeyword())
710 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000711 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000712 Elab1->getQualifier(),
713 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000714 return false;
715 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000716 Elab1->getNamedType(),
717 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000718 return false;
719 break;
720 }
721
John McCall3cb0ebd2010-03-10 03:28:59 +0000722 case Type::InjectedClassName: {
723 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
724 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
725 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000726 Inj1->getInjectedSpecializationType(),
727 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000728 return false;
729 break;
730 }
731
Douglas Gregor4714c122010-03-31 17:34:00 +0000732 case Type::DependentName: {
733 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
734 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000735 if (!IsStructurallyEquivalent(Context,
736 Typename1->getQualifier(),
737 Typename2->getQualifier()))
738 return false;
739 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
740 Typename2->getIdentifier()))
741 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000742
743 break;
744 }
745
John McCall33500952010-06-11 00:33:02 +0000746 case Type::DependentTemplateSpecialization: {
747 const DependentTemplateSpecializationType *Spec1 =
748 cast<DependentTemplateSpecializationType>(T1);
749 const DependentTemplateSpecializationType *Spec2 =
750 cast<DependentTemplateSpecializationType>(T2);
751 if (!IsStructurallyEquivalent(Context,
752 Spec1->getQualifier(),
753 Spec2->getQualifier()))
754 return false;
755 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
756 Spec2->getIdentifier()))
757 return false;
758 if (Spec1->getNumArgs() != Spec2->getNumArgs())
759 return false;
760 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
761 if (!IsStructurallyEquivalent(Context,
762 Spec1->getArg(I), Spec2->getArg(I)))
763 return false;
764 }
765 break;
766 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000767
768 case Type::PackExpansion:
769 if (!IsStructurallyEquivalent(Context,
770 cast<PackExpansionType>(T1)->getPattern(),
771 cast<PackExpansionType>(T2)->getPattern()))
772 return false;
773 break;
774
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000775 case Type::ObjCInterface: {
776 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
777 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
778 if (!IsStructurallyEquivalent(Context,
779 Iface1->getDecl(), Iface2->getDecl()))
780 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000781 break;
782 }
783
784 case Type::ObjCObject: {
785 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
786 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
787 if (!IsStructurallyEquivalent(Context,
788 Obj1->getBaseType(),
789 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000790 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000791 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
792 return false;
793 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000794 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000795 Obj1->getProtocol(I),
796 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000797 return false;
798 }
799 break;
800 }
801
802 case Type::ObjCObjectPointer: {
803 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
804 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
805 if (!IsStructurallyEquivalent(Context,
806 Ptr1->getPointeeType(),
807 Ptr2->getPointeeType()))
808 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000809 break;
810 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000811
812 case Type::Atomic: {
813 if (!IsStructurallyEquivalent(Context,
814 cast<AtomicType>(T1)->getValueType(),
815 cast<AtomicType>(T2)->getValueType()))
816 return false;
817 break;
818 }
819
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000820 } // end switch
821
822 return true;
823}
824
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000825/// \brief Determine structural equivalence of two fields.
826static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
827 FieldDecl *Field1, FieldDecl *Field2) {
828 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
829
830 if (!IsStructurallyEquivalent(Context,
831 Field1->getType(), Field2->getType())) {
832 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
833 << Context.C2.getTypeDeclType(Owner2);
834 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
835 << Field2->getDeclName() << Field2->getType();
836 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
837 << Field1->getDeclName() << Field1->getType();
838 return false;
839 }
840
841 if (Field1->isBitField() != Field2->isBitField()) {
842 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
843 << Context.C2.getTypeDeclType(Owner2);
844 if (Field1->isBitField()) {
845 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
846 << Field1->getDeclName() << Field1->getType()
847 << Field1->getBitWidthValue(Context.C1);
848 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
849 << Field2->getDeclName();
850 } else {
851 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
852 << Field2->getDeclName() << Field2->getType()
853 << Field2->getBitWidthValue(Context.C2);
854 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
855 << Field1->getDeclName();
856 }
857 return false;
858 }
859
860 if (Field1->isBitField()) {
861 // Make sure that the bit-fields are the same length.
862 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
863 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
864
865 if (Bits1 != Bits2) {
866 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
867 << Context.C2.getTypeDeclType(Owner2);
868 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
869 << Field2->getDeclName() << Field2->getType() << Bits2;
870 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
871 << Field1->getDeclName() << Field1->getType() << Bits1;
872 return false;
873 }
874 }
875
876 return true;
877}
878
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000879/// \brief Determine structural equivalence of two records.
880static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
881 RecordDecl *D1, RecordDecl *D2) {
882 if (D1->isUnion() != D2->isUnion()) {
883 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
884 << Context.C2.getTypeDeclType(D2);
885 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
886 << D1->getDeclName() << (unsigned)D1->getTagKind();
887 return false;
888 }
889
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000890 // If both declarations are class template specializations, we know
891 // the ODR applies, so check the template and template arguments.
892 ClassTemplateSpecializationDecl *Spec1
893 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
894 ClassTemplateSpecializationDecl *Spec2
895 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
896 if (Spec1 && Spec2) {
897 // Check that the specialized templates are the same.
898 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
899 Spec2->getSpecializedTemplate()))
900 return false;
901
902 // Check that the template arguments are the same.
903 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
904 return false;
905
906 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
907 if (!IsStructurallyEquivalent(Context,
908 Spec1->getTemplateArgs().get(I),
909 Spec2->getTemplateArgs().get(I)))
910 return false;
911 }
912 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000913 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000914 else if (Spec1 || Spec2)
915 return false;
916
Douglas Gregorea35d112010-02-15 23:54:17 +0000917 // Compare the definitions of these two records. If either or both are
918 // incomplete, we assume that they are equivalent.
919 D1 = D1->getDefinition();
920 D2 = D2->getDefinition();
921 if (!D1 || !D2)
922 return true;
923
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000924 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
925 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
926 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
927 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000928 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000929 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000930 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000931 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000932 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000933 return false;
934 }
935
936 // Check the base classes.
937 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
938 BaseEnd1 = D1CXX->bases_end(),
939 Base2 = D2CXX->bases_begin();
940 Base1 != BaseEnd1;
941 ++Base1, ++Base2) {
942 if (!IsStructurallyEquivalent(Context,
943 Base1->getType(), Base2->getType())) {
944 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
945 << Context.C2.getTypeDeclType(D2);
946 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
947 << Base2->getType()
948 << Base2->getSourceRange();
949 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
950 << Base1->getType()
951 << Base1->getSourceRange();
952 return false;
953 }
954
955 // Check virtual vs. non-virtual inheritance mismatch.
956 if (Base1->isVirtual() != Base2->isVirtual()) {
957 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
958 << Context.C2.getTypeDeclType(D2);
959 Context.Diag2(Base2->getSourceRange().getBegin(),
960 diag::note_odr_virtual_base)
961 << Base2->isVirtual() << Base2->getSourceRange();
962 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
963 << Base1->isVirtual()
964 << Base1->getSourceRange();
965 return false;
966 }
967 }
968 } else if (D1CXX->getNumBases() > 0) {
969 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
970 << Context.C2.getTypeDeclType(D2);
971 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
972 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
973 << Base1->getType()
974 << Base1->getSourceRange();
975 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
976 return false;
977 }
978 }
979
980 // Check the fields for consistency.
981 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
982 Field2End = D2->field_end();
983 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
984 Field1End = D1->field_end();
985 Field1 != Field1End;
986 ++Field1, ++Field2) {
987 if (Field2 == Field2End) {
988 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
989 << Context.C2.getTypeDeclType(D2);
990 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
991 << Field1->getDeclName() << Field1->getType();
992 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
993 return false;
994 }
995
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000996 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
997 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000998 }
999
1000 if (Field2 != Field2End) {
1001 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1002 << Context.C2.getTypeDeclType(D2);
1003 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1004 << Field2->getDeclName() << Field2->getType();
1005 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1006 return false;
1007 }
1008
1009 return true;
1010}
1011
1012/// \brief Determine structural equivalence of two enums.
1013static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1014 EnumDecl *D1, EnumDecl *D2) {
1015 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1016 EC2End = D2->enumerator_end();
1017 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1018 EC1End = D1->enumerator_end();
1019 EC1 != EC1End; ++EC1, ++EC2) {
1020 if (EC2 == EC2End) {
1021 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1022 << Context.C2.getTypeDeclType(D2);
1023 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1024 << EC1->getDeclName()
1025 << EC1->getInitVal().toString(10);
1026 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1027 return false;
1028 }
1029
1030 llvm::APSInt Val1 = EC1->getInitVal();
1031 llvm::APSInt Val2 = EC2->getInitVal();
1032 if (!IsSameValue(Val1, Val2) ||
1033 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1034 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1035 << Context.C2.getTypeDeclType(D2);
1036 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1037 << EC2->getDeclName()
1038 << EC2->getInitVal().toString(10);
1039 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1040 << EC1->getDeclName()
1041 << EC1->getInitVal().toString(10);
1042 return false;
1043 }
1044 }
1045
1046 if (EC2 != EC2End) {
1047 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1048 << Context.C2.getTypeDeclType(D2);
1049 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1050 << EC2->getDeclName()
1051 << EC2->getInitVal().toString(10);
1052 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1053 return false;
1054 }
1055
1056 return true;
1057}
Douglas Gregor040afae2010-11-30 19:14:50 +00001058
1059static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1060 TemplateParameterList *Params1,
1061 TemplateParameterList *Params2) {
1062 if (Params1->size() != Params2->size()) {
1063 Context.Diag2(Params2->getTemplateLoc(),
1064 diag::err_odr_different_num_template_parameters)
1065 << Params1->size() << Params2->size();
1066 Context.Diag1(Params1->getTemplateLoc(),
1067 diag::note_odr_template_parameter_list);
1068 return false;
1069 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001070
Douglas Gregor040afae2010-11-30 19:14:50 +00001071 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1072 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1073 Context.Diag2(Params2->getParam(I)->getLocation(),
1074 diag::err_odr_different_template_parameter_kind);
1075 Context.Diag1(Params1->getParam(I)->getLocation(),
1076 diag::note_odr_template_parameter_here);
1077 return false;
1078 }
1079
1080 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1081 Params2->getParam(I))) {
1082
1083 return false;
1084 }
1085 }
1086
1087 return true;
1088}
1089
1090static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1091 TemplateTypeParmDecl *D1,
1092 TemplateTypeParmDecl *D2) {
1093 if (D1->isParameterPack() != D2->isParameterPack()) {
1094 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1095 << D2->isParameterPack();
1096 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1097 << D1->isParameterPack();
1098 return false;
1099 }
1100
1101 return true;
1102}
1103
1104static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1105 NonTypeTemplateParmDecl *D1,
1106 NonTypeTemplateParmDecl *D2) {
1107 // FIXME: Enable once we have variadic templates.
1108#if 0
1109 if (D1->isParameterPack() != D2->isParameterPack()) {
1110 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1111 << D2->isParameterPack();
1112 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1113 << D1->isParameterPack();
1114 return false;
1115 }
1116#endif
1117
1118 // Check types.
1119 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1120 Context.Diag2(D2->getLocation(),
1121 diag::err_odr_non_type_parameter_type_inconsistent)
1122 << D2->getType() << D1->getType();
1123 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1124 << D1->getType();
1125 return false;
1126 }
1127
1128 return true;
1129}
1130
1131static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1132 TemplateTemplateParmDecl *D1,
1133 TemplateTemplateParmDecl *D2) {
1134 // FIXME: Enable once we have variadic templates.
1135#if 0
1136 if (D1->isParameterPack() != D2->isParameterPack()) {
1137 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1138 << D2->isParameterPack();
1139 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1140 << D1->isParameterPack();
1141 return false;
1142 }
1143#endif
1144
1145 // Check template parameter lists.
1146 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1147 D2->getTemplateParameters());
1148}
1149
1150static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1151 ClassTemplateDecl *D1,
1152 ClassTemplateDecl *D2) {
1153 // Check template parameters.
1154 if (!IsStructurallyEquivalent(Context,
1155 D1->getTemplateParameters(),
1156 D2->getTemplateParameters()))
1157 return false;
1158
1159 // Check the templated declaration.
1160 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1161 D2->getTemplatedDecl());
1162}
1163
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001164/// \brief Determine structural equivalence of two declarations.
1165static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1166 Decl *D1, Decl *D2) {
1167 // FIXME: Check for known structural equivalences via a callback of some sort.
1168
Douglas Gregorea35d112010-02-15 23:54:17 +00001169 // Check whether we already know that these two declarations are not
1170 // structurally equivalent.
1171 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1172 D2->getCanonicalDecl())))
1173 return false;
1174
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001175 // Determine whether we've already produced a tentative equivalence for D1.
1176 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1177 if (EquivToD1)
1178 return EquivToD1 == D2->getCanonicalDecl();
1179
1180 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1181 EquivToD1 = D2->getCanonicalDecl();
1182 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1183 return true;
1184}
1185
1186bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1187 Decl *D2) {
1188 if (!::IsStructurallyEquivalent(*this, D1, D2))
1189 return false;
1190
1191 return !Finish();
1192}
1193
1194bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1195 QualType T2) {
1196 if (!::IsStructurallyEquivalent(*this, T1, T2))
1197 return false;
1198
1199 return !Finish();
1200}
1201
1202bool StructuralEquivalenceContext::Finish() {
1203 while (!DeclsToCheck.empty()) {
1204 // Check the next declaration.
1205 Decl *D1 = DeclsToCheck.front();
1206 DeclsToCheck.pop_front();
1207
1208 Decl *D2 = TentativeEquivalences[D1];
1209 assert(D2 && "Unrecorded tentative equivalence?");
1210
Douglas Gregorea35d112010-02-15 23:54:17 +00001211 bool Equivalent = true;
1212
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001213 // FIXME: Switch on all declaration kinds. For now, we're just going to
1214 // check the obvious ones.
1215 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1216 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1217 // Check for equivalent structure names.
1218 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001219 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1220 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001221 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001222 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1223 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001224 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1225 !::IsStructurallyEquivalent(*this, Record1, Record2))
1226 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001227 } else {
1228 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001229 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001230 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001231 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001232 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1233 // Check for equivalent enum names.
1234 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001235 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1236 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001237 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001238 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1239 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001240 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1241 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1242 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001243 } else {
1244 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001245 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001246 }
Richard Smith162e1c12011-04-15 14:24:37 +00001247 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1248 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001249 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001250 Typedef2->getIdentifier()) ||
1251 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001252 Typedef1->getUnderlyingType(),
1253 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001254 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001255 } else {
1256 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001257 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001258 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001259 } else if (ClassTemplateDecl *ClassTemplate1
1260 = dyn_cast<ClassTemplateDecl>(D1)) {
1261 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1262 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1263 ClassTemplate2->getIdentifier()) ||
1264 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1265 Equivalent = false;
1266 } else {
1267 // Class template/non-class-template mismatch.
1268 Equivalent = false;
1269 }
1270 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1271 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1272 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1273 Equivalent = false;
1274 } else {
1275 // Kind mismatch.
1276 Equivalent = false;
1277 }
1278 } else if (NonTypeTemplateParmDecl *NTTP1
1279 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1280 if (NonTypeTemplateParmDecl *NTTP2
1281 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1282 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1283 Equivalent = false;
1284 } else {
1285 // Kind mismatch.
1286 Equivalent = false;
1287 }
1288 } else if (TemplateTemplateParmDecl *TTP1
1289 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1290 if (TemplateTemplateParmDecl *TTP2
1291 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1292 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1293 Equivalent = false;
1294 } else {
1295 // Kind mismatch.
1296 Equivalent = false;
1297 }
1298 }
1299
Douglas Gregorea35d112010-02-15 23:54:17 +00001300 if (!Equivalent) {
1301 // Note that these two declarations are not equivalent (and we already
1302 // know about it).
1303 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1304 D2->getCanonicalDecl()));
1305 return true;
1306 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001307 // FIXME: Check other declaration kinds!
1308 }
1309
1310 return false;
1311}
1312
1313//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001314// Import Types
1315//----------------------------------------------------------------------------
1316
John McCallf4c73712011-01-19 06:33:43 +00001317QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001318 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1319 << T->getTypeClassName();
1320 return QualType();
1321}
1322
John McCallf4c73712011-01-19 06:33:43 +00001323QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001324 switch (T->getKind()) {
John McCalle0a22d02011-10-18 21:02:43 +00001325#define SHARED_SINGLETON_TYPE(Expansion)
1326#define BUILTIN_TYPE(Id, SingletonId) \
1327 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1328#include "clang/AST/BuiltinTypes.def"
1329
1330 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1331 // context supports C++.
1332
1333 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1334 // context supports ObjC.
1335
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001336 case BuiltinType::Char_U:
1337 // The context we're importing from has an unsigned 'char'. If we're
1338 // importing into a context with a signed 'char', translate to
1339 // 'unsigned char' instead.
1340 if (Importer.getToContext().getLangOptions().CharIsSigned)
1341 return Importer.getToContext().UnsignedCharTy;
1342
1343 return Importer.getToContext().CharTy;
1344
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001345 case BuiltinType::Char_S:
1346 // The context we're importing from has an unsigned 'char'. If we're
1347 // importing into a context with a signed 'char', translate to
1348 // 'unsigned char' instead.
1349 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1350 return Importer.getToContext().SignedCharTy;
1351
1352 return Importer.getToContext().CharTy;
1353
Chris Lattner3f59c972010-12-25 23:25:43 +00001354 case BuiltinType::WChar_S:
1355 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001356 // FIXME: If not in C++, shall we translate to the C equivalent of
1357 // wchar_t?
1358 return Importer.getToContext().WCharTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001359 }
1360
1361 return QualType();
1362}
1363
John McCallf4c73712011-01-19 06:33:43 +00001364QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001365 QualType ToElementType = Importer.Import(T->getElementType());
1366 if (ToElementType.isNull())
1367 return QualType();
1368
1369 return Importer.getToContext().getComplexType(ToElementType);
1370}
1371
John McCallf4c73712011-01-19 06:33:43 +00001372QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001373 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1374 if (ToPointeeType.isNull())
1375 return QualType();
1376
1377 return Importer.getToContext().getPointerType(ToPointeeType);
1378}
1379
John McCallf4c73712011-01-19 06:33:43 +00001380QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001381 // FIXME: Check for blocks support in "to" context.
1382 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1383 if (ToPointeeType.isNull())
1384 return QualType();
1385
1386 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1387}
1388
John McCallf4c73712011-01-19 06:33:43 +00001389QualType
1390ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001391 // FIXME: Check for C++ support in "to" context.
1392 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1393 if (ToPointeeType.isNull())
1394 return QualType();
1395
1396 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1397}
1398
John McCallf4c73712011-01-19 06:33:43 +00001399QualType
1400ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001401 // FIXME: Check for C++0x support in "to" context.
1402 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1403 if (ToPointeeType.isNull())
1404 return QualType();
1405
1406 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1407}
1408
John McCallf4c73712011-01-19 06:33:43 +00001409QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001410 // FIXME: Check for C++ support in "to" context.
1411 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1412 if (ToPointeeType.isNull())
1413 return QualType();
1414
1415 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1416 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1417 ClassType.getTypePtr());
1418}
1419
John McCallf4c73712011-01-19 06:33:43 +00001420QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421 QualType ToElementType = Importer.Import(T->getElementType());
1422 if (ToElementType.isNull())
1423 return QualType();
1424
1425 return Importer.getToContext().getConstantArrayType(ToElementType,
1426 T->getSize(),
1427 T->getSizeModifier(),
1428 T->getIndexTypeCVRQualifiers());
1429}
1430
John McCallf4c73712011-01-19 06:33:43 +00001431QualType
1432ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001433 QualType ToElementType = Importer.Import(T->getElementType());
1434 if (ToElementType.isNull())
1435 return QualType();
1436
1437 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1438 T->getSizeModifier(),
1439 T->getIndexTypeCVRQualifiers());
1440}
1441
John McCallf4c73712011-01-19 06:33:43 +00001442QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001443 QualType ToElementType = Importer.Import(T->getElementType());
1444 if (ToElementType.isNull())
1445 return QualType();
1446
1447 Expr *Size = Importer.Import(T->getSizeExpr());
1448 if (!Size)
1449 return QualType();
1450
1451 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1452 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1453 T->getSizeModifier(),
1454 T->getIndexTypeCVRQualifiers(),
1455 Brackets);
1456}
1457
John McCallf4c73712011-01-19 06:33:43 +00001458QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001459 QualType ToElementType = Importer.Import(T->getElementType());
1460 if (ToElementType.isNull())
1461 return QualType();
1462
1463 return Importer.getToContext().getVectorType(ToElementType,
1464 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001465 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001466}
1467
John McCallf4c73712011-01-19 06:33:43 +00001468QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001469 QualType ToElementType = Importer.Import(T->getElementType());
1470 if (ToElementType.isNull())
1471 return QualType();
1472
1473 return Importer.getToContext().getExtVectorType(ToElementType,
1474 T->getNumElements());
1475}
1476
John McCallf4c73712011-01-19 06:33:43 +00001477QualType
1478ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001479 // FIXME: What happens if we're importing a function without a prototype
1480 // into C++? Should we make it variadic?
1481 QualType ToResultType = Importer.Import(T->getResultType());
1482 if (ToResultType.isNull())
1483 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001484
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001485 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001486 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001487}
1488
John McCallf4c73712011-01-19 06:33:43 +00001489QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001490 QualType ToResultType = Importer.Import(T->getResultType());
1491 if (ToResultType.isNull())
1492 return QualType();
1493
1494 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001495 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001496 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1497 AEnd = T->arg_type_end();
1498 A != AEnd; ++A) {
1499 QualType ArgType = Importer.Import(*A);
1500 if (ArgType.isNull())
1501 return QualType();
1502 ArgTypes.push_back(ArgType);
1503 }
1504
1505 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001506 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001507 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1508 EEnd = T->exception_end();
1509 E != EEnd; ++E) {
1510 QualType ExceptionType = Importer.Import(*E);
1511 if (ExceptionType.isNull())
1512 return QualType();
1513 ExceptionTypes.push_back(ExceptionType);
1514 }
John McCalle23cf432010-12-14 08:05:40 +00001515
1516 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1517 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001518
1519 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001520 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001521}
1522
Sean Callanan0aeb2892011-08-11 16:56:07 +00001523QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1524 QualType ToInnerType = Importer.Import(T->getInnerType());
1525 if (ToInnerType.isNull())
1526 return QualType();
1527
1528 return Importer.getToContext().getParenType(ToInnerType);
1529}
1530
John McCallf4c73712011-01-19 06:33:43 +00001531QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001532 TypedefNameDecl *ToDecl
1533 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001534 if (!ToDecl)
1535 return QualType();
1536
1537 return Importer.getToContext().getTypeDeclType(ToDecl);
1538}
1539
John McCallf4c73712011-01-19 06:33:43 +00001540QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001541 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1542 if (!ToExpr)
1543 return QualType();
1544
1545 return Importer.getToContext().getTypeOfExprType(ToExpr);
1546}
1547
John McCallf4c73712011-01-19 06:33:43 +00001548QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001549 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1550 if (ToUnderlyingType.isNull())
1551 return QualType();
1552
1553 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1554}
1555
John McCallf4c73712011-01-19 06:33:43 +00001556QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001557 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001558 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1559 if (!ToExpr)
1560 return QualType();
1561
1562 return Importer.getToContext().getDecltypeType(ToExpr);
1563}
1564
Sean Huntca63c202011-05-24 22:41:36 +00001565QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1566 QualType ToBaseType = Importer.Import(T->getBaseType());
1567 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1568 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1569 return QualType();
1570
1571 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1572 ToUnderlyingType,
1573 T->getUTTKind());
1574}
1575
Richard Smith34b41d92011-02-20 03:19:35 +00001576QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1577 // FIXME: Make sure that the "to" context supports C++0x!
1578 QualType FromDeduced = T->getDeducedType();
1579 QualType ToDeduced;
1580 if (!FromDeduced.isNull()) {
1581 ToDeduced = Importer.Import(FromDeduced);
1582 if (ToDeduced.isNull())
1583 return QualType();
1584 }
1585
1586 return Importer.getToContext().getAutoType(ToDeduced);
1587}
1588
John McCallf4c73712011-01-19 06:33:43 +00001589QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001590 RecordDecl *ToDecl
1591 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1592 if (!ToDecl)
1593 return QualType();
1594
1595 return Importer.getToContext().getTagDeclType(ToDecl);
1596}
1597
John McCallf4c73712011-01-19 06:33:43 +00001598QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001599 EnumDecl *ToDecl
1600 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1601 if (!ToDecl)
1602 return QualType();
1603
1604 return Importer.getToContext().getTagDeclType(ToDecl);
1605}
1606
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001607QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001608 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001609 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1610 if (ToTemplate.isNull())
1611 return QualType();
1612
Chris Lattner5f9e2722011-07-23 10:55:15 +00001613 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001614 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1615 return QualType();
1616
1617 QualType ToCanonType;
1618 if (!QualType(T, 0).isCanonical()) {
1619 QualType FromCanonType
1620 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1621 ToCanonType =Importer.Import(FromCanonType);
1622 if (ToCanonType.isNull())
1623 return QualType();
1624 }
1625 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1626 ToTemplateArgs.data(),
1627 ToTemplateArgs.size(),
1628 ToCanonType);
1629}
1630
John McCallf4c73712011-01-19 06:33:43 +00001631QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001632 NestedNameSpecifier *ToQualifier = 0;
1633 // Note: the qualifier in an ElaboratedType is optional.
1634 if (T->getQualifier()) {
1635 ToQualifier = Importer.Import(T->getQualifier());
1636 if (!ToQualifier)
1637 return QualType();
1638 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001639
1640 QualType ToNamedType = Importer.Import(T->getNamedType());
1641 if (ToNamedType.isNull())
1642 return QualType();
1643
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001644 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1645 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001646}
1647
John McCallf4c73712011-01-19 06:33:43 +00001648QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001649 ObjCInterfaceDecl *Class
1650 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1651 if (!Class)
1652 return QualType();
1653
John McCallc12c5bb2010-05-15 11:32:37 +00001654 return Importer.getToContext().getObjCInterfaceType(Class);
1655}
1656
John McCallf4c73712011-01-19 06:33:43 +00001657QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001658 QualType ToBaseType = Importer.Import(T->getBaseType());
1659 if (ToBaseType.isNull())
1660 return QualType();
1661
Chris Lattner5f9e2722011-07-23 10:55:15 +00001662 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001663 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001664 PEnd = T->qual_end();
1665 P != PEnd; ++P) {
1666 ObjCProtocolDecl *Protocol
1667 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1668 if (!Protocol)
1669 return QualType();
1670 Protocols.push_back(Protocol);
1671 }
1672
John McCallc12c5bb2010-05-15 11:32:37 +00001673 return Importer.getToContext().getObjCObjectType(ToBaseType,
1674 Protocols.data(),
1675 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001676}
1677
John McCallf4c73712011-01-19 06:33:43 +00001678QualType
1679ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001680 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1681 if (ToPointeeType.isNull())
1682 return QualType();
1683
John McCallc12c5bb2010-05-15 11:32:37 +00001684 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001685}
1686
Douglas Gregor089459a2010-02-08 21:09:39 +00001687//----------------------------------------------------------------------------
1688// Import Declarations
1689//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001690bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1691 DeclContext *&LexicalDC,
1692 DeclarationName &Name,
1693 SourceLocation &Loc) {
1694 // Import the context of this declaration.
1695 DC = Importer.ImportContext(D->getDeclContext());
1696 if (!DC)
1697 return true;
1698
1699 LexicalDC = DC;
1700 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1701 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1702 if (!LexicalDC)
1703 return true;
1704 }
1705
1706 // Import the name of this declaration.
1707 Name = Importer.Import(D->getDeclName());
1708 if (D->getDeclName() && !Name)
1709 return true;
1710
1711 // Import the location of this declaration.
1712 Loc = Importer.Import(D->getLocation());
1713 return false;
1714}
1715
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001716void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1717 if (!FromD)
1718 return;
1719
1720 if (!ToD) {
1721 ToD = Importer.Import(FromD);
1722 if (!ToD)
1723 return;
1724 }
1725
1726 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1727 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1728 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1729 ImportDefinition(FromRecord, ToRecord);
1730 }
1731 }
1732 return;
1733 }
1734
1735 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1736 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1737 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1738 ImportDefinition(FromEnum, ToEnum);
1739 }
1740 }
1741 return;
1742 }
1743}
1744
Abramo Bagnara25777432010-08-11 22:01:17 +00001745void
1746ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1747 DeclarationNameInfo& To) {
1748 // NOTE: To.Name and To.Loc are already imported.
1749 // We only have to import To.LocInfo.
1750 switch (To.getName().getNameKind()) {
1751 case DeclarationName::Identifier:
1752 case DeclarationName::ObjCZeroArgSelector:
1753 case DeclarationName::ObjCOneArgSelector:
1754 case DeclarationName::ObjCMultiArgSelector:
1755 case DeclarationName::CXXUsingDirective:
1756 return;
1757
1758 case DeclarationName::CXXOperatorName: {
1759 SourceRange Range = From.getCXXOperatorNameRange();
1760 To.setCXXOperatorNameRange(Importer.Import(Range));
1761 return;
1762 }
1763 case DeclarationName::CXXLiteralOperatorName: {
1764 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1765 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1766 return;
1767 }
1768 case DeclarationName::CXXConstructorName:
1769 case DeclarationName::CXXDestructorName:
1770 case DeclarationName::CXXConversionFunctionName: {
1771 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1772 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1773 return;
1774 }
Abramo Bagnara25777432010-08-11 22:01:17 +00001775 }
Douglas Gregor21a25162011-11-02 20:52:01 +00001776 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001777}
1778
Douglas Gregord8868a62011-01-18 03:11:38 +00001779void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1780 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001781 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001782 return;
1783 }
1784
Douglas Gregor083a8212010-02-21 18:24:45 +00001785 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1786 FromEnd = FromDC->decls_end();
1787 From != FromEnd;
1788 ++From)
1789 Importer.Import(*From);
1790}
1791
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001792bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1793 bool ForceImport) {
1794 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001795 return false;
1796
1797 To->startDefinition();
1798
1799 // Add base classes.
1800 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1801 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor27c72d82011-11-03 18:07:07 +00001802
1803 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1804 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1805 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
1806 ToData.UserDeclaredCopyConstructor = FromData.UserDeclaredCopyConstructor;
1807 ToData.UserDeclaredMoveConstructor = FromData.UserDeclaredMoveConstructor;
1808 ToData.UserDeclaredCopyAssignment = FromData.UserDeclaredCopyAssignment;
1809 ToData.UserDeclaredMoveAssignment = FromData.UserDeclaredMoveAssignment;
1810 ToData.UserDeclaredDestructor = FromData.UserDeclaredDestructor;
1811 ToData.Aggregate = FromData.Aggregate;
1812 ToData.PlainOldData = FromData.PlainOldData;
1813 ToData.Empty = FromData.Empty;
1814 ToData.Polymorphic = FromData.Polymorphic;
1815 ToData.Abstract = FromData.Abstract;
1816 ToData.IsStandardLayout = FromData.IsStandardLayout;
1817 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1818 ToData.HasPrivateFields = FromData.HasPrivateFields;
1819 ToData.HasProtectedFields = FromData.HasProtectedFields;
1820 ToData.HasPublicFields = FromData.HasPublicFields;
1821 ToData.HasMutableFields = FromData.HasMutableFields;
1822 ToData.HasTrivialDefaultConstructor = FromData.HasTrivialDefaultConstructor;
1823 ToData.HasConstexprNonCopyMoveConstructor
1824 = FromData.HasConstexprNonCopyMoveConstructor;
1825 ToData.HasTrivialCopyConstructor = FromData.HasTrivialCopyConstructor;
1826 ToData.HasTrivialMoveConstructor = FromData.HasTrivialMoveConstructor;
1827 ToData.HasTrivialCopyAssignment = FromData.HasTrivialCopyAssignment;
1828 ToData.HasTrivialMoveAssignment = FromData.HasTrivialMoveAssignment;
1829 ToData.HasTrivialDestructor = FromData.HasTrivialDestructor;
1830 ToData.HasNonLiteralTypeFieldsOrBases
1831 = FromData.HasNonLiteralTypeFieldsOrBases;
1832 ToData.UserProvidedDefaultConstructor
1833 = FromData.UserProvidedDefaultConstructor;
1834 ToData.DeclaredDefaultConstructor = FromData.DeclaredDefaultConstructor;
1835 ToData.DeclaredCopyConstructor = FromData.DeclaredCopyConstructor;
1836 ToData.DeclaredMoveConstructor = FromData.DeclaredMoveConstructor;
1837 ToData.DeclaredCopyAssignment = FromData.DeclaredCopyAssignment;
1838 ToData.DeclaredMoveAssignment = FromData.DeclaredMoveAssignment;
1839 ToData.DeclaredDestructor = FromData.DeclaredDestructor;
1840 ToData.FailedImplicitMoveConstructor
1841 = FromData.FailedImplicitMoveConstructor;
1842 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001843
Chris Lattner5f9e2722011-07-23 10:55:15 +00001844 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001845 for (CXXRecordDecl::base_class_iterator
1846 Base1 = FromCXX->bases_begin(),
1847 FromBaseEnd = FromCXX->bases_end();
1848 Base1 != FromBaseEnd;
1849 ++Base1) {
1850 QualType T = Importer.Import(Base1->getType());
1851 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001852 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001853
1854 SourceLocation EllipsisLoc;
1855 if (Base1->isPackExpansion())
1856 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001857
1858 // Ensure that we have a definition for the base.
1859 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1860
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001861 Bases.push_back(
1862 new (Importer.getToContext())
1863 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1864 Base1->isVirtual(),
1865 Base1->isBaseOfClass(),
1866 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001867 Importer.Import(Base1->getTypeSourceInfo()),
1868 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001869 }
1870 if (!Bases.empty())
1871 ToCXX->setBases(Bases.data(), Bases.size());
1872 }
1873
Sean Callanan673e7752011-07-19 22:38:25 +00001874 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001875 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001876 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001877}
1878
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001879bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1880 bool ForceImport) {
1881 if (To->getDefinition() || To->isBeingDefined())
1882 return false;
1883
1884 To->startDefinition();
1885
1886 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1887 if (T.isNull())
1888 return true;
1889
1890 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1891 if (ToPromotionType.isNull())
1892 return true;
1893
1894 ImportDeclContext(From, ForceImport);
1895
1896 // FIXME: we might need to merge the number of positive or negative bits
1897 // if the enumerator lists don't match.
1898 To->completeDefinition(T, ToPromotionType,
1899 From->getNumPositiveBits(),
1900 From->getNumNegativeBits());
1901 return false;
1902}
1903
Douglas Gregor040afae2010-11-30 19:14:50 +00001904TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1905 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001906 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001907 ToParams.reserve(Params->size());
1908 for (TemplateParameterList::iterator P = Params->begin(),
1909 PEnd = Params->end();
1910 P != PEnd; ++P) {
1911 Decl *To = Importer.Import(*P);
1912 if (!To)
1913 return 0;
1914
1915 ToParams.push_back(cast<NamedDecl>(To));
1916 }
1917
1918 return TemplateParameterList::Create(Importer.getToContext(),
1919 Importer.Import(Params->getTemplateLoc()),
1920 Importer.Import(Params->getLAngleLoc()),
1921 ToParams.data(), ToParams.size(),
1922 Importer.Import(Params->getRAngleLoc()));
1923}
1924
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001925TemplateArgument
1926ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1927 switch (From.getKind()) {
1928 case TemplateArgument::Null:
1929 return TemplateArgument();
1930
1931 case TemplateArgument::Type: {
1932 QualType ToType = Importer.Import(From.getAsType());
1933 if (ToType.isNull())
1934 return TemplateArgument();
1935 return TemplateArgument(ToType);
1936 }
1937
1938 case TemplateArgument::Integral: {
1939 QualType ToType = Importer.Import(From.getIntegralType());
1940 if (ToType.isNull())
1941 return TemplateArgument();
1942 return TemplateArgument(*From.getAsIntegral(), ToType);
1943 }
1944
1945 case TemplateArgument::Declaration:
1946 if (Decl *To = Importer.Import(From.getAsDecl()))
1947 return TemplateArgument(To);
1948 return TemplateArgument();
1949
1950 case TemplateArgument::Template: {
1951 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1952 if (ToTemplate.isNull())
1953 return TemplateArgument();
1954
1955 return TemplateArgument(ToTemplate);
1956 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001957
1958 case TemplateArgument::TemplateExpansion: {
1959 TemplateName ToTemplate
1960 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1961 if (ToTemplate.isNull())
1962 return TemplateArgument();
1963
Douglas Gregor2be29f42011-01-14 23:41:42 +00001964 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001965 }
1966
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001967 case TemplateArgument::Expression:
1968 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1969 return TemplateArgument(ToExpr);
1970 return TemplateArgument();
1971
1972 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001973 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001974 ToPack.reserve(From.pack_size());
1975 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1976 return TemplateArgument();
1977
1978 TemplateArgument *ToArgs
1979 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1980 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1981 return TemplateArgument(ToArgs, ToPack.size());
1982 }
1983 }
1984
1985 llvm_unreachable("Invalid template argument kind");
1986 return TemplateArgument();
1987}
1988
1989bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1990 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001991 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001992 for (unsigned I = 0; I != NumFromArgs; ++I) {
1993 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1994 if (To.isNull() && !FromArgs[I].isNull())
1995 return true;
1996
1997 ToArgs.push_back(To);
1998 }
1999
2000 return false;
2001}
2002
Douglas Gregor96a01b42010-02-11 00:48:18 +00002003bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002004 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002005 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002007 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002008 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002009}
2010
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002011bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002012 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002013 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002014 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002015 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002016}
2017
Douglas Gregor040afae2010-11-30 19:14:50 +00002018bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2019 ClassTemplateDecl *To) {
2020 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2021 Importer.getToContext(),
2022 Importer.getNonEquivalentDecls());
2023 return Ctx.IsStructurallyEquivalent(From, To);
2024}
2025
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002026Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002027 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002028 << D->getDeclKindName();
2029 return 0;
2030}
2031
Sean Callananf1b69462011-11-17 23:20:56 +00002032Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2033 TranslationUnitDecl *ToD =
2034 Importer.getToContext().getTranslationUnitDecl();
2035
2036 Importer.Imported(D, ToD);
2037
2038 return ToD;
2039}
2040
Douglas Gregor788c62d2010-02-21 18:26:36 +00002041Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2042 // Import the major distinguishing characteristics of this namespace.
2043 DeclContext *DC, *LexicalDC;
2044 DeclarationName Name;
2045 SourceLocation Loc;
2046 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2047 return 0;
2048
2049 NamespaceDecl *MergeWithNamespace = 0;
2050 if (!Name) {
2051 // This is an anonymous namespace. Adopt an existing anonymous
2052 // namespace if we can.
2053 // FIXME: Not testable.
2054 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2055 MergeWithNamespace = TU->getAnonymousNamespace();
2056 else
2057 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2058 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002059 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002060 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2061 DC->localUncachedLookup(Name, FoundDecls);
2062 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2063 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002064 continue;
2065
Douglas Gregorb75a3452011-10-15 00:10:27 +00002066 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002067 MergeWithNamespace = FoundNS;
2068 ConflictingDecls.clear();
2069 break;
2070 }
2071
Douglas Gregorb75a3452011-10-15 00:10:27 +00002072 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002073 }
2074
2075 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002076 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002077 ConflictingDecls.data(),
2078 ConflictingDecls.size());
2079 }
2080 }
2081
2082 // Create the "to" namespace, if needed.
2083 NamespaceDecl *ToNamespace = MergeWithNamespace;
2084 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002085 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002086 D->isInline(),
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002087 Importer.Import(D->getLocStart()),
Douglas Gregorf5c9f9f2012-01-07 09:11:48 +00002088 Loc, Name.getAsIdentifierInfo(),
2089 /*PrevDecl=*/0);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002090 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002091 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002092
2093 // If this is an anonymous namespace, register it as the anonymous
2094 // namespace within its context.
2095 if (!Name) {
2096 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2097 TU->setAnonymousNamespace(ToNamespace);
2098 else
2099 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2100 }
2101 }
2102 Importer.Imported(D, ToNamespace);
2103
2104 ImportDeclContext(D);
2105
2106 return ToNamespace;
2107}
2108
Richard Smith162e1c12011-04-15 14:24:37 +00002109Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002110 // Import the major distinguishing characteristics of this typedef.
2111 DeclContext *DC, *LexicalDC;
2112 DeclarationName Name;
2113 SourceLocation Loc;
2114 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2115 return 0;
2116
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002117 // If this typedef is not in block scope, determine whether we've
2118 // seen a typedef with the same name (that we can merge with) or any
2119 // other entity by that name (which name lookup could conflict with).
2120 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002121 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002122 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002123 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2124 DC->localUncachedLookup(Name, FoundDecls);
2125 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2126 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002127 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002128 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002129 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002130 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2131 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002132 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002133 }
2134
Douglas Gregorb75a3452011-10-15 00:10:27 +00002135 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002136 }
2137
2138 if (!ConflictingDecls.empty()) {
2139 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2140 ConflictingDecls.data(),
2141 ConflictingDecls.size());
2142 if (!Name)
2143 return 0;
2144 }
2145 }
2146
Douglas Gregorea35d112010-02-15 23:54:17 +00002147 // Import the underlying type of this typedef;
2148 QualType T = Importer.Import(D->getUnderlyingType());
2149 if (T.isNull())
2150 return 0;
2151
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002152 // Create the new typedef node.
2153 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002154 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002155 TypedefNameDecl *ToTypedef;
2156 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002157 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2158 StartL, Loc,
2159 Name.getAsIdentifierInfo(),
2160 TInfo);
2161 else
Richard Smith162e1c12011-04-15 14:24:37 +00002162 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2163 StartL, Loc,
2164 Name.getAsIdentifierInfo(),
2165 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002166
Douglas Gregor325bf172010-02-22 17:42:47 +00002167 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002168 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002169 Importer.Imported(D, ToTypedef);
Sean Callanan9faf8102011-10-21 02:57:43 +00002170 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002171
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002172 return ToTypedef;
2173}
2174
Richard Smith162e1c12011-04-15 14:24:37 +00002175Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2176 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2177}
2178
2179Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2180 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2181}
2182
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002183Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2184 // Import the major distinguishing characteristics of this enum.
2185 DeclContext *DC, *LexicalDC;
2186 DeclarationName Name;
2187 SourceLocation Loc;
2188 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2189 return 0;
2190
2191 // Figure out what enum name we're looking for.
2192 unsigned IDNS = Decl::IDNS_Tag;
2193 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002194 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2195 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002196 IDNS = Decl::IDNS_Ordinary;
2197 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2198 IDNS |= Decl::IDNS_Ordinary;
2199
2200 // We may already have an enum of the same name; try to find and match it.
2201 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002202 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002203 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2204 DC->localUncachedLookup(SearchName, FoundDecls);
2205 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2206 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002207 continue;
2208
Douglas Gregorb75a3452011-10-15 00:10:27 +00002209 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002210 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002211 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2212 Found = Tag->getDecl();
2213 }
2214
2215 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002216 if (IsStructuralMatch(D, FoundEnum))
2217 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002218 }
2219
Douglas Gregorb75a3452011-10-15 00:10:27 +00002220 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002221 }
2222
2223 if (!ConflictingDecls.empty()) {
2224 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2225 ConflictingDecls.data(),
2226 ConflictingDecls.size());
2227 }
2228 }
2229
2230 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002231 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2232 Importer.Import(D->getLocStart()),
2233 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002234 D->isScoped(), D->isScopedUsingClassTag(),
2235 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002236 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002237 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002238 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002239 D2->setLexicalDeclContext(LexicalDC);
2240 Importer.Imported(D, D2);
Sean Callanan9faf8102011-10-21 02:57:43 +00002241 LexicalDC->addDeclInternal(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002242
2243 // Import the integer type.
2244 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2245 if (ToIntegerType.isNull())
2246 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002247 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002248
2249 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002250 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002251 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002252
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002253 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002254}
2255
Douglas Gregor96a01b42010-02-11 00:48:18 +00002256Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2257 // If this record has a definition in the translation unit we're coming from,
2258 // but this particular declaration is not that definition, import the
2259 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002260 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002261 if (Definition && Definition != D) {
2262 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002263 if (!ImportedDef)
2264 return 0;
2265
2266 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002267 }
2268
2269 // Import the major distinguishing characteristics of this record.
2270 DeclContext *DC, *LexicalDC;
2271 DeclarationName Name;
2272 SourceLocation Loc;
2273 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2274 return 0;
2275
2276 // Figure out what structure name we're looking for.
2277 unsigned IDNS = Decl::IDNS_Tag;
2278 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002279 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2280 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002281 IDNS = Decl::IDNS_Ordinary;
2282 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2283 IDNS |= Decl::IDNS_Ordinary;
2284
2285 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002286 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002287 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002288 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002289 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2290 DC->localUncachedLookup(SearchName, FoundDecls);
2291 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2292 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002293 continue;
2294
Douglas Gregorb75a3452011-10-15 00:10:27 +00002295 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002296 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002297 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2298 Found = Tag->getDecl();
2299 }
2300
2301 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002302 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002303 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002304 // The record types structurally match, or the "from" translation
2305 // unit only had a forward declaration anyway; call it the same
2306 // function.
2307 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002308 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002309 }
2310 } else {
2311 // We have a forward declaration of this type, so adopt that forward
2312 // declaration rather than building a new one.
2313 AdoptDecl = FoundRecord;
2314 continue;
2315 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002316 }
2317
Douglas Gregorb75a3452011-10-15 00:10:27 +00002318 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002319 }
2320
2321 if (!ConflictingDecls.empty()) {
2322 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2323 ConflictingDecls.data(),
2324 ConflictingDecls.size());
2325 }
2326 }
2327
2328 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002329 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002330 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002332 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002333 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002334 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002335 DC, StartLoc, Loc,
2336 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002337 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002338 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002339 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002340 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002341 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002342 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002343
2344 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002345 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00002346 LexicalDC->addDeclInternal(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002347 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002348
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002349 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002350
John McCall5e1cdac2011-10-07 06:10:15 +00002351 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002352 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002353
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002354 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002355}
2356
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002357Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2358 // Import the major distinguishing characteristics of this enumerator.
2359 DeclContext *DC, *LexicalDC;
2360 DeclarationName Name;
2361 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002362 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002363 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002364
2365 QualType T = Importer.Import(D->getType());
2366 if (T.isNull())
2367 return 0;
2368
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002369 // Determine whether there are any other declarations with the same name and
2370 // in the same context.
2371 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002372 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002373 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002374 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2375 DC->localUncachedLookup(Name, FoundDecls);
2376 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2377 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002378 continue;
2379
Douglas Gregorb75a3452011-10-15 00:10:27 +00002380 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002381 }
2382
2383 if (!ConflictingDecls.empty()) {
2384 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2385 ConflictingDecls.data(),
2386 ConflictingDecls.size());
2387 if (!Name)
2388 return 0;
2389 }
2390 }
2391
2392 Expr *Init = Importer.Import(D->getInitExpr());
2393 if (D->getInitExpr() && !Init)
2394 return 0;
2395
2396 EnumConstantDecl *ToEnumerator
2397 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2398 Name.getAsIdentifierInfo(), T,
2399 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002400 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002401 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002402 Importer.Imported(D, ToEnumerator);
Sean Callanan9faf8102011-10-21 02:57:43 +00002403 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002404 return ToEnumerator;
2405}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002406
Douglas Gregora404ea62010-02-10 19:54:31 +00002407Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2408 // Import the major distinguishing characteristics of this function.
2409 DeclContext *DC, *LexicalDC;
2410 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002411 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002412 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002413 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002414
Douglas Gregora404ea62010-02-10 19:54:31 +00002415 // Try to find a function in our own ("to") context with the same name, same
2416 // type, and in the same context as the function we're importing.
2417 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002418 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002419 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002420 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2421 DC->localUncachedLookup(Name, FoundDecls);
2422 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2423 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002424 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002425
Douglas Gregorb75a3452011-10-15 00:10:27 +00002426 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002427 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2428 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002429 if (Importer.IsStructurallyEquivalent(D->getType(),
2430 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002431 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002432 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002433 }
2434
2435 // FIXME: Check for overloading more carefully, e.g., by boosting
2436 // Sema::IsOverload out to the AST library.
2437
2438 // Function overloading is okay in C++.
2439 if (Importer.getToContext().getLangOptions().CPlusPlus)
2440 continue;
2441
2442 // Complain about inconsistent function types.
2443 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002444 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002445 Importer.ToDiag(FoundFunction->getLocation(),
2446 diag::note_odr_value_here)
2447 << FoundFunction->getType();
2448 }
2449 }
2450
Douglas Gregorb75a3452011-10-15 00:10:27 +00002451 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002452 }
2453
2454 if (!ConflictingDecls.empty()) {
2455 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2456 ConflictingDecls.data(),
2457 ConflictingDecls.size());
2458 if (!Name)
2459 return 0;
2460 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002461 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002462
Abramo Bagnara25777432010-08-11 22:01:17 +00002463 DeclarationNameInfo NameInfo(Name, Loc);
2464 // Import additional name location/type info.
2465 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2466
Douglas Gregorea35d112010-02-15 23:54:17 +00002467 // Import the type.
2468 QualType T = Importer.Import(D->getType());
2469 if (T.isNull())
2470 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002471
2472 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002473 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002474 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2475 P != PEnd; ++P) {
2476 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2477 if (!ToP)
2478 return 0;
2479
2480 Parameters.push_back(ToP);
2481 }
2482
2483 // Create the imported function.
2484 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002485 FunctionDecl *ToFunction = 0;
2486 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2487 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2488 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002489 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002490 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002491 FromConstructor->isExplicit(),
2492 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002493 D->isImplicit(),
2494 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002495 } else if (isa<CXXDestructorDecl>(D)) {
2496 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2497 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002498 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002499 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002500 D->isInlineSpecified(),
2501 D->isImplicit());
2502 } else if (CXXConversionDecl *FromConversion
2503 = dyn_cast<CXXConversionDecl>(D)) {
2504 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2505 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002506 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002507 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002508 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002509 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002510 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002511 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002512 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2513 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2514 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002515 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002516 NameInfo, T, TInfo,
2517 Method->isStatic(),
2518 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002519 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002520 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002521 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002522 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002523 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002524 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002525 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002526 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002527 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002528 D->hasWrittenPrototype(),
2529 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002530 }
John McCallb6217662010-03-15 10:12:16 +00002531
2532 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002533 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002534 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002535 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002536 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2537 ToFunction->setTrivial(D->isTrivial());
2538 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002539 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002540
Douglas Gregora404ea62010-02-10 19:54:31 +00002541 // Set the parameters.
2542 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002543 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan9faf8102011-10-21 02:57:43 +00002544 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002545 }
David Blaikie4278c652011-09-21 18:16:56 +00002546 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002547
2548 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002549
2550 // Add this function to the lexical context.
Sean Callanan9faf8102011-10-21 02:57:43 +00002551 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor81134ad2010-10-01 23:55:07 +00002552
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002553 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002554}
2555
Douglas Gregorc144f352010-02-21 18:29:16 +00002556Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2557 return VisitFunctionDecl(D);
2558}
2559
2560Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2561 return VisitCXXMethodDecl(D);
2562}
2563
2564Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2565 return VisitCXXMethodDecl(D);
2566}
2567
2568Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2569 return VisitCXXMethodDecl(D);
2570}
2571
Douglas Gregor96a01b42010-02-11 00:48:18 +00002572Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2573 // Import the major distinguishing characteristics of a variable.
2574 DeclContext *DC, *LexicalDC;
2575 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002576 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002577 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2578 return 0;
2579
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002580 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002581 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2582 DC->localUncachedLookup(Name, FoundDecls);
2583 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2584 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002585 if (Importer.IsStructurallyEquivalent(D->getType(),
2586 FoundField->getType())) {
2587 Importer.Imported(D, FoundField);
2588 return FoundField;
2589 }
2590
2591 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2592 << Name << D->getType() << FoundField->getType();
2593 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2594 << FoundField->getType();
2595 return 0;
2596 }
2597 }
2598
Douglas Gregorea35d112010-02-15 23:54:17 +00002599 // Import the type.
2600 QualType T = Importer.Import(D->getType());
2601 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002602 return 0;
2603
2604 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2605 Expr *BitWidth = Importer.Import(D->getBitWidth());
2606 if (!BitWidth && D->getBitWidth())
2607 return 0;
2608
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002609 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2610 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002611 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002612 T, TInfo, BitWidth, D->isMutable(),
2613 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002614 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002615 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002616 if (ToField->hasInClassInitializer())
2617 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002618 Importer.Imported(D, ToField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002619 LexicalDC->addDeclInternal(ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002620 return ToField;
2621}
2622
Francois Pichet87c2e122010-11-21 06:08:52 +00002623Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2624 // Import the major distinguishing characteristics of a variable.
2625 DeclContext *DC, *LexicalDC;
2626 DeclarationName Name;
2627 SourceLocation Loc;
2628 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2629 return 0;
2630
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002631 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002632 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2633 DC->localUncachedLookup(Name, FoundDecls);
2634 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002635 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002636 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002637 if (Importer.IsStructurallyEquivalent(D->getType(),
2638 FoundField->getType())) {
2639 Importer.Imported(D, FoundField);
2640 return FoundField;
2641 }
2642
2643 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2644 << Name << D->getType() << FoundField->getType();
2645 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2646 << FoundField->getType();
2647 return 0;
2648 }
2649 }
2650
Francois Pichet87c2e122010-11-21 06:08:52 +00002651 // Import the type.
2652 QualType T = Importer.Import(D->getType());
2653 if (T.isNull())
2654 return 0;
2655
2656 NamedDecl **NamedChain =
2657 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2658
2659 unsigned i = 0;
2660 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2661 PE = D->chain_end(); PI != PE; ++PI) {
2662 Decl* D = Importer.Import(*PI);
2663 if (!D)
2664 return 0;
2665 NamedChain[i++] = cast<NamedDecl>(D);
2666 }
2667
2668 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2669 Importer.getToContext(), DC,
2670 Loc, Name.getAsIdentifierInfo(), T,
2671 NamedChain, D->getChainingSize());
2672 ToIndirectField->setAccess(D->getAccess());
2673 ToIndirectField->setLexicalDeclContext(LexicalDC);
2674 Importer.Imported(D, ToIndirectField);
Sean Callanan9faf8102011-10-21 02:57:43 +00002675 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet87c2e122010-11-21 06:08:52 +00002676 return ToIndirectField;
2677}
2678
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002679Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2680 // Import the major distinguishing characteristics of an ivar.
2681 DeclContext *DC, *LexicalDC;
2682 DeclarationName Name;
2683 SourceLocation Loc;
2684 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2685 return 0;
2686
2687 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002688 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2689 DC->localUncachedLookup(Name, FoundDecls);
2690 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2691 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002692 if (Importer.IsStructurallyEquivalent(D->getType(),
2693 FoundIvar->getType())) {
2694 Importer.Imported(D, FoundIvar);
2695 return FoundIvar;
2696 }
2697
2698 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2699 << Name << D->getType() << FoundIvar->getType();
2700 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2701 << FoundIvar->getType();
2702 return 0;
2703 }
2704 }
2705
2706 // Import the type.
2707 QualType T = Importer.Import(D->getType());
2708 if (T.isNull())
2709 return 0;
2710
2711 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2712 Expr *BitWidth = Importer.Import(D->getBitWidth());
2713 if (!BitWidth && D->getBitWidth())
2714 return 0;
2715
Daniel Dunbara0654922010-04-02 20:10:03 +00002716 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2717 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002718 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002719 Loc, Name.getAsIdentifierInfo(),
2720 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002721 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002722 ToIvar->setLexicalDeclContext(LexicalDC);
2723 Importer.Imported(D, ToIvar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002724 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002725 return ToIvar;
2726
2727}
2728
Douglas Gregora404ea62010-02-10 19:54:31 +00002729Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2730 // Import the major distinguishing characteristics of a variable.
2731 DeclContext *DC, *LexicalDC;
2732 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002733 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002734 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002735 return 0;
2736
Douglas Gregor089459a2010-02-08 21:09:39 +00002737 // Try to find a variable in our own ("to") context with the same name and
2738 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002739 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002740 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002742 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002743 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2744 DC->localUncachedLookup(Name, FoundDecls);
2745 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2746 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002747 continue;
2748
Douglas Gregorb75a3452011-10-15 00:10:27 +00002749 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002750 // We have found a variable that we may need to merge with. Check it.
2751 if (isExternalLinkage(FoundVar->getLinkage()) &&
2752 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002753 if (Importer.IsStructurallyEquivalent(D->getType(),
2754 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002755 MergeWithVar = FoundVar;
2756 break;
2757 }
2758
Douglas Gregord0145422010-02-12 17:23:39 +00002759 const ArrayType *FoundArray
2760 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2761 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002762 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002763 if (FoundArray && TArray) {
2764 if (isa<IncompleteArrayType>(FoundArray) &&
2765 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002766 // Import the type.
2767 QualType T = Importer.Import(D->getType());
2768 if (T.isNull())
2769 return 0;
2770
Douglas Gregord0145422010-02-12 17:23:39 +00002771 FoundVar->setType(T);
2772 MergeWithVar = FoundVar;
2773 break;
2774 } else if (isa<IncompleteArrayType>(TArray) &&
2775 isa<ConstantArrayType>(FoundArray)) {
2776 MergeWithVar = FoundVar;
2777 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002778 }
2779 }
2780
Douglas Gregor089459a2010-02-08 21:09:39 +00002781 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002782 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002783 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2784 << FoundVar->getType();
2785 }
2786 }
2787
Douglas Gregorb75a3452011-10-15 00:10:27 +00002788 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002789 }
2790
2791 if (MergeWithVar) {
2792 // An equivalent variable with external linkage has been found. Link
2793 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002794 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002795
2796 if (VarDecl *DDef = D->getDefinition()) {
2797 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2798 Importer.ToDiag(ExistingDef->getLocation(),
2799 diag::err_odr_variable_multiple_def)
2800 << Name;
2801 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2802 } else {
2803 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002804 MergeWithVar->setInit(Init);
Richard Smith099e7f62011-12-19 06:19:21 +00002805 if (DDef->isInitKnownICE()) {
2806 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
2807 Eval->CheckedICE = true;
2808 Eval->IsICE = DDef->isInitICE();
2809 }
Douglas Gregor089459a2010-02-08 21:09:39 +00002810 }
2811 }
2812
2813 return MergeWithVar;
2814 }
2815
2816 if (!ConflictingDecls.empty()) {
2817 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2818 ConflictingDecls.data(),
2819 ConflictingDecls.size());
2820 if (!Name)
2821 return 0;
2822 }
2823 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002824
Douglas Gregorea35d112010-02-15 23:54:17 +00002825 // Import the type.
2826 QualType T = Importer.Import(D->getType());
2827 if (T.isNull())
2828 return 0;
2829
Douglas Gregor089459a2010-02-08 21:09:39 +00002830 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002831 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002832 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2833 Importer.Import(D->getInnerLocStart()),
2834 Loc, Name.getAsIdentifierInfo(),
2835 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002836 D->getStorageClass(),
2837 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002838 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002839 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002840 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002841 Importer.Imported(D, ToVar);
Sean Callanan9faf8102011-10-21 02:57:43 +00002842 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002843
Douglas Gregor089459a2010-02-08 21:09:39 +00002844 // Merge the initializer.
2845 // FIXME: Can we really import any initializer? Alternatively, we could force
2846 // ourselves to import every declaration of a variable and then only use
2847 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002848 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002849
2850 // FIXME: Other bits to merge?
2851
2852 return ToVar;
2853}
2854
Douglas Gregor2cd00932010-02-17 21:22:52 +00002855Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2856 // Parameters are created in the translation unit's context, then moved
2857 // into the function declaration's context afterward.
2858 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2859
2860 // Import the name of this declaration.
2861 DeclarationName Name = Importer.Import(D->getDeclName());
2862 if (D->getDeclName() && !Name)
2863 return 0;
2864
2865 // Import the location of this declaration.
2866 SourceLocation Loc = Importer.Import(D->getLocation());
2867
2868 // Import the parameter's type.
2869 QualType T = Importer.Import(D->getType());
2870 if (T.isNull())
2871 return 0;
2872
2873 // Create the imported parameter.
2874 ImplicitParamDecl *ToParm
2875 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2876 Loc, Name.getAsIdentifierInfo(),
2877 T);
2878 return Importer.Imported(D, ToParm);
2879}
2880
Douglas Gregora404ea62010-02-10 19:54:31 +00002881Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2882 // Parameters are created in the translation unit's context, then moved
2883 // into the function declaration's context afterward.
2884 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2885
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002886 // Import the name of this declaration.
2887 DeclarationName Name = Importer.Import(D->getDeclName());
2888 if (D->getDeclName() && !Name)
2889 return 0;
2890
Douglas Gregora404ea62010-02-10 19:54:31 +00002891 // Import the location of this declaration.
2892 SourceLocation Loc = Importer.Import(D->getLocation());
2893
2894 // Import the parameter's type.
2895 QualType T = Importer.Import(D->getType());
2896 if (T.isNull())
2897 return 0;
2898
2899 // Create the imported parameter.
2900 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2901 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002902 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002903 Loc, Name.getAsIdentifierInfo(),
2904 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002905 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002906 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002907 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002908 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002909}
2910
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002911Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2912 // Import the major distinguishing characteristics of a method.
2913 DeclContext *DC, *LexicalDC;
2914 DeclarationName Name;
2915 SourceLocation Loc;
2916 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2917 return 0;
2918
Douglas Gregorb75a3452011-10-15 00:10:27 +00002919 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2920 DC->localUncachedLookup(Name, FoundDecls);
2921 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2922 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002923 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2924 continue;
2925
2926 // Check return types.
2927 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2928 FoundMethod->getResultType())) {
2929 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2930 << D->isInstanceMethod() << Name
2931 << D->getResultType() << FoundMethod->getResultType();
2932 Importer.ToDiag(FoundMethod->getLocation(),
2933 diag::note_odr_objc_method_here)
2934 << D->isInstanceMethod() << Name;
2935 return 0;
2936 }
2937
2938 // Check the number of parameters.
2939 if (D->param_size() != FoundMethod->param_size()) {
2940 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2941 << D->isInstanceMethod() << Name
2942 << D->param_size() << FoundMethod->param_size();
2943 Importer.ToDiag(FoundMethod->getLocation(),
2944 diag::note_odr_objc_method_here)
2945 << D->isInstanceMethod() << Name;
2946 return 0;
2947 }
2948
2949 // Check parameter types.
2950 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2951 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2952 P != PEnd; ++P, ++FoundP) {
2953 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2954 (*FoundP)->getType())) {
2955 Importer.FromDiag((*P)->getLocation(),
2956 diag::err_odr_objc_method_param_type_inconsistent)
2957 << D->isInstanceMethod() << Name
2958 << (*P)->getType() << (*FoundP)->getType();
2959 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2960 << (*FoundP)->getType();
2961 return 0;
2962 }
2963 }
2964
2965 // Check variadic/non-variadic.
2966 // Check the number of parameters.
2967 if (D->isVariadic() != FoundMethod->isVariadic()) {
2968 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2969 << D->isInstanceMethod() << Name;
2970 Importer.ToDiag(FoundMethod->getLocation(),
2971 diag::note_odr_objc_method_here)
2972 << D->isInstanceMethod() << Name;
2973 return 0;
2974 }
2975
2976 // FIXME: Any other bits we need to merge?
2977 return Importer.Imported(D, FoundMethod);
2978 }
2979 }
2980
2981 // Import the result type.
2982 QualType ResultTy = Importer.Import(D->getResultType());
2983 if (ResultTy.isNull())
2984 return 0;
2985
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002986 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2987
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002988 ObjCMethodDecl *ToMethod
2989 = ObjCMethodDecl::Create(Importer.getToContext(),
2990 Loc,
2991 Importer.Import(D->getLocEnd()),
2992 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002993 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002994 D->isInstanceMethod(),
2995 D->isVariadic(),
2996 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002997 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002998 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002999 D->getImplementationControl(),
3000 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003001
3002 // FIXME: When we decide to merge method definitions, we'll need to
3003 // deal with implicit parameters.
3004
3005 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00003006 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003007 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3008 FromPEnd = D->param_end();
3009 FromP != FromPEnd;
3010 ++FromP) {
3011 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3012 if (!ToP)
3013 return 0;
3014
3015 ToParams.push_back(ToP);
3016 }
3017
3018 // Set the parameters.
3019 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3020 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003021 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003022 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003023 SmallVector<SourceLocation, 12> SelLocs;
3024 D->getSelectorLocs(SelLocs);
3025 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003026
3027 ToMethod->setLexicalDeclContext(LexicalDC);
3028 Importer.Imported(D, ToMethod);
Sean Callanan9faf8102011-10-21 02:57:43 +00003029 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003030 return ToMethod;
3031}
3032
Douglas Gregorb4677b62010-02-18 01:47:50 +00003033Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3034 // Import the major distinguishing characteristics of a category.
3035 DeclContext *DC, *LexicalDC;
3036 DeclarationName Name;
3037 SourceLocation Loc;
3038 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3039 return 0;
3040
3041 ObjCInterfaceDecl *ToInterface
3042 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3043 if (!ToInterface)
3044 return 0;
3045
3046 // Determine if we've already encountered this category.
3047 ObjCCategoryDecl *MergeWithCategory
3048 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3049 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3050 if (!ToCategory) {
3051 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003052 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003053 Loc,
3054 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003055 Name.getAsIdentifierInfo(),
3056 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003057 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003058 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003059 Importer.Imported(D, ToCategory);
3060
Douglas Gregorb4677b62010-02-18 01:47:50 +00003061 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003062 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3063 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003064 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3065 = D->protocol_loc_begin();
3066 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3067 FromProtoEnd = D->protocol_end();
3068 FromProto != FromProtoEnd;
3069 ++FromProto, ++FromProtoLoc) {
3070 ObjCProtocolDecl *ToProto
3071 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3072 if (!ToProto)
3073 return 0;
3074 Protocols.push_back(ToProto);
3075 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3076 }
3077
3078 // FIXME: If we're merging, make sure that the protocol list is the same.
3079 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3080 ProtocolLocs.data(), Importer.getToContext());
3081
3082 } else {
3083 Importer.Imported(D, ToCategory);
3084 }
3085
3086 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003087 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003088
3089 // If we have an implementation, import it as well.
3090 if (D->getImplementation()) {
3091 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003092 = cast_or_null<ObjCCategoryImplDecl>(
3093 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003094 if (!Impl)
3095 return 0;
3096
3097 ToCategory->setImplementation(Impl);
3098 }
3099
3100 return ToCategory;
3101}
3102
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003103Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003104 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003105 DeclContext *DC, *LexicalDC;
3106 DeclarationName Name;
3107 SourceLocation Loc;
3108 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3109 return 0;
3110
3111 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003112 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3113 DC->localUncachedLookup(Name, FoundDecls);
3114 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3115 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003116 continue;
3117
Douglas Gregorb75a3452011-10-15 00:10:27 +00003118 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003119 break;
3120 }
3121
3122 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003123 if (!ToProto || !ToProto->hasDefinition()) {
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003124 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003125 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3126 Name.getAsIdentifierInfo(), Loc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003127 Importer.Import(D->getAtStartLoc()),
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +00003128 /*PrevDecl=*/0);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003129 ToProto->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003130 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003131 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00003132 if (!ToProto->hasDefinition())
3133 ToProto->startDefinition();
3134
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003135 Importer.Imported(D, ToProto);
3136
3137 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003138 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3139 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003140 ObjCProtocolDecl::protocol_loc_iterator
3141 FromProtoLoc = D->protocol_loc_begin();
3142 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3143 FromProtoEnd = D->protocol_end();
3144 FromProto != FromProtoEnd;
3145 ++FromProto, ++FromProtoLoc) {
3146 ObjCProtocolDecl *ToProto
3147 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3148 if (!ToProto)
3149 return 0;
3150 Protocols.push_back(ToProto);
3151 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3152 }
3153
3154 // FIXME: If we're merging, make sure that the protocol list is the same.
3155 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3156 ProtocolLocs.data(), Importer.getToContext());
3157 } else {
3158 Importer.Imported(D, ToProto);
3159 }
3160
Douglas Gregorb4677b62010-02-18 01:47:50 +00003161 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003162 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003163
3164 return ToProto;
3165}
3166
Douglas Gregora12d2942010-02-16 01:20:57 +00003167Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3168 // Import the major distinguishing characteristics of an @interface.
3169 DeclContext *DC, *LexicalDC;
3170 DeclarationName Name;
3171 SourceLocation Loc;
3172 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3173 return 0;
3174
3175 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003176 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3177 DC->localUncachedLookup(Name, FoundDecls);
3178 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3179 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003180 continue;
3181
Douglas Gregorb75a3452011-10-15 00:10:27 +00003182 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003183 break;
3184 }
3185
3186 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003187 if (!ToIface || !ToIface->hasDefinition()) {
Douglas Gregora12d2942010-02-16 01:20:57 +00003188 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003189 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3190 Importer.Import(D->getAtStartLoc()),
Douglas Gregor0af55012011-12-16 03:12:41 +00003191 Name.getAsIdentifierInfo(),
3192 /*PrevDecl=*/0,Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003193 D->isImplicitInterfaceDecl());
3194 ToIface->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003195 LexicalDC->addDeclInternal(ToIface);
Douglas Gregora12d2942010-02-16 01:20:57 +00003196 }
3197 Importer.Imported(D, ToIface);
3198
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003199 if (D->hasDefinition()) {
3200 if (!ToIface->hasDefinition())
3201 ToIface->startDefinition();
Douglas Gregora12d2942010-02-16 01:20:57 +00003202
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003203 if (D->getSuperClass()) {
3204 ObjCInterfaceDecl *Super
3205 = cast_or_null<ObjCInterfaceDecl>(
3206 Importer.Import(D->getSuperClass()));
3207 if (!Super)
3208 return 0;
3209
3210 ToIface->setSuperClass(Super);
3211 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3212 }
3213
3214 // Import protocols
3215 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3216 SmallVector<SourceLocation, 4> ProtocolLocs;
3217 ObjCInterfaceDecl::protocol_loc_iterator
3218 FromProtoLoc = D->protocol_loc_begin();
3219
3220 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3221 FromProtoEnd = D->protocol_end();
3222 FromProto != FromProtoEnd;
3223 ++FromProto, ++FromProtoLoc) {
3224 ObjCProtocolDecl *ToProto
3225 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3226 if (!ToProto)
3227 return 0;
3228 Protocols.push_back(ToProto);
3229 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3230 }
3231
3232 // FIXME: If we're merging, make sure that the protocol list is the same.
3233 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3234 ProtocolLocs.data(), Importer.getToContext());
Douglas Gregora12d2942010-02-16 01:20:57 +00003235 }
3236
Douglas Gregora12d2942010-02-16 01:20:57 +00003237 // Import @end range
3238 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3239 } else {
3240 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003241
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003242 if (D->hasDefinition()) {
3243 // Check for consistency of superclasses.
3244 DeclarationName FromSuperName, ToSuperName;
3245
3246 // If the superclass hasn't been imported yet, do so before checking.
3247 ObjCInterfaceDecl *DSuperClass = D->getSuperClass();
3248 ObjCInterfaceDecl *ToIfaceSuperClass = ToIface->getSuperClass();
3249
3250 if (DSuperClass && !ToIfaceSuperClass) {
3251 Decl *ImportedSuperClass = Importer.Import(DSuperClass);
3252 ObjCInterfaceDecl *ImportedSuperIface
3253 = cast<ObjCInterfaceDecl>(ImportedSuperClass);
3254
3255 ToIface->setSuperClass(ImportedSuperIface);
3256 }
Sean Callananb1ce7302011-11-11 17:39:52 +00003257
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003258 if (D->getSuperClass())
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003259 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3260 if (ToIface->getSuperClass())
3261 ToSuperName = ToIface->getSuperClass()->getDeclName();
3262 if (FromSuperName != ToSuperName) {
3263 Importer.ToDiag(ToIface->getLocation(),
3264 diag::err_odr_objc_superclass_inconsistent)
3265 << ToIface->getDeclName();
3266 if (ToIface->getSuperClass())
3267 Importer.ToDiag(ToIface->getSuperClassLoc(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003268 diag::note_odr_objc_superclass)
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003269 << ToIface->getSuperClass()->getDeclName();
3270 else
3271 Importer.ToDiag(ToIface->getLocation(),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003272 diag::note_odr_objc_missing_superclass);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003273 if (D->getSuperClass())
3274 Importer.FromDiag(D->getSuperClassLoc(),
3275 diag::note_odr_objc_superclass)
3276 << D->getSuperClass()->getDeclName();
3277 else
3278 Importer.FromDiag(D->getLocation(),
3279 diag::note_odr_objc_missing_superclass);
3280 return 0;
3281 }
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003282 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003283 }
3284
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003285 if (!D->hasDefinition())
3286 return ToIface;
3287
Douglas Gregorb4677b62010-02-18 01:47:50 +00003288 // Import categories. When the categories themselves are imported, they'll
3289 // hook themselves into this interface.
3290 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3291 FromCat = FromCat->getNextClassCategory())
3292 Importer.Import(FromCat);
3293
Douglas Gregora12d2942010-02-16 01:20:57 +00003294 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003295 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003296
3297 // If we have an @implementation, import it as well.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00003298 if ( D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003299 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3300 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003301 if (!Impl)
3302 return 0;
3303
3304 ToIface->setImplementation(Impl);
3305 }
3306
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003307 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003308}
3309
Douglas Gregor3daef292010-12-07 15:32:12 +00003310Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3311 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3312 Importer.Import(D->getCategoryDecl()));
3313 if (!Category)
3314 return 0;
3315
3316 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3317 if (!ToImpl) {
3318 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3319 if (!DC)
3320 return 0;
3321
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003322 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor3daef292010-12-07 15:32:12 +00003323 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003324 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003325 Category->getClassInterface(),
3326 Importer.Import(D->getLocation()),
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +00003327 Importer.Import(D->getAtStartLoc()),
3328 CategoryNameLoc);
Douglas Gregor3daef292010-12-07 15:32:12 +00003329
3330 DeclContext *LexicalDC = DC;
3331 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3332 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3333 if (!LexicalDC)
3334 return 0;
3335
3336 ToImpl->setLexicalDeclContext(LexicalDC);
3337 }
3338
Sean Callanan9faf8102011-10-21 02:57:43 +00003339 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor3daef292010-12-07 15:32:12 +00003340 Category->setImplementation(ToImpl);
3341 }
3342
3343 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003344 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003345 return ToImpl;
3346}
3347
Douglas Gregordd182ff2010-12-07 01:26:03 +00003348Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3349 // Find the corresponding interface.
3350 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3351 Importer.Import(D->getClassInterface()));
3352 if (!Iface)
3353 return 0;
3354
3355 // Import the superclass, if any.
3356 ObjCInterfaceDecl *Super = 0;
3357 if (D->getSuperClass()) {
3358 Super = cast_or_null<ObjCInterfaceDecl>(
3359 Importer.Import(D->getSuperClass()));
3360 if (!Super)
3361 return 0;
3362 }
3363
3364 ObjCImplementationDecl *Impl = Iface->getImplementation();
3365 if (!Impl) {
3366 // We haven't imported an implementation yet. Create a new @implementation
3367 // now.
3368 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3369 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003370 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003371 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003372 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003373
3374 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3375 DeclContext *LexicalDC
3376 = Importer.ImportContext(D->getLexicalDeclContext());
3377 if (!LexicalDC)
3378 return 0;
3379 Impl->setLexicalDeclContext(LexicalDC);
3380 }
3381
3382 // Associate the implementation with the class it implements.
3383 Iface->setImplementation(Impl);
3384 Importer.Imported(D, Iface->getImplementation());
3385 } else {
3386 Importer.Imported(D, Iface->getImplementation());
3387
3388 // Verify that the existing @implementation has the same superclass.
3389 if ((Super && !Impl->getSuperClass()) ||
3390 (!Super && Impl->getSuperClass()) ||
3391 (Super && Impl->getSuperClass() &&
Douglas Gregor60ef3082011-12-15 00:29:59 +00003392 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003393 Importer.ToDiag(Impl->getLocation(),
3394 diag::err_odr_objc_superclass_inconsistent)
3395 << Iface->getDeclName();
3396 // FIXME: It would be nice to have the location of the superclass
3397 // below.
3398 if (Impl->getSuperClass())
3399 Importer.ToDiag(Impl->getLocation(),
3400 diag::note_odr_objc_superclass)
3401 << Impl->getSuperClass()->getDeclName();
3402 else
3403 Importer.ToDiag(Impl->getLocation(),
3404 diag::note_odr_objc_missing_superclass);
3405 if (D->getSuperClass())
3406 Importer.FromDiag(D->getLocation(),
3407 diag::note_odr_objc_superclass)
3408 << D->getSuperClass()->getDeclName();
3409 else
3410 Importer.FromDiag(D->getLocation(),
3411 diag::note_odr_objc_missing_superclass);
3412 return 0;
3413 }
3414 }
3415
3416 // Import all of the members of this @implementation.
3417 ImportDeclContext(D);
3418
3419 return Impl;
3420}
3421
Douglas Gregore3261622010-02-17 18:02:10 +00003422Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3423 // Import the major distinguishing characteristics of an @property.
3424 DeclContext *DC, *LexicalDC;
3425 DeclarationName Name;
3426 SourceLocation Loc;
3427 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3428 return 0;
3429
3430 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003431 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3432 DC->localUncachedLookup(Name, FoundDecls);
3433 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003434 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003435 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003436 // Check property types.
3437 if (!Importer.IsStructurallyEquivalent(D->getType(),
3438 FoundProp->getType())) {
3439 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3440 << Name << D->getType() << FoundProp->getType();
3441 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3442 << FoundProp->getType();
3443 return 0;
3444 }
3445
3446 // FIXME: Check property attributes, getters, setters, etc.?
3447
3448 // Consider these properties to be equivalent.
3449 Importer.Imported(D, FoundProp);
3450 return FoundProp;
3451 }
3452 }
3453
3454 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003455 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3456 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003457 return 0;
3458
3459 // Create the new property.
3460 ObjCPropertyDecl *ToProperty
3461 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3462 Name.getAsIdentifierInfo(),
3463 Importer.Import(D->getAtLoc()),
3464 T,
3465 D->getPropertyImplementation());
3466 Importer.Imported(D, ToProperty);
3467 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003468 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregore3261622010-02-17 18:02:10 +00003469
3470 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003471 ToProperty->setPropertyAttributesAsWritten(
3472 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003473 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3474 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3475 ToProperty->setGetterMethodDecl(
3476 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3477 ToProperty->setSetterMethodDecl(
3478 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3479 ToProperty->setPropertyIvarDecl(
3480 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3481 return ToProperty;
3482}
3483
Douglas Gregor954e0c72010-12-07 18:32:03 +00003484Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3485 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3486 Importer.Import(D->getPropertyDecl()));
3487 if (!Property)
3488 return 0;
3489
3490 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3491 if (!DC)
3492 return 0;
3493
3494 // Import the lexical declaration context.
3495 DeclContext *LexicalDC = DC;
3496 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3497 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3498 if (!LexicalDC)
3499 return 0;
3500 }
3501
3502 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3503 if (!InImpl)
3504 return 0;
3505
3506 // Import the ivar (for an @synthesize).
3507 ObjCIvarDecl *Ivar = 0;
3508 if (D->getPropertyIvarDecl()) {
3509 Ivar = cast_or_null<ObjCIvarDecl>(
3510 Importer.Import(D->getPropertyIvarDecl()));
3511 if (!Ivar)
3512 return 0;
3513 }
3514
3515 ObjCPropertyImplDecl *ToImpl
3516 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3517 if (!ToImpl) {
3518 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3519 Importer.Import(D->getLocStart()),
3520 Importer.Import(D->getLocation()),
3521 Property,
3522 D->getPropertyImplementation(),
3523 Ivar,
3524 Importer.Import(D->getPropertyIvarDeclLoc()));
3525 ToImpl->setLexicalDeclContext(LexicalDC);
3526 Importer.Imported(D, ToImpl);
Sean Callanan9faf8102011-10-21 02:57:43 +00003527 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor954e0c72010-12-07 18:32:03 +00003528 } else {
3529 // Check that we have the same kind of property implementation (@synthesize
3530 // vs. @dynamic).
3531 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3532 Importer.ToDiag(ToImpl->getLocation(),
3533 diag::err_odr_objc_property_impl_kind_inconsistent)
3534 << Property->getDeclName()
3535 << (ToImpl->getPropertyImplementation()
3536 == ObjCPropertyImplDecl::Dynamic);
3537 Importer.FromDiag(D->getLocation(),
3538 diag::note_odr_objc_property_impl_kind)
3539 << D->getPropertyDecl()->getDeclName()
3540 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3541 return 0;
3542 }
3543
3544 // For @synthesize, check that we have the same
3545 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3546 Ivar != ToImpl->getPropertyIvarDecl()) {
3547 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3548 diag::err_odr_objc_synthesize_ivar_inconsistent)
3549 << Property->getDeclName()
3550 << ToImpl->getPropertyIvarDecl()->getDeclName()
3551 << Ivar->getDeclName();
3552 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3553 diag::note_odr_objc_synthesize_ivar_here)
3554 << D->getPropertyIvarDecl()->getDeclName();
3555 return 0;
3556 }
3557
3558 // Merge the existing implementation with the new implementation.
3559 Importer.Imported(D, ToImpl);
3560 }
3561
3562 return ToImpl;
3563}
3564
Douglas Gregor040afae2010-11-30 19:14:50 +00003565Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3566 // For template arguments, we adopt the translation unit as our declaration
3567 // context. This context will be fixed when the actual template declaration
3568 // is created.
3569
3570 // FIXME: Import default argument.
3571 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3572 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003573 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003574 Importer.Import(D->getLocation()),
3575 D->getDepth(),
3576 D->getIndex(),
3577 Importer.Import(D->getIdentifier()),
3578 D->wasDeclaredWithTypename(),
3579 D->isParameterPack());
3580}
3581
3582Decl *
3583ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3584 // Import the name of this declaration.
3585 DeclarationName Name = Importer.Import(D->getDeclName());
3586 if (D->getDeclName() && !Name)
3587 return 0;
3588
3589 // Import the location of this declaration.
3590 SourceLocation Loc = Importer.Import(D->getLocation());
3591
3592 // Import the type of this declaration.
3593 QualType T = Importer.Import(D->getType());
3594 if (T.isNull())
3595 return 0;
3596
3597 // Import type-source information.
3598 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3599 if (D->getTypeSourceInfo() && !TInfo)
3600 return 0;
3601
3602 // FIXME: Import default argument.
3603
3604 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3605 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003606 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003607 Loc, D->getDepth(), D->getPosition(),
3608 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003609 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003610}
3611
3612Decl *
3613ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3614 // Import the name of this declaration.
3615 DeclarationName Name = Importer.Import(D->getDeclName());
3616 if (D->getDeclName() && !Name)
3617 return 0;
3618
3619 // Import the location of this declaration.
3620 SourceLocation Loc = Importer.Import(D->getLocation());
3621
3622 // Import template parameters.
3623 TemplateParameterList *TemplateParams
3624 = ImportTemplateParameterList(D->getTemplateParameters());
3625 if (!TemplateParams)
3626 return 0;
3627
3628 // FIXME: Import default argument.
3629
3630 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3631 Importer.getToContext().getTranslationUnitDecl(),
3632 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003633 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003634 Name.getAsIdentifierInfo(),
3635 TemplateParams);
3636}
3637
3638Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3639 // If this record has a definition in the translation unit we're coming from,
3640 // but this particular declaration is not that definition, import the
3641 // definition and map to that.
3642 CXXRecordDecl *Definition
3643 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3644 if (Definition && Definition != D->getTemplatedDecl()) {
3645 Decl *ImportedDef
3646 = Importer.Import(Definition->getDescribedClassTemplate());
3647 if (!ImportedDef)
3648 return 0;
3649
3650 return Importer.Imported(D, ImportedDef);
3651 }
3652
3653 // Import the major distinguishing characteristics of this class template.
3654 DeclContext *DC, *LexicalDC;
3655 DeclarationName Name;
3656 SourceLocation Loc;
3657 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3658 return 0;
3659
3660 // We may already have a template of the same name; try to find and match it.
3661 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003662 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003663 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3664 DC->localUncachedLookup(Name, FoundDecls);
3665 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3666 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003667 continue;
3668
Douglas Gregorb75a3452011-10-15 00:10:27 +00003669 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003670 if (ClassTemplateDecl *FoundTemplate
3671 = dyn_cast<ClassTemplateDecl>(Found)) {
3672 if (IsStructuralMatch(D, FoundTemplate)) {
3673 // The class templates structurally match; call it the same template.
3674 // FIXME: We may be filling in a forward declaration here. Handle
3675 // this case!
3676 Importer.Imported(D->getTemplatedDecl(),
3677 FoundTemplate->getTemplatedDecl());
3678 return Importer.Imported(D, FoundTemplate);
3679 }
3680 }
3681
Douglas Gregorb75a3452011-10-15 00:10:27 +00003682 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003683 }
3684
3685 if (!ConflictingDecls.empty()) {
3686 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3687 ConflictingDecls.data(),
3688 ConflictingDecls.size());
3689 }
3690
3691 if (!Name)
3692 return 0;
3693 }
3694
3695 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3696
3697 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003698 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3699 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003700 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3701 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003702 DC, StartLoc, IdLoc,
3703 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003704 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003705 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003706 D2Templated->setLexicalDeclContext(LexicalDC);
3707
3708 // Create the class template declaration itself.
3709 TemplateParameterList *TemplateParams
3710 = ImportTemplateParameterList(D->getTemplateParameters());
3711 if (!TemplateParams)
3712 return 0;
3713
3714 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3715 Loc, Name, TemplateParams,
3716 D2Templated,
3717 /*PrevDecl=*/0);
3718 D2Templated->setDescribedClassTemplate(D2);
3719
3720 D2->setAccess(D->getAccess());
3721 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003722 LexicalDC->addDeclInternal(D2);
Douglas Gregor040afae2010-11-30 19:14:50 +00003723
3724 // Note the relationship between the class templates.
3725 Importer.Imported(D, D2);
3726 Importer.Imported(DTemplated, D2Templated);
3727
John McCall5e1cdac2011-10-07 06:10:15 +00003728 if (DTemplated->isCompleteDefinition() &&
3729 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003730 // FIXME: Import definition!
3731 }
3732
3733 return D2;
3734}
3735
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003736Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3737 ClassTemplateSpecializationDecl *D) {
3738 // If this record has a definition in the translation unit we're coming from,
3739 // but this particular declaration is not that definition, import the
3740 // definition and map to that.
3741 TagDecl *Definition = D->getDefinition();
3742 if (Definition && Definition != D) {
3743 Decl *ImportedDef = Importer.Import(Definition);
3744 if (!ImportedDef)
3745 return 0;
3746
3747 return Importer.Imported(D, ImportedDef);
3748 }
3749
3750 ClassTemplateDecl *ClassTemplate
3751 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3752 D->getSpecializedTemplate()));
3753 if (!ClassTemplate)
3754 return 0;
3755
3756 // Import the context of this declaration.
3757 DeclContext *DC = ClassTemplate->getDeclContext();
3758 if (!DC)
3759 return 0;
3760
3761 DeclContext *LexicalDC = DC;
3762 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3763 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3764 if (!LexicalDC)
3765 return 0;
3766 }
3767
3768 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003769 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3770 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003771
3772 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003773 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003774 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3775 D->getTemplateArgs().size(),
3776 TemplateArgs))
3777 return 0;
3778
3779 // Try to find an existing specialization with these template arguments.
3780 void *InsertPos = 0;
3781 ClassTemplateSpecializationDecl *D2
3782 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3783 TemplateArgs.size(), InsertPos);
3784 if (D2) {
3785 // We already have a class template specialization with these template
3786 // arguments.
3787
3788 // FIXME: Check for specialization vs. instantiation errors.
3789
3790 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003791 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003792 // The record types structurally match, or the "from" translation
3793 // unit only had a forward declaration anyway; call it the same
3794 // function.
3795 return Importer.Imported(D, FoundDef);
3796 }
3797 }
3798 } else {
3799 // Create a new specialization.
3800 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3801 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003802 StartLoc, IdLoc,
3803 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003804 TemplateArgs.data(),
3805 TemplateArgs.size(),
3806 /*PrevDecl=*/0);
3807 D2->setSpecializationKind(D->getSpecializationKind());
3808
3809 // Add this specialization to the class template.
3810 ClassTemplate->AddSpecialization(D2, InsertPos);
3811
3812 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003813 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003814
3815 // Add the specialization to this context.
3816 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan9faf8102011-10-21 02:57:43 +00003817 LexicalDC->addDeclInternal(D2);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003818 }
3819 Importer.Imported(D, D2);
3820
John McCall5e1cdac2011-10-07 06:10:15 +00003821 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003822 return 0;
3823
3824 return D2;
3825}
3826
Douglas Gregor4800d952010-02-11 19:21:55 +00003827//----------------------------------------------------------------------------
3828// Import Statements
3829//----------------------------------------------------------------------------
3830
3831Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3832 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3833 << S->getStmtClassName();
3834 return 0;
3835}
3836
3837//----------------------------------------------------------------------------
3838// Import Expressions
3839//----------------------------------------------------------------------------
3840Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3841 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3842 << E->getStmtClassName();
3843 return 0;
3844}
3845
Douglas Gregor44080632010-02-19 01:17:02 +00003846Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003847 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3848 if (!ToD)
3849 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003850
3851 NamedDecl *FoundD = 0;
3852 if (E->getDecl() != E->getFoundDecl()) {
3853 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3854 if (!FoundD)
3855 return 0;
3856 }
Douglas Gregor44080632010-02-19 01:17:02 +00003857
3858 QualType T = Importer.Import(E->getType());
3859 if (T.isNull())
3860 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003861
3862 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3863 Importer.Import(E->getQualifierLoc()),
3864 ToD,
3865 Importer.Import(E->getLocation()),
3866 T, E->getValueKind(),
3867 FoundD,
3868 /*FIXME:TemplateArgs=*/0);
3869 if (E->hadMultipleCandidates())
3870 DRE->setHadMultipleCandidates(true);
3871 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003872}
3873
Douglas Gregor4800d952010-02-11 19:21:55 +00003874Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3875 QualType T = Importer.Import(E->getType());
3876 if (T.isNull())
3877 return 0;
3878
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003879 return IntegerLiteral::Create(Importer.getToContext(),
3880 E->getValue(), T,
3881 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003882}
3883
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003884Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3885 QualType T = Importer.Import(E->getType());
3886 if (T.isNull())
3887 return 0;
3888
Douglas Gregor5cee1192011-07-27 05:40:30 +00003889 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3890 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003891 Importer.Import(E->getLocation()));
3892}
3893
Douglas Gregorf638f952010-02-19 01:07:06 +00003894Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3895 Expr *SubExpr = Importer.Import(E->getSubExpr());
3896 if (!SubExpr)
3897 return 0;
3898
3899 return new (Importer.getToContext())
3900 ParenExpr(Importer.Import(E->getLParen()),
3901 Importer.Import(E->getRParen()),
3902 SubExpr);
3903}
3904
3905Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3906 QualType T = Importer.Import(E->getType());
3907 if (T.isNull())
3908 return 0;
3909
3910 Expr *SubExpr = Importer.Import(E->getSubExpr());
3911 if (!SubExpr)
3912 return 0;
3913
3914 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003915 T, E->getValueKind(),
3916 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003917 Importer.Import(E->getOperatorLoc()));
3918}
3919
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003920Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3921 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003922 QualType ResultType = Importer.Import(E->getType());
3923
3924 if (E->isArgumentType()) {
3925 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3926 if (!TInfo)
3927 return 0;
3928
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003929 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3930 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003931 Importer.Import(E->getOperatorLoc()),
3932 Importer.Import(E->getRParenLoc()));
3933 }
3934
3935 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3936 if (!SubExpr)
3937 return 0;
3938
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003939 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3940 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003941 Importer.Import(E->getOperatorLoc()),
3942 Importer.Import(E->getRParenLoc()));
3943}
3944
Douglas Gregorf638f952010-02-19 01:07:06 +00003945Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3946 QualType T = Importer.Import(E->getType());
3947 if (T.isNull())
3948 return 0;
3949
3950 Expr *LHS = Importer.Import(E->getLHS());
3951 if (!LHS)
3952 return 0;
3953
3954 Expr *RHS = Importer.Import(E->getRHS());
3955 if (!RHS)
3956 return 0;
3957
3958 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003959 T, E->getValueKind(),
3960 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003961 Importer.Import(E->getOperatorLoc()));
3962}
3963
3964Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3965 QualType T = Importer.Import(E->getType());
3966 if (T.isNull())
3967 return 0;
3968
3969 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3970 if (CompLHSType.isNull())
3971 return 0;
3972
3973 QualType CompResultType = Importer.Import(E->getComputationResultType());
3974 if (CompResultType.isNull())
3975 return 0;
3976
3977 Expr *LHS = Importer.Import(E->getLHS());
3978 if (!LHS)
3979 return 0;
3980
3981 Expr *RHS = Importer.Import(E->getRHS());
3982 if (!RHS)
3983 return 0;
3984
3985 return new (Importer.getToContext())
3986 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003987 T, E->getValueKind(),
3988 E->getObjectKind(),
3989 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003990 Importer.Import(E->getOperatorLoc()));
3991}
3992
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003993static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003994 if (E->path_empty()) return false;
3995
3996 // TODO: import cast paths
3997 return true;
3998}
3999
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004000Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4001 QualType T = Importer.Import(E->getType());
4002 if (T.isNull())
4003 return 0;
4004
4005 Expr *SubExpr = Importer.Import(E->getSubExpr());
4006 if (!SubExpr)
4007 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004008
4009 CXXCastPath BasePath;
4010 if (ImportCastPath(E, BasePath))
4011 return 0;
4012
4013 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004014 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004015}
4016
Douglas Gregor008847a2010-02-19 01:32:14 +00004017Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4018 QualType T = Importer.Import(E->getType());
4019 if (T.isNull())
4020 return 0;
4021
4022 Expr *SubExpr = Importer.Import(E->getSubExpr());
4023 if (!SubExpr)
4024 return 0;
4025
4026 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4027 if (!TInfo && E->getTypeInfoAsWritten())
4028 return 0;
4029
John McCallf871d0c2010-08-07 06:22:56 +00004030 CXXCastPath BasePath;
4031 if (ImportCastPath(E, BasePath))
4032 return 0;
4033
John McCallf89e55a2010-11-18 06:31:45 +00004034 return CStyleCastExpr::Create(Importer.getToContext(), T,
4035 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004036 SubExpr, &BasePath, TInfo,
4037 Importer.Import(E->getLParenLoc()),
4038 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004039}
4040
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004041ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004042 ASTContext &FromContext, FileManager &FromFileManager,
4043 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004044 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004045 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4046 Minimal(MinimalImport)
4047{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004048 ImportedDecls[FromContext.getTranslationUnitDecl()]
4049 = ToContext.getTranslationUnitDecl();
4050}
4051
4052ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004053
4054QualType ASTImporter::Import(QualType FromT) {
4055 if (FromT.isNull())
4056 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004057
4058 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004059
Douglas Gregor169fba52010-02-08 15:18:58 +00004060 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004061 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4062 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004063 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004064 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004065
Douglas Gregor169fba52010-02-08 15:18:58 +00004066 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004067 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004068 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004069 if (ToT.isNull())
4070 return ToT;
4071
Douglas Gregor169fba52010-02-08 15:18:58 +00004072 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004073 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004074
John McCallf4c73712011-01-19 06:33:43 +00004075 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004076}
4077
Douglas Gregor9bed8792010-02-09 19:21:46 +00004078TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004079 if (!FromTSI)
4080 return FromTSI;
4081
4082 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004083 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004084 QualType T = Import(FromTSI->getType());
4085 if (T.isNull())
4086 return 0;
4087
4088 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004089 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004090}
4091
4092Decl *ASTImporter::Import(Decl *FromD) {
4093 if (!FromD)
4094 return 0;
4095
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004096 ASTNodeImporter Importer(*this);
4097
Douglas Gregor9bed8792010-02-09 19:21:46 +00004098 // Check whether we've already imported this declaration.
4099 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004100 if (Pos != ImportedDecls.end()) {
4101 Decl *ToD = Pos->second;
4102 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4103 return ToD;
4104 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004105
4106 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004107 Decl *ToD = Importer.Visit(FromD);
4108 if (!ToD)
4109 return 0;
4110
4111 // Record the imported declaration.
4112 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004113
4114 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4115 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004116 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004117 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004118 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004119 // When we've finished transforming a typedef, see whether it was the
4120 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004121 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004122 FromTag = AnonTagsWithPendingTypedefs.begin(),
4123 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4124 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004125 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004126 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4127 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004128 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004129 AnonTagsWithPendingTypedefs.erase(FromTag);
4130 break;
4131 }
4132 }
4133 }
4134 }
4135
Douglas Gregor9bed8792010-02-09 19:21:46 +00004136 return ToD;
4137}
4138
4139DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4140 if (!FromDC)
4141 return FromDC;
4142
4143 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4144}
4145
4146Expr *ASTImporter::Import(Expr *FromE) {
4147 if (!FromE)
4148 return 0;
4149
4150 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4151}
4152
4153Stmt *ASTImporter::Import(Stmt *FromS) {
4154 if (!FromS)
4155 return 0;
4156
Douglas Gregor4800d952010-02-11 19:21:55 +00004157 // Check whether we've already imported this declaration.
4158 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4159 if (Pos != ImportedStmts.end())
4160 return Pos->second;
4161
4162 // Import the type
4163 ASTNodeImporter Importer(*this);
4164 Stmt *ToS = Importer.Visit(FromS);
4165 if (!ToS)
4166 return 0;
4167
4168 // Record the imported declaration.
4169 ImportedStmts[FromS] = ToS;
4170 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004171}
4172
4173NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4174 if (!FromNNS)
4175 return 0;
4176
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004177 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4178
4179 switch (FromNNS->getKind()) {
4180 case NestedNameSpecifier::Identifier:
4181 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4182 return NestedNameSpecifier::Create(ToContext, prefix, II);
4183 }
4184 return 0;
4185
4186 case NestedNameSpecifier::Namespace:
4187 if (NamespaceDecl *NS =
4188 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4189 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4190 }
4191 return 0;
4192
4193 case NestedNameSpecifier::NamespaceAlias:
4194 if (NamespaceAliasDecl *NSAD =
4195 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4196 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4197 }
4198 return 0;
4199
4200 case NestedNameSpecifier::Global:
4201 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4202
4203 case NestedNameSpecifier::TypeSpec:
4204 case NestedNameSpecifier::TypeSpecWithTemplate: {
4205 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4206 if (!T.isNull()) {
4207 bool bTemplate = FromNNS->getKind() ==
4208 NestedNameSpecifier::TypeSpecWithTemplate;
4209 return NestedNameSpecifier::Create(ToContext, prefix,
4210 bTemplate, T.getTypePtr());
4211 }
4212 }
4213 return 0;
4214 }
4215
4216 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004217 return 0;
4218}
4219
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004220NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4221 // FIXME: Implement!
4222 return NestedNameSpecifierLoc();
4223}
4224
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004225TemplateName ASTImporter::Import(TemplateName From) {
4226 switch (From.getKind()) {
4227 case TemplateName::Template:
4228 if (TemplateDecl *ToTemplate
4229 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4230 return TemplateName(ToTemplate);
4231
4232 return TemplateName();
4233
4234 case TemplateName::OverloadedTemplate: {
4235 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4236 UnresolvedSet<2> ToTemplates;
4237 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4238 E = FromStorage->end();
4239 I != E; ++I) {
4240 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4241 ToTemplates.addDecl(To);
4242 else
4243 return TemplateName();
4244 }
4245 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4246 ToTemplates.end());
4247 }
4248
4249 case TemplateName::QualifiedTemplate: {
4250 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4251 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4252 if (!Qualifier)
4253 return TemplateName();
4254
4255 if (TemplateDecl *ToTemplate
4256 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4257 return ToContext.getQualifiedTemplateName(Qualifier,
4258 QTN->hasTemplateKeyword(),
4259 ToTemplate);
4260
4261 return TemplateName();
4262 }
4263
4264 case TemplateName::DependentTemplate: {
4265 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4266 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4267 if (!Qualifier)
4268 return TemplateName();
4269
4270 if (DTN->isIdentifier()) {
4271 return ToContext.getDependentTemplateName(Qualifier,
4272 Import(DTN->getIdentifier()));
4273 }
4274
4275 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4276 }
John McCall14606042011-06-30 08:33:18 +00004277
4278 case TemplateName::SubstTemplateTemplateParm: {
4279 SubstTemplateTemplateParmStorage *subst
4280 = From.getAsSubstTemplateTemplateParm();
4281 TemplateTemplateParmDecl *param
4282 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4283 if (!param)
4284 return TemplateName();
4285
4286 TemplateName replacement = Import(subst->getReplacement());
4287 if (replacement.isNull()) return TemplateName();
4288
4289 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4290 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004291
4292 case TemplateName::SubstTemplateTemplateParmPack: {
4293 SubstTemplateTemplateParmPackStorage *SubstPack
4294 = From.getAsSubstTemplateTemplateParmPack();
4295 TemplateTemplateParmDecl *Param
4296 = cast_or_null<TemplateTemplateParmDecl>(
4297 Import(SubstPack->getParameterPack()));
4298 if (!Param)
4299 return TemplateName();
4300
4301 ASTNodeImporter Importer(*this);
4302 TemplateArgument ArgPack
4303 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4304 if (ArgPack.isNull())
4305 return TemplateName();
4306
4307 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4308 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004309 }
4310
4311 llvm_unreachable("Invalid template name kind");
4312 return TemplateName();
4313}
4314
Douglas Gregor9bed8792010-02-09 19:21:46 +00004315SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4316 if (FromLoc.isInvalid())
4317 return SourceLocation();
4318
Douglas Gregor88523732010-02-10 00:15:17 +00004319 SourceManager &FromSM = FromContext.getSourceManager();
4320
4321 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004322 // don't have to import macro expansions.
4323 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004324 FromLoc = FromSM.getSpellingLoc(FromLoc);
4325 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4326 SourceManager &ToSM = ToContext.getSourceManager();
4327 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004328 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004329}
4330
4331SourceRange ASTImporter::Import(SourceRange FromRange) {
4332 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4333}
4334
Douglas Gregor88523732010-02-10 00:15:17 +00004335FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004336 llvm::DenseMap<FileID, FileID>::iterator Pos
4337 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004338 if (Pos != ImportedFileIDs.end())
4339 return Pos->second;
4340
4341 SourceManager &FromSM = FromContext.getSourceManager();
4342 SourceManager &ToSM = ToContext.getSourceManager();
4343 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004344 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004345
4346 // Include location of this file.
4347 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4348
4349 // Map the FileID for to the "to" source manager.
4350 FileID ToID;
4351 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004352 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004353 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4354 // disk again
4355 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4356 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004357 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004358 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4359 FromSLoc.getFile().getFileCharacteristic());
4360 } else {
4361 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004362 const llvm::MemoryBuffer *
4363 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004364 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004365 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004366 FromBuf->getBufferIdentifier());
4367 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4368 }
4369
4370
Sebastian Redl535a3e22010-09-30 01:03:06 +00004371 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004372 return ToID;
4373}
4374
Douglas Gregord8868a62011-01-18 03:11:38 +00004375void ASTImporter::ImportDefinition(Decl *From) {
4376 Decl *To = Import(From);
4377 if (!To)
4378 return;
4379
4380 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4381 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004382
4383 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4384 if (!ToRecord->getDefinition()) {
4385 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4386 /*ForceImport=*/true);
4387 return;
4388 }
4389 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004390
4391 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4392 if (!ToEnum->getDefinition()) {
4393 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4394 /*ForceImport=*/true);
4395 return;
4396 }
4397 }
4398
Douglas Gregord8868a62011-01-18 03:11:38 +00004399 Importer.ImportDeclContext(FromDC, true);
4400 }
4401}
4402
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004403DeclarationName ASTImporter::Import(DeclarationName FromName) {
4404 if (!FromName)
4405 return DeclarationName();
4406
4407 switch (FromName.getNameKind()) {
4408 case DeclarationName::Identifier:
4409 return Import(FromName.getAsIdentifierInfo());
4410
4411 case DeclarationName::ObjCZeroArgSelector:
4412 case DeclarationName::ObjCOneArgSelector:
4413 case DeclarationName::ObjCMultiArgSelector:
4414 return Import(FromName.getObjCSelector());
4415
4416 case DeclarationName::CXXConstructorName: {
4417 QualType T = Import(FromName.getCXXNameType());
4418 if (T.isNull())
4419 return DeclarationName();
4420
4421 return ToContext.DeclarationNames.getCXXConstructorName(
4422 ToContext.getCanonicalType(T));
4423 }
4424
4425 case DeclarationName::CXXDestructorName: {
4426 QualType T = Import(FromName.getCXXNameType());
4427 if (T.isNull())
4428 return DeclarationName();
4429
4430 return ToContext.DeclarationNames.getCXXDestructorName(
4431 ToContext.getCanonicalType(T));
4432 }
4433
4434 case DeclarationName::CXXConversionFunctionName: {
4435 QualType T = Import(FromName.getCXXNameType());
4436 if (T.isNull())
4437 return DeclarationName();
4438
4439 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4440 ToContext.getCanonicalType(T));
4441 }
4442
4443 case DeclarationName::CXXOperatorName:
4444 return ToContext.DeclarationNames.getCXXOperatorName(
4445 FromName.getCXXOverloadedOperator());
4446
4447 case DeclarationName::CXXLiteralOperatorName:
4448 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4449 Import(FromName.getCXXLiteralIdentifier()));
4450
4451 case DeclarationName::CXXUsingDirective:
4452 // FIXME: STATICS!
4453 return DeclarationName::getUsingDirectiveName();
4454 }
4455
4456 // Silence bogus GCC warning
4457 return DeclarationName();
4458}
4459
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004460IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004461 if (!FromId)
4462 return 0;
4463
4464 return &ToContext.Idents.get(FromId->getName());
4465}
Douglas Gregor089459a2010-02-08 21:09:39 +00004466
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004467Selector ASTImporter::Import(Selector FromSel) {
4468 if (FromSel.isNull())
4469 return Selector();
4470
Chris Lattner5f9e2722011-07-23 10:55:15 +00004471 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004472 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4473 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4474 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4475 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4476}
4477
Douglas Gregor089459a2010-02-08 21:09:39 +00004478DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4479 DeclContext *DC,
4480 unsigned IDNS,
4481 NamedDecl **Decls,
4482 unsigned NumDecls) {
4483 return Name;
4484}
4485
4486DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004487 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004488}
4489
4490DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004491 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004492}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004493
4494Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4495 ImportedDecls[From] = To;
4496 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004497}
Douglas Gregorea35d112010-02-15 23:54:17 +00004498
4499bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004500 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004501 = ImportedTypes.find(From.getTypePtr());
4502 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4503 return true;
4504
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004505 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004506 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004507}