blob: 7e252d1d163e310a61dce50ec9851169418fa51a [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
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
John McCallf4c73712011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
Sean Callanan0aeb2892011-08-11 16:56:07 +000062 QualType VisitParenType(const ParenType *T);
John McCallf4c73712011-01-19 06:33:43 +000063 QualType VisitTypedefType(const TypedefType *T);
64 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000065 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000066 QualType VisitTypeOfType(const TypeOfType *T);
67 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000068 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000069 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000070 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000071 QualType VisitRecordType(const RecordType *T);
72 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000073 // FIXME: TemplateTypeParmType
74 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000075 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
76 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000077 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000078 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000079 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
80 QualType VisitObjCObjectType(const ObjCObjectType *T);
81 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000082
83 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000084 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
85 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000086 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000087 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000088 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
89 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000090 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000091 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
92 bool ForceImport = false);
93 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
94 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000095 TemplateParameterList *ImportTemplateParameterList(
96 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000097 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
98 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
99 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000100 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000101 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000102 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000103 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000104 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000105 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000106 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000107 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000108 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000109 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000110 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000111 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000112 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000113 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
114 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
115 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
116 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000117 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000118 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000119 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000120 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000121 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000122 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000123 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000124 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000125 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000126 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000127 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000128 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000129 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000130 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000131 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000132 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000133 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
134 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
135 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
136 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000137 Decl *VisitClassTemplateSpecializationDecl(
138 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000139
Douglas Gregor4800d952010-02-11 19:21:55 +0000140 // Importing statements
141 Stmt *VisitStmt(Stmt *S);
142
143 // Importing expressions
144 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000145 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000146 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000147 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000148 Expr *VisitParenExpr(ParenExpr *E);
149 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000150 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000151 Expr *VisitBinaryOperator(BinaryOperator *E);
152 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000153 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000154 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000155 };
156}
157
158//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000159// Structural Equivalence
160//----------------------------------------------------------------------------
161
162namespace {
163 struct StructuralEquivalenceContext {
164 /// \brief AST contexts for which we are checking structural equivalence.
165 ASTContext &C1, &C2;
166
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000167 /// \brief The set of "tentative" equivalences between two canonical
168 /// declarations, mapping from a declaration in the first context to the
169 /// declaration in the second context that we believe to be equivalent.
170 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
171
172 /// \brief Queue of declarations in the first context whose equivalence
173 /// with a declaration in the second context still needs to be verified.
174 std::deque<Decl *> DeclsToCheck;
175
Douglas Gregorea35d112010-02-15 23:54:17 +0000176 /// \brief Declaration (from, to) pairs that are known not to be equivalent
177 /// (which we have already complained about).
178 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
179
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180 /// \brief Whether we're being strict about the spelling of types when
181 /// unifying two types.
182 bool StrictTypeSpelling;
183
184 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000185 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000186 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000187 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000188 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000189
190 /// \brief Determine whether the two declarations are structurally
191 /// equivalent.
192 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
193
194 /// \brief Determine whether the two types are structurally equivalent.
195 bool IsStructurallyEquivalent(QualType T1, QualType T2);
196
197 private:
198 /// \brief Finish checking all of the structural equivalences.
199 ///
200 /// \returns true if an error occurred, false otherwise.
201 bool Finish();
202
203 public:
204 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000205 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000206 }
207
208 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000209 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000210 }
211 };
212}
213
214static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
215 QualType T1, QualType T2);
216static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
217 Decl *D1, Decl *D2);
218
219/// \brief Determine if two APInts have the same value, after zero-extending
220/// one of them (if needed!) to ensure that the bit-widths match.
221static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
222 if (I1.getBitWidth() == I2.getBitWidth())
223 return I1 == I2;
224
225 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000226 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000227
Jay Foad9f71a8f2010-12-07 08:25:34 +0000228 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000229}
230
231/// \brief Determine if two APSInts have the same value, zero- or sign-extending
232/// as needed.
233static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
234 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
235 return I1 == I2;
236
237 // Check for a bit-width mismatch.
238 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000239 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000240 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000241 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000242
243 // We have a signedness mismatch. Turn the signed value into an unsigned
244 // value.
245 if (I1.isSigned()) {
246 if (I1.isNegative())
247 return false;
248
249 return llvm::APSInt(I1, true) == I2;
250 }
251
252 if (I2.isNegative())
253 return false;
254
255 return I1 == llvm::APSInt(I2, true);
256}
257
258/// \brief Determine structural equivalence of two expressions.
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 Expr *E1, Expr *E2) {
261 if (!E1 || !E2)
262 return E1 == E2;
263
264 // FIXME: Actually perform a structural comparison!
265 return true;
266}
267
268/// \brief Determine whether two identifiers are equivalent.
269static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
270 const IdentifierInfo *Name2) {
271 if (!Name1 || !Name2)
272 return Name1 == Name2;
273
274 return Name1->getName() == Name2->getName();
275}
276
277/// \brief Determine whether two nested-name-specifiers are equivalent.
278static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
279 NestedNameSpecifier *NNS1,
280 NestedNameSpecifier *NNS2) {
281 // FIXME: Implement!
282 return true;
283}
284
285/// \brief Determine whether two template arguments are equivalent.
286static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
287 const TemplateArgument &Arg1,
288 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000289 if (Arg1.getKind() != Arg2.getKind())
290 return false;
291
292 switch (Arg1.getKind()) {
293 case TemplateArgument::Null:
294 return true;
295
296 case TemplateArgument::Type:
297 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
298
299 case TemplateArgument::Integral:
300 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
301 Arg2.getIntegralType()))
302 return false;
303
304 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
305
306 case TemplateArgument::Declaration:
307 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
308
309 case TemplateArgument::Template:
310 return IsStructurallyEquivalent(Context,
311 Arg1.getAsTemplate(),
312 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000313
314 case TemplateArgument::TemplateExpansion:
315 return IsStructurallyEquivalent(Context,
316 Arg1.getAsTemplateOrTemplatePattern(),
317 Arg2.getAsTemplateOrTemplatePattern());
318
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000319 case TemplateArgument::Expression:
320 return IsStructurallyEquivalent(Context,
321 Arg1.getAsExpr(), Arg2.getAsExpr());
322
323 case TemplateArgument::Pack:
324 if (Arg1.pack_size() != Arg2.pack_size())
325 return false;
326
327 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
328 if (!IsStructurallyEquivalent(Context,
329 Arg1.pack_begin()[I],
330 Arg2.pack_begin()[I]))
331 return false;
332
333 return true;
334 }
335
336 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000337 return true;
338}
339
340/// \brief Determine structural equivalence for the common part of array
341/// types.
342static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
343 const ArrayType *Array1,
344 const ArrayType *Array2) {
345 if (!IsStructurallyEquivalent(Context,
346 Array1->getElementType(),
347 Array2->getElementType()))
348 return false;
349 if (Array1->getSizeModifier() != Array2->getSizeModifier())
350 return false;
351 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
352 return false;
353
354 return true;
355}
356
357/// \brief Determine structural equivalence of two types.
358static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
359 QualType T1, QualType T2) {
360 if (T1.isNull() || T2.isNull())
361 return T1.isNull() && T2.isNull();
362
363 if (!Context.StrictTypeSpelling) {
364 // We aren't being strict about token-to-token equivalence of types,
365 // so map down to the canonical type.
366 T1 = Context.C1.getCanonicalType(T1);
367 T2 = Context.C2.getCanonicalType(T2);
368 }
369
370 if (T1.getQualifiers() != T2.getQualifiers())
371 return false;
372
Douglas Gregorea35d112010-02-15 23:54:17 +0000373 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000374
Douglas Gregorea35d112010-02-15 23:54:17 +0000375 if (T1->getTypeClass() != T2->getTypeClass()) {
376 // Compare function types with prototypes vs. without prototypes as if
377 // both did not have prototypes.
378 if (T1->getTypeClass() == Type::FunctionProto &&
379 T2->getTypeClass() == Type::FunctionNoProto)
380 TC = Type::FunctionNoProto;
381 else if (T1->getTypeClass() == Type::FunctionNoProto &&
382 T2->getTypeClass() == Type::FunctionProto)
383 TC = Type::FunctionNoProto;
384 else
385 return false;
386 }
387
388 switch (TC) {
389 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000390 // FIXME: Deal with Char_S/Char_U.
391 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
392 return false;
393 break;
394
395 case Type::Complex:
396 if (!IsStructurallyEquivalent(Context,
397 cast<ComplexType>(T1)->getElementType(),
398 cast<ComplexType>(T2)->getElementType()))
399 return false;
400 break;
401
402 case Type::Pointer:
403 if (!IsStructurallyEquivalent(Context,
404 cast<PointerType>(T1)->getPointeeType(),
405 cast<PointerType>(T2)->getPointeeType()))
406 return false;
407 break;
408
409 case Type::BlockPointer:
410 if (!IsStructurallyEquivalent(Context,
411 cast<BlockPointerType>(T1)->getPointeeType(),
412 cast<BlockPointerType>(T2)->getPointeeType()))
413 return false;
414 break;
415
416 case Type::LValueReference:
417 case Type::RValueReference: {
418 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
419 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
420 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
421 return false;
422 if (Ref1->isInnerRef() != Ref2->isInnerRef())
423 return false;
424 if (!IsStructurallyEquivalent(Context,
425 Ref1->getPointeeTypeAsWritten(),
426 Ref2->getPointeeTypeAsWritten()))
427 return false;
428 break;
429 }
430
431 case Type::MemberPointer: {
432 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
433 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
434 if (!IsStructurallyEquivalent(Context,
435 MemPtr1->getPointeeType(),
436 MemPtr2->getPointeeType()))
437 return false;
438 if (!IsStructurallyEquivalent(Context,
439 QualType(MemPtr1->getClass(), 0),
440 QualType(MemPtr2->getClass(), 0)))
441 return false;
442 break;
443 }
444
445 case Type::ConstantArray: {
446 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
447 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
448 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
449 return false;
450
451 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
452 return false;
453 break;
454 }
455
456 case Type::IncompleteArray:
457 if (!IsArrayStructurallyEquivalent(Context,
458 cast<ArrayType>(T1),
459 cast<ArrayType>(T2)))
460 return false;
461 break;
462
463 case Type::VariableArray: {
464 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
465 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
466 if (!IsStructurallyEquivalent(Context,
467 Array1->getSizeExpr(), Array2->getSizeExpr()))
468 return false;
469
470 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
471 return false;
472
473 break;
474 }
475
476 case Type::DependentSizedArray: {
477 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
478 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Array1->getSizeExpr(), Array2->getSizeExpr()))
481 return false;
482
483 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
484 return false;
485
486 break;
487 }
488
489 case Type::DependentSizedExtVector: {
490 const DependentSizedExtVectorType *Vec1
491 = cast<DependentSizedExtVectorType>(T1);
492 const DependentSizedExtVectorType *Vec2
493 = cast<DependentSizedExtVectorType>(T2);
494 if (!IsStructurallyEquivalent(Context,
495 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
496 return false;
497 if (!IsStructurallyEquivalent(Context,
498 Vec1->getElementType(),
499 Vec2->getElementType()))
500 return false;
501 break;
502 }
503
504 case Type::Vector:
505 case Type::ExtVector: {
506 const VectorType *Vec1 = cast<VectorType>(T1);
507 const VectorType *Vec2 = cast<VectorType>(T2);
508 if (!IsStructurallyEquivalent(Context,
509 Vec1->getElementType(),
510 Vec2->getElementType()))
511 return false;
512 if (Vec1->getNumElements() != Vec2->getNumElements())
513 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000514 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000515 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000516 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000517 }
518
519 case Type::FunctionProto: {
520 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
521 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
522 if (Proto1->getNumArgs() != Proto2->getNumArgs())
523 return false;
524 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
525 if (!IsStructurallyEquivalent(Context,
526 Proto1->getArgType(I),
527 Proto2->getArgType(I)))
528 return false;
529 }
530 if (Proto1->isVariadic() != Proto2->isVariadic())
531 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000532 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000533 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000534 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
535 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
536 return false;
537 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
538 if (!IsStructurallyEquivalent(Context,
539 Proto1->getExceptionType(I),
540 Proto2->getExceptionType(I)))
541 return false;
542 }
543 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000544 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000545 Proto1->getNoexceptExpr(),
546 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000547 return false;
548 }
549 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
550 return false;
551
552 // Fall through to check the bits common with FunctionNoProtoType.
553 }
554
555 case Type::FunctionNoProto: {
556 const FunctionType *Function1 = cast<FunctionType>(T1);
557 const FunctionType *Function2 = cast<FunctionType>(T2);
558 if (!IsStructurallyEquivalent(Context,
559 Function1->getResultType(),
560 Function2->getResultType()))
561 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000562 if (Function1->getExtInfo() != Function2->getExtInfo())
563 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000564 break;
565 }
566
567 case Type::UnresolvedUsing:
568 if (!IsStructurallyEquivalent(Context,
569 cast<UnresolvedUsingType>(T1)->getDecl(),
570 cast<UnresolvedUsingType>(T2)->getDecl()))
571 return false;
572
573 break;
John McCall9d156a72011-01-06 01:58:22 +0000574
575 case Type::Attributed:
576 if (!IsStructurallyEquivalent(Context,
577 cast<AttributedType>(T1)->getModifiedType(),
578 cast<AttributedType>(T2)->getModifiedType()))
579 return false;
580 if (!IsStructurallyEquivalent(Context,
581 cast<AttributedType>(T1)->getEquivalentType(),
582 cast<AttributedType>(T2)->getEquivalentType()))
583 return false;
584 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000585
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000586 case Type::Paren:
587 if (!IsStructurallyEquivalent(Context,
588 cast<ParenType>(T1)->getInnerType(),
589 cast<ParenType>(T2)->getInnerType()))
590 return false;
591 break;
592
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000593 case Type::Typedef:
594 if (!IsStructurallyEquivalent(Context,
595 cast<TypedefType>(T1)->getDecl(),
596 cast<TypedefType>(T2)->getDecl()))
597 return false;
598 break;
599
600 case Type::TypeOfExpr:
601 if (!IsStructurallyEquivalent(Context,
602 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
603 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
604 return false;
605 break;
606
607 case Type::TypeOf:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypeOfType>(T1)->getUnderlyingType(),
610 cast<TypeOfType>(T2)->getUnderlyingType()))
611 return false;
612 break;
Sean Huntca63c202011-05-24 22:41:36 +0000613
614 case Type::UnaryTransform:
615 if (!IsStructurallyEquivalent(Context,
616 cast<UnaryTransformType>(T1)->getUnderlyingType(),
617 cast<UnaryTransformType>(T1)->getUnderlyingType()))
618 return false;
619 break;
620
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000621 case Type::Decltype:
622 if (!IsStructurallyEquivalent(Context,
623 cast<DecltypeType>(T1)->getUnderlyingExpr(),
624 cast<DecltypeType>(T2)->getUnderlyingExpr()))
625 return false;
626 break;
627
Richard Smith34b41d92011-02-20 03:19:35 +0000628 case Type::Auto:
629 if (!IsStructurallyEquivalent(Context,
630 cast<AutoType>(T1)->getDeducedType(),
631 cast<AutoType>(T2)->getDeducedType()))
632 return false;
633 break;
634
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000635 case Type::Record:
636 case Type::Enum:
637 if (!IsStructurallyEquivalent(Context,
638 cast<TagType>(T1)->getDecl(),
639 cast<TagType>(T2)->getDecl()))
640 return false;
641 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000642
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000643 case Type::TemplateTypeParm: {
644 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
645 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
646 if (Parm1->getDepth() != Parm2->getDepth())
647 return false;
648 if (Parm1->getIndex() != Parm2->getIndex())
649 return false;
650 if (Parm1->isParameterPack() != Parm2->isParameterPack())
651 return false;
652
653 // Names of template type parameters are never significant.
654 break;
655 }
656
657 case Type::SubstTemplateTypeParm: {
658 const SubstTemplateTypeParmType *Subst1
659 = cast<SubstTemplateTypeParmType>(T1);
660 const SubstTemplateTypeParmType *Subst2
661 = cast<SubstTemplateTypeParmType>(T2);
662 if (!IsStructurallyEquivalent(Context,
663 QualType(Subst1->getReplacedParameter(), 0),
664 QualType(Subst2->getReplacedParameter(), 0)))
665 return false;
666 if (!IsStructurallyEquivalent(Context,
667 Subst1->getReplacementType(),
668 Subst2->getReplacementType()))
669 return false;
670 break;
671 }
672
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000673 case Type::SubstTemplateTypeParmPack: {
674 const SubstTemplateTypeParmPackType *Subst1
675 = cast<SubstTemplateTypeParmPackType>(T1);
676 const SubstTemplateTypeParmPackType *Subst2
677 = cast<SubstTemplateTypeParmPackType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 QualType(Subst1->getReplacedParameter(), 0),
680 QualType(Subst2->getReplacedParameter(), 0)))
681 return false;
682 if (!IsStructurallyEquivalent(Context,
683 Subst1->getArgumentPack(),
684 Subst2->getArgumentPack()))
685 return false;
686 break;
687 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000688 case Type::TemplateSpecialization: {
689 const TemplateSpecializationType *Spec1
690 = cast<TemplateSpecializationType>(T1);
691 const TemplateSpecializationType *Spec2
692 = cast<TemplateSpecializationType>(T2);
693 if (!IsStructurallyEquivalent(Context,
694 Spec1->getTemplateName(),
695 Spec2->getTemplateName()))
696 return false;
697 if (Spec1->getNumArgs() != Spec2->getNumArgs())
698 return false;
699 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
700 if (!IsStructurallyEquivalent(Context,
701 Spec1->getArg(I), Spec2->getArg(I)))
702 return false;
703 }
704 break;
705 }
706
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000707 case Type::Elaborated: {
708 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
709 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
710 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
711 if (Elab1->getKeyword() != Elab2->getKeyword())
712 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000713 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000714 Elab1->getQualifier(),
715 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000716 return false;
717 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000718 Elab1->getNamedType(),
719 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000720 return false;
721 break;
722 }
723
John McCall3cb0ebd2010-03-10 03:28:59 +0000724 case Type::InjectedClassName: {
725 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
726 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
727 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000728 Inj1->getInjectedSpecializationType(),
729 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000730 return false;
731 break;
732 }
733
Douglas Gregor4714c122010-03-31 17:34:00 +0000734 case Type::DependentName: {
735 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
736 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000737 if (!IsStructurallyEquivalent(Context,
738 Typename1->getQualifier(),
739 Typename2->getQualifier()))
740 return false;
741 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
742 Typename2->getIdentifier()))
743 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000744
745 break;
746 }
747
John McCall33500952010-06-11 00:33:02 +0000748 case Type::DependentTemplateSpecialization: {
749 const DependentTemplateSpecializationType *Spec1 =
750 cast<DependentTemplateSpecializationType>(T1);
751 const DependentTemplateSpecializationType *Spec2 =
752 cast<DependentTemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getQualifier(),
755 Spec2->getQualifier()))
756 return false;
757 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
758 Spec2->getIdentifier()))
759 return false;
760 if (Spec1->getNumArgs() != Spec2->getNumArgs())
761 return false;
762 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
763 if (!IsStructurallyEquivalent(Context,
764 Spec1->getArg(I), Spec2->getArg(I)))
765 return false;
766 }
767 break;
768 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000769
770 case Type::PackExpansion:
771 if (!IsStructurallyEquivalent(Context,
772 cast<PackExpansionType>(T1)->getPattern(),
773 cast<PackExpansionType>(T2)->getPattern()))
774 return false;
775 break;
776
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000777 case Type::ObjCInterface: {
778 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
779 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
780 if (!IsStructurallyEquivalent(Context,
781 Iface1->getDecl(), Iface2->getDecl()))
782 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000783 break;
784 }
785
786 case Type::ObjCObject: {
787 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
788 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
789 if (!IsStructurallyEquivalent(Context,
790 Obj1->getBaseType(),
791 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000792 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000793 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
794 return false;
795 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000796 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000797 Obj1->getProtocol(I),
798 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000799 return false;
800 }
801 break;
802 }
803
804 case Type::ObjCObjectPointer: {
805 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
806 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
807 if (!IsStructurallyEquivalent(Context,
808 Ptr1->getPointeeType(),
809 Ptr2->getPointeeType()))
810 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000811 break;
812 }
Eli Friedmanb001de72011-10-06 23:00:33 +0000813
814 case Type::Atomic: {
815 if (!IsStructurallyEquivalent(Context,
816 cast<AtomicType>(T1)->getValueType(),
817 cast<AtomicType>(T2)->getValueType()))
818 return false;
819 break;
820 }
821
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000822 } // end switch
823
824 return true;
825}
826
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000827/// \brief Determine structural equivalence of two fields.
828static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
829 FieldDecl *Field1, FieldDecl *Field2) {
830 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
831
832 if (!IsStructurallyEquivalent(Context,
833 Field1->getType(), Field2->getType())) {
834 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
835 << Context.C2.getTypeDeclType(Owner2);
836 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
837 << Field2->getDeclName() << Field2->getType();
838 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
839 << Field1->getDeclName() << Field1->getType();
840 return false;
841 }
842
843 if (Field1->isBitField() != Field2->isBitField()) {
844 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
845 << Context.C2.getTypeDeclType(Owner2);
846 if (Field1->isBitField()) {
847 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
848 << Field1->getDeclName() << Field1->getType()
849 << Field1->getBitWidthValue(Context.C1);
850 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
851 << Field2->getDeclName();
852 } else {
853 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
854 << Field2->getDeclName() << Field2->getType()
855 << Field2->getBitWidthValue(Context.C2);
856 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
857 << Field1->getDeclName();
858 }
859 return false;
860 }
861
862 if (Field1->isBitField()) {
863 // Make sure that the bit-fields are the same length.
864 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
865 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
866
867 if (Bits1 != Bits2) {
868 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
869 << Context.C2.getTypeDeclType(Owner2);
870 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
871 << Field2->getDeclName() << Field2->getType() << Bits2;
872 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
873 << Field1->getDeclName() << Field1->getType() << Bits1;
874 return false;
875 }
876 }
877
878 return true;
879}
880
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000881/// \brief Determine structural equivalence of two records.
882static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
883 RecordDecl *D1, RecordDecl *D2) {
884 if (D1->isUnion() != D2->isUnion()) {
885 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
886 << Context.C2.getTypeDeclType(D2);
887 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
888 << D1->getDeclName() << (unsigned)D1->getTagKind();
889 return false;
890 }
891
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000892 // If both declarations are class template specializations, we know
893 // the ODR applies, so check the template and template arguments.
894 ClassTemplateSpecializationDecl *Spec1
895 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
896 ClassTemplateSpecializationDecl *Spec2
897 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
898 if (Spec1 && Spec2) {
899 // Check that the specialized templates are the same.
900 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
901 Spec2->getSpecializedTemplate()))
902 return false;
903
904 // Check that the template arguments are the same.
905 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
906 return false;
907
908 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
909 if (!IsStructurallyEquivalent(Context,
910 Spec1->getTemplateArgs().get(I),
911 Spec2->getTemplateArgs().get(I)))
912 return false;
913 }
914 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000915 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000916 else if (Spec1 || Spec2)
917 return false;
918
Douglas Gregorea35d112010-02-15 23:54:17 +0000919 // Compare the definitions of these two records. If either or both are
920 // incomplete, we assume that they are equivalent.
921 D1 = D1->getDefinition();
922 D2 = D2->getDefinition();
923 if (!D1 || !D2)
924 return true;
925
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000926 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
927 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
928 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
929 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000930 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000931 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000932 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000933 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000934 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000935 return false;
936 }
937
938 // Check the base classes.
939 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
940 BaseEnd1 = D1CXX->bases_end(),
941 Base2 = D2CXX->bases_begin();
942 Base1 != BaseEnd1;
943 ++Base1, ++Base2) {
944 if (!IsStructurallyEquivalent(Context,
945 Base1->getType(), Base2->getType())) {
946 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
947 << Context.C2.getTypeDeclType(D2);
948 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
949 << Base2->getType()
950 << Base2->getSourceRange();
951 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
952 << Base1->getType()
953 << Base1->getSourceRange();
954 return false;
955 }
956
957 // Check virtual vs. non-virtual inheritance mismatch.
958 if (Base1->isVirtual() != Base2->isVirtual()) {
959 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
960 << Context.C2.getTypeDeclType(D2);
961 Context.Diag2(Base2->getSourceRange().getBegin(),
962 diag::note_odr_virtual_base)
963 << Base2->isVirtual() << Base2->getSourceRange();
964 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
965 << Base1->isVirtual()
966 << Base1->getSourceRange();
967 return false;
968 }
969 }
970 } else if (D1CXX->getNumBases() > 0) {
971 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
972 << Context.C2.getTypeDeclType(D2);
973 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
974 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
975 << Base1->getType()
976 << Base1->getSourceRange();
977 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
978 return false;
979 }
980 }
981
982 // Check the fields for consistency.
983 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
984 Field2End = D2->field_end();
985 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
986 Field1End = D1->field_end();
987 Field1 != Field1End;
988 ++Field1, ++Field2) {
989 if (Field2 == Field2End) {
990 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
991 << Context.C2.getTypeDeclType(D2);
992 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
993 << Field1->getDeclName() << Field1->getType();
994 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
995 return false;
996 }
997
Douglas Gregor7c9412c2011-10-14 21:54:42 +0000998 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
999 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001000 }
1001
1002 if (Field2 != Field2End) {
1003 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1004 << Context.C2.getTypeDeclType(D2);
1005 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1006 << Field2->getDeclName() << Field2->getType();
1007 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1008 return false;
1009 }
1010
1011 return true;
1012}
1013
1014/// \brief Determine structural equivalence of two enums.
1015static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1016 EnumDecl *D1, EnumDecl *D2) {
1017 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1018 EC2End = D2->enumerator_end();
1019 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1020 EC1End = D1->enumerator_end();
1021 EC1 != EC1End; ++EC1, ++EC2) {
1022 if (EC2 == EC2End) {
1023 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1024 << Context.C2.getTypeDeclType(D2);
1025 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1026 << EC1->getDeclName()
1027 << EC1->getInitVal().toString(10);
1028 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1029 return false;
1030 }
1031
1032 llvm::APSInt Val1 = EC1->getInitVal();
1033 llvm::APSInt Val2 = EC2->getInitVal();
1034 if (!IsSameValue(Val1, Val2) ||
1035 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1036 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1037 << Context.C2.getTypeDeclType(D2);
1038 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1039 << EC2->getDeclName()
1040 << EC2->getInitVal().toString(10);
1041 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1042 << EC1->getDeclName()
1043 << EC1->getInitVal().toString(10);
1044 return false;
1045 }
1046 }
1047
1048 if (EC2 != EC2End) {
1049 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1050 << Context.C2.getTypeDeclType(D2);
1051 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1052 << EC2->getDeclName()
1053 << EC2->getInitVal().toString(10);
1054 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1055 return false;
1056 }
1057
1058 return true;
1059}
Douglas Gregor040afae2010-11-30 19:14:50 +00001060
1061static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1062 TemplateParameterList *Params1,
1063 TemplateParameterList *Params2) {
1064 if (Params1->size() != Params2->size()) {
1065 Context.Diag2(Params2->getTemplateLoc(),
1066 diag::err_odr_different_num_template_parameters)
1067 << Params1->size() << Params2->size();
1068 Context.Diag1(Params1->getTemplateLoc(),
1069 diag::note_odr_template_parameter_list);
1070 return false;
1071 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001072
Douglas Gregor040afae2010-11-30 19:14:50 +00001073 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1074 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1075 Context.Diag2(Params2->getParam(I)->getLocation(),
1076 diag::err_odr_different_template_parameter_kind);
1077 Context.Diag1(Params1->getParam(I)->getLocation(),
1078 diag::note_odr_template_parameter_here);
1079 return false;
1080 }
1081
1082 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1083 Params2->getParam(I))) {
1084
1085 return false;
1086 }
1087 }
1088
1089 return true;
1090}
1091
1092static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1093 TemplateTypeParmDecl *D1,
1094 TemplateTypeParmDecl *D2) {
1095 if (D1->isParameterPack() != D2->isParameterPack()) {
1096 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1097 << D2->isParameterPack();
1098 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1099 << D1->isParameterPack();
1100 return false;
1101 }
1102
1103 return true;
1104}
1105
1106static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1107 NonTypeTemplateParmDecl *D1,
1108 NonTypeTemplateParmDecl *D2) {
1109 // FIXME: Enable once we have variadic templates.
1110#if 0
1111 if (D1->isParameterPack() != D2->isParameterPack()) {
1112 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1113 << D2->isParameterPack();
1114 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1115 << D1->isParameterPack();
1116 return false;
1117 }
1118#endif
1119
1120 // Check types.
1121 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1122 Context.Diag2(D2->getLocation(),
1123 diag::err_odr_non_type_parameter_type_inconsistent)
1124 << D2->getType() << D1->getType();
1125 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1126 << D1->getType();
1127 return false;
1128 }
1129
1130 return true;
1131}
1132
1133static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1134 TemplateTemplateParmDecl *D1,
1135 TemplateTemplateParmDecl *D2) {
1136 // FIXME: Enable once we have variadic templates.
1137#if 0
1138 if (D1->isParameterPack() != D2->isParameterPack()) {
1139 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1140 << D2->isParameterPack();
1141 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1142 << D1->isParameterPack();
1143 return false;
1144 }
1145#endif
1146
1147 // Check template parameter lists.
1148 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1149 D2->getTemplateParameters());
1150}
1151
1152static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1153 ClassTemplateDecl *D1,
1154 ClassTemplateDecl *D2) {
1155 // Check template parameters.
1156 if (!IsStructurallyEquivalent(Context,
1157 D1->getTemplateParameters(),
1158 D2->getTemplateParameters()))
1159 return false;
1160
1161 // Check the templated declaration.
1162 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1163 D2->getTemplatedDecl());
1164}
1165
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001166/// \brief Determine structural equivalence of two declarations.
1167static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1168 Decl *D1, Decl *D2) {
1169 // FIXME: Check for known structural equivalences via a callback of some sort.
1170
Douglas Gregorea35d112010-02-15 23:54:17 +00001171 // Check whether we already know that these two declarations are not
1172 // structurally equivalent.
1173 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1174 D2->getCanonicalDecl())))
1175 return false;
1176
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001177 // Determine whether we've already produced a tentative equivalence for D1.
1178 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1179 if (EquivToD1)
1180 return EquivToD1 == D2->getCanonicalDecl();
1181
1182 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1183 EquivToD1 = D2->getCanonicalDecl();
1184 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1185 return true;
1186}
1187
1188bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1189 Decl *D2) {
1190 if (!::IsStructurallyEquivalent(*this, D1, D2))
1191 return false;
1192
1193 return !Finish();
1194}
1195
1196bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1197 QualType T2) {
1198 if (!::IsStructurallyEquivalent(*this, T1, T2))
1199 return false;
1200
1201 return !Finish();
1202}
1203
1204bool StructuralEquivalenceContext::Finish() {
1205 while (!DeclsToCheck.empty()) {
1206 // Check the next declaration.
1207 Decl *D1 = DeclsToCheck.front();
1208 DeclsToCheck.pop_front();
1209
1210 Decl *D2 = TentativeEquivalences[D1];
1211 assert(D2 && "Unrecorded tentative equivalence?");
1212
Douglas Gregorea35d112010-02-15 23:54:17 +00001213 bool Equivalent = true;
1214
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001215 // FIXME: Switch on all declaration kinds. For now, we're just going to
1216 // check the obvious ones.
1217 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1218 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1219 // Check for equivalent structure names.
1220 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001221 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1222 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001223 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001224 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1225 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001226 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1227 !::IsStructurallyEquivalent(*this, Record1, Record2))
1228 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001229 } else {
1230 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001231 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001232 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001233 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001234 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1235 // Check for equivalent enum names.
1236 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001237 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1238 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001239 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001240 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1241 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001242 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1243 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1244 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001245 } else {
1246 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001247 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001248 }
Richard Smith162e1c12011-04-15 14:24:37 +00001249 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1250 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001251 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001252 Typedef2->getIdentifier()) ||
1253 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001254 Typedef1->getUnderlyingType(),
1255 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001256 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001257 } else {
1258 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001259 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001260 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001261 } else if (ClassTemplateDecl *ClassTemplate1
1262 = dyn_cast<ClassTemplateDecl>(D1)) {
1263 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1264 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1265 ClassTemplate2->getIdentifier()) ||
1266 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1267 Equivalent = false;
1268 } else {
1269 // Class template/non-class-template mismatch.
1270 Equivalent = false;
1271 }
1272 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1273 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1274 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1275 Equivalent = false;
1276 } else {
1277 // Kind mismatch.
1278 Equivalent = false;
1279 }
1280 } else if (NonTypeTemplateParmDecl *NTTP1
1281 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1282 if (NonTypeTemplateParmDecl *NTTP2
1283 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1284 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1285 Equivalent = false;
1286 } else {
1287 // Kind mismatch.
1288 Equivalent = false;
1289 }
1290 } else if (TemplateTemplateParmDecl *TTP1
1291 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1292 if (TemplateTemplateParmDecl *TTP2
1293 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1294 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1295 Equivalent = false;
1296 } else {
1297 // Kind mismatch.
1298 Equivalent = false;
1299 }
1300 }
1301
Douglas Gregorea35d112010-02-15 23:54:17 +00001302 if (!Equivalent) {
1303 // Note that these two declarations are not equivalent (and we already
1304 // know about it).
1305 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1306 D2->getCanonicalDecl()));
1307 return true;
1308 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001309 // FIXME: Check other declaration kinds!
1310 }
1311
1312 return false;
1313}
1314
1315//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001316// Import Types
1317//----------------------------------------------------------------------------
1318
John McCallf4c73712011-01-19 06:33:43 +00001319QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001320 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1321 << T->getTypeClassName();
1322 return QualType();
1323}
1324
John McCallf4c73712011-01-19 06:33:43 +00001325QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001326 switch (T->getKind()) {
1327 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1328 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1329
1330 case BuiltinType::Char_U:
1331 // The context we're importing from has an unsigned 'char'. If we're
1332 // importing into a context with a signed 'char', translate to
1333 // 'unsigned char' instead.
1334 if (Importer.getToContext().getLangOptions().CharIsSigned)
1335 return Importer.getToContext().UnsignedCharTy;
1336
1337 return Importer.getToContext().CharTy;
1338
1339 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1340
1341 case BuiltinType::Char16:
1342 // FIXME: Make sure that the "to" context supports C++!
1343 return Importer.getToContext().Char16Ty;
1344
1345 case BuiltinType::Char32:
1346 // FIXME: Make sure that the "to" context supports C++!
1347 return Importer.getToContext().Char32Ty;
1348
1349 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1350 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1351 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1352 case BuiltinType::ULongLong:
1353 return Importer.getToContext().UnsignedLongLongTy;
1354 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1355
1356 case BuiltinType::Char_S:
1357 // The context we're importing from has an unsigned 'char'. If we're
1358 // importing into a context with a signed 'char', translate to
1359 // 'unsigned char' instead.
1360 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1361 return Importer.getToContext().SignedCharTy;
1362
1363 return Importer.getToContext().CharTy;
1364
1365 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001366 case BuiltinType::WChar_S:
1367 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001368 // FIXME: If not in C++, shall we translate to the C equivalent of
1369 // wchar_t?
1370 return Importer.getToContext().WCharTy;
1371
1372 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1373 case BuiltinType::Int : return Importer.getToContext().IntTy;
1374 case BuiltinType::Long : return Importer.getToContext().LongTy;
1375 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1376 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00001377 case BuiltinType::Half: return Importer.getToContext().HalfTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001378 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1379 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1380 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1381
1382 case BuiltinType::NullPtr:
1383 // FIXME: Make sure that the "to" context supports C++0x!
1384 return Importer.getToContext().NullPtrTy;
1385
1386 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1387 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001388 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001389 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
John McCall0ddaeb92011-10-17 18:09:15 +00001390 case BuiltinType::ARCUnbridgedCast:
1391 return Importer.getToContext().ARCUnbridgedCastTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001392
1393 case BuiltinType::ObjCId:
1394 // FIXME: Make sure that the "to" context supports Objective-C!
1395 return Importer.getToContext().ObjCBuiltinIdTy;
1396
1397 case BuiltinType::ObjCClass:
1398 return Importer.getToContext().ObjCBuiltinClassTy;
1399
1400 case BuiltinType::ObjCSel:
1401 return Importer.getToContext().ObjCBuiltinSelTy;
1402 }
1403
1404 return QualType();
1405}
1406
John McCallf4c73712011-01-19 06:33:43 +00001407QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001408 QualType ToElementType = Importer.Import(T->getElementType());
1409 if (ToElementType.isNull())
1410 return QualType();
1411
1412 return Importer.getToContext().getComplexType(ToElementType);
1413}
1414
John McCallf4c73712011-01-19 06:33:43 +00001415QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001416 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1417 if (ToPointeeType.isNull())
1418 return QualType();
1419
1420 return Importer.getToContext().getPointerType(ToPointeeType);
1421}
1422
John McCallf4c73712011-01-19 06:33:43 +00001423QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001424 // FIXME: Check for blocks support in "to" context.
1425 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1426 if (ToPointeeType.isNull())
1427 return QualType();
1428
1429 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1430}
1431
John McCallf4c73712011-01-19 06:33:43 +00001432QualType
1433ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001434 // FIXME: Check for C++ support in "to" context.
1435 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1436 if (ToPointeeType.isNull())
1437 return QualType();
1438
1439 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1440}
1441
John McCallf4c73712011-01-19 06:33:43 +00001442QualType
1443ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001444 // FIXME: Check for C++0x support in "to" context.
1445 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1446 if (ToPointeeType.isNull())
1447 return QualType();
1448
1449 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1450}
1451
John McCallf4c73712011-01-19 06:33:43 +00001452QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001453 // FIXME: Check for C++ support in "to" context.
1454 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1455 if (ToPointeeType.isNull())
1456 return QualType();
1457
1458 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1459 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1460 ClassType.getTypePtr());
1461}
1462
John McCallf4c73712011-01-19 06:33:43 +00001463QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001464 QualType ToElementType = Importer.Import(T->getElementType());
1465 if (ToElementType.isNull())
1466 return QualType();
1467
1468 return Importer.getToContext().getConstantArrayType(ToElementType,
1469 T->getSize(),
1470 T->getSizeModifier(),
1471 T->getIndexTypeCVRQualifiers());
1472}
1473
John McCallf4c73712011-01-19 06:33:43 +00001474QualType
1475ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001476 QualType ToElementType = Importer.Import(T->getElementType());
1477 if (ToElementType.isNull())
1478 return QualType();
1479
1480 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1481 T->getSizeModifier(),
1482 T->getIndexTypeCVRQualifiers());
1483}
1484
John McCallf4c73712011-01-19 06:33:43 +00001485QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001486 QualType ToElementType = Importer.Import(T->getElementType());
1487 if (ToElementType.isNull())
1488 return QualType();
1489
1490 Expr *Size = Importer.Import(T->getSizeExpr());
1491 if (!Size)
1492 return QualType();
1493
1494 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1495 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1496 T->getSizeModifier(),
1497 T->getIndexTypeCVRQualifiers(),
1498 Brackets);
1499}
1500
John McCallf4c73712011-01-19 06:33:43 +00001501QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001502 QualType ToElementType = Importer.Import(T->getElementType());
1503 if (ToElementType.isNull())
1504 return QualType();
1505
1506 return Importer.getToContext().getVectorType(ToElementType,
1507 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001508 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001509}
1510
John McCallf4c73712011-01-19 06:33:43 +00001511QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001512 QualType ToElementType = Importer.Import(T->getElementType());
1513 if (ToElementType.isNull())
1514 return QualType();
1515
1516 return Importer.getToContext().getExtVectorType(ToElementType,
1517 T->getNumElements());
1518}
1519
John McCallf4c73712011-01-19 06:33:43 +00001520QualType
1521ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001522 // FIXME: What happens if we're importing a function without a prototype
1523 // into C++? Should we make it variadic?
1524 QualType ToResultType = Importer.Import(T->getResultType());
1525 if (ToResultType.isNull())
1526 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001527
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001528 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001529 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001530}
1531
John McCallf4c73712011-01-19 06:33:43 +00001532QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001533 QualType ToResultType = Importer.Import(T->getResultType());
1534 if (ToResultType.isNull())
1535 return QualType();
1536
1537 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001538 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001539 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1540 AEnd = T->arg_type_end();
1541 A != AEnd; ++A) {
1542 QualType ArgType = Importer.Import(*A);
1543 if (ArgType.isNull())
1544 return QualType();
1545 ArgTypes.push_back(ArgType);
1546 }
1547
1548 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001549 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001550 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1551 EEnd = T->exception_end();
1552 E != EEnd; ++E) {
1553 QualType ExceptionType = Importer.Import(*E);
1554 if (ExceptionType.isNull())
1555 return QualType();
1556 ExceptionTypes.push_back(ExceptionType);
1557 }
John McCalle23cf432010-12-14 08:05:40 +00001558
1559 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1560 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001561
1562 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001563 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001564}
1565
Sean Callanan0aeb2892011-08-11 16:56:07 +00001566QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1567 QualType ToInnerType = Importer.Import(T->getInnerType());
1568 if (ToInnerType.isNull())
1569 return QualType();
1570
1571 return Importer.getToContext().getParenType(ToInnerType);
1572}
1573
John McCallf4c73712011-01-19 06:33:43 +00001574QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001575 TypedefNameDecl *ToDecl
1576 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001577 if (!ToDecl)
1578 return QualType();
1579
1580 return Importer.getToContext().getTypeDeclType(ToDecl);
1581}
1582
John McCallf4c73712011-01-19 06:33:43 +00001583QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001584 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1585 if (!ToExpr)
1586 return QualType();
1587
1588 return Importer.getToContext().getTypeOfExprType(ToExpr);
1589}
1590
John McCallf4c73712011-01-19 06:33:43 +00001591QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001592 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1593 if (ToUnderlyingType.isNull())
1594 return QualType();
1595
1596 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1597}
1598
John McCallf4c73712011-01-19 06:33:43 +00001599QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001600 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001601 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1602 if (!ToExpr)
1603 return QualType();
1604
1605 return Importer.getToContext().getDecltypeType(ToExpr);
1606}
1607
Sean Huntca63c202011-05-24 22:41:36 +00001608QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1609 QualType ToBaseType = Importer.Import(T->getBaseType());
1610 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1611 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1612 return QualType();
1613
1614 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1615 ToUnderlyingType,
1616 T->getUTTKind());
1617}
1618
Richard Smith34b41d92011-02-20 03:19:35 +00001619QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1620 // FIXME: Make sure that the "to" context supports C++0x!
1621 QualType FromDeduced = T->getDeducedType();
1622 QualType ToDeduced;
1623 if (!FromDeduced.isNull()) {
1624 ToDeduced = Importer.Import(FromDeduced);
1625 if (ToDeduced.isNull())
1626 return QualType();
1627 }
1628
1629 return Importer.getToContext().getAutoType(ToDeduced);
1630}
1631
John McCallf4c73712011-01-19 06:33:43 +00001632QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001633 RecordDecl *ToDecl
1634 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1635 if (!ToDecl)
1636 return QualType();
1637
1638 return Importer.getToContext().getTagDeclType(ToDecl);
1639}
1640
John McCallf4c73712011-01-19 06:33:43 +00001641QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001642 EnumDecl *ToDecl
1643 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1644 if (!ToDecl)
1645 return QualType();
1646
1647 return Importer.getToContext().getTagDeclType(ToDecl);
1648}
1649
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001650QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001651 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001652 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1653 if (ToTemplate.isNull())
1654 return QualType();
1655
Chris Lattner5f9e2722011-07-23 10:55:15 +00001656 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001657 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1658 return QualType();
1659
1660 QualType ToCanonType;
1661 if (!QualType(T, 0).isCanonical()) {
1662 QualType FromCanonType
1663 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1664 ToCanonType =Importer.Import(FromCanonType);
1665 if (ToCanonType.isNull())
1666 return QualType();
1667 }
1668 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1669 ToTemplateArgs.data(),
1670 ToTemplateArgs.size(),
1671 ToCanonType);
1672}
1673
John McCallf4c73712011-01-19 06:33:43 +00001674QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001675 NestedNameSpecifier *ToQualifier = 0;
1676 // Note: the qualifier in an ElaboratedType is optional.
1677 if (T->getQualifier()) {
1678 ToQualifier = Importer.Import(T->getQualifier());
1679 if (!ToQualifier)
1680 return QualType();
1681 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001682
1683 QualType ToNamedType = Importer.Import(T->getNamedType());
1684 if (ToNamedType.isNull())
1685 return QualType();
1686
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001687 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1688 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001689}
1690
John McCallf4c73712011-01-19 06:33:43 +00001691QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001692 ObjCInterfaceDecl *Class
1693 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1694 if (!Class)
1695 return QualType();
1696
John McCallc12c5bb2010-05-15 11:32:37 +00001697 return Importer.getToContext().getObjCInterfaceType(Class);
1698}
1699
John McCallf4c73712011-01-19 06:33:43 +00001700QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001701 QualType ToBaseType = Importer.Import(T->getBaseType());
1702 if (ToBaseType.isNull())
1703 return QualType();
1704
Chris Lattner5f9e2722011-07-23 10:55:15 +00001705 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001706 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001707 PEnd = T->qual_end();
1708 P != PEnd; ++P) {
1709 ObjCProtocolDecl *Protocol
1710 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1711 if (!Protocol)
1712 return QualType();
1713 Protocols.push_back(Protocol);
1714 }
1715
John McCallc12c5bb2010-05-15 11:32:37 +00001716 return Importer.getToContext().getObjCObjectType(ToBaseType,
1717 Protocols.data(),
1718 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001719}
1720
John McCallf4c73712011-01-19 06:33:43 +00001721QualType
1722ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001723 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1724 if (ToPointeeType.isNull())
1725 return QualType();
1726
John McCallc12c5bb2010-05-15 11:32:37 +00001727 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001728}
1729
Douglas Gregor089459a2010-02-08 21:09:39 +00001730//----------------------------------------------------------------------------
1731// Import Declarations
1732//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001733bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1734 DeclContext *&LexicalDC,
1735 DeclarationName &Name,
1736 SourceLocation &Loc) {
1737 // Import the context of this declaration.
1738 DC = Importer.ImportContext(D->getDeclContext());
1739 if (!DC)
1740 return true;
1741
1742 LexicalDC = DC;
1743 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1744 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1745 if (!LexicalDC)
1746 return true;
1747 }
1748
1749 // Import the name of this declaration.
1750 Name = Importer.Import(D->getDeclName());
1751 if (D->getDeclName() && !Name)
1752 return true;
1753
1754 // Import the location of this declaration.
1755 Loc = Importer.Import(D->getLocation());
1756 return false;
1757}
1758
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001759void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1760 if (!FromD)
1761 return;
1762
1763 if (!ToD) {
1764 ToD = Importer.Import(FromD);
1765 if (!ToD)
1766 return;
1767 }
1768
1769 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1770 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1771 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1772 ImportDefinition(FromRecord, ToRecord);
1773 }
1774 }
1775 return;
1776 }
1777
1778 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1779 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1780 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1781 ImportDefinition(FromEnum, ToEnum);
1782 }
1783 }
1784 return;
1785 }
1786}
1787
Abramo Bagnara25777432010-08-11 22:01:17 +00001788void
1789ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1790 DeclarationNameInfo& To) {
1791 // NOTE: To.Name and To.Loc are already imported.
1792 // We only have to import To.LocInfo.
1793 switch (To.getName().getNameKind()) {
1794 case DeclarationName::Identifier:
1795 case DeclarationName::ObjCZeroArgSelector:
1796 case DeclarationName::ObjCOneArgSelector:
1797 case DeclarationName::ObjCMultiArgSelector:
1798 case DeclarationName::CXXUsingDirective:
1799 return;
1800
1801 case DeclarationName::CXXOperatorName: {
1802 SourceRange Range = From.getCXXOperatorNameRange();
1803 To.setCXXOperatorNameRange(Importer.Import(Range));
1804 return;
1805 }
1806 case DeclarationName::CXXLiteralOperatorName: {
1807 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1808 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1809 return;
1810 }
1811 case DeclarationName::CXXConstructorName:
1812 case DeclarationName::CXXDestructorName:
1813 case DeclarationName::CXXConversionFunctionName: {
1814 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1815 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1816 return;
1817 }
David Blaikieb219cfc2011-09-23 05:06:16 +00001818 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001819 }
1820}
1821
Douglas Gregord8868a62011-01-18 03:11:38 +00001822void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1823 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001824 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001825 return;
1826 }
1827
Douglas Gregor083a8212010-02-21 18:24:45 +00001828 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1829 FromEnd = FromDC->decls_end();
1830 From != FromEnd;
1831 ++From)
1832 Importer.Import(*From);
1833}
1834
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001835bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1836 bool ForceImport) {
1837 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001838 return false;
1839
1840 To->startDefinition();
1841
1842 // Add base classes.
1843 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1844 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1845
Chris Lattner5f9e2722011-07-23 10:55:15 +00001846 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001847 for (CXXRecordDecl::base_class_iterator
1848 Base1 = FromCXX->bases_begin(),
1849 FromBaseEnd = FromCXX->bases_end();
1850 Base1 != FromBaseEnd;
1851 ++Base1) {
1852 QualType T = Importer.Import(Base1->getType());
1853 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001854 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001855
1856 SourceLocation EllipsisLoc;
1857 if (Base1->isPackExpansion())
1858 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001859
1860 // Ensure that we have a definition for the base.
1861 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1862
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001863 Bases.push_back(
1864 new (Importer.getToContext())
1865 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1866 Base1->isVirtual(),
1867 Base1->isBaseOfClass(),
1868 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001869 Importer.Import(Base1->getTypeSourceInfo()),
1870 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001871 }
1872 if (!Bases.empty())
1873 ToCXX->setBases(Bases.data(), Bases.size());
1874 }
1875
Sean Callanan673e7752011-07-19 22:38:25 +00001876 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001877 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001878 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001879}
1880
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001881bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1882 bool ForceImport) {
1883 if (To->getDefinition() || To->isBeingDefined())
1884 return false;
1885
1886 To->startDefinition();
1887
1888 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1889 if (T.isNull())
1890 return true;
1891
1892 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1893 if (ToPromotionType.isNull())
1894 return true;
1895
1896 ImportDeclContext(From, ForceImport);
1897
1898 // FIXME: we might need to merge the number of positive or negative bits
1899 // if the enumerator lists don't match.
1900 To->completeDefinition(T, ToPromotionType,
1901 From->getNumPositiveBits(),
1902 From->getNumNegativeBits());
1903 return false;
1904}
1905
Douglas Gregor040afae2010-11-30 19:14:50 +00001906TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1907 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001908 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001909 ToParams.reserve(Params->size());
1910 for (TemplateParameterList::iterator P = Params->begin(),
1911 PEnd = Params->end();
1912 P != PEnd; ++P) {
1913 Decl *To = Importer.Import(*P);
1914 if (!To)
1915 return 0;
1916
1917 ToParams.push_back(cast<NamedDecl>(To));
1918 }
1919
1920 return TemplateParameterList::Create(Importer.getToContext(),
1921 Importer.Import(Params->getTemplateLoc()),
1922 Importer.Import(Params->getLAngleLoc()),
1923 ToParams.data(), ToParams.size(),
1924 Importer.Import(Params->getRAngleLoc()));
1925}
1926
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001927TemplateArgument
1928ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1929 switch (From.getKind()) {
1930 case TemplateArgument::Null:
1931 return TemplateArgument();
1932
1933 case TemplateArgument::Type: {
1934 QualType ToType = Importer.Import(From.getAsType());
1935 if (ToType.isNull())
1936 return TemplateArgument();
1937 return TemplateArgument(ToType);
1938 }
1939
1940 case TemplateArgument::Integral: {
1941 QualType ToType = Importer.Import(From.getIntegralType());
1942 if (ToType.isNull())
1943 return TemplateArgument();
1944 return TemplateArgument(*From.getAsIntegral(), ToType);
1945 }
1946
1947 case TemplateArgument::Declaration:
1948 if (Decl *To = Importer.Import(From.getAsDecl()))
1949 return TemplateArgument(To);
1950 return TemplateArgument();
1951
1952 case TemplateArgument::Template: {
1953 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1954 if (ToTemplate.isNull())
1955 return TemplateArgument();
1956
1957 return TemplateArgument(ToTemplate);
1958 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001959
1960 case TemplateArgument::TemplateExpansion: {
1961 TemplateName ToTemplate
1962 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1963 if (ToTemplate.isNull())
1964 return TemplateArgument();
1965
Douglas Gregor2be29f42011-01-14 23:41:42 +00001966 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001967 }
1968
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001969 case TemplateArgument::Expression:
1970 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1971 return TemplateArgument(ToExpr);
1972 return TemplateArgument();
1973
1974 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001975 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001976 ToPack.reserve(From.pack_size());
1977 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1978 return TemplateArgument();
1979
1980 TemplateArgument *ToArgs
1981 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1982 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1983 return TemplateArgument(ToArgs, ToPack.size());
1984 }
1985 }
1986
1987 llvm_unreachable("Invalid template argument kind");
1988 return TemplateArgument();
1989}
1990
1991bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1992 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001993 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001994 for (unsigned I = 0; I != NumFromArgs; ++I) {
1995 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1996 if (To.isNull() && !FromArgs[I].isNull())
1997 return true;
1998
1999 ToArgs.push_back(To);
2000 }
2001
2002 return false;
2003}
2004
Douglas Gregor96a01b42010-02-11 00:48:18 +00002005bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002006 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002007 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002008 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002009 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002010 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002011}
2012
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002013bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002014 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002015 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002016 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002017 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002018}
2019
Douglas Gregor040afae2010-11-30 19:14:50 +00002020bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2021 ClassTemplateDecl *To) {
2022 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2023 Importer.getToContext(),
2024 Importer.getNonEquivalentDecls());
2025 return Ctx.IsStructurallyEquivalent(From, To);
2026}
2027
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002028Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002029 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002030 << D->getDeclKindName();
2031 return 0;
2032}
2033
Douglas Gregor788c62d2010-02-21 18:26:36 +00002034Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2035 // Import the major distinguishing characteristics of this namespace.
2036 DeclContext *DC, *LexicalDC;
2037 DeclarationName Name;
2038 SourceLocation Loc;
2039 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2040 return 0;
2041
2042 NamespaceDecl *MergeWithNamespace = 0;
2043 if (!Name) {
2044 // This is an anonymous namespace. Adopt an existing anonymous
2045 // namespace if we can.
2046 // FIXME: Not testable.
2047 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2048 MergeWithNamespace = TU->getAnonymousNamespace();
2049 else
2050 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2051 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002052 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002053 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2054 DC->localUncachedLookup(Name, FoundDecls);
2055 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2056 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002057 continue;
2058
Douglas Gregorb75a3452011-10-15 00:10:27 +00002059 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregor788c62d2010-02-21 18:26:36 +00002060 MergeWithNamespace = FoundNS;
2061 ConflictingDecls.clear();
2062 break;
2063 }
2064
Douglas Gregorb75a3452011-10-15 00:10:27 +00002065 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor788c62d2010-02-21 18:26:36 +00002066 }
2067
2068 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002069 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002070 ConflictingDecls.data(),
2071 ConflictingDecls.size());
2072 }
2073 }
2074
2075 // Create the "to" namespace, if needed.
2076 NamespaceDecl *ToNamespace = MergeWithNamespace;
2077 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002078 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2079 Importer.Import(D->getLocStart()),
2080 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002081 ToNamespace->setLexicalDeclContext(LexicalDC);
2082 LexicalDC->addDecl(ToNamespace);
2083
2084 // If this is an anonymous namespace, register it as the anonymous
2085 // namespace within its context.
2086 if (!Name) {
2087 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2088 TU->setAnonymousNamespace(ToNamespace);
2089 else
2090 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2091 }
2092 }
2093 Importer.Imported(D, ToNamespace);
2094
2095 ImportDeclContext(D);
2096
2097 return ToNamespace;
2098}
2099
Richard Smith162e1c12011-04-15 14:24:37 +00002100Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002101 // Import the major distinguishing characteristics of this typedef.
2102 DeclContext *DC, *LexicalDC;
2103 DeclarationName Name;
2104 SourceLocation Loc;
2105 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2106 return 0;
2107
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002108 // If this typedef is not in block scope, determine whether we've
2109 // seen a typedef with the same name (that we can merge with) or any
2110 // other entity by that name (which name lookup could conflict with).
2111 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002112 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002113 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002114 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2115 DC->localUncachedLookup(Name, FoundDecls);
2116 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2117 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002118 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002119 if (TypedefNameDecl *FoundTypedef =
Douglas Gregorb75a3452011-10-15 00:10:27 +00002120 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002121 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2122 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002123 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002124 }
2125
Douglas Gregorb75a3452011-10-15 00:10:27 +00002126 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002127 }
2128
2129 if (!ConflictingDecls.empty()) {
2130 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2131 ConflictingDecls.data(),
2132 ConflictingDecls.size());
2133 if (!Name)
2134 return 0;
2135 }
2136 }
2137
Douglas Gregorea35d112010-02-15 23:54:17 +00002138 // Import the underlying type of this typedef;
2139 QualType T = Importer.Import(D->getUnderlyingType());
2140 if (T.isNull())
2141 return 0;
2142
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002143 // Create the new typedef node.
2144 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002145 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002146 TypedefNameDecl *ToTypedef;
2147 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002148 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2149 StartL, Loc,
2150 Name.getAsIdentifierInfo(),
2151 TInfo);
2152 else
Richard Smith162e1c12011-04-15 14:24:37 +00002153 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2154 StartL, Loc,
2155 Name.getAsIdentifierInfo(),
2156 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002157
Douglas Gregor325bf172010-02-22 17:42:47 +00002158 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002159 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002160 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002161 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002162
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002163 return ToTypedef;
2164}
2165
Richard Smith162e1c12011-04-15 14:24:37 +00002166Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2167 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2168}
2169
2170Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2171 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2172}
2173
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002174Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2175 // Import the major distinguishing characteristics of this enum.
2176 DeclContext *DC, *LexicalDC;
2177 DeclarationName Name;
2178 SourceLocation Loc;
2179 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2180 return 0;
2181
2182 // Figure out what enum name we're looking for.
2183 unsigned IDNS = Decl::IDNS_Tag;
2184 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002185 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2186 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002187 IDNS = Decl::IDNS_Ordinary;
2188 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2189 IDNS |= Decl::IDNS_Ordinary;
2190
2191 // We may already have an enum of the same name; try to find and match it.
2192 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002193 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002194 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2195 DC->localUncachedLookup(SearchName, FoundDecls);
2196 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2197 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002198 continue;
2199
Douglas Gregorb75a3452011-10-15 00:10:27 +00002200 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002201 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002202 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2203 Found = Tag->getDecl();
2204 }
2205
2206 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002207 if (IsStructuralMatch(D, FoundEnum))
2208 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002209 }
2210
Douglas Gregorb75a3452011-10-15 00:10:27 +00002211 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002212 }
2213
2214 if (!ConflictingDecls.empty()) {
2215 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2216 ConflictingDecls.data(),
2217 ConflictingDecls.size());
2218 }
2219 }
2220
2221 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002222 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2223 Importer.Import(D->getLocStart()),
2224 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002225 D->isScoped(), D->isScopedUsingClassTag(),
2226 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002227 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002228 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002229 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002230 D2->setLexicalDeclContext(LexicalDC);
2231 Importer.Imported(D, D2);
2232 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002233
2234 // Import the integer type.
2235 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2236 if (ToIntegerType.isNull())
2237 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002238 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002239
2240 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002241 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002242 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002243
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002244 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002245}
2246
Douglas Gregor96a01b42010-02-11 00:48:18 +00002247Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2248 // If this record has a definition in the translation unit we're coming from,
2249 // but this particular declaration is not that definition, import the
2250 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002251 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002252 if (Definition && Definition != D) {
2253 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002254 if (!ImportedDef)
2255 return 0;
2256
2257 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002258 }
2259
2260 // Import the major distinguishing characteristics of this record.
2261 DeclContext *DC, *LexicalDC;
2262 DeclarationName Name;
2263 SourceLocation Loc;
2264 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2265 return 0;
2266
2267 // Figure out what structure name we're looking for.
2268 unsigned IDNS = Decl::IDNS_Tag;
2269 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002270 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2271 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002272 IDNS = Decl::IDNS_Ordinary;
2273 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2274 IDNS |= Decl::IDNS_Ordinary;
2275
2276 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002277 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002278 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002279 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002280 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2281 DC->localUncachedLookup(SearchName, FoundDecls);
2282 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2283 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor96a01b42010-02-11 00:48:18 +00002284 continue;
2285
Douglas Gregorb75a3452011-10-15 00:10:27 +00002286 Decl *Found = FoundDecls[I];
Richard Smith162e1c12011-04-15 14:24:37 +00002287 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002288 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2289 Found = Tag->getDecl();
2290 }
2291
2292 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002293 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002294 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002295 // The record types structurally match, or the "from" translation
2296 // unit only had a forward declaration anyway; call it the same
2297 // function.
2298 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002299 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002300 }
2301 } else {
2302 // We have a forward declaration of this type, so adopt that forward
2303 // declaration rather than building a new one.
2304 AdoptDecl = FoundRecord;
2305 continue;
2306 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002307 }
2308
Douglas Gregorb75a3452011-10-15 00:10:27 +00002309 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002310 }
2311
2312 if (!ConflictingDecls.empty()) {
2313 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2314 ConflictingDecls.data(),
2315 ConflictingDecls.size());
2316 }
2317 }
2318
2319 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002320 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002321 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002322 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002323 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002324 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002325 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002326 DC, StartLoc, Loc,
2327 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002328 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002329 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002330 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002331 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002332 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002333 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002334
2335 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002336 D2->setLexicalDeclContext(LexicalDC);
2337 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002338 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002339
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002340 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002341
John McCall5e1cdac2011-10-07 06:10:15 +00002342 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002343 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002344
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002345 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002346}
2347
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002348Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2349 // Import the major distinguishing characteristics of this enumerator.
2350 DeclContext *DC, *LexicalDC;
2351 DeclarationName Name;
2352 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002353 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002354 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002355
2356 QualType T = Importer.Import(D->getType());
2357 if (T.isNull())
2358 return 0;
2359
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002360 // Determine whether there are any other declarations with the same name and
2361 // in the same context.
2362 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002363 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002364 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002365 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2366 DC->localUncachedLookup(Name, FoundDecls);
2367 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2368 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002369 continue;
2370
Douglas Gregorb75a3452011-10-15 00:10:27 +00002371 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002372 }
2373
2374 if (!ConflictingDecls.empty()) {
2375 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2376 ConflictingDecls.data(),
2377 ConflictingDecls.size());
2378 if (!Name)
2379 return 0;
2380 }
2381 }
2382
2383 Expr *Init = Importer.Import(D->getInitExpr());
2384 if (D->getInitExpr() && !Init)
2385 return 0;
2386
2387 EnumConstantDecl *ToEnumerator
2388 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2389 Name.getAsIdentifierInfo(), T,
2390 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002391 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002392 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002393 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002394 LexicalDC->addDecl(ToEnumerator);
2395 return ToEnumerator;
2396}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002397
Douglas Gregora404ea62010-02-10 19:54:31 +00002398Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2399 // Import the major distinguishing characteristics of this function.
2400 DeclContext *DC, *LexicalDC;
2401 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002402 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002403 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002404 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002405
Douglas Gregora404ea62010-02-10 19:54:31 +00002406 // Try to find a function in our own ("to") context with the same name, same
2407 // type, and in the same context as the function we're importing.
2408 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002409 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002410 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002411 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2412 DC->localUncachedLookup(Name, FoundDecls);
2413 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2414 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregora404ea62010-02-10 19:54:31 +00002415 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002416
Douglas Gregorb75a3452011-10-15 00:10:27 +00002417 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002418 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2419 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002420 if (Importer.IsStructurallyEquivalent(D->getType(),
2421 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002422 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002423 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002424 }
2425
2426 // FIXME: Check for overloading more carefully, e.g., by boosting
2427 // Sema::IsOverload out to the AST library.
2428
2429 // Function overloading is okay in C++.
2430 if (Importer.getToContext().getLangOptions().CPlusPlus)
2431 continue;
2432
2433 // Complain about inconsistent function types.
2434 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002435 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002436 Importer.ToDiag(FoundFunction->getLocation(),
2437 diag::note_odr_value_here)
2438 << FoundFunction->getType();
2439 }
2440 }
2441
Douglas Gregorb75a3452011-10-15 00:10:27 +00002442 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002443 }
2444
2445 if (!ConflictingDecls.empty()) {
2446 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2447 ConflictingDecls.data(),
2448 ConflictingDecls.size());
2449 if (!Name)
2450 return 0;
2451 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002452 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002453
Abramo Bagnara25777432010-08-11 22:01:17 +00002454 DeclarationNameInfo NameInfo(Name, Loc);
2455 // Import additional name location/type info.
2456 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2457
Douglas Gregorea35d112010-02-15 23:54:17 +00002458 // Import the type.
2459 QualType T = Importer.Import(D->getType());
2460 if (T.isNull())
2461 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002462
2463 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002464 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002465 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2466 P != PEnd; ++P) {
2467 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2468 if (!ToP)
2469 return 0;
2470
2471 Parameters.push_back(ToP);
2472 }
2473
2474 // Create the imported function.
2475 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002476 FunctionDecl *ToFunction = 0;
2477 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2478 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2479 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002480 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002481 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002482 FromConstructor->isExplicit(),
2483 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002484 D->isImplicit(),
2485 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002486 } else if (isa<CXXDestructorDecl>(D)) {
2487 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2488 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002489 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002490 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002491 D->isInlineSpecified(),
2492 D->isImplicit());
2493 } else if (CXXConversionDecl *FromConversion
2494 = dyn_cast<CXXConversionDecl>(D)) {
2495 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2496 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002497 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002498 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002499 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002500 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002501 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002502 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002503 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2504 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2505 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002506 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002507 NameInfo, T, TInfo,
2508 Method->isStatic(),
2509 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002510 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002511 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002512 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002513 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002514 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002515 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002516 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002517 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002518 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002519 D->hasWrittenPrototype(),
2520 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002521 }
John McCallb6217662010-03-15 10:12:16 +00002522
2523 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002524 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002525 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002526 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002527 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2528 ToFunction->setTrivial(D->isTrivial());
2529 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002530 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002531
Douglas Gregora404ea62010-02-10 19:54:31 +00002532 // Set the parameters.
2533 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002534 Parameters[I]->setOwningFunction(ToFunction);
2535 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002536 }
David Blaikie4278c652011-09-21 18:16:56 +00002537 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002538
2539 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002540
2541 // Add this function to the lexical context.
2542 LexicalDC->addDecl(ToFunction);
2543
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002544 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002545}
2546
Douglas Gregorc144f352010-02-21 18:29:16 +00002547Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2548 return VisitFunctionDecl(D);
2549}
2550
2551Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2552 return VisitCXXMethodDecl(D);
2553}
2554
2555Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2556 return VisitCXXMethodDecl(D);
2557}
2558
2559Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2560 return VisitCXXMethodDecl(D);
2561}
2562
Douglas Gregor96a01b42010-02-11 00:48:18 +00002563Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2564 // Import the major distinguishing characteristics of a variable.
2565 DeclContext *DC, *LexicalDC;
2566 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002567 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002568 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2569 return 0;
2570
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002571 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002572 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2573 DC->localUncachedLookup(Name, FoundDecls);
2574 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2575 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002576 if (Importer.IsStructurallyEquivalent(D->getType(),
2577 FoundField->getType())) {
2578 Importer.Imported(D, FoundField);
2579 return FoundField;
2580 }
2581
2582 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2583 << Name << D->getType() << FoundField->getType();
2584 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2585 << FoundField->getType();
2586 return 0;
2587 }
2588 }
2589
Douglas Gregorea35d112010-02-15 23:54:17 +00002590 // Import the type.
2591 QualType T = Importer.Import(D->getType());
2592 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002593 return 0;
2594
2595 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2596 Expr *BitWidth = Importer.Import(D->getBitWidth());
2597 if (!BitWidth && D->getBitWidth())
2598 return 0;
2599
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002600 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2601 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002602 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002603 T, TInfo, BitWidth, D->isMutable(),
2604 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002605 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002606 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002607 if (ToField->hasInClassInitializer())
2608 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002609 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002610 LexicalDC->addDecl(ToField);
2611 return ToField;
2612}
2613
Francois Pichet87c2e122010-11-21 06:08:52 +00002614Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2615 // Import the major distinguishing characteristics of a variable.
2616 DeclContext *DC, *LexicalDC;
2617 DeclarationName Name;
2618 SourceLocation Loc;
2619 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2620 return 0;
2621
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002622 // Determine whether we've already imported this field.
Douglas Gregorb75a3452011-10-15 00:10:27 +00002623 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2624 DC->localUncachedLookup(Name, FoundDecls);
2625 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002626 if (IndirectFieldDecl *FoundField
Douglas Gregorb75a3452011-10-15 00:10:27 +00002627 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002628 if (Importer.IsStructurallyEquivalent(D->getType(),
2629 FoundField->getType())) {
2630 Importer.Imported(D, FoundField);
2631 return FoundField;
2632 }
2633
2634 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2635 << Name << D->getType() << FoundField->getType();
2636 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2637 << FoundField->getType();
2638 return 0;
2639 }
2640 }
2641
Francois Pichet87c2e122010-11-21 06:08:52 +00002642 // Import the type.
2643 QualType T = Importer.Import(D->getType());
2644 if (T.isNull())
2645 return 0;
2646
2647 NamedDecl **NamedChain =
2648 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2649
2650 unsigned i = 0;
2651 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2652 PE = D->chain_end(); PI != PE; ++PI) {
2653 Decl* D = Importer.Import(*PI);
2654 if (!D)
2655 return 0;
2656 NamedChain[i++] = cast<NamedDecl>(D);
2657 }
2658
2659 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2660 Importer.getToContext(), DC,
2661 Loc, Name.getAsIdentifierInfo(), T,
2662 NamedChain, D->getChainingSize());
2663 ToIndirectField->setAccess(D->getAccess());
2664 ToIndirectField->setLexicalDeclContext(LexicalDC);
2665 Importer.Imported(D, ToIndirectField);
2666 LexicalDC->addDecl(ToIndirectField);
2667 return ToIndirectField;
2668}
2669
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002670Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2671 // Import the major distinguishing characteristics of an ivar.
2672 DeclContext *DC, *LexicalDC;
2673 DeclarationName Name;
2674 SourceLocation Loc;
2675 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2676 return 0;
2677
2678 // Determine whether we've already imported this ivar
Douglas Gregorb75a3452011-10-15 00:10:27 +00002679 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2680 DC->localUncachedLookup(Name, FoundDecls);
2681 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2682 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002683 if (Importer.IsStructurallyEquivalent(D->getType(),
2684 FoundIvar->getType())) {
2685 Importer.Imported(D, FoundIvar);
2686 return FoundIvar;
2687 }
2688
2689 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2690 << Name << D->getType() << FoundIvar->getType();
2691 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2692 << FoundIvar->getType();
2693 return 0;
2694 }
2695 }
2696
2697 // Import the type.
2698 QualType T = Importer.Import(D->getType());
2699 if (T.isNull())
2700 return 0;
2701
2702 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2703 Expr *BitWidth = Importer.Import(D->getBitWidth());
2704 if (!BitWidth && D->getBitWidth())
2705 return 0;
2706
Daniel Dunbara0654922010-04-02 20:10:03 +00002707 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2708 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002709 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002710 Loc, Name.getAsIdentifierInfo(),
2711 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002712 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002713 ToIvar->setLexicalDeclContext(LexicalDC);
2714 Importer.Imported(D, ToIvar);
2715 LexicalDC->addDecl(ToIvar);
2716 return ToIvar;
2717
2718}
2719
Douglas Gregora404ea62010-02-10 19:54:31 +00002720Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2721 // Import the major distinguishing characteristics of a variable.
2722 DeclContext *DC, *LexicalDC;
2723 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002724 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002725 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002726 return 0;
2727
Douglas Gregor089459a2010-02-08 21:09:39 +00002728 // Try to find a variable in our own ("to") context with the same name and
2729 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002730 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002731 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002732 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002733 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregorb75a3452011-10-15 00:10:27 +00002734 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2735 DC->localUncachedLookup(Name, FoundDecls);
2736 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2737 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor089459a2010-02-08 21:09:39 +00002738 continue;
2739
Douglas Gregorb75a3452011-10-15 00:10:27 +00002740 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002741 // We have found a variable that we may need to merge with. Check it.
2742 if (isExternalLinkage(FoundVar->getLinkage()) &&
2743 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002744 if (Importer.IsStructurallyEquivalent(D->getType(),
2745 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002746 MergeWithVar = FoundVar;
2747 break;
2748 }
2749
Douglas Gregord0145422010-02-12 17:23:39 +00002750 const ArrayType *FoundArray
2751 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2752 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002753 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002754 if (FoundArray && TArray) {
2755 if (isa<IncompleteArrayType>(FoundArray) &&
2756 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002757 // Import the type.
2758 QualType T = Importer.Import(D->getType());
2759 if (T.isNull())
2760 return 0;
2761
Douglas Gregord0145422010-02-12 17:23:39 +00002762 FoundVar->setType(T);
2763 MergeWithVar = FoundVar;
2764 break;
2765 } else if (isa<IncompleteArrayType>(TArray) &&
2766 isa<ConstantArrayType>(FoundArray)) {
2767 MergeWithVar = FoundVar;
2768 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002769 }
2770 }
2771
Douglas Gregor089459a2010-02-08 21:09:39 +00002772 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002773 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002774 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2775 << FoundVar->getType();
2776 }
2777 }
2778
Douglas Gregorb75a3452011-10-15 00:10:27 +00002779 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor089459a2010-02-08 21:09:39 +00002780 }
2781
2782 if (MergeWithVar) {
2783 // An equivalent variable with external linkage has been found. Link
2784 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002785 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002786
2787 if (VarDecl *DDef = D->getDefinition()) {
2788 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2789 Importer.ToDiag(ExistingDef->getLocation(),
2790 diag::err_odr_variable_multiple_def)
2791 << Name;
2792 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2793 } else {
2794 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002795 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002796 }
2797 }
2798
2799 return MergeWithVar;
2800 }
2801
2802 if (!ConflictingDecls.empty()) {
2803 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2804 ConflictingDecls.data(),
2805 ConflictingDecls.size());
2806 if (!Name)
2807 return 0;
2808 }
2809 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002810
Douglas Gregorea35d112010-02-15 23:54:17 +00002811 // Import the type.
2812 QualType T = Importer.Import(D->getType());
2813 if (T.isNull())
2814 return 0;
2815
Douglas Gregor089459a2010-02-08 21:09:39 +00002816 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002817 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002818 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2819 Importer.Import(D->getInnerLocStart()),
2820 Loc, Name.getAsIdentifierInfo(),
2821 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002822 D->getStorageClass(),
2823 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002824 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002825 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002826 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002827 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002828 LexicalDC->addDecl(ToVar);
2829
Douglas Gregor089459a2010-02-08 21:09:39 +00002830 // Merge the initializer.
2831 // FIXME: Can we really import any initializer? Alternatively, we could force
2832 // ourselves to import every declaration of a variable and then only use
2833 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002834 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002835
2836 // FIXME: Other bits to merge?
2837
2838 return ToVar;
2839}
2840
Douglas Gregor2cd00932010-02-17 21:22:52 +00002841Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2842 // Parameters are created in the translation unit's context, then moved
2843 // into the function declaration's context afterward.
2844 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2845
2846 // Import the name of this declaration.
2847 DeclarationName Name = Importer.Import(D->getDeclName());
2848 if (D->getDeclName() && !Name)
2849 return 0;
2850
2851 // Import the location of this declaration.
2852 SourceLocation Loc = Importer.Import(D->getLocation());
2853
2854 // Import the parameter's type.
2855 QualType T = Importer.Import(D->getType());
2856 if (T.isNull())
2857 return 0;
2858
2859 // Create the imported parameter.
2860 ImplicitParamDecl *ToParm
2861 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2862 Loc, Name.getAsIdentifierInfo(),
2863 T);
2864 return Importer.Imported(D, ToParm);
2865}
2866
Douglas Gregora404ea62010-02-10 19:54:31 +00002867Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2868 // Parameters are created in the translation unit's context, then moved
2869 // into the function declaration's context afterward.
2870 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2871
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002872 // Import the name of this declaration.
2873 DeclarationName Name = Importer.Import(D->getDeclName());
2874 if (D->getDeclName() && !Name)
2875 return 0;
2876
Douglas Gregora404ea62010-02-10 19:54:31 +00002877 // Import the location of this declaration.
2878 SourceLocation Loc = Importer.Import(D->getLocation());
2879
2880 // Import the parameter's type.
2881 QualType T = Importer.Import(D->getType());
2882 if (T.isNull())
2883 return 0;
2884
2885 // Create the imported parameter.
2886 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2887 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002888 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002889 Loc, Name.getAsIdentifierInfo(),
2890 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002891 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002892 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002893 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002894 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002895}
2896
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002897Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2898 // Import the major distinguishing characteristics of a method.
2899 DeclContext *DC, *LexicalDC;
2900 DeclarationName Name;
2901 SourceLocation Loc;
2902 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2903 return 0;
2904
Douglas Gregorb75a3452011-10-15 00:10:27 +00002905 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
2906 DC->localUncachedLookup(Name, FoundDecls);
2907 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2908 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002909 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2910 continue;
2911
2912 // Check return types.
2913 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2914 FoundMethod->getResultType())) {
2915 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2916 << D->isInstanceMethod() << Name
2917 << D->getResultType() << FoundMethod->getResultType();
2918 Importer.ToDiag(FoundMethod->getLocation(),
2919 diag::note_odr_objc_method_here)
2920 << D->isInstanceMethod() << Name;
2921 return 0;
2922 }
2923
2924 // Check the number of parameters.
2925 if (D->param_size() != FoundMethod->param_size()) {
2926 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2927 << D->isInstanceMethod() << Name
2928 << D->param_size() << FoundMethod->param_size();
2929 Importer.ToDiag(FoundMethod->getLocation(),
2930 diag::note_odr_objc_method_here)
2931 << D->isInstanceMethod() << Name;
2932 return 0;
2933 }
2934
2935 // Check parameter types.
2936 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2937 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2938 P != PEnd; ++P, ++FoundP) {
2939 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2940 (*FoundP)->getType())) {
2941 Importer.FromDiag((*P)->getLocation(),
2942 diag::err_odr_objc_method_param_type_inconsistent)
2943 << D->isInstanceMethod() << Name
2944 << (*P)->getType() << (*FoundP)->getType();
2945 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2946 << (*FoundP)->getType();
2947 return 0;
2948 }
2949 }
2950
2951 // Check variadic/non-variadic.
2952 // Check the number of parameters.
2953 if (D->isVariadic() != FoundMethod->isVariadic()) {
2954 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2955 << D->isInstanceMethod() << Name;
2956 Importer.ToDiag(FoundMethod->getLocation(),
2957 diag::note_odr_objc_method_here)
2958 << D->isInstanceMethod() << Name;
2959 return 0;
2960 }
2961
2962 // FIXME: Any other bits we need to merge?
2963 return Importer.Imported(D, FoundMethod);
2964 }
2965 }
2966
2967 // Import the result type.
2968 QualType ResultTy = Importer.Import(D->getResultType());
2969 if (ResultTy.isNull())
2970 return 0;
2971
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002972 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2973
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002974 ObjCMethodDecl *ToMethod
2975 = ObjCMethodDecl::Create(Importer.getToContext(),
2976 Loc,
2977 Importer.Import(D->getLocEnd()),
2978 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002979 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002980 D->isInstanceMethod(),
2981 D->isVariadic(),
2982 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002983 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002984 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002985 D->getImplementationControl(),
2986 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002987
2988 // FIXME: When we decide to merge method definitions, we'll need to
2989 // deal with implicit parameters.
2990
2991 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002992 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002993 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2994 FromPEnd = D->param_end();
2995 FromP != FromPEnd;
2996 ++FromP) {
2997 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2998 if (!ToP)
2999 return 0;
3000
3001 ToParams.push_back(ToP);
3002 }
3003
3004 // Set the parameters.
3005 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3006 ToParams[I]->setOwningFunction(ToMethod);
3007 ToMethod->addDecl(ToParams[I]);
3008 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003009 SmallVector<SourceLocation, 12> SelLocs;
3010 D->getSelectorLocs(SelLocs);
3011 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003012
3013 ToMethod->setLexicalDeclContext(LexicalDC);
3014 Importer.Imported(D, ToMethod);
3015 LexicalDC->addDecl(ToMethod);
3016 return ToMethod;
3017}
3018
Douglas Gregorb4677b62010-02-18 01:47:50 +00003019Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3020 // Import the major distinguishing characteristics of a category.
3021 DeclContext *DC, *LexicalDC;
3022 DeclarationName Name;
3023 SourceLocation Loc;
3024 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3025 return 0;
3026
3027 ObjCInterfaceDecl *ToInterface
3028 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3029 if (!ToInterface)
3030 return 0;
3031
3032 // Determine if we've already encountered this category.
3033 ObjCCategoryDecl *MergeWithCategory
3034 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3035 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3036 if (!ToCategory) {
3037 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003038 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003039 Loc,
3040 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003041 Name.getAsIdentifierInfo(),
3042 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003043 ToCategory->setLexicalDeclContext(LexicalDC);
3044 LexicalDC->addDecl(ToCategory);
3045 Importer.Imported(D, ToCategory);
3046
Douglas Gregorb4677b62010-02-18 01:47:50 +00003047 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3049 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003050 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3051 = D->protocol_loc_begin();
3052 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3053 FromProtoEnd = D->protocol_end();
3054 FromProto != FromProtoEnd;
3055 ++FromProto, ++FromProtoLoc) {
3056 ObjCProtocolDecl *ToProto
3057 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3058 if (!ToProto)
3059 return 0;
3060 Protocols.push_back(ToProto);
3061 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3062 }
3063
3064 // FIXME: If we're merging, make sure that the protocol list is the same.
3065 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3066 ProtocolLocs.data(), Importer.getToContext());
3067
3068 } else {
3069 Importer.Imported(D, ToCategory);
3070 }
3071
3072 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003073 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003074
3075 // If we have an implementation, import it as well.
3076 if (D->getImplementation()) {
3077 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003078 = cast_or_null<ObjCCategoryImplDecl>(
3079 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003080 if (!Impl)
3081 return 0;
3082
3083 ToCategory->setImplementation(Impl);
3084 }
3085
3086 return ToCategory;
3087}
3088
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003089Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003090 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003091 DeclContext *DC, *LexicalDC;
3092 DeclarationName Name;
3093 SourceLocation Loc;
3094 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3095 return 0;
3096
3097 ObjCProtocolDecl *MergeWithProtocol = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003098 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3099 DC->localUncachedLookup(Name, FoundDecls);
3100 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3101 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003102 continue;
3103
Douglas Gregorb75a3452011-10-15 00:10:27 +00003104 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003105 break;
3106 }
3107
3108 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3109 if (!ToProto || ToProto->isForwardDecl()) {
3110 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003111 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3112 Name.getAsIdentifierInfo(), Loc,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +00003113 Importer.Import(D->getAtStartLoc()),
3114 D->isInitiallyForwardDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003115 ToProto->setForwardDecl(D->isForwardDecl());
3116 ToProto->setLexicalDeclContext(LexicalDC);
3117 LexicalDC->addDecl(ToProto);
3118 }
3119 Importer.Imported(D, ToProto);
3120
3121 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3123 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003124 ObjCProtocolDecl::protocol_loc_iterator
3125 FromProtoLoc = D->protocol_loc_begin();
3126 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3127 FromProtoEnd = D->protocol_end();
3128 FromProto != FromProtoEnd;
3129 ++FromProto, ++FromProtoLoc) {
3130 ObjCProtocolDecl *ToProto
3131 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3132 if (!ToProto)
3133 return 0;
3134 Protocols.push_back(ToProto);
3135 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3136 }
3137
3138 // FIXME: If we're merging, make sure that the protocol list is the same.
3139 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3140 ProtocolLocs.data(), Importer.getToContext());
3141 } else {
3142 Importer.Imported(D, ToProto);
3143 }
3144
Douglas Gregorb4677b62010-02-18 01:47:50 +00003145 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003146 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003147
3148 return ToProto;
3149}
3150
Douglas Gregora12d2942010-02-16 01:20:57 +00003151Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3152 // Import the major distinguishing characteristics of an @interface.
3153 DeclContext *DC, *LexicalDC;
3154 DeclarationName Name;
3155 SourceLocation Loc;
3156 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3157 return 0;
3158
3159 ObjCInterfaceDecl *MergeWithIface = 0;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003160 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3161 DC->localUncachedLookup(Name, FoundDecls);
3162 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3163 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora12d2942010-02-16 01:20:57 +00003164 continue;
3165
Douglas Gregorb75a3452011-10-15 00:10:27 +00003166 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregora12d2942010-02-16 01:20:57 +00003167 break;
3168 }
3169
3170 ObjCInterfaceDecl *ToIface = MergeWithIface;
3171 if (!ToIface || ToIface->isForwardDecl()) {
3172 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003173 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3174 Importer.Import(D->getAtStartLoc()),
3175 Name.getAsIdentifierInfo(), Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003176 D->isForwardDecl(),
3177 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003178 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003179 ToIface->setLexicalDeclContext(LexicalDC);
3180 LexicalDC->addDecl(ToIface);
3181 }
3182 Importer.Imported(D, ToIface);
3183
Douglas Gregora12d2942010-02-16 01:20:57 +00003184 if (D->getSuperClass()) {
3185 ObjCInterfaceDecl *Super
3186 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3187 if (!Super)
3188 return 0;
3189
3190 ToIface->setSuperClass(Super);
3191 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3192 }
3193
3194 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003195 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3196 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003197 ObjCInterfaceDecl::protocol_loc_iterator
3198 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003199
3200 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003201 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3202 FromProtoEnd = D->protocol_end();
3203 FromProto != FromProtoEnd;
3204 ++FromProto, ++FromProtoLoc) {
3205 ObjCProtocolDecl *ToProto
3206 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3207 if (!ToProto)
3208 return 0;
3209 Protocols.push_back(ToProto);
3210 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3211 }
3212
3213 // FIXME: If we're merging, make sure that the protocol list is the same.
3214 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3215 ProtocolLocs.data(), Importer.getToContext());
3216
Douglas Gregora12d2942010-02-16 01:20:57 +00003217 // Import @end range
3218 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3219 } else {
3220 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003221
3222 // Check for consistency of superclasses.
3223 DeclarationName FromSuperName, ToSuperName;
3224 if (D->getSuperClass())
3225 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3226 if (ToIface->getSuperClass())
3227 ToSuperName = ToIface->getSuperClass()->getDeclName();
3228 if (FromSuperName != ToSuperName) {
3229 Importer.ToDiag(ToIface->getLocation(),
3230 diag::err_odr_objc_superclass_inconsistent)
3231 << ToIface->getDeclName();
3232 if (ToIface->getSuperClass())
3233 Importer.ToDiag(ToIface->getSuperClassLoc(),
3234 diag::note_odr_objc_superclass)
3235 << ToIface->getSuperClass()->getDeclName();
3236 else
3237 Importer.ToDiag(ToIface->getLocation(),
3238 diag::note_odr_objc_missing_superclass);
3239 if (D->getSuperClass())
3240 Importer.FromDiag(D->getSuperClassLoc(),
3241 diag::note_odr_objc_superclass)
3242 << D->getSuperClass()->getDeclName();
3243 else
3244 Importer.FromDiag(D->getLocation(),
3245 diag::note_odr_objc_missing_superclass);
3246 return 0;
3247 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003248 }
3249
Douglas Gregorb4677b62010-02-18 01:47:50 +00003250 // Import categories. When the categories themselves are imported, they'll
3251 // hook themselves into this interface.
3252 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3253 FromCat = FromCat->getNextClassCategory())
3254 Importer.Import(FromCat);
3255
Douglas Gregora12d2942010-02-16 01:20:57 +00003256 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003257 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003258
3259 // If we have an @implementation, import it as well.
3260 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003261 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3262 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003263 if (!Impl)
3264 return 0;
3265
3266 ToIface->setImplementation(Impl);
3267 }
3268
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003269 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003270}
3271
Douglas Gregor3daef292010-12-07 15:32:12 +00003272Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3273 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3274 Importer.Import(D->getCategoryDecl()));
3275 if (!Category)
3276 return 0;
3277
3278 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3279 if (!ToImpl) {
3280 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3281 if (!DC)
3282 return 0;
3283
3284 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003285 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003286 Category->getClassInterface(),
3287 Importer.Import(D->getLocation()),
3288 Importer.Import(D->getAtStartLoc()));
Douglas Gregor3daef292010-12-07 15:32:12 +00003289
3290 DeclContext *LexicalDC = DC;
3291 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3292 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3293 if (!LexicalDC)
3294 return 0;
3295
3296 ToImpl->setLexicalDeclContext(LexicalDC);
3297 }
3298
3299 LexicalDC->addDecl(ToImpl);
3300 Category->setImplementation(ToImpl);
3301 }
3302
3303 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003304 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003305 return ToImpl;
3306}
3307
Douglas Gregordd182ff2010-12-07 01:26:03 +00003308Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3309 // Find the corresponding interface.
3310 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3311 Importer.Import(D->getClassInterface()));
3312 if (!Iface)
3313 return 0;
3314
3315 // Import the superclass, if any.
3316 ObjCInterfaceDecl *Super = 0;
3317 if (D->getSuperClass()) {
3318 Super = cast_or_null<ObjCInterfaceDecl>(
3319 Importer.Import(D->getSuperClass()));
3320 if (!Super)
3321 return 0;
3322 }
3323
3324 ObjCImplementationDecl *Impl = Iface->getImplementation();
3325 if (!Impl) {
3326 // We haven't imported an implementation yet. Create a new @implementation
3327 // now.
3328 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3329 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003330 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003331 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003332 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003333
3334 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3335 DeclContext *LexicalDC
3336 = Importer.ImportContext(D->getLexicalDeclContext());
3337 if (!LexicalDC)
3338 return 0;
3339 Impl->setLexicalDeclContext(LexicalDC);
3340 }
3341
3342 // Associate the implementation with the class it implements.
3343 Iface->setImplementation(Impl);
3344 Importer.Imported(D, Iface->getImplementation());
3345 } else {
3346 Importer.Imported(D, Iface->getImplementation());
3347
3348 // Verify that the existing @implementation has the same superclass.
3349 if ((Super && !Impl->getSuperClass()) ||
3350 (!Super && Impl->getSuperClass()) ||
3351 (Super && Impl->getSuperClass() &&
3352 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3353 Importer.ToDiag(Impl->getLocation(),
3354 diag::err_odr_objc_superclass_inconsistent)
3355 << Iface->getDeclName();
3356 // FIXME: It would be nice to have the location of the superclass
3357 // below.
3358 if (Impl->getSuperClass())
3359 Importer.ToDiag(Impl->getLocation(),
3360 diag::note_odr_objc_superclass)
3361 << Impl->getSuperClass()->getDeclName();
3362 else
3363 Importer.ToDiag(Impl->getLocation(),
3364 diag::note_odr_objc_missing_superclass);
3365 if (D->getSuperClass())
3366 Importer.FromDiag(D->getLocation(),
3367 diag::note_odr_objc_superclass)
3368 << D->getSuperClass()->getDeclName();
3369 else
3370 Importer.FromDiag(D->getLocation(),
3371 diag::note_odr_objc_missing_superclass);
3372 return 0;
3373 }
3374 }
3375
3376 // Import all of the members of this @implementation.
3377 ImportDeclContext(D);
3378
3379 return Impl;
3380}
3381
Douglas Gregore3261622010-02-17 18:02:10 +00003382Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3383 // Import the major distinguishing characteristics of an @property.
3384 DeclContext *DC, *LexicalDC;
3385 DeclarationName Name;
3386 SourceLocation Loc;
3387 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3388 return 0;
3389
3390 // Check whether we have already imported this property.
Douglas Gregorb75a3452011-10-15 00:10:27 +00003391 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3392 DC->localUncachedLookup(Name, FoundDecls);
3393 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregore3261622010-02-17 18:02:10 +00003394 if (ObjCPropertyDecl *FoundProp
Douglas Gregorb75a3452011-10-15 00:10:27 +00003395 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregore3261622010-02-17 18:02:10 +00003396 // Check property types.
3397 if (!Importer.IsStructurallyEquivalent(D->getType(),
3398 FoundProp->getType())) {
3399 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3400 << Name << D->getType() << FoundProp->getType();
3401 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3402 << FoundProp->getType();
3403 return 0;
3404 }
3405
3406 // FIXME: Check property attributes, getters, setters, etc.?
3407
3408 // Consider these properties to be equivalent.
3409 Importer.Imported(D, FoundProp);
3410 return FoundProp;
3411 }
3412 }
3413
3414 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003415 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3416 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003417 return 0;
3418
3419 // Create the new property.
3420 ObjCPropertyDecl *ToProperty
3421 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3422 Name.getAsIdentifierInfo(),
3423 Importer.Import(D->getAtLoc()),
3424 T,
3425 D->getPropertyImplementation());
3426 Importer.Imported(D, ToProperty);
3427 ToProperty->setLexicalDeclContext(LexicalDC);
3428 LexicalDC->addDecl(ToProperty);
3429
3430 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003431 ToProperty->setPropertyAttributesAsWritten(
3432 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003433 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3434 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3435 ToProperty->setGetterMethodDecl(
3436 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3437 ToProperty->setSetterMethodDecl(
3438 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3439 ToProperty->setPropertyIvarDecl(
3440 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3441 return ToProperty;
3442}
3443
Douglas Gregor954e0c72010-12-07 18:32:03 +00003444Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3445 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3446 Importer.Import(D->getPropertyDecl()));
3447 if (!Property)
3448 return 0;
3449
3450 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3451 if (!DC)
3452 return 0;
3453
3454 // Import the lexical declaration context.
3455 DeclContext *LexicalDC = DC;
3456 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3457 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3458 if (!LexicalDC)
3459 return 0;
3460 }
3461
3462 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3463 if (!InImpl)
3464 return 0;
3465
3466 // Import the ivar (for an @synthesize).
3467 ObjCIvarDecl *Ivar = 0;
3468 if (D->getPropertyIvarDecl()) {
3469 Ivar = cast_or_null<ObjCIvarDecl>(
3470 Importer.Import(D->getPropertyIvarDecl()));
3471 if (!Ivar)
3472 return 0;
3473 }
3474
3475 ObjCPropertyImplDecl *ToImpl
3476 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3477 if (!ToImpl) {
3478 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3479 Importer.Import(D->getLocStart()),
3480 Importer.Import(D->getLocation()),
3481 Property,
3482 D->getPropertyImplementation(),
3483 Ivar,
3484 Importer.Import(D->getPropertyIvarDeclLoc()));
3485 ToImpl->setLexicalDeclContext(LexicalDC);
3486 Importer.Imported(D, ToImpl);
3487 LexicalDC->addDecl(ToImpl);
3488 } else {
3489 // Check that we have the same kind of property implementation (@synthesize
3490 // vs. @dynamic).
3491 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3492 Importer.ToDiag(ToImpl->getLocation(),
3493 diag::err_odr_objc_property_impl_kind_inconsistent)
3494 << Property->getDeclName()
3495 << (ToImpl->getPropertyImplementation()
3496 == ObjCPropertyImplDecl::Dynamic);
3497 Importer.FromDiag(D->getLocation(),
3498 diag::note_odr_objc_property_impl_kind)
3499 << D->getPropertyDecl()->getDeclName()
3500 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3501 return 0;
3502 }
3503
3504 // For @synthesize, check that we have the same
3505 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3506 Ivar != ToImpl->getPropertyIvarDecl()) {
3507 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3508 diag::err_odr_objc_synthesize_ivar_inconsistent)
3509 << Property->getDeclName()
3510 << ToImpl->getPropertyIvarDecl()->getDeclName()
3511 << Ivar->getDeclName();
3512 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3513 diag::note_odr_objc_synthesize_ivar_here)
3514 << D->getPropertyIvarDecl()->getDeclName();
3515 return 0;
3516 }
3517
3518 // Merge the existing implementation with the new implementation.
3519 Importer.Imported(D, ToImpl);
3520 }
3521
3522 return ToImpl;
3523}
3524
Douglas Gregor2b785022010-02-18 02:12:22 +00003525Decl *
3526ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3527 // Import the context of this declaration.
3528 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3529 if (!DC)
3530 return 0;
3531
3532 DeclContext *LexicalDC = DC;
3533 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3534 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3535 if (!LexicalDC)
3536 return 0;
3537 }
3538
3539 // Import the location of this declaration.
3540 SourceLocation Loc = Importer.Import(D->getLocation());
3541
Chris Lattner5f9e2722011-07-23 10:55:15 +00003542 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3543 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003544 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3545 = D->protocol_loc_begin();
3546 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3547 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3548 FromProto != FromProtoEnd;
3549 ++FromProto, ++FromProtoLoc) {
3550 ObjCProtocolDecl *ToProto
3551 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3552 if (!ToProto)
3553 continue;
3554
3555 Protocols.push_back(ToProto);
3556 Locations.push_back(Importer.Import(*FromProtoLoc));
3557 }
3558
3559 ObjCForwardProtocolDecl *ToForward
3560 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3561 Protocols.data(), Protocols.size(),
3562 Locations.data());
3563 ToForward->setLexicalDeclContext(LexicalDC);
3564 LexicalDC->addDecl(ToForward);
3565 Importer.Imported(D, ToForward);
3566 return ToForward;
3567}
3568
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003569Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3570 // Import the context of this declaration.
3571 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3572 if (!DC)
3573 return 0;
3574
3575 DeclContext *LexicalDC = DC;
3576 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3577 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3578 if (!LexicalDC)
3579 return 0;
3580 }
3581
3582 // Import the location of this declaration.
3583 SourceLocation Loc = Importer.Import(D->getLocation());
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003584 ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
3585 ObjCInterfaceDecl *ToIface
3586 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003587 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003588 Loc,
3589 ToIface,
3590 Importer.Import(From->getLocation()));
3591
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003592 ToClass->setLexicalDeclContext(LexicalDC);
3593 LexicalDC->addDecl(ToClass);
3594 Importer.Imported(D, ToClass);
3595 return ToClass;
3596}
3597
Douglas Gregor040afae2010-11-30 19:14:50 +00003598Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3599 // For template arguments, we adopt the translation unit as our declaration
3600 // context. This context will be fixed when the actual template declaration
3601 // is created.
3602
3603 // FIXME: Import default argument.
3604 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3605 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003606 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003607 Importer.Import(D->getLocation()),
3608 D->getDepth(),
3609 D->getIndex(),
3610 Importer.Import(D->getIdentifier()),
3611 D->wasDeclaredWithTypename(),
3612 D->isParameterPack());
3613}
3614
3615Decl *
3616ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3617 // Import the name of this declaration.
3618 DeclarationName Name = Importer.Import(D->getDeclName());
3619 if (D->getDeclName() && !Name)
3620 return 0;
3621
3622 // Import the location of this declaration.
3623 SourceLocation Loc = Importer.Import(D->getLocation());
3624
3625 // Import the type of this declaration.
3626 QualType T = Importer.Import(D->getType());
3627 if (T.isNull())
3628 return 0;
3629
3630 // Import type-source information.
3631 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3632 if (D->getTypeSourceInfo() && !TInfo)
3633 return 0;
3634
3635 // FIXME: Import default argument.
3636
3637 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3638 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003639 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003640 Loc, D->getDepth(), D->getPosition(),
3641 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003642 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003643}
3644
3645Decl *
3646ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3647 // Import the name of this declaration.
3648 DeclarationName Name = Importer.Import(D->getDeclName());
3649 if (D->getDeclName() && !Name)
3650 return 0;
3651
3652 // Import the location of this declaration.
3653 SourceLocation Loc = Importer.Import(D->getLocation());
3654
3655 // Import template parameters.
3656 TemplateParameterList *TemplateParams
3657 = ImportTemplateParameterList(D->getTemplateParameters());
3658 if (!TemplateParams)
3659 return 0;
3660
3661 // FIXME: Import default argument.
3662
3663 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3664 Importer.getToContext().getTranslationUnitDecl(),
3665 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003666 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003667 Name.getAsIdentifierInfo(),
3668 TemplateParams);
3669}
3670
3671Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3672 // If this record has a definition in the translation unit we're coming from,
3673 // but this particular declaration is not that definition, import the
3674 // definition and map to that.
3675 CXXRecordDecl *Definition
3676 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3677 if (Definition && Definition != D->getTemplatedDecl()) {
3678 Decl *ImportedDef
3679 = Importer.Import(Definition->getDescribedClassTemplate());
3680 if (!ImportedDef)
3681 return 0;
3682
3683 return Importer.Imported(D, ImportedDef);
3684 }
3685
3686 // Import the major distinguishing characteristics of this class template.
3687 DeclContext *DC, *LexicalDC;
3688 DeclarationName Name;
3689 SourceLocation Loc;
3690 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3691 return 0;
3692
3693 // We may already have a template of the same name; try to find and match it.
3694 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003695 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorb75a3452011-10-15 00:10:27 +00003696 llvm::SmallVector<NamedDecl *, 2> FoundDecls;
3697 DC->localUncachedLookup(Name, FoundDecls);
3698 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3699 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor040afae2010-11-30 19:14:50 +00003700 continue;
3701
Douglas Gregorb75a3452011-10-15 00:10:27 +00003702 Decl *Found = FoundDecls[I];
Douglas Gregor040afae2010-11-30 19:14:50 +00003703 if (ClassTemplateDecl *FoundTemplate
3704 = dyn_cast<ClassTemplateDecl>(Found)) {
3705 if (IsStructuralMatch(D, FoundTemplate)) {
3706 // The class templates structurally match; call it the same template.
3707 // FIXME: We may be filling in a forward declaration here. Handle
3708 // this case!
3709 Importer.Imported(D->getTemplatedDecl(),
3710 FoundTemplate->getTemplatedDecl());
3711 return Importer.Imported(D, FoundTemplate);
3712 }
3713 }
3714
Douglas Gregorb75a3452011-10-15 00:10:27 +00003715 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor040afae2010-11-30 19:14:50 +00003716 }
3717
3718 if (!ConflictingDecls.empty()) {
3719 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3720 ConflictingDecls.data(),
3721 ConflictingDecls.size());
3722 }
3723
3724 if (!Name)
3725 return 0;
3726 }
3727
3728 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3729
3730 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003731 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3732 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003733 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3734 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003735 DC, StartLoc, IdLoc,
3736 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003737 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003738 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003739 D2Templated->setLexicalDeclContext(LexicalDC);
3740
3741 // Create the class template declaration itself.
3742 TemplateParameterList *TemplateParams
3743 = ImportTemplateParameterList(D->getTemplateParameters());
3744 if (!TemplateParams)
3745 return 0;
3746
3747 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3748 Loc, Name, TemplateParams,
3749 D2Templated,
3750 /*PrevDecl=*/0);
3751 D2Templated->setDescribedClassTemplate(D2);
3752
3753 D2->setAccess(D->getAccess());
3754 D2->setLexicalDeclContext(LexicalDC);
3755 LexicalDC->addDecl(D2);
3756
3757 // Note the relationship between the class templates.
3758 Importer.Imported(D, D2);
3759 Importer.Imported(DTemplated, D2Templated);
3760
John McCall5e1cdac2011-10-07 06:10:15 +00003761 if (DTemplated->isCompleteDefinition() &&
3762 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003763 // FIXME: Import definition!
3764 }
3765
3766 return D2;
3767}
3768
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003769Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3770 ClassTemplateSpecializationDecl *D) {
3771 // If this record has a definition in the translation unit we're coming from,
3772 // but this particular declaration is not that definition, import the
3773 // definition and map to that.
3774 TagDecl *Definition = D->getDefinition();
3775 if (Definition && Definition != D) {
3776 Decl *ImportedDef = Importer.Import(Definition);
3777 if (!ImportedDef)
3778 return 0;
3779
3780 return Importer.Imported(D, ImportedDef);
3781 }
3782
3783 ClassTemplateDecl *ClassTemplate
3784 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3785 D->getSpecializedTemplate()));
3786 if (!ClassTemplate)
3787 return 0;
3788
3789 // Import the context of this declaration.
3790 DeclContext *DC = ClassTemplate->getDeclContext();
3791 if (!DC)
3792 return 0;
3793
3794 DeclContext *LexicalDC = DC;
3795 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3796 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3797 if (!LexicalDC)
3798 return 0;
3799 }
3800
3801 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003802 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3803 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003804
3805 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003806 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003807 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3808 D->getTemplateArgs().size(),
3809 TemplateArgs))
3810 return 0;
3811
3812 // Try to find an existing specialization with these template arguments.
3813 void *InsertPos = 0;
3814 ClassTemplateSpecializationDecl *D2
3815 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3816 TemplateArgs.size(), InsertPos);
3817 if (D2) {
3818 // We already have a class template specialization with these template
3819 // arguments.
3820
3821 // FIXME: Check for specialization vs. instantiation errors.
3822
3823 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003824 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003825 // The record types structurally match, or the "from" translation
3826 // unit only had a forward declaration anyway; call it the same
3827 // function.
3828 return Importer.Imported(D, FoundDef);
3829 }
3830 }
3831 } else {
3832 // Create a new specialization.
3833 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3834 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003835 StartLoc, IdLoc,
3836 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003837 TemplateArgs.data(),
3838 TemplateArgs.size(),
3839 /*PrevDecl=*/0);
3840 D2->setSpecializationKind(D->getSpecializationKind());
3841
3842 // Add this specialization to the class template.
3843 ClassTemplate->AddSpecialization(D2, InsertPos);
3844
3845 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003846 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003847
3848 // Add the specialization to this context.
3849 D2->setLexicalDeclContext(LexicalDC);
3850 LexicalDC->addDecl(D2);
3851 }
3852 Importer.Imported(D, D2);
3853
John McCall5e1cdac2011-10-07 06:10:15 +00003854 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003855 return 0;
3856
3857 return D2;
3858}
3859
Douglas Gregor4800d952010-02-11 19:21:55 +00003860//----------------------------------------------------------------------------
3861// Import Statements
3862//----------------------------------------------------------------------------
3863
3864Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3865 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3866 << S->getStmtClassName();
3867 return 0;
3868}
3869
3870//----------------------------------------------------------------------------
3871// Import Expressions
3872//----------------------------------------------------------------------------
3873Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3874 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3875 << E->getStmtClassName();
3876 return 0;
3877}
3878
Douglas Gregor44080632010-02-19 01:17:02 +00003879Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003880 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3881 if (!ToD)
3882 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003883
3884 NamedDecl *FoundD = 0;
3885 if (E->getDecl() != E->getFoundDecl()) {
3886 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3887 if (!FoundD)
3888 return 0;
3889 }
Douglas Gregor44080632010-02-19 01:17:02 +00003890
3891 QualType T = Importer.Import(E->getType());
3892 if (T.isNull())
3893 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003894
3895 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3896 Importer.Import(E->getQualifierLoc()),
3897 ToD,
3898 Importer.Import(E->getLocation()),
3899 T, E->getValueKind(),
3900 FoundD,
3901 /*FIXME:TemplateArgs=*/0);
3902 if (E->hadMultipleCandidates())
3903 DRE->setHadMultipleCandidates(true);
3904 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003905}
3906
Douglas Gregor4800d952010-02-11 19:21:55 +00003907Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3908 QualType T = Importer.Import(E->getType());
3909 if (T.isNull())
3910 return 0;
3911
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003912 return IntegerLiteral::Create(Importer.getToContext(),
3913 E->getValue(), T,
3914 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003915}
3916
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003917Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3918 QualType T = Importer.Import(E->getType());
3919 if (T.isNull())
3920 return 0;
3921
Douglas Gregor5cee1192011-07-27 05:40:30 +00003922 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3923 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003924 Importer.Import(E->getLocation()));
3925}
3926
Douglas Gregorf638f952010-02-19 01:07:06 +00003927Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3928 Expr *SubExpr = Importer.Import(E->getSubExpr());
3929 if (!SubExpr)
3930 return 0;
3931
3932 return new (Importer.getToContext())
3933 ParenExpr(Importer.Import(E->getLParen()),
3934 Importer.Import(E->getRParen()),
3935 SubExpr);
3936}
3937
3938Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3939 QualType T = Importer.Import(E->getType());
3940 if (T.isNull())
3941 return 0;
3942
3943 Expr *SubExpr = Importer.Import(E->getSubExpr());
3944 if (!SubExpr)
3945 return 0;
3946
3947 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003948 T, E->getValueKind(),
3949 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003950 Importer.Import(E->getOperatorLoc()));
3951}
3952
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003953Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3954 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003955 QualType ResultType = Importer.Import(E->getType());
3956
3957 if (E->isArgumentType()) {
3958 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3959 if (!TInfo)
3960 return 0;
3961
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003962 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3963 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003964 Importer.Import(E->getOperatorLoc()),
3965 Importer.Import(E->getRParenLoc()));
3966 }
3967
3968 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3969 if (!SubExpr)
3970 return 0;
3971
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003972 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3973 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003974 Importer.Import(E->getOperatorLoc()),
3975 Importer.Import(E->getRParenLoc()));
3976}
3977
Douglas Gregorf638f952010-02-19 01:07:06 +00003978Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3979 QualType T = Importer.Import(E->getType());
3980 if (T.isNull())
3981 return 0;
3982
3983 Expr *LHS = Importer.Import(E->getLHS());
3984 if (!LHS)
3985 return 0;
3986
3987 Expr *RHS = Importer.Import(E->getRHS());
3988 if (!RHS)
3989 return 0;
3990
3991 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003992 T, E->getValueKind(),
3993 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003994 Importer.Import(E->getOperatorLoc()));
3995}
3996
3997Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3998 QualType T = Importer.Import(E->getType());
3999 if (T.isNull())
4000 return 0;
4001
4002 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4003 if (CompLHSType.isNull())
4004 return 0;
4005
4006 QualType CompResultType = Importer.Import(E->getComputationResultType());
4007 if (CompResultType.isNull())
4008 return 0;
4009
4010 Expr *LHS = Importer.Import(E->getLHS());
4011 if (!LHS)
4012 return 0;
4013
4014 Expr *RHS = Importer.Import(E->getRHS());
4015 if (!RHS)
4016 return 0;
4017
4018 return new (Importer.getToContext())
4019 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004020 T, E->getValueKind(),
4021 E->getObjectKind(),
4022 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004023 Importer.Import(E->getOperatorLoc()));
4024}
4025
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004026static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004027 if (E->path_empty()) return false;
4028
4029 // TODO: import cast paths
4030 return true;
4031}
4032
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004033Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4034 QualType T = Importer.Import(E->getType());
4035 if (T.isNull())
4036 return 0;
4037
4038 Expr *SubExpr = Importer.Import(E->getSubExpr());
4039 if (!SubExpr)
4040 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004041
4042 CXXCastPath BasePath;
4043 if (ImportCastPath(E, BasePath))
4044 return 0;
4045
4046 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004047 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004048}
4049
Douglas Gregor008847a2010-02-19 01:32:14 +00004050Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4051 QualType T = Importer.Import(E->getType());
4052 if (T.isNull())
4053 return 0;
4054
4055 Expr *SubExpr = Importer.Import(E->getSubExpr());
4056 if (!SubExpr)
4057 return 0;
4058
4059 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4060 if (!TInfo && E->getTypeInfoAsWritten())
4061 return 0;
4062
John McCallf871d0c2010-08-07 06:22:56 +00004063 CXXCastPath BasePath;
4064 if (ImportCastPath(E, BasePath))
4065 return 0;
4066
John McCallf89e55a2010-11-18 06:31:45 +00004067 return CStyleCastExpr::Create(Importer.getToContext(), T,
4068 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004069 SubExpr, &BasePath, TInfo,
4070 Importer.Import(E->getLParenLoc()),
4071 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004072}
4073
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004074ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004075 ASTContext &FromContext, FileManager &FromFileManager,
4076 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004077 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004078 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4079 Minimal(MinimalImport)
4080{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004081 ImportedDecls[FromContext.getTranslationUnitDecl()]
4082 = ToContext.getTranslationUnitDecl();
4083}
4084
4085ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004086
4087QualType ASTImporter::Import(QualType FromT) {
4088 if (FromT.isNull())
4089 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004090
4091 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004092
Douglas Gregor169fba52010-02-08 15:18:58 +00004093 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004094 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4095 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004096 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004097 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004098
Douglas Gregor169fba52010-02-08 15:18:58 +00004099 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004100 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004101 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004102 if (ToT.isNull())
4103 return ToT;
4104
Douglas Gregor169fba52010-02-08 15:18:58 +00004105 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004106 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004107
John McCallf4c73712011-01-19 06:33:43 +00004108 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004109}
4110
Douglas Gregor9bed8792010-02-09 19:21:46 +00004111TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004112 if (!FromTSI)
4113 return FromTSI;
4114
4115 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004116 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004117 QualType T = Import(FromTSI->getType());
4118 if (T.isNull())
4119 return 0;
4120
4121 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004122 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004123}
4124
4125Decl *ASTImporter::Import(Decl *FromD) {
4126 if (!FromD)
4127 return 0;
4128
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004129 ASTNodeImporter Importer(*this);
4130
Douglas Gregor9bed8792010-02-09 19:21:46 +00004131 // Check whether we've already imported this declaration.
4132 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004133 if (Pos != ImportedDecls.end()) {
4134 Decl *ToD = Pos->second;
4135 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4136 return ToD;
4137 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004138
4139 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004140 Decl *ToD = Importer.Visit(FromD);
4141 if (!ToD)
4142 return 0;
4143
4144 // Record the imported declaration.
4145 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004146
4147 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4148 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004149 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004150 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004151 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004152 // When we've finished transforming a typedef, see whether it was the
4153 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004154 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004155 FromTag = AnonTagsWithPendingTypedefs.begin(),
4156 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4157 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004158 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004159 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4160 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004161 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004162 AnonTagsWithPendingTypedefs.erase(FromTag);
4163 break;
4164 }
4165 }
4166 }
4167 }
4168
Douglas Gregor9bed8792010-02-09 19:21:46 +00004169 return ToD;
4170}
4171
4172DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4173 if (!FromDC)
4174 return FromDC;
4175
4176 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4177}
4178
4179Expr *ASTImporter::Import(Expr *FromE) {
4180 if (!FromE)
4181 return 0;
4182
4183 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4184}
4185
4186Stmt *ASTImporter::Import(Stmt *FromS) {
4187 if (!FromS)
4188 return 0;
4189
Douglas Gregor4800d952010-02-11 19:21:55 +00004190 // Check whether we've already imported this declaration.
4191 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4192 if (Pos != ImportedStmts.end())
4193 return Pos->second;
4194
4195 // Import the type
4196 ASTNodeImporter Importer(*this);
4197 Stmt *ToS = Importer.Visit(FromS);
4198 if (!ToS)
4199 return 0;
4200
4201 // Record the imported declaration.
4202 ImportedStmts[FromS] = ToS;
4203 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004204}
4205
4206NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4207 if (!FromNNS)
4208 return 0;
4209
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004210 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4211
4212 switch (FromNNS->getKind()) {
4213 case NestedNameSpecifier::Identifier:
4214 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4215 return NestedNameSpecifier::Create(ToContext, prefix, II);
4216 }
4217 return 0;
4218
4219 case NestedNameSpecifier::Namespace:
4220 if (NamespaceDecl *NS =
4221 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4222 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4223 }
4224 return 0;
4225
4226 case NestedNameSpecifier::NamespaceAlias:
4227 if (NamespaceAliasDecl *NSAD =
4228 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4229 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4230 }
4231 return 0;
4232
4233 case NestedNameSpecifier::Global:
4234 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4235
4236 case NestedNameSpecifier::TypeSpec:
4237 case NestedNameSpecifier::TypeSpecWithTemplate: {
4238 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4239 if (!T.isNull()) {
4240 bool bTemplate = FromNNS->getKind() ==
4241 NestedNameSpecifier::TypeSpecWithTemplate;
4242 return NestedNameSpecifier::Create(ToContext, prefix,
4243 bTemplate, T.getTypePtr());
4244 }
4245 }
4246 return 0;
4247 }
4248
4249 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004250 return 0;
4251}
4252
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004253NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4254 // FIXME: Implement!
4255 return NestedNameSpecifierLoc();
4256}
4257
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004258TemplateName ASTImporter::Import(TemplateName From) {
4259 switch (From.getKind()) {
4260 case TemplateName::Template:
4261 if (TemplateDecl *ToTemplate
4262 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4263 return TemplateName(ToTemplate);
4264
4265 return TemplateName();
4266
4267 case TemplateName::OverloadedTemplate: {
4268 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4269 UnresolvedSet<2> ToTemplates;
4270 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4271 E = FromStorage->end();
4272 I != E; ++I) {
4273 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4274 ToTemplates.addDecl(To);
4275 else
4276 return TemplateName();
4277 }
4278 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4279 ToTemplates.end());
4280 }
4281
4282 case TemplateName::QualifiedTemplate: {
4283 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4284 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4285 if (!Qualifier)
4286 return TemplateName();
4287
4288 if (TemplateDecl *ToTemplate
4289 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4290 return ToContext.getQualifiedTemplateName(Qualifier,
4291 QTN->hasTemplateKeyword(),
4292 ToTemplate);
4293
4294 return TemplateName();
4295 }
4296
4297 case TemplateName::DependentTemplate: {
4298 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4299 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4300 if (!Qualifier)
4301 return TemplateName();
4302
4303 if (DTN->isIdentifier()) {
4304 return ToContext.getDependentTemplateName(Qualifier,
4305 Import(DTN->getIdentifier()));
4306 }
4307
4308 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4309 }
John McCall14606042011-06-30 08:33:18 +00004310
4311 case TemplateName::SubstTemplateTemplateParm: {
4312 SubstTemplateTemplateParmStorage *subst
4313 = From.getAsSubstTemplateTemplateParm();
4314 TemplateTemplateParmDecl *param
4315 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4316 if (!param)
4317 return TemplateName();
4318
4319 TemplateName replacement = Import(subst->getReplacement());
4320 if (replacement.isNull()) return TemplateName();
4321
4322 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4323 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004324
4325 case TemplateName::SubstTemplateTemplateParmPack: {
4326 SubstTemplateTemplateParmPackStorage *SubstPack
4327 = From.getAsSubstTemplateTemplateParmPack();
4328 TemplateTemplateParmDecl *Param
4329 = cast_or_null<TemplateTemplateParmDecl>(
4330 Import(SubstPack->getParameterPack()));
4331 if (!Param)
4332 return TemplateName();
4333
4334 ASTNodeImporter Importer(*this);
4335 TemplateArgument ArgPack
4336 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4337 if (ArgPack.isNull())
4338 return TemplateName();
4339
4340 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4341 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004342 }
4343
4344 llvm_unreachable("Invalid template name kind");
4345 return TemplateName();
4346}
4347
Douglas Gregor9bed8792010-02-09 19:21:46 +00004348SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4349 if (FromLoc.isInvalid())
4350 return SourceLocation();
4351
Douglas Gregor88523732010-02-10 00:15:17 +00004352 SourceManager &FromSM = FromContext.getSourceManager();
4353
4354 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004355 // don't have to import macro expansions.
4356 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004357 FromLoc = FromSM.getSpellingLoc(FromLoc);
4358 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4359 SourceManager &ToSM = ToContext.getSourceManager();
4360 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004361 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004362}
4363
4364SourceRange ASTImporter::Import(SourceRange FromRange) {
4365 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4366}
4367
Douglas Gregor88523732010-02-10 00:15:17 +00004368FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004369 llvm::DenseMap<FileID, FileID>::iterator Pos
4370 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004371 if (Pos != ImportedFileIDs.end())
4372 return Pos->second;
4373
4374 SourceManager &FromSM = FromContext.getSourceManager();
4375 SourceManager &ToSM = ToContext.getSourceManager();
4376 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004377 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004378
4379 // Include location of this file.
4380 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4381
4382 // Map the FileID for to the "to" source manager.
4383 FileID ToID;
4384 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004385 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004386 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4387 // disk again
4388 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4389 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004390 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004391 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4392 FromSLoc.getFile().getFileCharacteristic());
4393 } else {
4394 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004395 const llvm::MemoryBuffer *
4396 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004397 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004398 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004399 FromBuf->getBufferIdentifier());
4400 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4401 }
4402
4403
Sebastian Redl535a3e22010-09-30 01:03:06 +00004404 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004405 return ToID;
4406}
4407
Douglas Gregord8868a62011-01-18 03:11:38 +00004408void ASTImporter::ImportDefinition(Decl *From) {
4409 Decl *To = Import(From);
4410 if (!To)
4411 return;
4412
4413 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4414 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004415
4416 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4417 if (!ToRecord->getDefinition()) {
4418 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4419 /*ForceImport=*/true);
4420 return;
4421 }
4422 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004423
4424 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4425 if (!ToEnum->getDefinition()) {
4426 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4427 /*ForceImport=*/true);
4428 return;
4429 }
4430 }
4431
Douglas Gregord8868a62011-01-18 03:11:38 +00004432 Importer.ImportDeclContext(FromDC, true);
4433 }
4434}
4435
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004436DeclarationName ASTImporter::Import(DeclarationName FromName) {
4437 if (!FromName)
4438 return DeclarationName();
4439
4440 switch (FromName.getNameKind()) {
4441 case DeclarationName::Identifier:
4442 return Import(FromName.getAsIdentifierInfo());
4443
4444 case DeclarationName::ObjCZeroArgSelector:
4445 case DeclarationName::ObjCOneArgSelector:
4446 case DeclarationName::ObjCMultiArgSelector:
4447 return Import(FromName.getObjCSelector());
4448
4449 case DeclarationName::CXXConstructorName: {
4450 QualType T = Import(FromName.getCXXNameType());
4451 if (T.isNull())
4452 return DeclarationName();
4453
4454 return ToContext.DeclarationNames.getCXXConstructorName(
4455 ToContext.getCanonicalType(T));
4456 }
4457
4458 case DeclarationName::CXXDestructorName: {
4459 QualType T = Import(FromName.getCXXNameType());
4460 if (T.isNull())
4461 return DeclarationName();
4462
4463 return ToContext.DeclarationNames.getCXXDestructorName(
4464 ToContext.getCanonicalType(T));
4465 }
4466
4467 case DeclarationName::CXXConversionFunctionName: {
4468 QualType T = Import(FromName.getCXXNameType());
4469 if (T.isNull())
4470 return DeclarationName();
4471
4472 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4473 ToContext.getCanonicalType(T));
4474 }
4475
4476 case DeclarationName::CXXOperatorName:
4477 return ToContext.DeclarationNames.getCXXOperatorName(
4478 FromName.getCXXOverloadedOperator());
4479
4480 case DeclarationName::CXXLiteralOperatorName:
4481 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4482 Import(FromName.getCXXLiteralIdentifier()));
4483
4484 case DeclarationName::CXXUsingDirective:
4485 // FIXME: STATICS!
4486 return DeclarationName::getUsingDirectiveName();
4487 }
4488
4489 // Silence bogus GCC warning
4490 return DeclarationName();
4491}
4492
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004493IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004494 if (!FromId)
4495 return 0;
4496
4497 return &ToContext.Idents.get(FromId->getName());
4498}
Douglas Gregor089459a2010-02-08 21:09:39 +00004499
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004500Selector ASTImporter::Import(Selector FromSel) {
4501 if (FromSel.isNull())
4502 return Selector();
4503
Chris Lattner5f9e2722011-07-23 10:55:15 +00004504 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004505 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4506 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4507 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4508 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4509}
4510
Douglas Gregor089459a2010-02-08 21:09:39 +00004511DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4512 DeclContext *DC,
4513 unsigned IDNS,
4514 NamedDecl **Decls,
4515 unsigned NumDecls) {
4516 return Name;
4517}
4518
4519DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004520 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004521}
4522
4523DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004524 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004525}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004526
4527Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4528 ImportedDecls[From] = To;
4529 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004530}
Douglas Gregorea35d112010-02-15 23:54:17 +00004531
4532bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004533 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004534 = ImportedTypes.find(From.getTypePtr());
4535 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4536 return true;
4537
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004538 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004539 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004540}