blob: ab5c8cf5fe176ecec00784f1c24e1e788dc28ea7 [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;
1377 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1378 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1379 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1380
1381 case BuiltinType::NullPtr:
1382 // FIXME: Make sure that the "to" context supports C++0x!
1383 return Importer.getToContext().NullPtrTy;
1384
1385 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1386 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001387 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001388 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001389
1390 case BuiltinType::ObjCId:
1391 // FIXME: Make sure that the "to" context supports Objective-C!
1392 return Importer.getToContext().ObjCBuiltinIdTy;
1393
1394 case BuiltinType::ObjCClass:
1395 return Importer.getToContext().ObjCBuiltinClassTy;
1396
1397 case BuiltinType::ObjCSel:
1398 return Importer.getToContext().ObjCBuiltinSelTy;
1399 }
1400
1401 return QualType();
1402}
1403
John McCallf4c73712011-01-19 06:33:43 +00001404QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001405 QualType ToElementType = Importer.Import(T->getElementType());
1406 if (ToElementType.isNull())
1407 return QualType();
1408
1409 return Importer.getToContext().getComplexType(ToElementType);
1410}
1411
John McCallf4c73712011-01-19 06:33:43 +00001412QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001413 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1414 if (ToPointeeType.isNull())
1415 return QualType();
1416
1417 return Importer.getToContext().getPointerType(ToPointeeType);
1418}
1419
John McCallf4c73712011-01-19 06:33:43 +00001420QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421 // FIXME: Check for blocks support in "to" context.
1422 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1423 if (ToPointeeType.isNull())
1424 return QualType();
1425
1426 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1427}
1428
John McCallf4c73712011-01-19 06:33:43 +00001429QualType
1430ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001431 // FIXME: Check for C++ support in "to" context.
1432 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1433 if (ToPointeeType.isNull())
1434 return QualType();
1435
1436 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1437}
1438
John McCallf4c73712011-01-19 06:33:43 +00001439QualType
1440ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001441 // FIXME: Check for C++0x support in "to" context.
1442 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1443 if (ToPointeeType.isNull())
1444 return QualType();
1445
1446 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1447}
1448
John McCallf4c73712011-01-19 06:33:43 +00001449QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001450 // FIXME: Check for C++ support in "to" context.
1451 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1452 if (ToPointeeType.isNull())
1453 return QualType();
1454
1455 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1456 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1457 ClassType.getTypePtr());
1458}
1459
John McCallf4c73712011-01-19 06:33:43 +00001460QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001461 QualType ToElementType = Importer.Import(T->getElementType());
1462 if (ToElementType.isNull())
1463 return QualType();
1464
1465 return Importer.getToContext().getConstantArrayType(ToElementType,
1466 T->getSize(),
1467 T->getSizeModifier(),
1468 T->getIndexTypeCVRQualifiers());
1469}
1470
John McCallf4c73712011-01-19 06:33:43 +00001471QualType
1472ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001473 QualType ToElementType = Importer.Import(T->getElementType());
1474 if (ToElementType.isNull())
1475 return QualType();
1476
1477 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1478 T->getSizeModifier(),
1479 T->getIndexTypeCVRQualifiers());
1480}
1481
John McCallf4c73712011-01-19 06:33:43 +00001482QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001483 QualType ToElementType = Importer.Import(T->getElementType());
1484 if (ToElementType.isNull())
1485 return QualType();
1486
1487 Expr *Size = Importer.Import(T->getSizeExpr());
1488 if (!Size)
1489 return QualType();
1490
1491 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1492 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1493 T->getSizeModifier(),
1494 T->getIndexTypeCVRQualifiers(),
1495 Brackets);
1496}
1497
John McCallf4c73712011-01-19 06:33:43 +00001498QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001499 QualType ToElementType = Importer.Import(T->getElementType());
1500 if (ToElementType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getVectorType(ToElementType,
1504 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001505 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001506}
1507
John McCallf4c73712011-01-19 06:33:43 +00001508QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001509 QualType ToElementType = Importer.Import(T->getElementType());
1510 if (ToElementType.isNull())
1511 return QualType();
1512
1513 return Importer.getToContext().getExtVectorType(ToElementType,
1514 T->getNumElements());
1515}
1516
John McCallf4c73712011-01-19 06:33:43 +00001517QualType
1518ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001519 // FIXME: What happens if we're importing a function without a prototype
1520 // into C++? Should we make it variadic?
1521 QualType ToResultType = Importer.Import(T->getResultType());
1522 if (ToResultType.isNull())
1523 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001524
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001525 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001526 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001527}
1528
John McCallf4c73712011-01-19 06:33:43 +00001529QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001530 QualType ToResultType = Importer.Import(T->getResultType());
1531 if (ToResultType.isNull())
1532 return QualType();
1533
1534 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001535 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001536 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1537 AEnd = T->arg_type_end();
1538 A != AEnd; ++A) {
1539 QualType ArgType = Importer.Import(*A);
1540 if (ArgType.isNull())
1541 return QualType();
1542 ArgTypes.push_back(ArgType);
1543 }
1544
1545 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001546 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001547 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1548 EEnd = T->exception_end();
1549 E != EEnd; ++E) {
1550 QualType ExceptionType = Importer.Import(*E);
1551 if (ExceptionType.isNull())
1552 return QualType();
1553 ExceptionTypes.push_back(ExceptionType);
1554 }
John McCalle23cf432010-12-14 08:05:40 +00001555
1556 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1557 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001558
1559 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001560 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001561}
1562
Sean Callanan0aeb2892011-08-11 16:56:07 +00001563QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1564 QualType ToInnerType = Importer.Import(T->getInnerType());
1565 if (ToInnerType.isNull())
1566 return QualType();
1567
1568 return Importer.getToContext().getParenType(ToInnerType);
1569}
1570
John McCallf4c73712011-01-19 06:33:43 +00001571QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001572 TypedefNameDecl *ToDecl
1573 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001574 if (!ToDecl)
1575 return QualType();
1576
1577 return Importer.getToContext().getTypeDeclType(ToDecl);
1578}
1579
John McCallf4c73712011-01-19 06:33:43 +00001580QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001581 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1582 if (!ToExpr)
1583 return QualType();
1584
1585 return Importer.getToContext().getTypeOfExprType(ToExpr);
1586}
1587
John McCallf4c73712011-01-19 06:33:43 +00001588QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001589 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1590 if (ToUnderlyingType.isNull())
1591 return QualType();
1592
1593 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1594}
1595
John McCallf4c73712011-01-19 06:33:43 +00001596QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001597 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001598 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1599 if (!ToExpr)
1600 return QualType();
1601
1602 return Importer.getToContext().getDecltypeType(ToExpr);
1603}
1604
Sean Huntca63c202011-05-24 22:41:36 +00001605QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1606 QualType ToBaseType = Importer.Import(T->getBaseType());
1607 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1608 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1609 return QualType();
1610
1611 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1612 ToUnderlyingType,
1613 T->getUTTKind());
1614}
1615
Richard Smith34b41d92011-02-20 03:19:35 +00001616QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1617 // FIXME: Make sure that the "to" context supports C++0x!
1618 QualType FromDeduced = T->getDeducedType();
1619 QualType ToDeduced;
1620 if (!FromDeduced.isNull()) {
1621 ToDeduced = Importer.Import(FromDeduced);
1622 if (ToDeduced.isNull())
1623 return QualType();
1624 }
1625
1626 return Importer.getToContext().getAutoType(ToDeduced);
1627}
1628
John McCallf4c73712011-01-19 06:33:43 +00001629QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001630 RecordDecl *ToDecl
1631 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1632 if (!ToDecl)
1633 return QualType();
1634
1635 return Importer.getToContext().getTagDeclType(ToDecl);
1636}
1637
John McCallf4c73712011-01-19 06:33:43 +00001638QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001639 EnumDecl *ToDecl
1640 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1641 if (!ToDecl)
1642 return QualType();
1643
1644 return Importer.getToContext().getTagDeclType(ToDecl);
1645}
1646
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001647QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001648 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001649 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1650 if (ToTemplate.isNull())
1651 return QualType();
1652
Chris Lattner5f9e2722011-07-23 10:55:15 +00001653 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001654 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1655 return QualType();
1656
1657 QualType ToCanonType;
1658 if (!QualType(T, 0).isCanonical()) {
1659 QualType FromCanonType
1660 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1661 ToCanonType =Importer.Import(FromCanonType);
1662 if (ToCanonType.isNull())
1663 return QualType();
1664 }
1665 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1666 ToTemplateArgs.data(),
1667 ToTemplateArgs.size(),
1668 ToCanonType);
1669}
1670
John McCallf4c73712011-01-19 06:33:43 +00001671QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001672 NestedNameSpecifier *ToQualifier = 0;
1673 // Note: the qualifier in an ElaboratedType is optional.
1674 if (T->getQualifier()) {
1675 ToQualifier = Importer.Import(T->getQualifier());
1676 if (!ToQualifier)
1677 return QualType();
1678 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001679
1680 QualType ToNamedType = Importer.Import(T->getNamedType());
1681 if (ToNamedType.isNull())
1682 return QualType();
1683
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001684 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1685 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001686}
1687
John McCallf4c73712011-01-19 06:33:43 +00001688QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001689 ObjCInterfaceDecl *Class
1690 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1691 if (!Class)
1692 return QualType();
1693
John McCallc12c5bb2010-05-15 11:32:37 +00001694 return Importer.getToContext().getObjCInterfaceType(Class);
1695}
1696
John McCallf4c73712011-01-19 06:33:43 +00001697QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001698 QualType ToBaseType = Importer.Import(T->getBaseType());
1699 if (ToBaseType.isNull())
1700 return QualType();
1701
Chris Lattner5f9e2722011-07-23 10:55:15 +00001702 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001703 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001704 PEnd = T->qual_end();
1705 P != PEnd; ++P) {
1706 ObjCProtocolDecl *Protocol
1707 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1708 if (!Protocol)
1709 return QualType();
1710 Protocols.push_back(Protocol);
1711 }
1712
John McCallc12c5bb2010-05-15 11:32:37 +00001713 return Importer.getToContext().getObjCObjectType(ToBaseType,
1714 Protocols.data(),
1715 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001716}
1717
John McCallf4c73712011-01-19 06:33:43 +00001718QualType
1719ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001720 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1721 if (ToPointeeType.isNull())
1722 return QualType();
1723
John McCallc12c5bb2010-05-15 11:32:37 +00001724 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001725}
1726
Douglas Gregor089459a2010-02-08 21:09:39 +00001727//----------------------------------------------------------------------------
1728// Import Declarations
1729//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001730bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1731 DeclContext *&LexicalDC,
1732 DeclarationName &Name,
1733 SourceLocation &Loc) {
1734 // Import the context of this declaration.
1735 DC = Importer.ImportContext(D->getDeclContext());
1736 if (!DC)
1737 return true;
1738
1739 LexicalDC = DC;
1740 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1741 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1742 if (!LexicalDC)
1743 return true;
1744 }
1745
1746 // Import the name of this declaration.
1747 Name = Importer.Import(D->getDeclName());
1748 if (D->getDeclName() && !Name)
1749 return true;
1750
1751 // Import the location of this declaration.
1752 Loc = Importer.Import(D->getLocation());
1753 return false;
1754}
1755
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001756void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1757 if (!FromD)
1758 return;
1759
1760 if (!ToD) {
1761 ToD = Importer.Import(FromD);
1762 if (!ToD)
1763 return;
1764 }
1765
1766 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1767 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1768 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1769 ImportDefinition(FromRecord, ToRecord);
1770 }
1771 }
1772 return;
1773 }
1774
1775 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1776 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1777 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1778 ImportDefinition(FromEnum, ToEnum);
1779 }
1780 }
1781 return;
1782 }
1783}
1784
Abramo Bagnara25777432010-08-11 22:01:17 +00001785void
1786ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1787 DeclarationNameInfo& To) {
1788 // NOTE: To.Name and To.Loc are already imported.
1789 // We only have to import To.LocInfo.
1790 switch (To.getName().getNameKind()) {
1791 case DeclarationName::Identifier:
1792 case DeclarationName::ObjCZeroArgSelector:
1793 case DeclarationName::ObjCOneArgSelector:
1794 case DeclarationName::ObjCMultiArgSelector:
1795 case DeclarationName::CXXUsingDirective:
1796 return;
1797
1798 case DeclarationName::CXXOperatorName: {
1799 SourceRange Range = From.getCXXOperatorNameRange();
1800 To.setCXXOperatorNameRange(Importer.Import(Range));
1801 return;
1802 }
1803 case DeclarationName::CXXLiteralOperatorName: {
1804 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1805 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1806 return;
1807 }
1808 case DeclarationName::CXXConstructorName:
1809 case DeclarationName::CXXDestructorName:
1810 case DeclarationName::CXXConversionFunctionName: {
1811 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1812 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1813 return;
1814 }
David Blaikieb219cfc2011-09-23 05:06:16 +00001815 llvm_unreachable("Unknown name kind.");
Abramo Bagnara25777432010-08-11 22:01:17 +00001816 }
1817}
1818
Douglas Gregord8868a62011-01-18 03:11:38 +00001819void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1820 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001821 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001822 return;
1823 }
1824
Douglas Gregor083a8212010-02-21 18:24:45 +00001825 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1826 FromEnd = FromDC->decls_end();
1827 From != FromEnd;
1828 ++From)
1829 Importer.Import(*From);
1830}
1831
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001832bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1833 bool ForceImport) {
1834 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001835 return false;
1836
1837 To->startDefinition();
1838
1839 // Add base classes.
1840 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1841 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1842
Chris Lattner5f9e2722011-07-23 10:55:15 +00001843 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001844 for (CXXRecordDecl::base_class_iterator
1845 Base1 = FromCXX->bases_begin(),
1846 FromBaseEnd = FromCXX->bases_end();
1847 Base1 != FromBaseEnd;
1848 ++Base1) {
1849 QualType T = Importer.Import(Base1->getType());
1850 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001851 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001852
1853 SourceLocation EllipsisLoc;
1854 if (Base1->isPackExpansion())
1855 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001856
1857 // Ensure that we have a definition for the base.
1858 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1859
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001860 Bases.push_back(
1861 new (Importer.getToContext())
1862 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1863 Base1->isVirtual(),
1864 Base1->isBaseOfClass(),
1865 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001866 Importer.Import(Base1->getTypeSourceInfo()),
1867 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001868 }
1869 if (!Bases.empty())
1870 ToCXX->setBases(Bases.data(), Bases.size());
1871 }
1872
Sean Callanan673e7752011-07-19 22:38:25 +00001873 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001874 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001875 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001876}
1877
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001878bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1879 bool ForceImport) {
1880 if (To->getDefinition() || To->isBeingDefined())
1881 return false;
1882
1883 To->startDefinition();
1884
1885 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1886 if (T.isNull())
1887 return true;
1888
1889 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1890 if (ToPromotionType.isNull())
1891 return true;
1892
1893 ImportDeclContext(From, ForceImport);
1894
1895 // FIXME: we might need to merge the number of positive or negative bits
1896 // if the enumerator lists don't match.
1897 To->completeDefinition(T, ToPromotionType,
1898 From->getNumPositiveBits(),
1899 From->getNumNegativeBits());
1900 return false;
1901}
1902
Douglas Gregor040afae2010-11-30 19:14:50 +00001903TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1904 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001905 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001906 ToParams.reserve(Params->size());
1907 for (TemplateParameterList::iterator P = Params->begin(),
1908 PEnd = Params->end();
1909 P != PEnd; ++P) {
1910 Decl *To = Importer.Import(*P);
1911 if (!To)
1912 return 0;
1913
1914 ToParams.push_back(cast<NamedDecl>(To));
1915 }
1916
1917 return TemplateParameterList::Create(Importer.getToContext(),
1918 Importer.Import(Params->getTemplateLoc()),
1919 Importer.Import(Params->getLAngleLoc()),
1920 ToParams.data(), ToParams.size(),
1921 Importer.Import(Params->getRAngleLoc()));
1922}
1923
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001924TemplateArgument
1925ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1926 switch (From.getKind()) {
1927 case TemplateArgument::Null:
1928 return TemplateArgument();
1929
1930 case TemplateArgument::Type: {
1931 QualType ToType = Importer.Import(From.getAsType());
1932 if (ToType.isNull())
1933 return TemplateArgument();
1934 return TemplateArgument(ToType);
1935 }
1936
1937 case TemplateArgument::Integral: {
1938 QualType ToType = Importer.Import(From.getIntegralType());
1939 if (ToType.isNull())
1940 return TemplateArgument();
1941 return TemplateArgument(*From.getAsIntegral(), ToType);
1942 }
1943
1944 case TemplateArgument::Declaration:
1945 if (Decl *To = Importer.Import(From.getAsDecl()))
1946 return TemplateArgument(To);
1947 return TemplateArgument();
1948
1949 case TemplateArgument::Template: {
1950 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1951 if (ToTemplate.isNull())
1952 return TemplateArgument();
1953
1954 return TemplateArgument(ToTemplate);
1955 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001956
1957 case TemplateArgument::TemplateExpansion: {
1958 TemplateName ToTemplate
1959 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1960 if (ToTemplate.isNull())
1961 return TemplateArgument();
1962
Douglas Gregor2be29f42011-01-14 23:41:42 +00001963 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001964 }
1965
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001966 case TemplateArgument::Expression:
1967 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1968 return TemplateArgument(ToExpr);
1969 return TemplateArgument();
1970
1971 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001972 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001973 ToPack.reserve(From.pack_size());
1974 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1975 return TemplateArgument();
1976
1977 TemplateArgument *ToArgs
1978 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1979 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1980 return TemplateArgument(ToArgs, ToPack.size());
1981 }
1982 }
1983
1984 llvm_unreachable("Invalid template argument kind");
1985 return TemplateArgument();
1986}
1987
1988bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1989 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001990 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001991 for (unsigned I = 0; I != NumFromArgs; ++I) {
1992 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1993 if (To.isNull() && !FromArgs[I].isNull())
1994 return true;
1995
1996 ToArgs.push_back(To);
1997 }
1998
1999 return false;
2000}
2001
Douglas Gregor96a01b42010-02-11 00:48:18 +00002002bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002003 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002004 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002005 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002006 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002007 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002008}
2009
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002010bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002011 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002012 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00002013 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00002014 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002015}
2016
Douglas Gregor040afae2010-11-30 19:14:50 +00002017bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2018 ClassTemplateDecl *To) {
2019 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2020 Importer.getToContext(),
2021 Importer.getNonEquivalentDecls());
2022 return Ctx.IsStructurallyEquivalent(From, To);
2023}
2024
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002025Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002026 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002027 << D->getDeclKindName();
2028 return 0;
2029}
2030
Douglas Gregor788c62d2010-02-21 18:26:36 +00002031Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2032 // Import the major distinguishing characteristics of this namespace.
2033 DeclContext *DC, *LexicalDC;
2034 DeclarationName Name;
2035 SourceLocation Loc;
2036 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2037 return 0;
2038
2039 NamespaceDecl *MergeWithNamespace = 0;
2040 if (!Name) {
2041 // This is an anonymous namespace. Adopt an existing anonymous
2042 // namespace if we can.
2043 // FIXME: Not testable.
2044 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2045 MergeWithNamespace = TU->getAnonymousNamespace();
2046 else
2047 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2048 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002049 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002050 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2051 Lookup.first != Lookup.second;
2052 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00002053 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002054 continue;
2055
2056 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2057 MergeWithNamespace = FoundNS;
2058 ConflictingDecls.clear();
2059 break;
2060 }
2061
2062 ConflictingDecls.push_back(*Lookup.first);
2063 }
2064
2065 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002066 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002067 ConflictingDecls.data(),
2068 ConflictingDecls.size());
2069 }
2070 }
2071
2072 // Create the "to" namespace, if needed.
2073 NamespaceDecl *ToNamespace = MergeWithNamespace;
2074 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002075 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2076 Importer.Import(D->getLocStart()),
2077 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002078 ToNamespace->setLexicalDeclContext(LexicalDC);
2079 LexicalDC->addDecl(ToNamespace);
2080
2081 // If this is an anonymous namespace, register it as the anonymous
2082 // namespace within its context.
2083 if (!Name) {
2084 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2085 TU->setAnonymousNamespace(ToNamespace);
2086 else
2087 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2088 }
2089 }
2090 Importer.Imported(D, ToNamespace);
2091
2092 ImportDeclContext(D);
2093
2094 return ToNamespace;
2095}
2096
Richard Smith162e1c12011-04-15 14:24:37 +00002097Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002098 // Import the major distinguishing characteristics of this typedef.
2099 DeclContext *DC, *LexicalDC;
2100 DeclarationName Name;
2101 SourceLocation Loc;
2102 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2103 return 0;
2104
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002105 // If this typedef is not in block scope, determine whether we've
2106 // seen a typedef with the same name (that we can merge with) or any
2107 // other entity by that name (which name lookup could conflict with).
2108 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002109 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002110 unsigned IDNS = Decl::IDNS_Ordinary;
2111 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2112 Lookup.first != Lookup.second;
2113 ++Lookup.first) {
2114 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2115 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002116 if (TypedefNameDecl *FoundTypedef =
2117 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002118 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2119 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002120 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002121 }
2122
2123 ConflictingDecls.push_back(*Lookup.first);
2124 }
2125
2126 if (!ConflictingDecls.empty()) {
2127 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2128 ConflictingDecls.data(),
2129 ConflictingDecls.size());
2130 if (!Name)
2131 return 0;
2132 }
2133 }
2134
Douglas Gregorea35d112010-02-15 23:54:17 +00002135 // Import the underlying type of this typedef;
2136 QualType T = Importer.Import(D->getUnderlyingType());
2137 if (T.isNull())
2138 return 0;
2139
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002140 // Create the new typedef node.
2141 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002142 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002143 TypedefNameDecl *ToTypedef;
2144 if (IsAlias)
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002145 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2146 StartL, Loc,
2147 Name.getAsIdentifierInfo(),
2148 TInfo);
2149 else
Richard Smith162e1c12011-04-15 14:24:37 +00002150 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2151 StartL, Loc,
2152 Name.getAsIdentifierInfo(),
2153 TInfo);
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002154
Douglas Gregor325bf172010-02-22 17:42:47 +00002155 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002156 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002157 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002158 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002159
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002160 return ToTypedef;
2161}
2162
Richard Smith162e1c12011-04-15 14:24:37 +00002163Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2164 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2165}
2166
2167Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2168 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2169}
2170
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002171Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2172 // Import the major distinguishing characteristics of this enum.
2173 DeclContext *DC, *LexicalDC;
2174 DeclarationName Name;
2175 SourceLocation Loc;
2176 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2177 return 0;
2178
2179 // Figure out what enum name we're looking for.
2180 unsigned IDNS = Decl::IDNS_Tag;
2181 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002182 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2183 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002184 IDNS = Decl::IDNS_Ordinary;
2185 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2186 IDNS |= Decl::IDNS_Ordinary;
2187
2188 // We may already have an enum of the same name; try to find and match it.
2189 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002190 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002191 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002192 Lookup.first != Lookup.second;
2193 ++Lookup.first) {
2194 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2195 continue;
2196
2197 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002198 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002199 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2200 Found = Tag->getDecl();
2201 }
2202
2203 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002204 if (IsStructuralMatch(D, FoundEnum))
2205 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002206 }
2207
2208 ConflictingDecls.push_back(*Lookup.first);
2209 }
2210
2211 if (!ConflictingDecls.empty()) {
2212 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2213 ConflictingDecls.data(),
2214 ConflictingDecls.size());
2215 }
2216 }
2217
2218 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002219 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2220 Importer.Import(D->getLocStart()),
2221 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002222 D->isScoped(), D->isScopedUsingClassTag(),
2223 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002224 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002225 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002226 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002227 D2->setLexicalDeclContext(LexicalDC);
2228 Importer.Imported(D, D2);
2229 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002230
2231 // Import the integer type.
2232 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2233 if (ToIntegerType.isNull())
2234 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002235 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002236
2237 // Import the definition
John McCall5e1cdac2011-10-07 06:10:15 +00002238 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002239 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002240
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002241 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002242}
2243
Douglas Gregor96a01b42010-02-11 00:48:18 +00002244Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2245 // If this record has a definition in the translation unit we're coming from,
2246 // but this particular declaration is not that definition, import the
2247 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002248 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002249 if (Definition && Definition != D) {
2250 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002251 if (!ImportedDef)
2252 return 0;
2253
2254 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002255 }
2256
2257 // Import the major distinguishing characteristics of this record.
2258 DeclContext *DC, *LexicalDC;
2259 DeclarationName Name;
2260 SourceLocation Loc;
2261 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2262 return 0;
2263
2264 // Figure out what structure name we're looking for.
2265 unsigned IDNS = Decl::IDNS_Tag;
2266 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002267 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2268 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002269 IDNS = Decl::IDNS_Ordinary;
2270 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2271 IDNS |= Decl::IDNS_Ordinary;
2272
2273 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002274 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002275 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002276 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3340e272011-09-22 17:51:56 +00002277 for (DeclContext::lookup_result Lookup = DC->lookup(SearchName);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002278 Lookup.first != Lookup.second;
2279 ++Lookup.first) {
2280 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2281 continue;
2282
2283 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002284 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002285 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2286 Found = Tag->getDecl();
2287 }
2288
2289 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002290 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00002291 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002292 // The record types structurally match, or the "from" translation
2293 // unit only had a forward declaration anyway; call it the same
2294 // function.
2295 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002296 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002297 }
2298 } else {
2299 // We have a forward declaration of this type, so adopt that forward
2300 // declaration rather than building a new one.
2301 AdoptDecl = FoundRecord;
2302 continue;
2303 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002304 }
2305
2306 ConflictingDecls.push_back(*Lookup.first);
2307 }
2308
2309 if (!ConflictingDecls.empty()) {
2310 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2311 ConflictingDecls.data(),
2312 ConflictingDecls.size());
2313 }
2314 }
2315
2316 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002317 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002318 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002319 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002320 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002321 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002322 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002323 DC, StartLoc, Loc,
2324 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002325 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002326 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002327 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002328 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002329 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002330 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002331
2332 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002333 D2->setLexicalDeclContext(LexicalDC);
2334 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002335 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002336
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002337 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002338
John McCall5e1cdac2011-10-07 06:10:15 +00002339 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002340 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002341
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002342 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002343}
2344
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002345Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2346 // Import the major distinguishing characteristics of this enumerator.
2347 DeclContext *DC, *LexicalDC;
2348 DeclarationName Name;
2349 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002350 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002351 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002352
2353 QualType T = Importer.Import(D->getType());
2354 if (T.isNull())
2355 return 0;
2356
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002357 // Determine whether there are any other declarations with the same name and
2358 // in the same context.
2359 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002360 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002361 unsigned IDNS = Decl::IDNS_Ordinary;
2362 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2363 Lookup.first != Lookup.second;
2364 ++Lookup.first) {
2365 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2366 continue;
2367
2368 ConflictingDecls.push_back(*Lookup.first);
2369 }
2370
2371 if (!ConflictingDecls.empty()) {
2372 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2373 ConflictingDecls.data(),
2374 ConflictingDecls.size());
2375 if (!Name)
2376 return 0;
2377 }
2378 }
2379
2380 Expr *Init = Importer.Import(D->getInitExpr());
2381 if (D->getInitExpr() && !Init)
2382 return 0;
2383
2384 EnumConstantDecl *ToEnumerator
2385 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2386 Name.getAsIdentifierInfo(), T,
2387 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002388 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002389 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002390 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002391 LexicalDC->addDecl(ToEnumerator);
2392 return ToEnumerator;
2393}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002394
Douglas Gregora404ea62010-02-10 19:54:31 +00002395Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2396 // Import the major distinguishing characteristics of this function.
2397 DeclContext *DC, *LexicalDC;
2398 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002399 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002400 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002401 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002402
Douglas Gregora404ea62010-02-10 19:54:31 +00002403 // Try to find a function in our own ("to") context with the same name, same
2404 // type, and in the same context as the function we're importing.
2405 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002406 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002407 unsigned IDNS = Decl::IDNS_Ordinary;
2408 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2409 Lookup.first != Lookup.second;
2410 ++Lookup.first) {
2411 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2412 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002413
Douglas Gregora404ea62010-02-10 19:54:31 +00002414 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2415 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2416 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002417 if (Importer.IsStructurallyEquivalent(D->getType(),
2418 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002419 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002420 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002421 }
2422
2423 // FIXME: Check for overloading more carefully, e.g., by boosting
2424 // Sema::IsOverload out to the AST library.
2425
2426 // Function overloading is okay in C++.
2427 if (Importer.getToContext().getLangOptions().CPlusPlus)
2428 continue;
2429
2430 // Complain about inconsistent function types.
2431 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002432 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002433 Importer.ToDiag(FoundFunction->getLocation(),
2434 diag::note_odr_value_here)
2435 << FoundFunction->getType();
2436 }
2437 }
2438
2439 ConflictingDecls.push_back(*Lookup.first);
2440 }
2441
2442 if (!ConflictingDecls.empty()) {
2443 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2444 ConflictingDecls.data(),
2445 ConflictingDecls.size());
2446 if (!Name)
2447 return 0;
2448 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002449 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002450
Abramo Bagnara25777432010-08-11 22:01:17 +00002451 DeclarationNameInfo NameInfo(Name, Loc);
2452 // Import additional name location/type info.
2453 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2454
Douglas Gregorea35d112010-02-15 23:54:17 +00002455 // Import the type.
2456 QualType T = Importer.Import(D->getType());
2457 if (T.isNull())
2458 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002459
2460 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002461 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002462 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2463 P != PEnd; ++P) {
2464 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2465 if (!ToP)
2466 return 0;
2467
2468 Parameters.push_back(ToP);
2469 }
2470
2471 // Create the imported function.
2472 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002473 FunctionDecl *ToFunction = 0;
2474 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2475 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2476 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002477 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002478 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002479 FromConstructor->isExplicit(),
2480 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002481 D->isImplicit(),
2482 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002483 } else if (isa<CXXDestructorDecl>(D)) {
2484 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2485 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002486 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002487 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002488 D->isInlineSpecified(),
2489 D->isImplicit());
2490 } else if (CXXConversionDecl *FromConversion
2491 = dyn_cast<CXXConversionDecl>(D)) {
2492 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2493 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002494 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002495 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002496 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002497 FromConversion->isExplicit(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002498 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002499 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002500 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2501 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2502 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002503 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002504 NameInfo, T, TInfo,
2505 Method->isStatic(),
2506 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002507 Method->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002508 D->isConstexpr(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002509 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002510 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002511 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002512 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002513 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002514 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002515 D->isInlineSpecified(),
Richard Smithaf1fc7a2011-08-15 21:04:07 +00002516 D->hasWrittenPrototype(),
2517 D->isConstexpr());
Douglas Gregorc144f352010-02-21 18:29:16 +00002518 }
John McCallb6217662010-03-15 10:12:16 +00002519
2520 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002521 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002522 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002523 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002524 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2525 ToFunction->setTrivial(D->isTrivial());
2526 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002527 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002528
Douglas Gregora404ea62010-02-10 19:54:31 +00002529 // Set the parameters.
2530 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002531 Parameters[I]->setOwningFunction(ToFunction);
2532 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002533 }
David Blaikie4278c652011-09-21 18:16:56 +00002534 ToFunction->setParams(Parameters);
Douglas Gregora404ea62010-02-10 19:54:31 +00002535
2536 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002537
2538 // Add this function to the lexical context.
2539 LexicalDC->addDecl(ToFunction);
2540
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002541 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002542}
2543
Douglas Gregorc144f352010-02-21 18:29:16 +00002544Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2545 return VisitFunctionDecl(D);
2546}
2547
2548Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2549 return VisitCXXMethodDecl(D);
2550}
2551
2552Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2553 return VisitCXXMethodDecl(D);
2554}
2555
2556Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2557 return VisitCXXMethodDecl(D);
2558}
2559
Douglas Gregor96a01b42010-02-11 00:48:18 +00002560Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2561 // Import the major distinguishing characteristics of a variable.
2562 DeclContext *DC, *LexicalDC;
2563 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002564 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002565 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2566 return 0;
2567
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002568 // Determine whether we've already imported this field.
2569 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2570 Lookup.first != Lookup.second;
2571 ++Lookup.first) {
2572 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(*Lookup.first)) {
2573 if (Importer.IsStructurallyEquivalent(D->getType(),
2574 FoundField->getType())) {
2575 Importer.Imported(D, FoundField);
2576 return FoundField;
2577 }
2578
2579 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2580 << Name << D->getType() << FoundField->getType();
2581 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2582 << FoundField->getType();
2583 return 0;
2584 }
2585 }
2586
Douglas Gregorea35d112010-02-15 23:54:17 +00002587 // Import the type.
2588 QualType T = Importer.Import(D->getType());
2589 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002590 return 0;
2591
2592 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2593 Expr *BitWidth = Importer.Import(D->getBitWidth());
2594 if (!BitWidth && D->getBitWidth())
2595 return 0;
2596
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002597 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2598 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002599 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002600 T, TInfo, BitWidth, D->isMutable(),
2601 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002602 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002603 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002604 if (ToField->hasInClassInitializer())
2605 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002606 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002607 LexicalDC->addDecl(ToField);
2608 return ToField;
2609}
2610
Francois Pichet87c2e122010-11-21 06:08:52 +00002611Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2612 // Import the major distinguishing characteristics of a variable.
2613 DeclContext *DC, *LexicalDC;
2614 DeclarationName Name;
2615 SourceLocation Loc;
2616 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2617 return 0;
2618
Douglas Gregor7c9412c2011-10-14 21:54:42 +00002619 // Determine whether we've already imported this field.
2620 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2621 Lookup.first != Lookup.second;
2622 ++Lookup.first) {
2623 if (IndirectFieldDecl *FoundField
2624 = dyn_cast<IndirectFieldDecl>(*Lookup.first)) {
2625 if (Importer.IsStructurallyEquivalent(D->getType(),
2626 FoundField->getType())) {
2627 Importer.Imported(D, FoundField);
2628 return FoundField;
2629 }
2630
2631 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2632 << Name << D->getType() << FoundField->getType();
2633 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2634 << FoundField->getType();
2635 return 0;
2636 }
2637 }
2638
Francois Pichet87c2e122010-11-21 06:08:52 +00002639 // Import the type.
2640 QualType T = Importer.Import(D->getType());
2641 if (T.isNull())
2642 return 0;
2643
2644 NamedDecl **NamedChain =
2645 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2646
2647 unsigned i = 0;
2648 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2649 PE = D->chain_end(); PI != PE; ++PI) {
2650 Decl* D = Importer.Import(*PI);
2651 if (!D)
2652 return 0;
2653 NamedChain[i++] = cast<NamedDecl>(D);
2654 }
2655
2656 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2657 Importer.getToContext(), DC,
2658 Loc, Name.getAsIdentifierInfo(), T,
2659 NamedChain, D->getChainingSize());
2660 ToIndirectField->setAccess(D->getAccess());
2661 ToIndirectField->setLexicalDeclContext(LexicalDC);
2662 Importer.Imported(D, ToIndirectField);
2663 LexicalDC->addDecl(ToIndirectField);
2664 return ToIndirectField;
2665}
2666
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002667Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2668 // Import the major distinguishing characteristics of an ivar.
2669 DeclContext *DC, *LexicalDC;
2670 DeclarationName Name;
2671 SourceLocation Loc;
2672 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2673 return 0;
2674
2675 // Determine whether we've already imported this ivar
2676 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2677 Lookup.first != Lookup.second;
2678 ++Lookup.first) {
2679 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2680 if (Importer.IsStructurallyEquivalent(D->getType(),
2681 FoundIvar->getType())) {
2682 Importer.Imported(D, FoundIvar);
2683 return FoundIvar;
2684 }
2685
2686 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2687 << Name << D->getType() << FoundIvar->getType();
2688 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2689 << FoundIvar->getType();
2690 return 0;
2691 }
2692 }
2693
2694 // Import the type.
2695 QualType T = Importer.Import(D->getType());
2696 if (T.isNull())
2697 return 0;
2698
2699 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2700 Expr *BitWidth = Importer.Import(D->getBitWidth());
2701 if (!BitWidth && D->getBitWidth())
2702 return 0;
2703
Daniel Dunbara0654922010-04-02 20:10:03 +00002704 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2705 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002706 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002707 Loc, Name.getAsIdentifierInfo(),
2708 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002709 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002710 ToIvar->setLexicalDeclContext(LexicalDC);
2711 Importer.Imported(D, ToIvar);
2712 LexicalDC->addDecl(ToIvar);
2713 return ToIvar;
2714
2715}
2716
Douglas Gregora404ea62010-02-10 19:54:31 +00002717Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2718 // Import the major distinguishing characteristics of a variable.
2719 DeclContext *DC, *LexicalDC;
2720 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002721 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002722 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002723 return 0;
2724
Douglas Gregor089459a2010-02-08 21:09:39 +00002725 // Try to find a variable in our own ("to") context with the same name and
2726 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002727 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002728 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002729 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002730 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002731 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002732 Lookup.first != Lookup.second;
2733 ++Lookup.first) {
2734 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2735 continue;
2736
2737 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2738 // We have found a variable that we may need to merge with. Check it.
2739 if (isExternalLinkage(FoundVar->getLinkage()) &&
2740 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002741 if (Importer.IsStructurallyEquivalent(D->getType(),
2742 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002743 MergeWithVar = FoundVar;
2744 break;
2745 }
2746
Douglas Gregord0145422010-02-12 17:23:39 +00002747 const ArrayType *FoundArray
2748 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2749 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002750 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002751 if (FoundArray && TArray) {
2752 if (isa<IncompleteArrayType>(FoundArray) &&
2753 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002754 // Import the type.
2755 QualType T = Importer.Import(D->getType());
2756 if (T.isNull())
2757 return 0;
2758
Douglas Gregord0145422010-02-12 17:23:39 +00002759 FoundVar->setType(T);
2760 MergeWithVar = FoundVar;
2761 break;
2762 } else if (isa<IncompleteArrayType>(TArray) &&
2763 isa<ConstantArrayType>(FoundArray)) {
2764 MergeWithVar = FoundVar;
2765 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002766 }
2767 }
2768
Douglas Gregor089459a2010-02-08 21:09:39 +00002769 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002770 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002771 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2772 << FoundVar->getType();
2773 }
2774 }
2775
2776 ConflictingDecls.push_back(*Lookup.first);
2777 }
2778
2779 if (MergeWithVar) {
2780 // An equivalent variable with external linkage has been found. Link
2781 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002782 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002783
2784 if (VarDecl *DDef = D->getDefinition()) {
2785 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2786 Importer.ToDiag(ExistingDef->getLocation(),
2787 diag::err_odr_variable_multiple_def)
2788 << Name;
2789 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2790 } else {
2791 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002792 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002793 }
2794 }
2795
2796 return MergeWithVar;
2797 }
2798
2799 if (!ConflictingDecls.empty()) {
2800 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2801 ConflictingDecls.data(),
2802 ConflictingDecls.size());
2803 if (!Name)
2804 return 0;
2805 }
2806 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002807
Douglas Gregorea35d112010-02-15 23:54:17 +00002808 // Import the type.
2809 QualType T = Importer.Import(D->getType());
2810 if (T.isNull())
2811 return 0;
2812
Douglas Gregor089459a2010-02-08 21:09:39 +00002813 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002814 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002815 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2816 Importer.Import(D->getInnerLocStart()),
2817 Loc, Name.getAsIdentifierInfo(),
2818 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002819 D->getStorageClass(),
2820 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002821 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002822 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002823 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002824 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002825 LexicalDC->addDecl(ToVar);
2826
Douglas Gregor089459a2010-02-08 21:09:39 +00002827 // Merge the initializer.
2828 // FIXME: Can we really import any initializer? Alternatively, we could force
2829 // ourselves to import every declaration of a variable and then only use
2830 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002831 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002832
2833 // FIXME: Other bits to merge?
2834
2835 return ToVar;
2836}
2837
Douglas Gregor2cd00932010-02-17 21:22:52 +00002838Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2839 // Parameters are created in the translation unit's context, then moved
2840 // into the function declaration's context afterward.
2841 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2842
2843 // Import the name of this declaration.
2844 DeclarationName Name = Importer.Import(D->getDeclName());
2845 if (D->getDeclName() && !Name)
2846 return 0;
2847
2848 // Import the location of this declaration.
2849 SourceLocation Loc = Importer.Import(D->getLocation());
2850
2851 // Import the parameter's type.
2852 QualType T = Importer.Import(D->getType());
2853 if (T.isNull())
2854 return 0;
2855
2856 // Create the imported parameter.
2857 ImplicitParamDecl *ToParm
2858 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2859 Loc, Name.getAsIdentifierInfo(),
2860 T);
2861 return Importer.Imported(D, ToParm);
2862}
2863
Douglas Gregora404ea62010-02-10 19:54:31 +00002864Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2865 // Parameters are created in the translation unit's context, then moved
2866 // into the function declaration's context afterward.
2867 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2868
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002869 // Import the name of this declaration.
2870 DeclarationName Name = Importer.Import(D->getDeclName());
2871 if (D->getDeclName() && !Name)
2872 return 0;
2873
Douglas Gregora404ea62010-02-10 19:54:31 +00002874 // Import the location of this declaration.
2875 SourceLocation Loc = Importer.Import(D->getLocation());
2876
2877 // Import the parameter's type.
2878 QualType T = Importer.Import(D->getType());
2879 if (T.isNull())
2880 return 0;
2881
2882 // Create the imported parameter.
2883 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2884 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002885 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002886 Loc, Name.getAsIdentifierInfo(),
2887 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002888 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002889 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002890 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002891 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002892}
2893
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002894Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2895 // Import the major distinguishing characteristics of a method.
2896 DeclContext *DC, *LexicalDC;
2897 DeclarationName Name;
2898 SourceLocation Loc;
2899 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2900 return 0;
2901
2902 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2903 Lookup.first != Lookup.second;
2904 ++Lookup.first) {
2905 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2906 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2907 continue;
2908
2909 // Check return types.
2910 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2911 FoundMethod->getResultType())) {
2912 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2913 << D->isInstanceMethod() << Name
2914 << D->getResultType() << FoundMethod->getResultType();
2915 Importer.ToDiag(FoundMethod->getLocation(),
2916 diag::note_odr_objc_method_here)
2917 << D->isInstanceMethod() << Name;
2918 return 0;
2919 }
2920
2921 // Check the number of parameters.
2922 if (D->param_size() != FoundMethod->param_size()) {
2923 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2924 << D->isInstanceMethod() << Name
2925 << D->param_size() << FoundMethod->param_size();
2926 Importer.ToDiag(FoundMethod->getLocation(),
2927 diag::note_odr_objc_method_here)
2928 << D->isInstanceMethod() << Name;
2929 return 0;
2930 }
2931
2932 // Check parameter types.
2933 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2934 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2935 P != PEnd; ++P, ++FoundP) {
2936 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2937 (*FoundP)->getType())) {
2938 Importer.FromDiag((*P)->getLocation(),
2939 diag::err_odr_objc_method_param_type_inconsistent)
2940 << D->isInstanceMethod() << Name
2941 << (*P)->getType() << (*FoundP)->getType();
2942 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2943 << (*FoundP)->getType();
2944 return 0;
2945 }
2946 }
2947
2948 // Check variadic/non-variadic.
2949 // Check the number of parameters.
2950 if (D->isVariadic() != FoundMethod->isVariadic()) {
2951 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2952 << D->isInstanceMethod() << Name;
2953 Importer.ToDiag(FoundMethod->getLocation(),
2954 diag::note_odr_objc_method_here)
2955 << D->isInstanceMethod() << Name;
2956 return 0;
2957 }
2958
2959 // FIXME: Any other bits we need to merge?
2960 return Importer.Imported(D, FoundMethod);
2961 }
2962 }
2963
2964 // Import the result type.
2965 QualType ResultTy = Importer.Import(D->getResultType());
2966 if (ResultTy.isNull())
2967 return 0;
2968
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002969 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2970
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002971 ObjCMethodDecl *ToMethod
2972 = ObjCMethodDecl::Create(Importer.getToContext(),
2973 Loc,
2974 Importer.Import(D->getLocEnd()),
2975 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002976 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002977 D->isInstanceMethod(),
2978 D->isVariadic(),
2979 D->isSynthesized(),
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002980 D->isImplicit(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002981 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002982 D->getImplementationControl(),
2983 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002984
2985 // FIXME: When we decide to merge method definitions, we'll need to
2986 // deal with implicit parameters.
2987
2988 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002989 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002990 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2991 FromPEnd = D->param_end();
2992 FromP != FromPEnd;
2993 ++FromP) {
2994 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2995 if (!ToP)
2996 return 0;
2997
2998 ToParams.push_back(ToP);
2999 }
3000
3001 // Set the parameters.
3002 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3003 ToParams[I]->setOwningFunction(ToMethod);
3004 ToMethod->addDecl(ToParams[I]);
3005 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003006 SmallVector<SourceLocation, 12> SelLocs;
3007 D->getSelectorLocs(SelLocs);
3008 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00003009
3010 ToMethod->setLexicalDeclContext(LexicalDC);
3011 Importer.Imported(D, ToMethod);
3012 LexicalDC->addDecl(ToMethod);
3013 return ToMethod;
3014}
3015
Douglas Gregorb4677b62010-02-18 01:47:50 +00003016Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3017 // Import the major distinguishing characteristics of a category.
3018 DeclContext *DC, *LexicalDC;
3019 DeclarationName Name;
3020 SourceLocation Loc;
3021 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3022 return 0;
3023
3024 ObjCInterfaceDecl *ToInterface
3025 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3026 if (!ToInterface)
3027 return 0;
3028
3029 // Determine if we've already encountered this category.
3030 ObjCCategoryDecl *MergeWithCategory
3031 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3032 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3033 if (!ToCategory) {
3034 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003035 Importer.Import(D->getAtStartLoc()),
Douglas Gregorb4677b62010-02-18 01:47:50 +00003036 Loc,
3037 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +00003038 Name.getAsIdentifierInfo(),
3039 ToInterface);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003040 ToCategory->setLexicalDeclContext(LexicalDC);
3041 LexicalDC->addDecl(ToCategory);
3042 Importer.Imported(D, ToCategory);
3043
Douglas Gregorb4677b62010-02-18 01:47:50 +00003044 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003045 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3046 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00003047 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3048 = D->protocol_loc_begin();
3049 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3050 FromProtoEnd = D->protocol_end();
3051 FromProto != FromProtoEnd;
3052 ++FromProto, ++FromProtoLoc) {
3053 ObjCProtocolDecl *ToProto
3054 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3055 if (!ToProto)
3056 return 0;
3057 Protocols.push_back(ToProto);
3058 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3059 }
3060
3061 // FIXME: If we're merging, make sure that the protocol list is the same.
3062 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3063 ProtocolLocs.data(), Importer.getToContext());
3064
3065 } else {
3066 Importer.Imported(D, ToCategory);
3067 }
3068
3069 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003070 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003071
3072 // If we have an implementation, import it as well.
3073 if (D->getImplementation()) {
3074 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003075 = cast_or_null<ObjCCategoryImplDecl>(
3076 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003077 if (!Impl)
3078 return 0;
3079
3080 ToCategory->setImplementation(Impl);
3081 }
3082
3083 return ToCategory;
3084}
3085
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003086Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003087 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003088 DeclContext *DC, *LexicalDC;
3089 DeclarationName Name;
3090 SourceLocation Loc;
3091 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3092 return 0;
3093
3094 ObjCProtocolDecl *MergeWithProtocol = 0;
3095 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3096 Lookup.first != Lookup.second;
3097 ++Lookup.first) {
3098 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3099 continue;
3100
3101 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
3102 break;
3103 }
3104
3105 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3106 if (!ToProto || ToProto->isForwardDecl()) {
3107 if (!ToProto) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003108 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3109 Name.getAsIdentifierInfo(), Loc,
3110 Importer.Import(D->getAtStartLoc()));
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003111 ToProto->setForwardDecl(D->isForwardDecl());
3112 ToProto->setLexicalDeclContext(LexicalDC);
3113 LexicalDC->addDecl(ToProto);
3114 }
3115 Importer.Imported(D, ToProto);
3116
3117 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003118 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3119 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003120 ObjCProtocolDecl::protocol_loc_iterator
3121 FromProtoLoc = D->protocol_loc_begin();
3122 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3123 FromProtoEnd = D->protocol_end();
3124 FromProto != FromProtoEnd;
3125 ++FromProto, ++FromProtoLoc) {
3126 ObjCProtocolDecl *ToProto
3127 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3128 if (!ToProto)
3129 return 0;
3130 Protocols.push_back(ToProto);
3131 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3132 }
3133
3134 // FIXME: If we're merging, make sure that the protocol list is the same.
3135 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3136 ProtocolLocs.data(), Importer.getToContext());
3137 } else {
3138 Importer.Imported(D, ToProto);
3139 }
3140
Douglas Gregorb4677b62010-02-18 01:47:50 +00003141 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003142 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003143
3144 return ToProto;
3145}
3146
Douglas Gregora12d2942010-02-16 01:20:57 +00003147Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3148 // Import the major distinguishing characteristics of an @interface.
3149 DeclContext *DC, *LexicalDC;
3150 DeclarationName Name;
3151 SourceLocation Loc;
3152 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3153 return 0;
3154
3155 ObjCInterfaceDecl *MergeWithIface = 0;
3156 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3157 Lookup.first != Lookup.second;
3158 ++Lookup.first) {
3159 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3160 continue;
3161
3162 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3163 break;
3164 }
3165
3166 ObjCInterfaceDecl *ToIface = MergeWithIface;
3167 if (!ToIface || ToIface->isForwardDecl()) {
3168 if (!ToIface) {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003169 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3170 Importer.Import(D->getAtStartLoc()),
3171 Name.getAsIdentifierInfo(), Loc,
Douglas Gregora12d2942010-02-16 01:20:57 +00003172 D->isForwardDecl(),
3173 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003174 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003175 ToIface->setLexicalDeclContext(LexicalDC);
3176 LexicalDC->addDecl(ToIface);
3177 }
3178 Importer.Imported(D, ToIface);
3179
Douglas Gregora12d2942010-02-16 01:20:57 +00003180 if (D->getSuperClass()) {
3181 ObjCInterfaceDecl *Super
3182 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3183 if (!Super)
3184 return 0;
3185
3186 ToIface->setSuperClass(Super);
3187 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3188 }
3189
3190 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003191 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3192 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003193 ObjCInterfaceDecl::protocol_loc_iterator
3194 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003195
3196 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003197 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3198 FromProtoEnd = D->protocol_end();
3199 FromProto != FromProtoEnd;
3200 ++FromProto, ++FromProtoLoc) {
3201 ObjCProtocolDecl *ToProto
3202 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3203 if (!ToProto)
3204 return 0;
3205 Protocols.push_back(ToProto);
3206 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3207 }
3208
3209 // FIXME: If we're merging, make sure that the protocol list is the same.
3210 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3211 ProtocolLocs.data(), Importer.getToContext());
3212
Douglas Gregora12d2942010-02-16 01:20:57 +00003213 // Import @end range
3214 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3215 } else {
3216 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003217
3218 // Check for consistency of superclasses.
3219 DeclarationName FromSuperName, ToSuperName;
3220 if (D->getSuperClass())
3221 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3222 if (ToIface->getSuperClass())
3223 ToSuperName = ToIface->getSuperClass()->getDeclName();
3224 if (FromSuperName != ToSuperName) {
3225 Importer.ToDiag(ToIface->getLocation(),
3226 diag::err_odr_objc_superclass_inconsistent)
3227 << ToIface->getDeclName();
3228 if (ToIface->getSuperClass())
3229 Importer.ToDiag(ToIface->getSuperClassLoc(),
3230 diag::note_odr_objc_superclass)
3231 << ToIface->getSuperClass()->getDeclName();
3232 else
3233 Importer.ToDiag(ToIface->getLocation(),
3234 diag::note_odr_objc_missing_superclass);
3235 if (D->getSuperClass())
3236 Importer.FromDiag(D->getSuperClassLoc(),
3237 diag::note_odr_objc_superclass)
3238 << D->getSuperClass()->getDeclName();
3239 else
3240 Importer.FromDiag(D->getLocation(),
3241 diag::note_odr_objc_missing_superclass);
3242 return 0;
3243 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003244 }
3245
Douglas Gregorb4677b62010-02-18 01:47:50 +00003246 // Import categories. When the categories themselves are imported, they'll
3247 // hook themselves into this interface.
3248 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3249 FromCat = FromCat->getNextClassCategory())
3250 Importer.Import(FromCat);
3251
Douglas Gregora12d2942010-02-16 01:20:57 +00003252 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003253 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003254
3255 // If we have an @implementation, import it as well.
3256 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003257 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3258 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003259 if (!Impl)
3260 return 0;
3261
3262 ToIface->setImplementation(Impl);
3263 }
3264
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003265 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003266}
3267
Douglas Gregor3daef292010-12-07 15:32:12 +00003268Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3269 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3270 Importer.Import(D->getCategoryDecl()));
3271 if (!Category)
3272 return 0;
3273
3274 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3275 if (!ToImpl) {
3276 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3277 if (!DC)
3278 return 0;
3279
3280 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor3daef292010-12-07 15:32:12 +00003281 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003282 Category->getClassInterface(),
3283 Importer.Import(D->getLocation()),
3284 Importer.Import(D->getAtStartLoc()));
Douglas Gregor3daef292010-12-07 15:32:12 +00003285
3286 DeclContext *LexicalDC = DC;
3287 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3288 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3289 if (!LexicalDC)
3290 return 0;
3291
3292 ToImpl->setLexicalDeclContext(LexicalDC);
3293 }
3294
3295 LexicalDC->addDecl(ToImpl);
3296 Category->setImplementation(ToImpl);
3297 }
3298
3299 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003300 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003301 return ToImpl;
3302}
3303
Douglas Gregordd182ff2010-12-07 01:26:03 +00003304Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3305 // Find the corresponding interface.
3306 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3307 Importer.Import(D->getClassInterface()));
3308 if (!Iface)
3309 return 0;
3310
3311 // Import the superclass, if any.
3312 ObjCInterfaceDecl *Super = 0;
3313 if (D->getSuperClass()) {
3314 Super = cast_or_null<ObjCInterfaceDecl>(
3315 Importer.Import(D->getSuperClass()));
3316 if (!Super)
3317 return 0;
3318 }
3319
3320 ObjCImplementationDecl *Impl = Iface->getImplementation();
3321 if (!Impl) {
3322 // We haven't imported an implementation yet. Create a new @implementation
3323 // now.
3324 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3325 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003326 Iface, Super,
Douglas Gregordd182ff2010-12-07 01:26:03 +00003327 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00003328 Importer.Import(D->getAtStartLoc()));
Douglas Gregordd182ff2010-12-07 01:26:03 +00003329
3330 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3331 DeclContext *LexicalDC
3332 = Importer.ImportContext(D->getLexicalDeclContext());
3333 if (!LexicalDC)
3334 return 0;
3335 Impl->setLexicalDeclContext(LexicalDC);
3336 }
3337
3338 // Associate the implementation with the class it implements.
3339 Iface->setImplementation(Impl);
3340 Importer.Imported(D, Iface->getImplementation());
3341 } else {
3342 Importer.Imported(D, Iface->getImplementation());
3343
3344 // Verify that the existing @implementation has the same superclass.
3345 if ((Super && !Impl->getSuperClass()) ||
3346 (!Super && Impl->getSuperClass()) ||
3347 (Super && Impl->getSuperClass() &&
3348 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3349 Importer.ToDiag(Impl->getLocation(),
3350 diag::err_odr_objc_superclass_inconsistent)
3351 << Iface->getDeclName();
3352 // FIXME: It would be nice to have the location of the superclass
3353 // below.
3354 if (Impl->getSuperClass())
3355 Importer.ToDiag(Impl->getLocation(),
3356 diag::note_odr_objc_superclass)
3357 << Impl->getSuperClass()->getDeclName();
3358 else
3359 Importer.ToDiag(Impl->getLocation(),
3360 diag::note_odr_objc_missing_superclass);
3361 if (D->getSuperClass())
3362 Importer.FromDiag(D->getLocation(),
3363 diag::note_odr_objc_superclass)
3364 << D->getSuperClass()->getDeclName();
3365 else
3366 Importer.FromDiag(D->getLocation(),
3367 diag::note_odr_objc_missing_superclass);
3368 return 0;
3369 }
3370 }
3371
3372 // Import all of the members of this @implementation.
3373 ImportDeclContext(D);
3374
3375 return Impl;
3376}
3377
Douglas Gregore3261622010-02-17 18:02:10 +00003378Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3379 // Import the major distinguishing characteristics of an @property.
3380 DeclContext *DC, *LexicalDC;
3381 DeclarationName Name;
3382 SourceLocation Loc;
3383 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3384 return 0;
3385
3386 // Check whether we have already imported this property.
3387 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3388 Lookup.first != Lookup.second;
3389 ++Lookup.first) {
3390 if (ObjCPropertyDecl *FoundProp
3391 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3392 // Check property types.
3393 if (!Importer.IsStructurallyEquivalent(D->getType(),
3394 FoundProp->getType())) {
3395 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3396 << Name << D->getType() << FoundProp->getType();
3397 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3398 << FoundProp->getType();
3399 return 0;
3400 }
3401
3402 // FIXME: Check property attributes, getters, setters, etc.?
3403
3404 // Consider these properties to be equivalent.
3405 Importer.Imported(D, FoundProp);
3406 return FoundProp;
3407 }
3408 }
3409
3410 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003411 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3412 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003413 return 0;
3414
3415 // Create the new property.
3416 ObjCPropertyDecl *ToProperty
3417 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3418 Name.getAsIdentifierInfo(),
3419 Importer.Import(D->getAtLoc()),
3420 T,
3421 D->getPropertyImplementation());
3422 Importer.Imported(D, ToProperty);
3423 ToProperty->setLexicalDeclContext(LexicalDC);
3424 LexicalDC->addDecl(ToProperty);
3425
3426 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003427 ToProperty->setPropertyAttributesAsWritten(
3428 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003429 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3430 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3431 ToProperty->setGetterMethodDecl(
3432 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3433 ToProperty->setSetterMethodDecl(
3434 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3435 ToProperty->setPropertyIvarDecl(
3436 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3437 return ToProperty;
3438}
3439
Douglas Gregor954e0c72010-12-07 18:32:03 +00003440Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3441 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3442 Importer.Import(D->getPropertyDecl()));
3443 if (!Property)
3444 return 0;
3445
3446 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3447 if (!DC)
3448 return 0;
3449
3450 // Import the lexical declaration context.
3451 DeclContext *LexicalDC = DC;
3452 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3453 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3454 if (!LexicalDC)
3455 return 0;
3456 }
3457
3458 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3459 if (!InImpl)
3460 return 0;
3461
3462 // Import the ivar (for an @synthesize).
3463 ObjCIvarDecl *Ivar = 0;
3464 if (D->getPropertyIvarDecl()) {
3465 Ivar = cast_or_null<ObjCIvarDecl>(
3466 Importer.Import(D->getPropertyIvarDecl()));
3467 if (!Ivar)
3468 return 0;
3469 }
3470
3471 ObjCPropertyImplDecl *ToImpl
3472 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3473 if (!ToImpl) {
3474 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3475 Importer.Import(D->getLocStart()),
3476 Importer.Import(D->getLocation()),
3477 Property,
3478 D->getPropertyImplementation(),
3479 Ivar,
3480 Importer.Import(D->getPropertyIvarDeclLoc()));
3481 ToImpl->setLexicalDeclContext(LexicalDC);
3482 Importer.Imported(D, ToImpl);
3483 LexicalDC->addDecl(ToImpl);
3484 } else {
3485 // Check that we have the same kind of property implementation (@synthesize
3486 // vs. @dynamic).
3487 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3488 Importer.ToDiag(ToImpl->getLocation(),
3489 diag::err_odr_objc_property_impl_kind_inconsistent)
3490 << Property->getDeclName()
3491 << (ToImpl->getPropertyImplementation()
3492 == ObjCPropertyImplDecl::Dynamic);
3493 Importer.FromDiag(D->getLocation(),
3494 diag::note_odr_objc_property_impl_kind)
3495 << D->getPropertyDecl()->getDeclName()
3496 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3497 return 0;
3498 }
3499
3500 // For @synthesize, check that we have the same
3501 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3502 Ivar != ToImpl->getPropertyIvarDecl()) {
3503 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3504 diag::err_odr_objc_synthesize_ivar_inconsistent)
3505 << Property->getDeclName()
3506 << ToImpl->getPropertyIvarDecl()->getDeclName()
3507 << Ivar->getDeclName();
3508 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3509 diag::note_odr_objc_synthesize_ivar_here)
3510 << D->getPropertyIvarDecl()->getDeclName();
3511 return 0;
3512 }
3513
3514 // Merge the existing implementation with the new implementation.
3515 Importer.Imported(D, ToImpl);
3516 }
3517
3518 return ToImpl;
3519}
3520
Douglas Gregor2b785022010-02-18 02:12:22 +00003521Decl *
3522ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3523 // Import the context of this declaration.
3524 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3525 if (!DC)
3526 return 0;
3527
3528 DeclContext *LexicalDC = DC;
3529 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3530 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3531 if (!LexicalDC)
3532 return 0;
3533 }
3534
3535 // Import the location of this declaration.
3536 SourceLocation Loc = Importer.Import(D->getLocation());
3537
Chris Lattner5f9e2722011-07-23 10:55:15 +00003538 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3539 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003540 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3541 = D->protocol_loc_begin();
3542 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3543 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3544 FromProto != FromProtoEnd;
3545 ++FromProto, ++FromProtoLoc) {
3546 ObjCProtocolDecl *ToProto
3547 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3548 if (!ToProto)
3549 continue;
3550
3551 Protocols.push_back(ToProto);
3552 Locations.push_back(Importer.Import(*FromProtoLoc));
3553 }
3554
3555 ObjCForwardProtocolDecl *ToForward
3556 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3557 Protocols.data(), Protocols.size(),
3558 Locations.data());
3559 ToForward->setLexicalDeclContext(LexicalDC);
3560 LexicalDC->addDecl(ToForward);
3561 Importer.Imported(D, ToForward);
3562 return ToForward;
3563}
3564
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003565Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3566 // Import the context of this declaration.
3567 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3568 if (!DC)
3569 return 0;
3570
3571 DeclContext *LexicalDC = DC;
3572 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3573 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3574 if (!LexicalDC)
3575 return 0;
3576 }
3577
3578 // Import the location of this declaration.
3579 SourceLocation Loc = Importer.Import(D->getLocation());
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003580 ObjCClassDecl::ObjCClassRef *From = D->getForwardDecl();
3581 ObjCInterfaceDecl *ToIface
3582 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003583 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00003584 Loc,
3585 ToIface,
3586 Importer.Import(From->getLocation()));
3587
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003588 ToClass->setLexicalDeclContext(LexicalDC);
3589 LexicalDC->addDecl(ToClass);
3590 Importer.Imported(D, ToClass);
3591 return ToClass;
3592}
3593
Douglas Gregor040afae2010-11-30 19:14:50 +00003594Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3595 // For template arguments, we adopt the translation unit as our declaration
3596 // context. This context will be fixed when the actual template declaration
3597 // is created.
3598
3599 // FIXME: Import default argument.
3600 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3601 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003602 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003603 Importer.Import(D->getLocation()),
3604 D->getDepth(),
3605 D->getIndex(),
3606 Importer.Import(D->getIdentifier()),
3607 D->wasDeclaredWithTypename(),
3608 D->isParameterPack());
3609}
3610
3611Decl *
3612ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3613 // Import the name of this declaration.
3614 DeclarationName Name = Importer.Import(D->getDeclName());
3615 if (D->getDeclName() && !Name)
3616 return 0;
3617
3618 // Import the location of this declaration.
3619 SourceLocation Loc = Importer.Import(D->getLocation());
3620
3621 // Import the type of this declaration.
3622 QualType T = Importer.Import(D->getType());
3623 if (T.isNull())
3624 return 0;
3625
3626 // Import type-source information.
3627 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3628 if (D->getTypeSourceInfo() && !TInfo)
3629 return 0;
3630
3631 // FIXME: Import default argument.
3632
3633 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3634 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003635 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003636 Loc, D->getDepth(), D->getPosition(),
3637 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003638 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003639}
3640
3641Decl *
3642ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3643 // Import the name of this declaration.
3644 DeclarationName Name = Importer.Import(D->getDeclName());
3645 if (D->getDeclName() && !Name)
3646 return 0;
3647
3648 // Import the location of this declaration.
3649 SourceLocation Loc = Importer.Import(D->getLocation());
3650
3651 // Import template parameters.
3652 TemplateParameterList *TemplateParams
3653 = ImportTemplateParameterList(D->getTemplateParameters());
3654 if (!TemplateParams)
3655 return 0;
3656
3657 // FIXME: Import default argument.
3658
3659 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3660 Importer.getToContext().getTranslationUnitDecl(),
3661 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003662 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003663 Name.getAsIdentifierInfo(),
3664 TemplateParams);
3665}
3666
3667Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3668 // If this record has a definition in the translation unit we're coming from,
3669 // but this particular declaration is not that definition, import the
3670 // definition and map to that.
3671 CXXRecordDecl *Definition
3672 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3673 if (Definition && Definition != D->getTemplatedDecl()) {
3674 Decl *ImportedDef
3675 = Importer.Import(Definition->getDescribedClassTemplate());
3676 if (!ImportedDef)
3677 return 0;
3678
3679 return Importer.Imported(D, ImportedDef);
3680 }
3681
3682 // Import the major distinguishing characteristics of this class template.
3683 DeclContext *DC, *LexicalDC;
3684 DeclarationName Name;
3685 SourceLocation Loc;
3686 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3687 return 0;
3688
3689 // We may already have a template of the same name; try to find and match it.
3690 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003691 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor040afae2010-11-30 19:14:50 +00003692 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3693 Lookup.first != Lookup.second;
3694 ++Lookup.first) {
3695 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3696 continue;
3697
3698 Decl *Found = *Lookup.first;
3699 if (ClassTemplateDecl *FoundTemplate
3700 = dyn_cast<ClassTemplateDecl>(Found)) {
3701 if (IsStructuralMatch(D, FoundTemplate)) {
3702 // The class templates structurally match; call it the same template.
3703 // FIXME: We may be filling in a forward declaration here. Handle
3704 // this case!
3705 Importer.Imported(D->getTemplatedDecl(),
3706 FoundTemplate->getTemplatedDecl());
3707 return Importer.Imported(D, FoundTemplate);
3708 }
3709 }
3710
3711 ConflictingDecls.push_back(*Lookup.first);
3712 }
3713
3714 if (!ConflictingDecls.empty()) {
3715 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3716 ConflictingDecls.data(),
3717 ConflictingDecls.size());
3718 }
3719
3720 if (!Name)
3721 return 0;
3722 }
3723
3724 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3725
3726 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003727 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3728 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003729 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3730 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003731 DC, StartLoc, IdLoc,
3732 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003733 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003734 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003735 D2Templated->setLexicalDeclContext(LexicalDC);
3736
3737 // Create the class template declaration itself.
3738 TemplateParameterList *TemplateParams
3739 = ImportTemplateParameterList(D->getTemplateParameters());
3740 if (!TemplateParams)
3741 return 0;
3742
3743 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3744 Loc, Name, TemplateParams,
3745 D2Templated,
3746 /*PrevDecl=*/0);
3747 D2Templated->setDescribedClassTemplate(D2);
3748
3749 D2->setAccess(D->getAccess());
3750 D2->setLexicalDeclContext(LexicalDC);
3751 LexicalDC->addDecl(D2);
3752
3753 // Note the relationship between the class templates.
3754 Importer.Imported(D, D2);
3755 Importer.Imported(DTemplated, D2Templated);
3756
John McCall5e1cdac2011-10-07 06:10:15 +00003757 if (DTemplated->isCompleteDefinition() &&
3758 !D2Templated->isCompleteDefinition()) {
Douglas Gregor040afae2010-11-30 19:14:50 +00003759 // FIXME: Import definition!
3760 }
3761
3762 return D2;
3763}
3764
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003765Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3766 ClassTemplateSpecializationDecl *D) {
3767 // If this record has a definition in the translation unit we're coming from,
3768 // but this particular declaration is not that definition, import the
3769 // definition and map to that.
3770 TagDecl *Definition = D->getDefinition();
3771 if (Definition && Definition != D) {
3772 Decl *ImportedDef = Importer.Import(Definition);
3773 if (!ImportedDef)
3774 return 0;
3775
3776 return Importer.Imported(D, ImportedDef);
3777 }
3778
3779 ClassTemplateDecl *ClassTemplate
3780 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3781 D->getSpecializedTemplate()));
3782 if (!ClassTemplate)
3783 return 0;
3784
3785 // Import the context of this declaration.
3786 DeclContext *DC = ClassTemplate->getDeclContext();
3787 if (!DC)
3788 return 0;
3789
3790 DeclContext *LexicalDC = DC;
3791 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3792 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3793 if (!LexicalDC)
3794 return 0;
3795 }
3796
3797 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003798 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3799 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003800
3801 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003802 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003803 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3804 D->getTemplateArgs().size(),
3805 TemplateArgs))
3806 return 0;
3807
3808 // Try to find an existing specialization with these template arguments.
3809 void *InsertPos = 0;
3810 ClassTemplateSpecializationDecl *D2
3811 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3812 TemplateArgs.size(), InsertPos);
3813 if (D2) {
3814 // We already have a class template specialization with these template
3815 // arguments.
3816
3817 // FIXME: Check for specialization vs. instantiation errors.
3818
3819 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCall5e1cdac2011-10-07 06:10:15 +00003820 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003821 // The record types structurally match, or the "from" translation
3822 // unit only had a forward declaration anyway; call it the same
3823 // function.
3824 return Importer.Imported(D, FoundDef);
3825 }
3826 }
3827 } else {
3828 // Create a new specialization.
3829 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3830 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003831 StartLoc, IdLoc,
3832 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003833 TemplateArgs.data(),
3834 TemplateArgs.size(),
3835 /*PrevDecl=*/0);
3836 D2->setSpecializationKind(D->getSpecializationKind());
3837
3838 // Add this specialization to the class template.
3839 ClassTemplate->AddSpecialization(D2, InsertPos);
3840
3841 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003842 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003843
3844 // Add the specialization to this context.
3845 D2->setLexicalDeclContext(LexicalDC);
3846 LexicalDC->addDecl(D2);
3847 }
3848 Importer.Imported(D, D2);
3849
John McCall5e1cdac2011-10-07 06:10:15 +00003850 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003851 return 0;
3852
3853 return D2;
3854}
3855
Douglas Gregor4800d952010-02-11 19:21:55 +00003856//----------------------------------------------------------------------------
3857// Import Statements
3858//----------------------------------------------------------------------------
3859
3860Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3861 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3862 << S->getStmtClassName();
3863 return 0;
3864}
3865
3866//----------------------------------------------------------------------------
3867// Import Expressions
3868//----------------------------------------------------------------------------
3869Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3870 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3871 << E->getStmtClassName();
3872 return 0;
3873}
3874
Douglas Gregor44080632010-02-19 01:17:02 +00003875Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003876 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3877 if (!ToD)
3878 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003879
3880 NamedDecl *FoundD = 0;
3881 if (E->getDecl() != E->getFoundDecl()) {
3882 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3883 if (!FoundD)
3884 return 0;
3885 }
Douglas Gregor44080632010-02-19 01:17:02 +00003886
3887 QualType T = Importer.Import(E->getType());
3888 if (T.isNull())
3889 return 0;
Abramo Bagnara7cc58b42011-10-05 07:56:41 +00003890
3891 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
3892 Importer.Import(E->getQualifierLoc()),
3893 ToD,
3894 Importer.Import(E->getLocation()),
3895 T, E->getValueKind(),
3896 FoundD,
3897 /*FIXME:TemplateArgs=*/0);
3898 if (E->hadMultipleCandidates())
3899 DRE->setHadMultipleCandidates(true);
3900 return DRE;
Douglas Gregor44080632010-02-19 01:17:02 +00003901}
3902
Douglas Gregor4800d952010-02-11 19:21:55 +00003903Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3904 QualType T = Importer.Import(E->getType());
3905 if (T.isNull())
3906 return 0;
3907
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003908 return IntegerLiteral::Create(Importer.getToContext(),
3909 E->getValue(), T,
3910 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003911}
3912
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003913Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3914 QualType T = Importer.Import(E->getType());
3915 if (T.isNull())
3916 return 0;
3917
Douglas Gregor5cee1192011-07-27 05:40:30 +00003918 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3919 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003920 Importer.Import(E->getLocation()));
3921}
3922
Douglas Gregorf638f952010-02-19 01:07:06 +00003923Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3924 Expr *SubExpr = Importer.Import(E->getSubExpr());
3925 if (!SubExpr)
3926 return 0;
3927
3928 return new (Importer.getToContext())
3929 ParenExpr(Importer.Import(E->getLParen()),
3930 Importer.Import(E->getRParen()),
3931 SubExpr);
3932}
3933
3934Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3935 QualType T = Importer.Import(E->getType());
3936 if (T.isNull())
3937 return 0;
3938
3939 Expr *SubExpr = Importer.Import(E->getSubExpr());
3940 if (!SubExpr)
3941 return 0;
3942
3943 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003944 T, E->getValueKind(),
3945 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003946 Importer.Import(E->getOperatorLoc()));
3947}
3948
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003949Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3950 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003951 QualType ResultType = Importer.Import(E->getType());
3952
3953 if (E->isArgumentType()) {
3954 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3955 if (!TInfo)
3956 return 0;
3957
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003958 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3959 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003960 Importer.Import(E->getOperatorLoc()),
3961 Importer.Import(E->getRParenLoc()));
3962 }
3963
3964 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3965 if (!SubExpr)
3966 return 0;
3967
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003968 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3969 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003970 Importer.Import(E->getOperatorLoc()),
3971 Importer.Import(E->getRParenLoc()));
3972}
3973
Douglas Gregorf638f952010-02-19 01:07:06 +00003974Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3975 QualType T = Importer.Import(E->getType());
3976 if (T.isNull())
3977 return 0;
3978
3979 Expr *LHS = Importer.Import(E->getLHS());
3980 if (!LHS)
3981 return 0;
3982
3983 Expr *RHS = Importer.Import(E->getRHS());
3984 if (!RHS)
3985 return 0;
3986
3987 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003988 T, E->getValueKind(),
3989 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003990 Importer.Import(E->getOperatorLoc()));
3991}
3992
3993Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3994 QualType T = Importer.Import(E->getType());
3995 if (T.isNull())
3996 return 0;
3997
3998 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3999 if (CompLHSType.isNull())
4000 return 0;
4001
4002 QualType CompResultType = Importer.Import(E->getComputationResultType());
4003 if (CompResultType.isNull())
4004 return 0;
4005
4006 Expr *LHS = Importer.Import(E->getLHS());
4007 if (!LHS)
4008 return 0;
4009
4010 Expr *RHS = Importer.Import(E->getRHS());
4011 if (!RHS)
4012 return 0;
4013
4014 return new (Importer.getToContext())
4015 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00004016 T, E->getValueKind(),
4017 E->getObjectKind(),
4018 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00004019 Importer.Import(E->getOperatorLoc()));
4020}
4021
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00004022static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00004023 if (E->path_empty()) return false;
4024
4025 // TODO: import cast paths
4026 return true;
4027}
4028
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004029Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4030 QualType T = Importer.Import(E->getType());
4031 if (T.isNull())
4032 return 0;
4033
4034 Expr *SubExpr = Importer.Import(E->getSubExpr());
4035 if (!SubExpr)
4036 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00004037
4038 CXXCastPath BasePath;
4039 if (ImportCastPath(E, BasePath))
4040 return 0;
4041
4042 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00004043 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00004044}
4045
Douglas Gregor008847a2010-02-19 01:32:14 +00004046Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4047 QualType T = Importer.Import(E->getType());
4048 if (T.isNull())
4049 return 0;
4050
4051 Expr *SubExpr = Importer.Import(E->getSubExpr());
4052 if (!SubExpr)
4053 return 0;
4054
4055 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4056 if (!TInfo && E->getTypeInfoAsWritten())
4057 return 0;
4058
John McCallf871d0c2010-08-07 06:22:56 +00004059 CXXCastPath BasePath;
4060 if (ImportCastPath(E, BasePath))
4061 return 0;
4062
John McCallf89e55a2010-11-18 06:31:45 +00004063 return CStyleCastExpr::Create(Importer.getToContext(), T,
4064 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004065 SubExpr, &BasePath, TInfo,
4066 Importer.Import(E->getLParenLoc()),
4067 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004068}
4069
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004070ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004071 ASTContext &FromContext, FileManager &FromFileManager,
4072 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004073 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004074 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4075 Minimal(MinimalImport)
4076{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004077 ImportedDecls[FromContext.getTranslationUnitDecl()]
4078 = ToContext.getTranslationUnitDecl();
4079}
4080
4081ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004082
4083QualType ASTImporter::Import(QualType FromT) {
4084 if (FromT.isNull())
4085 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004086
4087 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004088
Douglas Gregor169fba52010-02-08 15:18:58 +00004089 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004090 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4091 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004092 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004093 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004094
Douglas Gregor169fba52010-02-08 15:18:58 +00004095 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004096 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004097 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004098 if (ToT.isNull())
4099 return ToT;
4100
Douglas Gregor169fba52010-02-08 15:18:58 +00004101 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004102 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004103
John McCallf4c73712011-01-19 06:33:43 +00004104 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004105}
4106
Douglas Gregor9bed8792010-02-09 19:21:46 +00004107TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004108 if (!FromTSI)
4109 return FromTSI;
4110
4111 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004112 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004113 QualType T = Import(FromTSI->getType());
4114 if (T.isNull())
4115 return 0;
4116
4117 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004118 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004119}
4120
4121Decl *ASTImporter::Import(Decl *FromD) {
4122 if (!FromD)
4123 return 0;
4124
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004125 ASTNodeImporter Importer(*this);
4126
Douglas Gregor9bed8792010-02-09 19:21:46 +00004127 // Check whether we've already imported this declaration.
4128 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004129 if (Pos != ImportedDecls.end()) {
4130 Decl *ToD = Pos->second;
4131 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4132 return ToD;
4133 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004134
4135 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004136 Decl *ToD = Importer.Visit(FromD);
4137 if (!ToD)
4138 return 0;
4139
4140 // Record the imported declaration.
4141 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004142
4143 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4144 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004145 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004146 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004147 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004148 // When we've finished transforming a typedef, see whether it was the
4149 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004150 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004151 FromTag = AnonTagsWithPendingTypedefs.begin(),
4152 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4153 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004154 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004155 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4156 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004157 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004158 AnonTagsWithPendingTypedefs.erase(FromTag);
4159 break;
4160 }
4161 }
4162 }
4163 }
4164
Douglas Gregor9bed8792010-02-09 19:21:46 +00004165 return ToD;
4166}
4167
4168DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4169 if (!FromDC)
4170 return FromDC;
4171
4172 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4173}
4174
4175Expr *ASTImporter::Import(Expr *FromE) {
4176 if (!FromE)
4177 return 0;
4178
4179 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4180}
4181
4182Stmt *ASTImporter::Import(Stmt *FromS) {
4183 if (!FromS)
4184 return 0;
4185
Douglas Gregor4800d952010-02-11 19:21:55 +00004186 // Check whether we've already imported this declaration.
4187 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4188 if (Pos != ImportedStmts.end())
4189 return Pos->second;
4190
4191 // Import the type
4192 ASTNodeImporter Importer(*this);
4193 Stmt *ToS = Importer.Visit(FromS);
4194 if (!ToS)
4195 return 0;
4196
4197 // Record the imported declaration.
4198 ImportedStmts[FromS] = ToS;
4199 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004200}
4201
4202NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4203 if (!FromNNS)
4204 return 0;
4205
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004206 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4207
4208 switch (FromNNS->getKind()) {
4209 case NestedNameSpecifier::Identifier:
4210 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4211 return NestedNameSpecifier::Create(ToContext, prefix, II);
4212 }
4213 return 0;
4214
4215 case NestedNameSpecifier::Namespace:
4216 if (NamespaceDecl *NS =
4217 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4218 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4219 }
4220 return 0;
4221
4222 case NestedNameSpecifier::NamespaceAlias:
4223 if (NamespaceAliasDecl *NSAD =
4224 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4225 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4226 }
4227 return 0;
4228
4229 case NestedNameSpecifier::Global:
4230 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4231
4232 case NestedNameSpecifier::TypeSpec:
4233 case NestedNameSpecifier::TypeSpecWithTemplate: {
4234 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4235 if (!T.isNull()) {
4236 bool bTemplate = FromNNS->getKind() ==
4237 NestedNameSpecifier::TypeSpecWithTemplate;
4238 return NestedNameSpecifier::Create(ToContext, prefix,
4239 bTemplate, T.getTypePtr());
4240 }
4241 }
4242 return 0;
4243 }
4244
4245 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004246 return 0;
4247}
4248
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004249NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4250 // FIXME: Implement!
4251 return NestedNameSpecifierLoc();
4252}
4253
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004254TemplateName ASTImporter::Import(TemplateName From) {
4255 switch (From.getKind()) {
4256 case TemplateName::Template:
4257 if (TemplateDecl *ToTemplate
4258 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4259 return TemplateName(ToTemplate);
4260
4261 return TemplateName();
4262
4263 case TemplateName::OverloadedTemplate: {
4264 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4265 UnresolvedSet<2> ToTemplates;
4266 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4267 E = FromStorage->end();
4268 I != E; ++I) {
4269 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4270 ToTemplates.addDecl(To);
4271 else
4272 return TemplateName();
4273 }
4274 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4275 ToTemplates.end());
4276 }
4277
4278 case TemplateName::QualifiedTemplate: {
4279 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4280 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4281 if (!Qualifier)
4282 return TemplateName();
4283
4284 if (TemplateDecl *ToTemplate
4285 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4286 return ToContext.getQualifiedTemplateName(Qualifier,
4287 QTN->hasTemplateKeyword(),
4288 ToTemplate);
4289
4290 return TemplateName();
4291 }
4292
4293 case TemplateName::DependentTemplate: {
4294 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4295 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4296 if (!Qualifier)
4297 return TemplateName();
4298
4299 if (DTN->isIdentifier()) {
4300 return ToContext.getDependentTemplateName(Qualifier,
4301 Import(DTN->getIdentifier()));
4302 }
4303
4304 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4305 }
John McCall14606042011-06-30 08:33:18 +00004306
4307 case TemplateName::SubstTemplateTemplateParm: {
4308 SubstTemplateTemplateParmStorage *subst
4309 = From.getAsSubstTemplateTemplateParm();
4310 TemplateTemplateParmDecl *param
4311 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4312 if (!param)
4313 return TemplateName();
4314
4315 TemplateName replacement = Import(subst->getReplacement());
4316 if (replacement.isNull()) return TemplateName();
4317
4318 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4319 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004320
4321 case TemplateName::SubstTemplateTemplateParmPack: {
4322 SubstTemplateTemplateParmPackStorage *SubstPack
4323 = From.getAsSubstTemplateTemplateParmPack();
4324 TemplateTemplateParmDecl *Param
4325 = cast_or_null<TemplateTemplateParmDecl>(
4326 Import(SubstPack->getParameterPack()));
4327 if (!Param)
4328 return TemplateName();
4329
4330 ASTNodeImporter Importer(*this);
4331 TemplateArgument ArgPack
4332 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4333 if (ArgPack.isNull())
4334 return TemplateName();
4335
4336 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4337 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004338 }
4339
4340 llvm_unreachable("Invalid template name kind");
4341 return TemplateName();
4342}
4343
Douglas Gregor9bed8792010-02-09 19:21:46 +00004344SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4345 if (FromLoc.isInvalid())
4346 return SourceLocation();
4347
Douglas Gregor88523732010-02-10 00:15:17 +00004348 SourceManager &FromSM = FromContext.getSourceManager();
4349
4350 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004351 // don't have to import macro expansions.
4352 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004353 FromLoc = FromSM.getSpellingLoc(FromLoc);
4354 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4355 SourceManager &ToSM = ToContext.getSourceManager();
4356 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidisa64ccef2011-09-19 20:40:19 +00004357 .getLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004358}
4359
4360SourceRange ASTImporter::Import(SourceRange FromRange) {
4361 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4362}
4363
Douglas Gregor88523732010-02-10 00:15:17 +00004364FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004365 llvm::DenseMap<FileID, FileID>::iterator Pos
4366 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004367 if (Pos != ImportedFileIDs.end())
4368 return Pos->second;
4369
4370 SourceManager &FromSM = FromContext.getSourceManager();
4371 SourceManager &ToSM = ToContext.getSourceManager();
4372 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004373 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004374
4375 // Include location of this file.
4376 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4377
4378 // Map the FileID for to the "to" source manager.
4379 FileID ToID;
4380 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004381 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004382 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4383 // disk again
4384 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4385 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004386 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004387 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4388 FromSLoc.getFile().getFileCharacteristic());
4389 } else {
4390 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004391 const llvm::MemoryBuffer *
4392 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004393 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004394 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004395 FromBuf->getBufferIdentifier());
4396 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4397 }
4398
4399
Sebastian Redl535a3e22010-09-30 01:03:06 +00004400 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004401 return ToID;
4402}
4403
Douglas Gregord8868a62011-01-18 03:11:38 +00004404void ASTImporter::ImportDefinition(Decl *From) {
4405 Decl *To = Import(From);
4406 if (!To)
4407 return;
4408
4409 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4410 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004411
4412 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4413 if (!ToRecord->getDefinition()) {
4414 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4415 /*ForceImport=*/true);
4416 return;
4417 }
4418 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004419
4420 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4421 if (!ToEnum->getDefinition()) {
4422 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4423 /*ForceImport=*/true);
4424 return;
4425 }
4426 }
4427
Douglas Gregord8868a62011-01-18 03:11:38 +00004428 Importer.ImportDeclContext(FromDC, true);
4429 }
4430}
4431
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004432DeclarationName ASTImporter::Import(DeclarationName FromName) {
4433 if (!FromName)
4434 return DeclarationName();
4435
4436 switch (FromName.getNameKind()) {
4437 case DeclarationName::Identifier:
4438 return Import(FromName.getAsIdentifierInfo());
4439
4440 case DeclarationName::ObjCZeroArgSelector:
4441 case DeclarationName::ObjCOneArgSelector:
4442 case DeclarationName::ObjCMultiArgSelector:
4443 return Import(FromName.getObjCSelector());
4444
4445 case DeclarationName::CXXConstructorName: {
4446 QualType T = Import(FromName.getCXXNameType());
4447 if (T.isNull())
4448 return DeclarationName();
4449
4450 return ToContext.DeclarationNames.getCXXConstructorName(
4451 ToContext.getCanonicalType(T));
4452 }
4453
4454 case DeclarationName::CXXDestructorName: {
4455 QualType T = Import(FromName.getCXXNameType());
4456 if (T.isNull())
4457 return DeclarationName();
4458
4459 return ToContext.DeclarationNames.getCXXDestructorName(
4460 ToContext.getCanonicalType(T));
4461 }
4462
4463 case DeclarationName::CXXConversionFunctionName: {
4464 QualType T = Import(FromName.getCXXNameType());
4465 if (T.isNull())
4466 return DeclarationName();
4467
4468 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4469 ToContext.getCanonicalType(T));
4470 }
4471
4472 case DeclarationName::CXXOperatorName:
4473 return ToContext.DeclarationNames.getCXXOperatorName(
4474 FromName.getCXXOverloadedOperator());
4475
4476 case DeclarationName::CXXLiteralOperatorName:
4477 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4478 Import(FromName.getCXXLiteralIdentifier()));
4479
4480 case DeclarationName::CXXUsingDirective:
4481 // FIXME: STATICS!
4482 return DeclarationName::getUsingDirectiveName();
4483 }
4484
4485 // Silence bogus GCC warning
4486 return DeclarationName();
4487}
4488
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004489IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004490 if (!FromId)
4491 return 0;
4492
4493 return &ToContext.Idents.get(FromId->getName());
4494}
Douglas Gregor089459a2010-02-08 21:09:39 +00004495
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004496Selector ASTImporter::Import(Selector FromSel) {
4497 if (FromSel.isNull())
4498 return Selector();
4499
Chris Lattner5f9e2722011-07-23 10:55:15 +00004500 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004501 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4502 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4503 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4504 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4505}
4506
Douglas Gregor089459a2010-02-08 21:09:39 +00004507DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4508 DeclContext *DC,
4509 unsigned IDNS,
4510 NamedDecl **Decls,
4511 unsigned NumDecls) {
4512 return Name;
4513}
4514
4515DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004516 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004517}
4518
4519DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004520 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004521}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004522
4523Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4524 ImportedDecls[From] = To;
4525 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004526}
Douglas Gregorea35d112010-02-15 23:54:17 +00004527
4528bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004529 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004530 = ImportedTypes.find(From.getTypePtr());
4531 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4532 return true;
4533
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004534 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004535 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004536}