blob: 2b15904fdba5c6535e1f9feda094b87e57c5dbb2 [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
John McCallf4c73712011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000084 SourceLocation &Loc);
Abramo Bagnara25777432010-08-11 22:01:17 +000085 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
86 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000087 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000088 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregor040afae2010-11-30 19:14:50 +000089 TemplateParameterList *ImportTemplateParameterList(
90 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000091 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
92 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
93 unsigned NumFromArgs,
94 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +000095 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +000096 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +000097 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +000098 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +000099 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000100 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000101 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000102 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000103 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000104 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000105 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000106 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000107 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
108 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
109 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
110 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000111 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000112 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000113 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000114 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000115 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000116 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000117 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000118 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000119 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000120 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000121 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000122 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000123 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000124 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000125 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000126 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000127 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
128 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
129 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
130 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000131 Decl *VisitClassTemplateSpecializationDecl(
132 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000133
Douglas Gregor4800d952010-02-11 19:21:55 +0000134 // Importing statements
135 Stmt *VisitStmt(Stmt *S);
136
137 // Importing expressions
138 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000139 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000140 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000141 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000142 Expr *VisitParenExpr(ParenExpr *E);
143 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000144 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000145 Expr *VisitBinaryOperator(BinaryOperator *E);
146 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000147 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000148 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000149 };
150}
151
152//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000153// Structural Equivalence
154//----------------------------------------------------------------------------
155
156namespace {
157 struct StructuralEquivalenceContext {
158 /// \brief AST contexts for which we are checking structural equivalence.
159 ASTContext &C1, &C2;
160
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000161 /// \brief The set of "tentative" equivalences between two canonical
162 /// declarations, mapping from a declaration in the first context to the
163 /// declaration in the second context that we believe to be equivalent.
164 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
165
166 /// \brief Queue of declarations in the first context whose equivalence
167 /// with a declaration in the second context still needs to be verified.
168 std::deque<Decl *> DeclsToCheck;
169
Douglas Gregorea35d112010-02-15 23:54:17 +0000170 /// \brief Declaration (from, to) pairs that are known not to be equivalent
171 /// (which we have already complained about).
172 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
173
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000174 /// \brief Whether we're being strict about the spelling of types when
175 /// unifying two types.
176 bool StrictTypeSpelling;
177
178 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000179 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000180 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000181 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000182 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000183
184 /// \brief Determine whether the two declarations are structurally
185 /// equivalent.
186 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
187
188 /// \brief Determine whether the two types are structurally equivalent.
189 bool IsStructurallyEquivalent(QualType T1, QualType T2);
190
191 private:
192 /// \brief Finish checking all of the structural equivalences.
193 ///
194 /// \returns true if an error occurred, false otherwise.
195 bool Finish();
196
197 public:
198 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000199 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000200 }
201
202 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000203 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000204 }
205 };
206}
207
208static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
209 QualType T1, QualType T2);
210static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
211 Decl *D1, Decl *D2);
212
213/// \brief Determine if two APInts have the same value, after zero-extending
214/// one of them (if needed!) to ensure that the bit-widths match.
215static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
216 if (I1.getBitWidth() == I2.getBitWidth())
217 return I1 == I2;
218
219 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000220 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000221
Jay Foad9f71a8f2010-12-07 08:25:34 +0000222 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000223}
224
225/// \brief Determine if two APSInts have the same value, zero- or sign-extending
226/// as needed.
227static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
228 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
229 return I1 == I2;
230
231 // Check for a bit-width mismatch.
232 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000233 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000234 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000235 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000236
237 // We have a signedness mismatch. Turn the signed value into an unsigned
238 // value.
239 if (I1.isSigned()) {
240 if (I1.isNegative())
241 return false;
242
243 return llvm::APSInt(I1, true) == I2;
244 }
245
246 if (I2.isNegative())
247 return false;
248
249 return I1 == llvm::APSInt(I2, true);
250}
251
252/// \brief Determine structural equivalence of two expressions.
253static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
254 Expr *E1, Expr *E2) {
255 if (!E1 || !E2)
256 return E1 == E2;
257
258 // FIXME: Actually perform a structural comparison!
259 return true;
260}
261
262/// \brief Determine whether two identifiers are equivalent.
263static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
264 const IdentifierInfo *Name2) {
265 if (!Name1 || !Name2)
266 return Name1 == Name2;
267
268 return Name1->getName() == Name2->getName();
269}
270
271/// \brief Determine whether two nested-name-specifiers are equivalent.
272static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
273 NestedNameSpecifier *NNS1,
274 NestedNameSpecifier *NNS2) {
275 // FIXME: Implement!
276 return true;
277}
278
279/// \brief Determine whether two template arguments are equivalent.
280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
281 const TemplateArgument &Arg1,
282 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000283 if (Arg1.getKind() != Arg2.getKind())
284 return false;
285
286 switch (Arg1.getKind()) {
287 case TemplateArgument::Null:
288 return true;
289
290 case TemplateArgument::Type:
291 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
292
293 case TemplateArgument::Integral:
294 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
295 Arg2.getIntegralType()))
296 return false;
297
298 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
299
300 case TemplateArgument::Declaration:
301 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
302
303 case TemplateArgument::Template:
304 return IsStructurallyEquivalent(Context,
305 Arg1.getAsTemplate(),
306 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000307
308 case TemplateArgument::TemplateExpansion:
309 return IsStructurallyEquivalent(Context,
310 Arg1.getAsTemplateOrTemplatePattern(),
311 Arg2.getAsTemplateOrTemplatePattern());
312
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000313 case TemplateArgument::Expression:
314 return IsStructurallyEquivalent(Context,
315 Arg1.getAsExpr(), Arg2.getAsExpr());
316
317 case TemplateArgument::Pack:
318 if (Arg1.pack_size() != Arg2.pack_size())
319 return false;
320
321 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
322 if (!IsStructurallyEquivalent(Context,
323 Arg1.pack_begin()[I],
324 Arg2.pack_begin()[I]))
325 return false;
326
327 return true;
328 }
329
330 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000331 return true;
332}
333
334/// \brief Determine structural equivalence for the common part of array
335/// types.
336static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
337 const ArrayType *Array1,
338 const ArrayType *Array2) {
339 if (!IsStructurallyEquivalent(Context,
340 Array1->getElementType(),
341 Array2->getElementType()))
342 return false;
343 if (Array1->getSizeModifier() != Array2->getSizeModifier())
344 return false;
345 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
346 return false;
347
348 return true;
349}
350
351/// \brief Determine structural equivalence of two types.
352static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
353 QualType T1, QualType T2) {
354 if (T1.isNull() || T2.isNull())
355 return T1.isNull() && T2.isNull();
356
357 if (!Context.StrictTypeSpelling) {
358 // We aren't being strict about token-to-token equivalence of types,
359 // so map down to the canonical type.
360 T1 = Context.C1.getCanonicalType(T1);
361 T2 = Context.C2.getCanonicalType(T2);
362 }
363
364 if (T1.getQualifiers() != T2.getQualifiers())
365 return false;
366
Douglas Gregorea35d112010-02-15 23:54:17 +0000367 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000368
Douglas Gregorea35d112010-02-15 23:54:17 +0000369 if (T1->getTypeClass() != T2->getTypeClass()) {
370 // Compare function types with prototypes vs. without prototypes as if
371 // both did not have prototypes.
372 if (T1->getTypeClass() == Type::FunctionProto &&
373 T2->getTypeClass() == Type::FunctionNoProto)
374 TC = Type::FunctionNoProto;
375 else if (T1->getTypeClass() == Type::FunctionNoProto &&
376 T2->getTypeClass() == Type::FunctionProto)
377 TC = Type::FunctionNoProto;
378 else
379 return false;
380 }
381
382 switch (TC) {
383 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000384 // FIXME: Deal with Char_S/Char_U.
385 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
386 return false;
387 break;
388
389 case Type::Complex:
390 if (!IsStructurallyEquivalent(Context,
391 cast<ComplexType>(T1)->getElementType(),
392 cast<ComplexType>(T2)->getElementType()))
393 return false;
394 break;
395
396 case Type::Pointer:
397 if (!IsStructurallyEquivalent(Context,
398 cast<PointerType>(T1)->getPointeeType(),
399 cast<PointerType>(T2)->getPointeeType()))
400 return false;
401 break;
402
403 case Type::BlockPointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<BlockPointerType>(T1)->getPointeeType(),
406 cast<BlockPointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::LValueReference:
411 case Type::RValueReference: {
412 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
413 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
414 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
415 return false;
416 if (Ref1->isInnerRef() != Ref2->isInnerRef())
417 return false;
418 if (!IsStructurallyEquivalent(Context,
419 Ref1->getPointeeTypeAsWritten(),
420 Ref2->getPointeeTypeAsWritten()))
421 return false;
422 break;
423 }
424
425 case Type::MemberPointer: {
426 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
427 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
428 if (!IsStructurallyEquivalent(Context,
429 MemPtr1->getPointeeType(),
430 MemPtr2->getPointeeType()))
431 return false;
432 if (!IsStructurallyEquivalent(Context,
433 QualType(MemPtr1->getClass(), 0),
434 QualType(MemPtr2->getClass(), 0)))
435 return false;
436 break;
437 }
438
439 case Type::ConstantArray: {
440 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
441 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
442 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
443 return false;
444
445 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
446 return false;
447 break;
448 }
449
450 case Type::IncompleteArray:
451 if (!IsArrayStructurallyEquivalent(Context,
452 cast<ArrayType>(T1),
453 cast<ArrayType>(T2)))
454 return false;
455 break;
456
457 case Type::VariableArray: {
458 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
459 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
460 if (!IsStructurallyEquivalent(Context,
461 Array1->getSizeExpr(), Array2->getSizeExpr()))
462 return false;
463
464 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
465 return false;
466
467 break;
468 }
469
470 case Type::DependentSizedArray: {
471 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
472 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
473 if (!IsStructurallyEquivalent(Context,
474 Array1->getSizeExpr(), Array2->getSizeExpr()))
475 return false;
476
477 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
478 return false;
479
480 break;
481 }
482
483 case Type::DependentSizedExtVector: {
484 const DependentSizedExtVectorType *Vec1
485 = cast<DependentSizedExtVectorType>(T1);
486 const DependentSizedExtVectorType *Vec2
487 = cast<DependentSizedExtVectorType>(T2);
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
490 return false;
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 break;
496 }
497
498 case Type::Vector:
499 case Type::ExtVector: {
500 const VectorType *Vec1 = cast<VectorType>(T1);
501 const VectorType *Vec2 = cast<VectorType>(T2);
502 if (!IsStructurallyEquivalent(Context,
503 Vec1->getElementType(),
504 Vec2->getElementType()))
505 return false;
506 if (Vec1->getNumElements() != Vec2->getNumElements())
507 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000508 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000509 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000510 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000511 }
512
513 case Type::FunctionProto: {
514 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
515 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
516 if (Proto1->getNumArgs() != Proto2->getNumArgs())
517 return false;
518 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
519 if (!IsStructurallyEquivalent(Context,
520 Proto1->getArgType(I),
521 Proto2->getArgType(I)))
522 return false;
523 }
524 if (Proto1->isVariadic() != Proto2->isVariadic())
525 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000527 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000528 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
529 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
530 return false;
531 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
532 if (!IsStructurallyEquivalent(Context,
533 Proto1->getExceptionType(I),
534 Proto2->getExceptionType(I)))
535 return false;
536 }
537 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000538 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000539 Proto1->getNoexceptExpr(),
540 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000541 return false;
542 }
543 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
544 return false;
545
546 // Fall through to check the bits common with FunctionNoProtoType.
547 }
548
549 case Type::FunctionNoProto: {
550 const FunctionType *Function1 = cast<FunctionType>(T1);
551 const FunctionType *Function2 = cast<FunctionType>(T2);
552 if (!IsStructurallyEquivalent(Context,
553 Function1->getResultType(),
554 Function2->getResultType()))
555 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000556 if (Function1->getExtInfo() != Function2->getExtInfo())
557 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000558 break;
559 }
560
561 case Type::UnresolvedUsing:
562 if (!IsStructurallyEquivalent(Context,
563 cast<UnresolvedUsingType>(T1)->getDecl(),
564 cast<UnresolvedUsingType>(T2)->getDecl()))
565 return false;
566
567 break;
John McCall9d156a72011-01-06 01:58:22 +0000568
569 case Type::Attributed:
570 if (!IsStructurallyEquivalent(Context,
571 cast<AttributedType>(T1)->getModifiedType(),
572 cast<AttributedType>(T2)->getModifiedType()))
573 return false;
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getEquivalentType(),
576 cast<AttributedType>(T2)->getEquivalentType()))
577 return false;
578 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000579
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000580 case Type::Paren:
581 if (!IsStructurallyEquivalent(Context,
582 cast<ParenType>(T1)->getInnerType(),
583 cast<ParenType>(T2)->getInnerType()))
584 return false;
585 break;
586
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000587 case Type::Typedef:
588 if (!IsStructurallyEquivalent(Context,
589 cast<TypedefType>(T1)->getDecl(),
590 cast<TypedefType>(T2)->getDecl()))
591 return false;
592 break;
593
594 case Type::TypeOfExpr:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
597 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
598 return false;
599 break;
600
601 case Type::TypeOf:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfType>(T1)->getUnderlyingType(),
604 cast<TypeOfType>(T2)->getUnderlyingType()))
605 return false;
606 break;
607
608 case Type::Decltype:
609 if (!IsStructurallyEquivalent(Context,
610 cast<DecltypeType>(T1)->getUnderlyingExpr(),
611 cast<DecltypeType>(T2)->getUnderlyingExpr()))
612 return false;
613 break;
614
Richard Smith34b41d92011-02-20 03:19:35 +0000615 case Type::Auto:
616 if (!IsStructurallyEquivalent(Context,
617 cast<AutoType>(T1)->getDeducedType(),
618 cast<AutoType>(T2)->getDeducedType()))
619 return false;
620 break;
621
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000622 case Type::Record:
623 case Type::Enum:
624 if (!IsStructurallyEquivalent(Context,
625 cast<TagType>(T1)->getDecl(),
626 cast<TagType>(T2)->getDecl()))
627 return false;
628 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000629
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000630 case Type::TemplateTypeParm: {
631 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
632 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
633 if (Parm1->getDepth() != Parm2->getDepth())
634 return false;
635 if (Parm1->getIndex() != Parm2->getIndex())
636 return false;
637 if (Parm1->isParameterPack() != Parm2->isParameterPack())
638 return false;
639
640 // Names of template type parameters are never significant.
641 break;
642 }
643
644 case Type::SubstTemplateTypeParm: {
645 const SubstTemplateTypeParmType *Subst1
646 = cast<SubstTemplateTypeParmType>(T1);
647 const SubstTemplateTypeParmType *Subst2
648 = cast<SubstTemplateTypeParmType>(T2);
649 if (!IsStructurallyEquivalent(Context,
650 QualType(Subst1->getReplacedParameter(), 0),
651 QualType(Subst2->getReplacedParameter(), 0)))
652 return false;
653 if (!IsStructurallyEquivalent(Context,
654 Subst1->getReplacementType(),
655 Subst2->getReplacementType()))
656 return false;
657 break;
658 }
659
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000660 case Type::SubstTemplateTypeParmPack: {
661 const SubstTemplateTypeParmPackType *Subst1
662 = cast<SubstTemplateTypeParmPackType>(T1);
663 const SubstTemplateTypeParmPackType *Subst2
664 = cast<SubstTemplateTypeParmPackType>(T2);
665 if (!IsStructurallyEquivalent(Context,
666 QualType(Subst1->getReplacedParameter(), 0),
667 QualType(Subst2->getReplacedParameter(), 0)))
668 return false;
669 if (!IsStructurallyEquivalent(Context,
670 Subst1->getArgumentPack(),
671 Subst2->getArgumentPack()))
672 return false;
673 break;
674 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000675 case Type::TemplateSpecialization: {
676 const TemplateSpecializationType *Spec1
677 = cast<TemplateSpecializationType>(T1);
678 const TemplateSpecializationType *Spec2
679 = cast<TemplateSpecializationType>(T2);
680 if (!IsStructurallyEquivalent(Context,
681 Spec1->getTemplateName(),
682 Spec2->getTemplateName()))
683 return false;
684 if (Spec1->getNumArgs() != Spec2->getNumArgs())
685 return false;
686 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getArg(I), Spec2->getArg(I)))
689 return false;
690 }
691 break;
692 }
693
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000694 case Type::Elaborated: {
695 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
696 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
697 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
698 if (Elab1->getKeyword() != Elab2->getKeyword())
699 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000700 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000701 Elab1->getQualifier(),
702 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000703 return false;
704 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000705 Elab1->getNamedType(),
706 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000707 return false;
708 break;
709 }
710
John McCall3cb0ebd2010-03-10 03:28:59 +0000711 case Type::InjectedClassName: {
712 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
713 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
714 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000715 Inj1->getInjectedSpecializationType(),
716 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000717 return false;
718 break;
719 }
720
Douglas Gregor4714c122010-03-31 17:34:00 +0000721 case Type::DependentName: {
722 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
723 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000724 if (!IsStructurallyEquivalent(Context,
725 Typename1->getQualifier(),
726 Typename2->getQualifier()))
727 return false;
728 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
729 Typename2->getIdentifier()))
730 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000731
732 break;
733 }
734
John McCall33500952010-06-11 00:33:02 +0000735 case Type::DependentTemplateSpecialization: {
736 const DependentTemplateSpecializationType *Spec1 =
737 cast<DependentTemplateSpecializationType>(T1);
738 const DependentTemplateSpecializationType *Spec2 =
739 cast<DependentTemplateSpecializationType>(T2);
740 if (!IsStructurallyEquivalent(Context,
741 Spec1->getQualifier(),
742 Spec2->getQualifier()))
743 return false;
744 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
745 Spec2->getIdentifier()))
746 return false;
747 if (Spec1->getNumArgs() != Spec2->getNumArgs())
748 return false;
749 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
750 if (!IsStructurallyEquivalent(Context,
751 Spec1->getArg(I), Spec2->getArg(I)))
752 return false;
753 }
754 break;
755 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000756
757 case Type::PackExpansion:
758 if (!IsStructurallyEquivalent(Context,
759 cast<PackExpansionType>(T1)->getPattern(),
760 cast<PackExpansionType>(T2)->getPattern()))
761 return false;
762 break;
763
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000764 case Type::ObjCInterface: {
765 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
766 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
767 if (!IsStructurallyEquivalent(Context,
768 Iface1->getDecl(), Iface2->getDecl()))
769 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000770 break;
771 }
772
773 case Type::ObjCObject: {
774 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
775 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
776 if (!IsStructurallyEquivalent(Context,
777 Obj1->getBaseType(),
778 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000779 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000780 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
781 return false;
782 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000783 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000784 Obj1->getProtocol(I),
785 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000786 return false;
787 }
788 break;
789 }
790
791 case Type::ObjCObjectPointer: {
792 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
793 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
794 if (!IsStructurallyEquivalent(Context,
795 Ptr1->getPointeeType(),
796 Ptr2->getPointeeType()))
797 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 break;
799 }
800
801 } // end switch
802
803 return true;
804}
805
806/// \brief Determine structural equivalence of two records.
807static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
808 RecordDecl *D1, RecordDecl *D2) {
809 if (D1->isUnion() != D2->isUnion()) {
810 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
811 << Context.C2.getTypeDeclType(D2);
812 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
813 << D1->getDeclName() << (unsigned)D1->getTagKind();
814 return false;
815 }
816
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000817 // If both declarations are class template specializations, we know
818 // the ODR applies, so check the template and template arguments.
819 ClassTemplateSpecializationDecl *Spec1
820 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
821 ClassTemplateSpecializationDecl *Spec2
822 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
823 if (Spec1 && Spec2) {
824 // Check that the specialized templates are the same.
825 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
826 Spec2->getSpecializedTemplate()))
827 return false;
828
829 // Check that the template arguments are the same.
830 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
831 return false;
832
833 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
834 if (!IsStructurallyEquivalent(Context,
835 Spec1->getTemplateArgs().get(I),
836 Spec2->getTemplateArgs().get(I)))
837 return false;
838 }
839 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000840 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000841 else if (Spec1 || Spec2)
842 return false;
843
Douglas Gregorea35d112010-02-15 23:54:17 +0000844 // Compare the definitions of these two records. If either or both are
845 // incomplete, we assume that they are equivalent.
846 D1 = D1->getDefinition();
847 D2 = D2->getDefinition();
848 if (!D1 || !D2)
849 return true;
850
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000851 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
852 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
853 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
854 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000855 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000856 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000857 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000858 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000859 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000860 return false;
861 }
862
863 // Check the base classes.
864 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
865 BaseEnd1 = D1CXX->bases_end(),
866 Base2 = D2CXX->bases_begin();
867 Base1 != BaseEnd1;
868 ++Base1, ++Base2) {
869 if (!IsStructurallyEquivalent(Context,
870 Base1->getType(), Base2->getType())) {
871 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
872 << Context.C2.getTypeDeclType(D2);
873 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
874 << Base2->getType()
875 << Base2->getSourceRange();
876 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
877 << Base1->getType()
878 << Base1->getSourceRange();
879 return false;
880 }
881
882 // Check virtual vs. non-virtual inheritance mismatch.
883 if (Base1->isVirtual() != Base2->isVirtual()) {
884 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
885 << Context.C2.getTypeDeclType(D2);
886 Context.Diag2(Base2->getSourceRange().getBegin(),
887 diag::note_odr_virtual_base)
888 << Base2->isVirtual() << Base2->getSourceRange();
889 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
890 << Base1->isVirtual()
891 << Base1->getSourceRange();
892 return false;
893 }
894 }
895 } else if (D1CXX->getNumBases() > 0) {
896 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
897 << Context.C2.getTypeDeclType(D2);
898 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
899 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
900 << Base1->getType()
901 << Base1->getSourceRange();
902 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
903 return false;
904 }
905 }
906
907 // Check the fields for consistency.
908 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
909 Field2End = D2->field_end();
910 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
911 Field1End = D1->field_end();
912 Field1 != Field1End;
913 ++Field1, ++Field2) {
914 if (Field2 == Field2End) {
915 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
916 << Context.C2.getTypeDeclType(D2);
917 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
918 << Field1->getDeclName() << Field1->getType();
919 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
920 return false;
921 }
922
923 if (!IsStructurallyEquivalent(Context,
924 Field1->getType(), Field2->getType())) {
925 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
926 << Context.C2.getTypeDeclType(D2);
927 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
928 << Field2->getDeclName() << Field2->getType();
929 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
930 << Field1->getDeclName() << Field1->getType();
931 return false;
932 }
933
934 if (Field1->isBitField() != Field2->isBitField()) {
935 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
936 << Context.C2.getTypeDeclType(D2);
937 if (Field1->isBitField()) {
938 llvm::APSInt Bits;
939 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
940 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
941 << Field1->getDeclName() << Field1->getType()
942 << Bits.toString(10, false);
943 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
944 << Field2->getDeclName();
945 } else {
946 llvm::APSInt Bits;
947 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
948 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
949 << Field2->getDeclName() << Field2->getType()
950 << Bits.toString(10, false);
951 Context.Diag1(Field1->getLocation(),
952 diag::note_odr_not_bit_field)
953 << Field1->getDeclName();
954 }
955 return false;
956 }
957
958 if (Field1->isBitField()) {
959 // Make sure that the bit-fields are the same length.
960 llvm::APSInt Bits1, Bits2;
961 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
962 return false;
963 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
964 return false;
965
966 if (!IsSameValue(Bits1, Bits2)) {
967 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
968 << Context.C2.getTypeDeclType(D2);
969 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
970 << Field2->getDeclName() << Field2->getType()
971 << Bits2.toString(10, false);
972 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
973 << Field1->getDeclName() << Field1->getType()
974 << Bits1.toString(10, false);
975 return false;
976 }
977 }
978 }
979
980 if (Field2 != Field2End) {
981 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
982 << Context.C2.getTypeDeclType(D2);
983 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
984 << Field2->getDeclName() << Field2->getType();
985 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
986 return false;
987 }
988
989 return true;
990}
991
992/// \brief Determine structural equivalence of two enums.
993static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
994 EnumDecl *D1, EnumDecl *D2) {
995 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
996 EC2End = D2->enumerator_end();
997 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
998 EC1End = D1->enumerator_end();
999 EC1 != EC1End; ++EC1, ++EC2) {
1000 if (EC2 == EC2End) {
1001 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1002 << Context.C2.getTypeDeclType(D2);
1003 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1004 << EC1->getDeclName()
1005 << EC1->getInitVal().toString(10);
1006 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1007 return false;
1008 }
1009
1010 llvm::APSInt Val1 = EC1->getInitVal();
1011 llvm::APSInt Val2 = EC2->getInitVal();
1012 if (!IsSameValue(Val1, Val2) ||
1013 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1014 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1015 << Context.C2.getTypeDeclType(D2);
1016 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1017 << EC2->getDeclName()
1018 << EC2->getInitVal().toString(10);
1019 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1020 << EC1->getDeclName()
1021 << EC1->getInitVal().toString(10);
1022 return false;
1023 }
1024 }
1025
1026 if (EC2 != EC2End) {
1027 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1028 << Context.C2.getTypeDeclType(D2);
1029 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1030 << EC2->getDeclName()
1031 << EC2->getInitVal().toString(10);
1032 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1033 return false;
1034 }
1035
1036 return true;
1037}
Douglas Gregor040afae2010-11-30 19:14:50 +00001038
1039static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1040 TemplateParameterList *Params1,
1041 TemplateParameterList *Params2) {
1042 if (Params1->size() != Params2->size()) {
1043 Context.Diag2(Params2->getTemplateLoc(),
1044 diag::err_odr_different_num_template_parameters)
1045 << Params1->size() << Params2->size();
1046 Context.Diag1(Params1->getTemplateLoc(),
1047 diag::note_odr_template_parameter_list);
1048 return false;
1049 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001050
Douglas Gregor040afae2010-11-30 19:14:50 +00001051 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1052 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1053 Context.Diag2(Params2->getParam(I)->getLocation(),
1054 diag::err_odr_different_template_parameter_kind);
1055 Context.Diag1(Params1->getParam(I)->getLocation(),
1056 diag::note_odr_template_parameter_here);
1057 return false;
1058 }
1059
1060 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1061 Params2->getParam(I))) {
1062
1063 return false;
1064 }
1065 }
1066
1067 return true;
1068}
1069
1070static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1071 TemplateTypeParmDecl *D1,
1072 TemplateTypeParmDecl *D2) {
1073 if (D1->isParameterPack() != D2->isParameterPack()) {
1074 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1075 << D2->isParameterPack();
1076 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1077 << D1->isParameterPack();
1078 return false;
1079 }
1080
1081 return true;
1082}
1083
1084static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1085 NonTypeTemplateParmDecl *D1,
1086 NonTypeTemplateParmDecl *D2) {
1087 // FIXME: Enable once we have variadic templates.
1088#if 0
1089 if (D1->isParameterPack() != D2->isParameterPack()) {
1090 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1091 << D2->isParameterPack();
1092 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1093 << D1->isParameterPack();
1094 return false;
1095 }
1096#endif
1097
1098 // Check types.
1099 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1100 Context.Diag2(D2->getLocation(),
1101 diag::err_odr_non_type_parameter_type_inconsistent)
1102 << D2->getType() << D1->getType();
1103 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1104 << D1->getType();
1105 return false;
1106 }
1107
1108 return true;
1109}
1110
1111static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1112 TemplateTemplateParmDecl *D1,
1113 TemplateTemplateParmDecl *D2) {
1114 // FIXME: Enable once we have variadic templates.
1115#if 0
1116 if (D1->isParameterPack() != D2->isParameterPack()) {
1117 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1118 << D2->isParameterPack();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1120 << D1->isParameterPack();
1121 return false;
1122 }
1123#endif
1124
1125 // Check template parameter lists.
1126 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1127 D2->getTemplateParameters());
1128}
1129
1130static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1131 ClassTemplateDecl *D1,
1132 ClassTemplateDecl *D2) {
1133 // Check template parameters.
1134 if (!IsStructurallyEquivalent(Context,
1135 D1->getTemplateParameters(),
1136 D2->getTemplateParameters()))
1137 return false;
1138
1139 // Check the templated declaration.
1140 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1141 D2->getTemplatedDecl());
1142}
1143
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001144/// \brief Determine structural equivalence of two declarations.
1145static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1146 Decl *D1, Decl *D2) {
1147 // FIXME: Check for known structural equivalences via a callback of some sort.
1148
Douglas Gregorea35d112010-02-15 23:54:17 +00001149 // Check whether we already know that these two declarations are not
1150 // structurally equivalent.
1151 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1152 D2->getCanonicalDecl())))
1153 return false;
1154
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001155 // Determine whether we've already produced a tentative equivalence for D1.
1156 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1157 if (EquivToD1)
1158 return EquivToD1 == D2->getCanonicalDecl();
1159
1160 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1161 EquivToD1 = D2->getCanonicalDecl();
1162 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1163 return true;
1164}
1165
1166bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1167 Decl *D2) {
1168 if (!::IsStructurallyEquivalent(*this, D1, D2))
1169 return false;
1170
1171 return !Finish();
1172}
1173
1174bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1175 QualType T2) {
1176 if (!::IsStructurallyEquivalent(*this, T1, T2))
1177 return false;
1178
1179 return !Finish();
1180}
1181
1182bool StructuralEquivalenceContext::Finish() {
1183 while (!DeclsToCheck.empty()) {
1184 // Check the next declaration.
1185 Decl *D1 = DeclsToCheck.front();
1186 DeclsToCheck.pop_front();
1187
1188 Decl *D2 = TentativeEquivalences[D1];
1189 assert(D2 && "Unrecorded tentative equivalence?");
1190
Douglas Gregorea35d112010-02-15 23:54:17 +00001191 bool Equivalent = true;
1192
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001193 // FIXME: Switch on all declaration kinds. For now, we're just going to
1194 // check the obvious ones.
1195 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1196 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1197 // Check for equivalent structure names.
1198 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001199 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1200 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001201 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001202 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1203 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001204 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1205 !::IsStructurallyEquivalent(*this, Record1, Record2))
1206 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001207 } else {
1208 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001209 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001210 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001211 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001212 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1213 // Check for equivalent enum names.
1214 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001215 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1216 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001217 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001218 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1219 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001220 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1221 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1222 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001223 } else {
1224 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001225 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001226 }
Richard Smith162e1c12011-04-15 14:24:37 +00001227 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1228 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001229 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001230 Typedef2->getIdentifier()) ||
1231 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001232 Typedef1->getUnderlyingType(),
1233 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001234 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001235 } else {
1236 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001237 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001238 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001239 } else if (ClassTemplateDecl *ClassTemplate1
1240 = dyn_cast<ClassTemplateDecl>(D1)) {
1241 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1242 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1243 ClassTemplate2->getIdentifier()) ||
1244 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1245 Equivalent = false;
1246 } else {
1247 // Class template/non-class-template mismatch.
1248 Equivalent = false;
1249 }
1250 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1251 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1252 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1253 Equivalent = false;
1254 } else {
1255 // Kind mismatch.
1256 Equivalent = false;
1257 }
1258 } else if (NonTypeTemplateParmDecl *NTTP1
1259 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1260 if (NonTypeTemplateParmDecl *NTTP2
1261 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1262 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1263 Equivalent = false;
1264 } else {
1265 // Kind mismatch.
1266 Equivalent = false;
1267 }
1268 } else if (TemplateTemplateParmDecl *TTP1
1269 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1270 if (TemplateTemplateParmDecl *TTP2
1271 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1272 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1273 Equivalent = false;
1274 } else {
1275 // Kind mismatch.
1276 Equivalent = false;
1277 }
1278 }
1279
Douglas Gregorea35d112010-02-15 23:54:17 +00001280 if (!Equivalent) {
1281 // Note that these two declarations are not equivalent (and we already
1282 // know about it).
1283 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1284 D2->getCanonicalDecl()));
1285 return true;
1286 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001287 // FIXME: Check other declaration kinds!
1288 }
1289
1290 return false;
1291}
1292
1293//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001294// Import Types
1295//----------------------------------------------------------------------------
1296
John McCallf4c73712011-01-19 06:33:43 +00001297QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001298 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1299 << T->getTypeClassName();
1300 return QualType();
1301}
1302
John McCallf4c73712011-01-19 06:33:43 +00001303QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001304 switch (T->getKind()) {
1305 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1306 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1307
1308 case BuiltinType::Char_U:
1309 // The context we're importing from has an unsigned 'char'. If we're
1310 // importing into a context with a signed 'char', translate to
1311 // 'unsigned char' instead.
1312 if (Importer.getToContext().getLangOptions().CharIsSigned)
1313 return Importer.getToContext().UnsignedCharTy;
1314
1315 return Importer.getToContext().CharTy;
1316
1317 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1318
1319 case BuiltinType::Char16:
1320 // FIXME: Make sure that the "to" context supports C++!
1321 return Importer.getToContext().Char16Ty;
1322
1323 case BuiltinType::Char32:
1324 // FIXME: Make sure that the "to" context supports C++!
1325 return Importer.getToContext().Char32Ty;
1326
1327 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1328 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1329 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1330 case BuiltinType::ULongLong:
1331 return Importer.getToContext().UnsignedLongLongTy;
1332 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1333
1334 case BuiltinType::Char_S:
1335 // The context we're importing from has an unsigned 'char'. If we're
1336 // importing into a context with a signed 'char', translate to
1337 // 'unsigned char' instead.
1338 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1339 return Importer.getToContext().SignedCharTy;
1340
1341 return Importer.getToContext().CharTy;
1342
1343 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001344 case BuiltinType::WChar_S:
1345 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001346 // FIXME: If not in C++, shall we translate to the C equivalent of
1347 // wchar_t?
1348 return Importer.getToContext().WCharTy;
1349
1350 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1351 case BuiltinType::Int : return Importer.getToContext().IntTy;
1352 case BuiltinType::Long : return Importer.getToContext().LongTy;
1353 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1354 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1355 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1356 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1357 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1358
1359 case BuiltinType::NullPtr:
1360 // FIXME: Make sure that the "to" context supports C++0x!
1361 return Importer.getToContext().NullPtrTy;
1362
1363 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1364 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001365 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001366
1367 case BuiltinType::ObjCId:
1368 // FIXME: Make sure that the "to" context supports Objective-C!
1369 return Importer.getToContext().ObjCBuiltinIdTy;
1370
1371 case BuiltinType::ObjCClass:
1372 return Importer.getToContext().ObjCBuiltinClassTy;
1373
1374 case BuiltinType::ObjCSel:
1375 return Importer.getToContext().ObjCBuiltinSelTy;
1376 }
1377
1378 return QualType();
1379}
1380
John McCallf4c73712011-01-19 06:33:43 +00001381QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001382 QualType ToElementType = Importer.Import(T->getElementType());
1383 if (ToElementType.isNull())
1384 return QualType();
1385
1386 return Importer.getToContext().getComplexType(ToElementType);
1387}
1388
John McCallf4c73712011-01-19 06:33:43 +00001389QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001390 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1391 if (ToPointeeType.isNull())
1392 return QualType();
1393
1394 return Importer.getToContext().getPointerType(ToPointeeType);
1395}
1396
John McCallf4c73712011-01-19 06:33:43 +00001397QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001398 // FIXME: Check for blocks support in "to" context.
1399 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1400 if (ToPointeeType.isNull())
1401 return QualType();
1402
1403 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1404}
1405
John McCallf4c73712011-01-19 06:33:43 +00001406QualType
1407ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001408 // FIXME: Check for C++ support in "to" context.
1409 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1410 if (ToPointeeType.isNull())
1411 return QualType();
1412
1413 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1414}
1415
John McCallf4c73712011-01-19 06:33:43 +00001416QualType
1417ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001418 // FIXME: Check for C++0x support in "to" context.
1419 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1420 if (ToPointeeType.isNull())
1421 return QualType();
1422
1423 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1424}
1425
John McCallf4c73712011-01-19 06:33:43 +00001426QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001427 // FIXME: Check for C++ support in "to" context.
1428 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1429 if (ToPointeeType.isNull())
1430 return QualType();
1431
1432 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1433 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1434 ClassType.getTypePtr());
1435}
1436
John McCallf4c73712011-01-19 06:33:43 +00001437QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001438 QualType ToElementType = Importer.Import(T->getElementType());
1439 if (ToElementType.isNull())
1440 return QualType();
1441
1442 return Importer.getToContext().getConstantArrayType(ToElementType,
1443 T->getSize(),
1444 T->getSizeModifier(),
1445 T->getIndexTypeCVRQualifiers());
1446}
1447
John McCallf4c73712011-01-19 06:33:43 +00001448QualType
1449ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001450 QualType ToElementType = Importer.Import(T->getElementType());
1451 if (ToElementType.isNull())
1452 return QualType();
1453
1454 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1455 T->getSizeModifier(),
1456 T->getIndexTypeCVRQualifiers());
1457}
1458
John McCallf4c73712011-01-19 06:33:43 +00001459QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001460 QualType ToElementType = Importer.Import(T->getElementType());
1461 if (ToElementType.isNull())
1462 return QualType();
1463
1464 Expr *Size = Importer.Import(T->getSizeExpr());
1465 if (!Size)
1466 return QualType();
1467
1468 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1469 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1470 T->getSizeModifier(),
1471 T->getIndexTypeCVRQualifiers(),
1472 Brackets);
1473}
1474
John McCallf4c73712011-01-19 06:33:43 +00001475QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001476 QualType ToElementType = Importer.Import(T->getElementType());
1477 if (ToElementType.isNull())
1478 return QualType();
1479
1480 return Importer.getToContext().getVectorType(ToElementType,
1481 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001482 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001483}
1484
John McCallf4c73712011-01-19 06:33:43 +00001485QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001486 QualType ToElementType = Importer.Import(T->getElementType());
1487 if (ToElementType.isNull())
1488 return QualType();
1489
1490 return Importer.getToContext().getExtVectorType(ToElementType,
1491 T->getNumElements());
1492}
1493
John McCallf4c73712011-01-19 06:33:43 +00001494QualType
1495ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001496 // FIXME: What happens if we're importing a function without a prototype
1497 // into C++? Should we make it variadic?
1498 QualType ToResultType = Importer.Import(T->getResultType());
1499 if (ToResultType.isNull())
1500 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001501
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001502 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001503 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001504}
1505
John McCallf4c73712011-01-19 06:33:43 +00001506QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001507 QualType ToResultType = Importer.Import(T->getResultType());
1508 if (ToResultType.isNull())
1509 return QualType();
1510
1511 // Import argument types
1512 llvm::SmallVector<QualType, 4> ArgTypes;
1513 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1514 AEnd = T->arg_type_end();
1515 A != AEnd; ++A) {
1516 QualType ArgType = Importer.Import(*A);
1517 if (ArgType.isNull())
1518 return QualType();
1519 ArgTypes.push_back(ArgType);
1520 }
1521
1522 // Import exception types
1523 llvm::SmallVector<QualType, 4> ExceptionTypes;
1524 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1525 EEnd = T->exception_end();
1526 E != EEnd; ++E) {
1527 QualType ExceptionType = Importer.Import(*E);
1528 if (ExceptionType.isNull())
1529 return QualType();
1530 ExceptionTypes.push_back(ExceptionType);
1531 }
John McCalle23cf432010-12-14 08:05:40 +00001532
1533 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1534 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001535
1536 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001537 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001538}
1539
John McCallf4c73712011-01-19 06:33:43 +00001540QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001541 TypedefNameDecl *ToDecl
1542 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001543 if (!ToDecl)
1544 return QualType();
1545
1546 return Importer.getToContext().getTypeDeclType(ToDecl);
1547}
1548
John McCallf4c73712011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001550 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1551 if (!ToExpr)
1552 return QualType();
1553
1554 return Importer.getToContext().getTypeOfExprType(ToExpr);
1555}
1556
John McCallf4c73712011-01-19 06:33:43 +00001557QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001558 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1559 if (ToUnderlyingType.isNull())
1560 return QualType();
1561
1562 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1563}
1564
John McCallf4c73712011-01-19 06:33:43 +00001565QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001566 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001567 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1568 if (!ToExpr)
1569 return QualType();
1570
1571 return Importer.getToContext().getDecltypeType(ToExpr);
1572}
1573
Richard Smith34b41d92011-02-20 03:19:35 +00001574QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1575 // FIXME: Make sure that the "to" context supports C++0x!
1576 QualType FromDeduced = T->getDeducedType();
1577 QualType ToDeduced;
1578 if (!FromDeduced.isNull()) {
1579 ToDeduced = Importer.Import(FromDeduced);
1580 if (ToDeduced.isNull())
1581 return QualType();
1582 }
1583
1584 return Importer.getToContext().getAutoType(ToDeduced);
1585}
1586
John McCallf4c73712011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001588 RecordDecl *ToDecl
1589 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1590 if (!ToDecl)
1591 return QualType();
1592
1593 return Importer.getToContext().getTagDeclType(ToDecl);
1594}
1595
John McCallf4c73712011-01-19 06:33:43 +00001596QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001597 EnumDecl *ToDecl
1598 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1599 if (!ToDecl)
1600 return QualType();
1601
1602 return Importer.getToContext().getTagDeclType(ToDecl);
1603}
1604
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001605QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001606 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001607 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1608 if (ToTemplate.isNull())
1609 return QualType();
1610
1611 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1612 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1613 return QualType();
1614
1615 QualType ToCanonType;
1616 if (!QualType(T, 0).isCanonical()) {
1617 QualType FromCanonType
1618 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1619 ToCanonType =Importer.Import(FromCanonType);
1620 if (ToCanonType.isNull())
1621 return QualType();
1622 }
1623 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1624 ToTemplateArgs.data(),
1625 ToTemplateArgs.size(),
1626 ToCanonType);
1627}
1628
John McCallf4c73712011-01-19 06:33:43 +00001629QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001630 NestedNameSpecifier *ToQualifier = 0;
1631 // Note: the qualifier in an ElaboratedType is optional.
1632 if (T->getQualifier()) {
1633 ToQualifier = Importer.Import(T->getQualifier());
1634 if (!ToQualifier)
1635 return QualType();
1636 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001637
1638 QualType ToNamedType = Importer.Import(T->getNamedType());
1639 if (ToNamedType.isNull())
1640 return QualType();
1641
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001642 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1643 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001644}
1645
John McCallf4c73712011-01-19 06:33:43 +00001646QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001647 ObjCInterfaceDecl *Class
1648 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1649 if (!Class)
1650 return QualType();
1651
John McCallc12c5bb2010-05-15 11:32:37 +00001652 return Importer.getToContext().getObjCInterfaceType(Class);
1653}
1654
John McCallf4c73712011-01-19 06:33:43 +00001655QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001656 QualType ToBaseType = Importer.Import(T->getBaseType());
1657 if (ToBaseType.isNull())
1658 return QualType();
1659
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001660 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001661 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001662 PEnd = T->qual_end();
1663 P != PEnd; ++P) {
1664 ObjCProtocolDecl *Protocol
1665 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1666 if (!Protocol)
1667 return QualType();
1668 Protocols.push_back(Protocol);
1669 }
1670
John McCallc12c5bb2010-05-15 11:32:37 +00001671 return Importer.getToContext().getObjCObjectType(ToBaseType,
1672 Protocols.data(),
1673 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001674}
1675
John McCallf4c73712011-01-19 06:33:43 +00001676QualType
1677ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001678 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1679 if (ToPointeeType.isNull())
1680 return QualType();
1681
John McCallc12c5bb2010-05-15 11:32:37 +00001682 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001683}
1684
Douglas Gregor089459a2010-02-08 21:09:39 +00001685//----------------------------------------------------------------------------
1686// Import Declarations
1687//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001688bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1689 DeclContext *&LexicalDC,
1690 DeclarationName &Name,
1691 SourceLocation &Loc) {
1692 // Import the context of this declaration.
1693 DC = Importer.ImportContext(D->getDeclContext());
1694 if (!DC)
1695 return true;
1696
1697 LexicalDC = DC;
1698 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1699 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1700 if (!LexicalDC)
1701 return true;
1702 }
1703
1704 // Import the name of this declaration.
1705 Name = Importer.Import(D->getDeclName());
1706 if (D->getDeclName() && !Name)
1707 return true;
1708
1709 // Import the location of this declaration.
1710 Loc = Importer.Import(D->getLocation());
1711 return false;
1712}
1713
Abramo Bagnara25777432010-08-11 22:01:17 +00001714void
1715ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1716 DeclarationNameInfo& To) {
1717 // NOTE: To.Name and To.Loc are already imported.
1718 // We only have to import To.LocInfo.
1719 switch (To.getName().getNameKind()) {
1720 case DeclarationName::Identifier:
1721 case DeclarationName::ObjCZeroArgSelector:
1722 case DeclarationName::ObjCOneArgSelector:
1723 case DeclarationName::ObjCMultiArgSelector:
1724 case DeclarationName::CXXUsingDirective:
1725 return;
1726
1727 case DeclarationName::CXXOperatorName: {
1728 SourceRange Range = From.getCXXOperatorNameRange();
1729 To.setCXXOperatorNameRange(Importer.Import(Range));
1730 return;
1731 }
1732 case DeclarationName::CXXLiteralOperatorName: {
1733 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1734 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1735 return;
1736 }
1737 case DeclarationName::CXXConstructorName:
1738 case DeclarationName::CXXDestructorName:
1739 case DeclarationName::CXXConversionFunctionName: {
1740 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1741 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1742 return;
1743 }
1744 assert(0 && "Unknown name kind.");
1745 }
1746}
1747
Douglas Gregord8868a62011-01-18 03:11:38 +00001748void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1749 if (Importer.isMinimalImport() && !ForceImport) {
1750 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1751 ToDC->setHasExternalLexicalStorage();
1752 ToDC->setHasExternalVisibleStorage();
1753 }
1754 return;
1755 }
1756
Douglas Gregor083a8212010-02-21 18:24:45 +00001757 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1758 FromEnd = FromDC->decls_end();
1759 From != FromEnd;
1760 ++From)
1761 Importer.Import(*From);
1762}
1763
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001764bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1765 if (To->getDefinition())
1766 return false;
1767
1768 To->startDefinition();
1769
1770 // Add base classes.
1771 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1772 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1773
1774 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1775 for (CXXRecordDecl::base_class_iterator
1776 Base1 = FromCXX->bases_begin(),
1777 FromBaseEnd = FromCXX->bases_end();
1778 Base1 != FromBaseEnd;
1779 ++Base1) {
1780 QualType T = Importer.Import(Base1->getType());
1781 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001782 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001783
1784 SourceLocation EllipsisLoc;
1785 if (Base1->isPackExpansion())
1786 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001787
1788 Bases.push_back(
1789 new (Importer.getToContext())
1790 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1791 Base1->isVirtual(),
1792 Base1->isBaseOfClass(),
1793 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001794 Importer.Import(Base1->getTypeSourceInfo()),
1795 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001796 }
1797 if (!Bases.empty())
1798 ToCXX->setBases(Bases.data(), Bases.size());
1799 }
1800
1801 ImportDeclContext(From);
1802 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001803 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001804}
1805
Douglas Gregor040afae2010-11-30 19:14:50 +00001806TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1807 TemplateParameterList *Params) {
1808 llvm::SmallVector<NamedDecl *, 4> ToParams;
1809 ToParams.reserve(Params->size());
1810 for (TemplateParameterList::iterator P = Params->begin(),
1811 PEnd = Params->end();
1812 P != PEnd; ++P) {
1813 Decl *To = Importer.Import(*P);
1814 if (!To)
1815 return 0;
1816
1817 ToParams.push_back(cast<NamedDecl>(To));
1818 }
1819
1820 return TemplateParameterList::Create(Importer.getToContext(),
1821 Importer.Import(Params->getTemplateLoc()),
1822 Importer.Import(Params->getLAngleLoc()),
1823 ToParams.data(), ToParams.size(),
1824 Importer.Import(Params->getRAngleLoc()));
1825}
1826
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001827TemplateArgument
1828ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1829 switch (From.getKind()) {
1830 case TemplateArgument::Null:
1831 return TemplateArgument();
1832
1833 case TemplateArgument::Type: {
1834 QualType ToType = Importer.Import(From.getAsType());
1835 if (ToType.isNull())
1836 return TemplateArgument();
1837 return TemplateArgument(ToType);
1838 }
1839
1840 case TemplateArgument::Integral: {
1841 QualType ToType = Importer.Import(From.getIntegralType());
1842 if (ToType.isNull())
1843 return TemplateArgument();
1844 return TemplateArgument(*From.getAsIntegral(), ToType);
1845 }
1846
1847 case TemplateArgument::Declaration:
1848 if (Decl *To = Importer.Import(From.getAsDecl()))
1849 return TemplateArgument(To);
1850 return TemplateArgument();
1851
1852 case TemplateArgument::Template: {
1853 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1854 if (ToTemplate.isNull())
1855 return TemplateArgument();
1856
1857 return TemplateArgument(ToTemplate);
1858 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001859
1860 case TemplateArgument::TemplateExpansion: {
1861 TemplateName ToTemplate
1862 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1863 if (ToTemplate.isNull())
1864 return TemplateArgument();
1865
Douglas Gregor2be29f42011-01-14 23:41:42 +00001866 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001867 }
1868
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001869 case TemplateArgument::Expression:
1870 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1871 return TemplateArgument(ToExpr);
1872 return TemplateArgument();
1873
1874 case TemplateArgument::Pack: {
1875 llvm::SmallVector<TemplateArgument, 2> ToPack;
1876 ToPack.reserve(From.pack_size());
1877 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1878 return TemplateArgument();
1879
1880 TemplateArgument *ToArgs
1881 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1882 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1883 return TemplateArgument(ToArgs, ToPack.size());
1884 }
1885 }
1886
1887 llvm_unreachable("Invalid template argument kind");
1888 return TemplateArgument();
1889}
1890
1891bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1892 unsigned NumFromArgs,
1893 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1894 for (unsigned I = 0; I != NumFromArgs; ++I) {
1895 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1896 if (To.isNull() && !FromArgs[I].isNull())
1897 return true;
1898
1899 ToArgs.push_back(To);
1900 }
1901
1902 return false;
1903}
1904
Douglas Gregor96a01b42010-02-11 00:48:18 +00001905bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001906 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001907 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001908 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001909 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001910 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001911}
1912
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001913bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001914 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001915 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001916 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001917 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001918}
1919
Douglas Gregor040afae2010-11-30 19:14:50 +00001920bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1921 ClassTemplateDecl *To) {
1922 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1923 Importer.getToContext(),
1924 Importer.getNonEquivalentDecls());
1925 return Ctx.IsStructurallyEquivalent(From, To);
1926}
1927
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001928Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00001929 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001930 << D->getDeclKindName();
1931 return 0;
1932}
1933
Douglas Gregor788c62d2010-02-21 18:26:36 +00001934Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1935 // Import the major distinguishing characteristics of this namespace.
1936 DeclContext *DC, *LexicalDC;
1937 DeclarationName Name;
1938 SourceLocation Loc;
1939 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1940 return 0;
1941
1942 NamespaceDecl *MergeWithNamespace = 0;
1943 if (!Name) {
1944 // This is an anonymous namespace. Adopt an existing anonymous
1945 // namespace if we can.
1946 // FIXME: Not testable.
1947 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1948 MergeWithNamespace = TU->getAnonymousNamespace();
1949 else
1950 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1951 } else {
1952 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1953 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1954 Lookup.first != Lookup.second;
1955 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00001956 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00001957 continue;
1958
1959 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1960 MergeWithNamespace = FoundNS;
1961 ConflictingDecls.clear();
1962 break;
1963 }
1964
1965 ConflictingDecls.push_back(*Lookup.first);
1966 }
1967
1968 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00001969 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00001970 ConflictingDecls.data(),
1971 ConflictingDecls.size());
1972 }
1973 }
1974
1975 // Create the "to" namespace, if needed.
1976 NamespaceDecl *ToNamespace = MergeWithNamespace;
1977 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00001978 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1979 Importer.Import(D->getLocStart()),
1980 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00001981 ToNamespace->setLexicalDeclContext(LexicalDC);
1982 LexicalDC->addDecl(ToNamespace);
1983
1984 // If this is an anonymous namespace, register it as the anonymous
1985 // namespace within its context.
1986 if (!Name) {
1987 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1988 TU->setAnonymousNamespace(ToNamespace);
1989 else
1990 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1991 }
1992 }
1993 Importer.Imported(D, ToNamespace);
1994
1995 ImportDeclContext(D);
1996
1997 return ToNamespace;
1998}
1999
Richard Smith162e1c12011-04-15 14:24:37 +00002000Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002001 // Import the major distinguishing characteristics of this typedef.
2002 DeclContext *DC, *LexicalDC;
2003 DeclarationName Name;
2004 SourceLocation Loc;
2005 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2006 return 0;
2007
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002008 // If this typedef is not in block scope, determine whether we've
2009 // seen a typedef with the same name (that we can merge with) or any
2010 // other entity by that name (which name lookup could conflict with).
2011 if (!DC->isFunctionOrMethod()) {
2012 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2013 unsigned IDNS = Decl::IDNS_Ordinary;
2014 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2015 Lookup.first != Lookup.second;
2016 ++Lookup.first) {
2017 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2018 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002019 if (TypedefNameDecl *FoundTypedef =
2020 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002021 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2022 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002023 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002024 }
2025
2026 ConflictingDecls.push_back(*Lookup.first);
2027 }
2028
2029 if (!ConflictingDecls.empty()) {
2030 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2031 ConflictingDecls.data(),
2032 ConflictingDecls.size());
2033 if (!Name)
2034 return 0;
2035 }
2036 }
2037
Douglas Gregorea35d112010-02-15 23:54:17 +00002038 // Import the underlying type of this typedef;
2039 QualType T = Importer.Import(D->getUnderlyingType());
2040 if (T.isNull())
2041 return 0;
2042
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002043 // Create the new typedef node.
2044 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002045 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002046 TypedefNameDecl *ToTypedef;
2047 if (IsAlias)
2048 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2049 StartL, Loc,
2050 Name.getAsIdentifierInfo(),
2051 TInfo);
2052 else
2053 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2054 StartL, Loc,
2055 Name.getAsIdentifierInfo(),
2056 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002057 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002058 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002059 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002060 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002061
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002062 return ToTypedef;
2063}
2064
Richard Smith162e1c12011-04-15 14:24:37 +00002065Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2066 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2067}
2068
2069Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2070 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2071}
2072
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002073Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2074 // Import the major distinguishing characteristics of this enum.
2075 DeclContext *DC, *LexicalDC;
2076 DeclarationName Name;
2077 SourceLocation Loc;
2078 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2079 return 0;
2080
2081 // Figure out what enum name we're looking for.
2082 unsigned IDNS = Decl::IDNS_Tag;
2083 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002084 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2085 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002086 IDNS = Decl::IDNS_Ordinary;
2087 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2088 IDNS |= Decl::IDNS_Ordinary;
2089
2090 // We may already have an enum of the same name; try to find and match it.
2091 if (!DC->isFunctionOrMethod() && SearchName) {
2092 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2093 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2094 Lookup.first != Lookup.second;
2095 ++Lookup.first) {
2096 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2097 continue;
2098
2099 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002100 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002101 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2102 Found = Tag->getDecl();
2103 }
2104
2105 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002106 if (IsStructuralMatch(D, FoundEnum))
2107 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002108 }
2109
2110 ConflictingDecls.push_back(*Lookup.first);
2111 }
2112
2113 if (!ConflictingDecls.empty()) {
2114 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2115 ConflictingDecls.data(),
2116 ConflictingDecls.size());
2117 }
2118 }
2119
2120 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002121 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2122 Importer.Import(D->getLocStart()),
2123 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002124 D->isScoped(), D->isScopedUsingClassTag(),
2125 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002126 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002127 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002128 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002129 D2->setLexicalDeclContext(LexicalDC);
2130 Importer.Imported(D, D2);
2131 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002132
2133 // Import the integer type.
2134 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2135 if (ToIntegerType.isNull())
2136 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002137 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002138
2139 // Import the definition
2140 if (D->isDefinition()) {
2141 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2142 if (T.isNull())
2143 return 0;
2144
2145 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2146 if (ToPromotionType.isNull())
2147 return 0;
2148
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002149 D2->startDefinition();
Douglas Gregor083a8212010-02-21 18:24:45 +00002150 ImportDeclContext(D);
John McCall1b5a6182010-05-06 08:49:23 +00002151
2152 // FIXME: we might need to merge the number of positive or negative bits
2153 // if the enumerator lists don't match.
2154 D2->completeDefinition(T, ToPromotionType,
2155 D->getNumPositiveBits(),
2156 D->getNumNegativeBits());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002157 }
2158
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002159 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002160}
2161
Douglas Gregor96a01b42010-02-11 00:48:18 +00002162Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2163 // If this record has a definition in the translation unit we're coming from,
2164 // but this particular declaration is not that definition, import the
2165 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002166 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002167 if (Definition && Definition != D) {
2168 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002169 if (!ImportedDef)
2170 return 0;
2171
2172 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002173 }
2174
2175 // Import the major distinguishing characteristics of this record.
2176 DeclContext *DC, *LexicalDC;
2177 DeclarationName Name;
2178 SourceLocation Loc;
2179 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2180 return 0;
2181
2182 // Figure out what structure name we're looking for.
2183 unsigned IDNS = Decl::IDNS_Tag;
2184 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002185 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2186 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002187 IDNS = Decl::IDNS_Ordinary;
2188 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2189 IDNS |= Decl::IDNS_Ordinary;
2190
2191 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002192 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002193 if (!DC->isFunctionOrMethod() && SearchName) {
2194 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2195 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2196 Lookup.first != Lookup.second;
2197 ++Lookup.first) {
2198 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2199 continue;
2200
2201 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002202 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002203 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2204 Found = Tag->getDecl();
2205 }
2206
2207 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002208 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2209 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2210 // The record types structurally match, or the "from" translation
2211 // unit only had a forward declaration anyway; call it the same
2212 // function.
2213 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002214 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002215 }
2216 } else {
2217 // We have a forward declaration of this type, so adopt that forward
2218 // declaration rather than building a new one.
2219 AdoptDecl = FoundRecord;
2220 continue;
2221 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002222 }
2223
2224 ConflictingDecls.push_back(*Lookup.first);
2225 }
2226
2227 if (!ConflictingDecls.empty()) {
2228 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2229 ConflictingDecls.data(),
2230 ConflictingDecls.size());
2231 }
2232 }
2233
2234 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002235 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002236 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002237 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002238 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002239 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002240 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002241 DC, StartLoc, Loc,
2242 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002243 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002244 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002245 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002246 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002247 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002248 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002249
2250 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002251 D2->setLexicalDeclContext(LexicalDC);
2252 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002253 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002254
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002255 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002256
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002257 if (D->isDefinition() && ImportDefinition(D, D2))
2258 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002259
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002260 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002261}
2262
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002263Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2264 // Import the major distinguishing characteristics of this enumerator.
2265 DeclContext *DC, *LexicalDC;
2266 DeclarationName Name;
2267 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002268 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002269 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002270
2271 QualType T = Importer.Import(D->getType());
2272 if (T.isNull())
2273 return 0;
2274
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002275 // Determine whether there are any other declarations with the same name and
2276 // in the same context.
2277 if (!LexicalDC->isFunctionOrMethod()) {
2278 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2279 unsigned IDNS = Decl::IDNS_Ordinary;
2280 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2281 Lookup.first != Lookup.second;
2282 ++Lookup.first) {
2283 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2284 continue;
2285
2286 ConflictingDecls.push_back(*Lookup.first);
2287 }
2288
2289 if (!ConflictingDecls.empty()) {
2290 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2291 ConflictingDecls.data(),
2292 ConflictingDecls.size());
2293 if (!Name)
2294 return 0;
2295 }
2296 }
2297
2298 Expr *Init = Importer.Import(D->getInitExpr());
2299 if (D->getInitExpr() && !Init)
2300 return 0;
2301
2302 EnumConstantDecl *ToEnumerator
2303 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2304 Name.getAsIdentifierInfo(), T,
2305 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002306 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002307 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002308 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002309 LexicalDC->addDecl(ToEnumerator);
2310 return ToEnumerator;
2311}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002312
Douglas Gregora404ea62010-02-10 19:54:31 +00002313Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2314 // Import the major distinguishing characteristics of this function.
2315 DeclContext *DC, *LexicalDC;
2316 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002317 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002318 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002319 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002320
Douglas Gregora404ea62010-02-10 19:54:31 +00002321 // Try to find a function in our own ("to") context with the same name, same
2322 // type, and in the same context as the function we're importing.
2323 if (!LexicalDC->isFunctionOrMethod()) {
2324 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2325 unsigned IDNS = Decl::IDNS_Ordinary;
2326 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2327 Lookup.first != Lookup.second;
2328 ++Lookup.first) {
2329 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2330 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002331
Douglas Gregora404ea62010-02-10 19:54:31 +00002332 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2333 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2334 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002335 if (Importer.IsStructurallyEquivalent(D->getType(),
2336 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002337 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002338 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002339 }
2340
2341 // FIXME: Check for overloading more carefully, e.g., by boosting
2342 // Sema::IsOverload out to the AST library.
2343
2344 // Function overloading is okay in C++.
2345 if (Importer.getToContext().getLangOptions().CPlusPlus)
2346 continue;
2347
2348 // Complain about inconsistent function types.
2349 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002350 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002351 Importer.ToDiag(FoundFunction->getLocation(),
2352 diag::note_odr_value_here)
2353 << FoundFunction->getType();
2354 }
2355 }
2356
2357 ConflictingDecls.push_back(*Lookup.first);
2358 }
2359
2360 if (!ConflictingDecls.empty()) {
2361 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2362 ConflictingDecls.data(),
2363 ConflictingDecls.size());
2364 if (!Name)
2365 return 0;
2366 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002367 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002368
Abramo Bagnara25777432010-08-11 22:01:17 +00002369 DeclarationNameInfo NameInfo(Name, Loc);
2370 // Import additional name location/type info.
2371 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2372
Douglas Gregorea35d112010-02-15 23:54:17 +00002373 // Import the type.
2374 QualType T = Importer.Import(D->getType());
2375 if (T.isNull())
2376 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002377
2378 // Import the function parameters.
2379 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2380 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2381 P != PEnd; ++P) {
2382 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2383 if (!ToP)
2384 return 0;
2385
2386 Parameters.push_back(ToP);
2387 }
2388
2389 // Create the imported function.
2390 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002391 FunctionDecl *ToFunction = 0;
2392 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2393 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2394 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002395 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002396 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002397 FromConstructor->isExplicit(),
2398 D->isInlineSpecified(),
2399 D->isImplicit());
2400 } else if (isa<CXXDestructorDecl>(D)) {
2401 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2402 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002403 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002404 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002405 D->isInlineSpecified(),
2406 D->isImplicit());
2407 } else if (CXXConversionDecl *FromConversion
2408 = dyn_cast<CXXConversionDecl>(D)) {
2409 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2410 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002411 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002412 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002413 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002414 FromConversion->isExplicit(),
2415 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002416 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2417 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2418 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002419 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002420 NameInfo, T, TInfo,
2421 Method->isStatic(),
2422 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002423 Method->isInlineSpecified(),
2424 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002425 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002426 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002427 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002428 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002429 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002430 D->isInlineSpecified(),
2431 D->hasWrittenPrototype());
2432 }
John McCallb6217662010-03-15 10:12:16 +00002433
2434 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002435 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002436 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002437 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002438 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2439 ToFunction->setTrivial(D->isTrivial());
2440 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002441 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002442
Douglas Gregora404ea62010-02-10 19:54:31 +00002443 // Set the parameters.
2444 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002445 Parameters[I]->setOwningFunction(ToFunction);
2446 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002447 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002448 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002449
2450 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002451
2452 // Add this function to the lexical context.
2453 LexicalDC->addDecl(ToFunction);
2454
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002455 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002456}
2457
Douglas Gregorc144f352010-02-21 18:29:16 +00002458Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2459 return VisitFunctionDecl(D);
2460}
2461
2462Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2463 return VisitCXXMethodDecl(D);
2464}
2465
2466Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2467 return VisitCXXMethodDecl(D);
2468}
2469
2470Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2471 return VisitCXXMethodDecl(D);
2472}
2473
Douglas Gregor96a01b42010-02-11 00:48:18 +00002474Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2475 // Import the major distinguishing characteristics of a variable.
2476 DeclContext *DC, *LexicalDC;
2477 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002478 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002479 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2480 return 0;
2481
2482 // Import the type.
2483 QualType T = Importer.Import(D->getType());
2484 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002485 return 0;
2486
2487 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2488 Expr *BitWidth = Importer.Import(D->getBitWidth());
2489 if (!BitWidth && D->getBitWidth())
2490 return 0;
2491
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002492 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2493 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002494 Loc, Name.getAsIdentifierInfo(),
2495 T, TInfo, BitWidth, D->isMutable());
Douglas Gregor325bf172010-02-22 17:42:47 +00002496 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002497 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002498 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002499 LexicalDC->addDecl(ToField);
2500 return ToField;
2501}
2502
Francois Pichet87c2e122010-11-21 06:08:52 +00002503Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2504 // Import the major distinguishing characteristics of a variable.
2505 DeclContext *DC, *LexicalDC;
2506 DeclarationName Name;
2507 SourceLocation Loc;
2508 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2509 return 0;
2510
2511 // Import the type.
2512 QualType T = Importer.Import(D->getType());
2513 if (T.isNull())
2514 return 0;
2515
2516 NamedDecl **NamedChain =
2517 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2518
2519 unsigned i = 0;
2520 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2521 PE = D->chain_end(); PI != PE; ++PI) {
2522 Decl* D = Importer.Import(*PI);
2523 if (!D)
2524 return 0;
2525 NamedChain[i++] = cast<NamedDecl>(D);
2526 }
2527
2528 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2529 Importer.getToContext(), DC,
2530 Loc, Name.getAsIdentifierInfo(), T,
2531 NamedChain, D->getChainingSize());
2532 ToIndirectField->setAccess(D->getAccess());
2533 ToIndirectField->setLexicalDeclContext(LexicalDC);
2534 Importer.Imported(D, ToIndirectField);
2535 LexicalDC->addDecl(ToIndirectField);
2536 return ToIndirectField;
2537}
2538
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002539Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2540 // Import the major distinguishing characteristics of an ivar.
2541 DeclContext *DC, *LexicalDC;
2542 DeclarationName Name;
2543 SourceLocation Loc;
2544 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2545 return 0;
2546
2547 // Determine whether we've already imported this ivar
2548 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2549 Lookup.first != Lookup.second;
2550 ++Lookup.first) {
2551 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2552 if (Importer.IsStructurallyEquivalent(D->getType(),
2553 FoundIvar->getType())) {
2554 Importer.Imported(D, FoundIvar);
2555 return FoundIvar;
2556 }
2557
2558 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2559 << Name << D->getType() << FoundIvar->getType();
2560 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2561 << FoundIvar->getType();
2562 return 0;
2563 }
2564 }
2565
2566 // Import the type.
2567 QualType T = Importer.Import(D->getType());
2568 if (T.isNull())
2569 return 0;
2570
2571 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2572 Expr *BitWidth = Importer.Import(D->getBitWidth());
2573 if (!BitWidth && D->getBitWidth())
2574 return 0;
2575
Daniel Dunbara0654922010-04-02 20:10:03 +00002576 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2577 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002578 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002579 Loc, Name.getAsIdentifierInfo(),
2580 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002581 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002582 ToIvar->setLexicalDeclContext(LexicalDC);
2583 Importer.Imported(D, ToIvar);
2584 LexicalDC->addDecl(ToIvar);
2585 return ToIvar;
2586
2587}
2588
Douglas Gregora404ea62010-02-10 19:54:31 +00002589Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2590 // Import the major distinguishing characteristics of a variable.
2591 DeclContext *DC, *LexicalDC;
2592 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002593 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002594 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002595 return 0;
2596
Douglas Gregor089459a2010-02-08 21:09:39 +00002597 // Try to find a variable in our own ("to") context with the same name and
2598 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002599 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002600 VarDecl *MergeWithVar = 0;
2601 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2602 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002603 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002604 Lookup.first != Lookup.second;
2605 ++Lookup.first) {
2606 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2607 continue;
2608
2609 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2610 // We have found a variable that we may need to merge with. Check it.
2611 if (isExternalLinkage(FoundVar->getLinkage()) &&
2612 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002613 if (Importer.IsStructurallyEquivalent(D->getType(),
2614 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002615 MergeWithVar = FoundVar;
2616 break;
2617 }
2618
Douglas Gregord0145422010-02-12 17:23:39 +00002619 const ArrayType *FoundArray
2620 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2621 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002622 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002623 if (FoundArray && TArray) {
2624 if (isa<IncompleteArrayType>(FoundArray) &&
2625 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002626 // Import the type.
2627 QualType T = Importer.Import(D->getType());
2628 if (T.isNull())
2629 return 0;
2630
Douglas Gregord0145422010-02-12 17:23:39 +00002631 FoundVar->setType(T);
2632 MergeWithVar = FoundVar;
2633 break;
2634 } else if (isa<IncompleteArrayType>(TArray) &&
2635 isa<ConstantArrayType>(FoundArray)) {
2636 MergeWithVar = FoundVar;
2637 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002638 }
2639 }
2640
Douglas Gregor089459a2010-02-08 21:09:39 +00002641 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002642 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002643 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2644 << FoundVar->getType();
2645 }
2646 }
2647
2648 ConflictingDecls.push_back(*Lookup.first);
2649 }
2650
2651 if (MergeWithVar) {
2652 // An equivalent variable with external linkage has been found. Link
2653 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002654 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002655
2656 if (VarDecl *DDef = D->getDefinition()) {
2657 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2658 Importer.ToDiag(ExistingDef->getLocation(),
2659 diag::err_odr_variable_multiple_def)
2660 << Name;
2661 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2662 } else {
2663 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002664 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002665 }
2666 }
2667
2668 return MergeWithVar;
2669 }
2670
2671 if (!ConflictingDecls.empty()) {
2672 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2673 ConflictingDecls.data(),
2674 ConflictingDecls.size());
2675 if (!Name)
2676 return 0;
2677 }
2678 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002679
Douglas Gregorea35d112010-02-15 23:54:17 +00002680 // Import the type.
2681 QualType T = Importer.Import(D->getType());
2682 if (T.isNull())
2683 return 0;
2684
Douglas Gregor089459a2010-02-08 21:09:39 +00002685 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002686 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002687 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2688 Importer.Import(D->getInnerLocStart()),
2689 Loc, Name.getAsIdentifierInfo(),
2690 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002691 D->getStorageClass(),
2692 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002693 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002694 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002695 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002696 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002697 LexicalDC->addDecl(ToVar);
2698
Douglas Gregor089459a2010-02-08 21:09:39 +00002699 // Merge the initializer.
2700 // FIXME: Can we really import any initializer? Alternatively, we could force
2701 // ourselves to import every declaration of a variable and then only use
2702 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002703 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002704
2705 // FIXME: Other bits to merge?
2706
2707 return ToVar;
2708}
2709
Douglas Gregor2cd00932010-02-17 21:22:52 +00002710Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2711 // Parameters are created in the translation unit's context, then moved
2712 // into the function declaration's context afterward.
2713 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2714
2715 // Import the name of this declaration.
2716 DeclarationName Name = Importer.Import(D->getDeclName());
2717 if (D->getDeclName() && !Name)
2718 return 0;
2719
2720 // Import the location of this declaration.
2721 SourceLocation Loc = Importer.Import(D->getLocation());
2722
2723 // Import the parameter's type.
2724 QualType T = Importer.Import(D->getType());
2725 if (T.isNull())
2726 return 0;
2727
2728 // Create the imported parameter.
2729 ImplicitParamDecl *ToParm
2730 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2731 Loc, Name.getAsIdentifierInfo(),
2732 T);
2733 return Importer.Imported(D, ToParm);
2734}
2735
Douglas Gregora404ea62010-02-10 19:54:31 +00002736Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2737 // Parameters are created in the translation unit's context, then moved
2738 // into the function declaration's context afterward.
2739 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2740
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002741 // Import the name of this declaration.
2742 DeclarationName Name = Importer.Import(D->getDeclName());
2743 if (D->getDeclName() && !Name)
2744 return 0;
2745
Douglas Gregora404ea62010-02-10 19:54:31 +00002746 // Import the location of this declaration.
2747 SourceLocation Loc = Importer.Import(D->getLocation());
2748
2749 // Import the parameter's type.
2750 QualType T = Importer.Import(D->getType());
2751 if (T.isNull())
2752 return 0;
2753
2754 // Create the imported parameter.
2755 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2756 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002757 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002758 Loc, Name.getAsIdentifierInfo(),
2759 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002760 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002761 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002762 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002763 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002764}
2765
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002766Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2767 // Import the major distinguishing characteristics of a method.
2768 DeclContext *DC, *LexicalDC;
2769 DeclarationName Name;
2770 SourceLocation Loc;
2771 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2772 return 0;
2773
2774 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2775 Lookup.first != Lookup.second;
2776 ++Lookup.first) {
2777 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2778 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2779 continue;
2780
2781 // Check return types.
2782 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2783 FoundMethod->getResultType())) {
2784 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2785 << D->isInstanceMethod() << Name
2786 << D->getResultType() << FoundMethod->getResultType();
2787 Importer.ToDiag(FoundMethod->getLocation(),
2788 diag::note_odr_objc_method_here)
2789 << D->isInstanceMethod() << Name;
2790 return 0;
2791 }
2792
2793 // Check the number of parameters.
2794 if (D->param_size() != FoundMethod->param_size()) {
2795 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2796 << D->isInstanceMethod() << Name
2797 << D->param_size() << FoundMethod->param_size();
2798 Importer.ToDiag(FoundMethod->getLocation(),
2799 diag::note_odr_objc_method_here)
2800 << D->isInstanceMethod() << Name;
2801 return 0;
2802 }
2803
2804 // Check parameter types.
2805 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2806 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2807 P != PEnd; ++P, ++FoundP) {
2808 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2809 (*FoundP)->getType())) {
2810 Importer.FromDiag((*P)->getLocation(),
2811 diag::err_odr_objc_method_param_type_inconsistent)
2812 << D->isInstanceMethod() << Name
2813 << (*P)->getType() << (*FoundP)->getType();
2814 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2815 << (*FoundP)->getType();
2816 return 0;
2817 }
2818 }
2819
2820 // Check variadic/non-variadic.
2821 // Check the number of parameters.
2822 if (D->isVariadic() != FoundMethod->isVariadic()) {
2823 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2824 << D->isInstanceMethod() << Name;
2825 Importer.ToDiag(FoundMethod->getLocation(),
2826 diag::note_odr_objc_method_here)
2827 << D->isInstanceMethod() << Name;
2828 return 0;
2829 }
2830
2831 // FIXME: Any other bits we need to merge?
2832 return Importer.Imported(D, FoundMethod);
2833 }
2834 }
2835
2836 // Import the result type.
2837 QualType ResultTy = Importer.Import(D->getResultType());
2838 if (ResultTy.isNull())
2839 return 0;
2840
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002841 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2842
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002843 ObjCMethodDecl *ToMethod
2844 = ObjCMethodDecl::Create(Importer.getToContext(),
2845 Loc,
2846 Importer.Import(D->getLocEnd()),
2847 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002848 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002849 D->isInstanceMethod(),
2850 D->isVariadic(),
2851 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002852 D->isDefined(),
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002853 D->getImplementationControl());
2854
2855 // FIXME: When we decide to merge method definitions, we'll need to
2856 // deal with implicit parameters.
2857
2858 // Import the parameters
2859 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2860 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2861 FromPEnd = D->param_end();
2862 FromP != FromPEnd;
2863 ++FromP) {
2864 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2865 if (!ToP)
2866 return 0;
2867
2868 ToParams.push_back(ToP);
2869 }
2870
2871 // Set the parameters.
2872 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2873 ToParams[I]->setOwningFunction(ToMethod);
2874 ToMethod->addDecl(ToParams[I]);
2875 }
2876 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002877 ToParams.data(), ToParams.size(),
2878 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002879
2880 ToMethod->setLexicalDeclContext(LexicalDC);
2881 Importer.Imported(D, ToMethod);
2882 LexicalDC->addDecl(ToMethod);
2883 return ToMethod;
2884}
2885
Douglas Gregorb4677b62010-02-18 01:47:50 +00002886Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2887 // Import the major distinguishing characteristics of a category.
2888 DeclContext *DC, *LexicalDC;
2889 DeclarationName Name;
2890 SourceLocation Loc;
2891 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2892 return 0;
2893
2894 ObjCInterfaceDecl *ToInterface
2895 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2896 if (!ToInterface)
2897 return 0;
2898
2899 // Determine if we've already encountered this category.
2900 ObjCCategoryDecl *MergeWithCategory
2901 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2902 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2903 if (!ToCategory) {
2904 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2905 Importer.Import(D->getAtLoc()),
2906 Loc,
2907 Importer.Import(D->getCategoryNameLoc()),
2908 Name.getAsIdentifierInfo());
2909 ToCategory->setLexicalDeclContext(LexicalDC);
2910 LexicalDC->addDecl(ToCategory);
2911 Importer.Imported(D, ToCategory);
2912
2913 // Link this category into its class's category list.
2914 ToCategory->setClassInterface(ToInterface);
2915 ToCategory->insertNextClassCategory();
2916
2917 // Import protocols
2918 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2919 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2920 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2921 = D->protocol_loc_begin();
2922 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2923 FromProtoEnd = D->protocol_end();
2924 FromProto != FromProtoEnd;
2925 ++FromProto, ++FromProtoLoc) {
2926 ObjCProtocolDecl *ToProto
2927 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2928 if (!ToProto)
2929 return 0;
2930 Protocols.push_back(ToProto);
2931 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2932 }
2933
2934 // FIXME: If we're merging, make sure that the protocol list is the same.
2935 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2936 ProtocolLocs.data(), Importer.getToContext());
2937
2938 } else {
2939 Importer.Imported(D, ToCategory);
2940 }
2941
2942 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00002943 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00002944
2945 // If we have an implementation, import it as well.
2946 if (D->getImplementation()) {
2947 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00002948 = cast_or_null<ObjCCategoryImplDecl>(
2949 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00002950 if (!Impl)
2951 return 0;
2952
2953 ToCategory->setImplementation(Impl);
2954 }
2955
2956 return ToCategory;
2957}
2958
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002959Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00002960 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00002961 DeclContext *DC, *LexicalDC;
2962 DeclarationName Name;
2963 SourceLocation Loc;
2964 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2965 return 0;
2966
2967 ObjCProtocolDecl *MergeWithProtocol = 0;
2968 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2969 Lookup.first != Lookup.second;
2970 ++Lookup.first) {
2971 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2972 continue;
2973
2974 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2975 break;
2976 }
2977
2978 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2979 if (!ToProto || ToProto->isForwardDecl()) {
2980 if (!ToProto) {
2981 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2982 Name.getAsIdentifierInfo());
2983 ToProto->setForwardDecl(D->isForwardDecl());
2984 ToProto->setLexicalDeclContext(LexicalDC);
2985 LexicalDC->addDecl(ToProto);
2986 }
2987 Importer.Imported(D, ToProto);
2988
2989 // Import protocols
2990 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2991 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2992 ObjCProtocolDecl::protocol_loc_iterator
2993 FromProtoLoc = D->protocol_loc_begin();
2994 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2995 FromProtoEnd = D->protocol_end();
2996 FromProto != FromProtoEnd;
2997 ++FromProto, ++FromProtoLoc) {
2998 ObjCProtocolDecl *ToProto
2999 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3000 if (!ToProto)
3001 return 0;
3002 Protocols.push_back(ToProto);
3003 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3004 }
3005
3006 // FIXME: If we're merging, make sure that the protocol list is the same.
3007 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3008 ProtocolLocs.data(), Importer.getToContext());
3009 } else {
3010 Importer.Imported(D, ToProto);
3011 }
3012
Douglas Gregorb4677b62010-02-18 01:47:50 +00003013 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003014 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003015
3016 return ToProto;
3017}
3018
Douglas Gregora12d2942010-02-16 01:20:57 +00003019Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3020 // Import the major distinguishing characteristics of an @interface.
3021 DeclContext *DC, *LexicalDC;
3022 DeclarationName Name;
3023 SourceLocation Loc;
3024 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3025 return 0;
3026
3027 ObjCInterfaceDecl *MergeWithIface = 0;
3028 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3029 Lookup.first != Lookup.second;
3030 ++Lookup.first) {
3031 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3032 continue;
3033
3034 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3035 break;
3036 }
3037
3038 ObjCInterfaceDecl *ToIface = MergeWithIface;
3039 if (!ToIface || ToIface->isForwardDecl()) {
3040 if (!ToIface) {
3041 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3042 DC, Loc,
3043 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00003044 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00003045 D->isForwardDecl(),
3046 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003047 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003048 ToIface->setLexicalDeclContext(LexicalDC);
3049 LexicalDC->addDecl(ToIface);
3050 }
3051 Importer.Imported(D, ToIface);
3052
Douglas Gregora12d2942010-02-16 01:20:57 +00003053 if (D->getSuperClass()) {
3054 ObjCInterfaceDecl *Super
3055 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3056 if (!Super)
3057 return 0;
3058
3059 ToIface->setSuperClass(Super);
3060 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3061 }
3062
3063 // Import protocols
3064 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3065 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3066 ObjCInterfaceDecl::protocol_loc_iterator
3067 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003068
3069 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003070 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3071 FromProtoEnd = D->protocol_end();
3072 FromProto != FromProtoEnd;
3073 ++FromProto, ++FromProtoLoc) {
3074 ObjCProtocolDecl *ToProto
3075 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3076 if (!ToProto)
3077 return 0;
3078 Protocols.push_back(ToProto);
3079 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3080 }
3081
3082 // FIXME: If we're merging, make sure that the protocol list is the same.
3083 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3084 ProtocolLocs.data(), Importer.getToContext());
3085
Douglas Gregora12d2942010-02-16 01:20:57 +00003086 // Import @end range
3087 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3088 } else {
3089 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003090
3091 // Check for consistency of superclasses.
3092 DeclarationName FromSuperName, ToSuperName;
3093 if (D->getSuperClass())
3094 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3095 if (ToIface->getSuperClass())
3096 ToSuperName = ToIface->getSuperClass()->getDeclName();
3097 if (FromSuperName != ToSuperName) {
3098 Importer.ToDiag(ToIface->getLocation(),
3099 diag::err_odr_objc_superclass_inconsistent)
3100 << ToIface->getDeclName();
3101 if (ToIface->getSuperClass())
3102 Importer.ToDiag(ToIface->getSuperClassLoc(),
3103 diag::note_odr_objc_superclass)
3104 << ToIface->getSuperClass()->getDeclName();
3105 else
3106 Importer.ToDiag(ToIface->getLocation(),
3107 diag::note_odr_objc_missing_superclass);
3108 if (D->getSuperClass())
3109 Importer.FromDiag(D->getSuperClassLoc(),
3110 diag::note_odr_objc_superclass)
3111 << D->getSuperClass()->getDeclName();
3112 else
3113 Importer.FromDiag(D->getLocation(),
3114 diag::note_odr_objc_missing_superclass);
3115 return 0;
3116 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003117 }
3118
Douglas Gregorb4677b62010-02-18 01:47:50 +00003119 // Import categories. When the categories themselves are imported, they'll
3120 // hook themselves into this interface.
3121 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3122 FromCat = FromCat->getNextClassCategory())
3123 Importer.Import(FromCat);
3124
Douglas Gregora12d2942010-02-16 01:20:57 +00003125 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003126 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003127
3128 // If we have an @implementation, import it as well.
3129 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003130 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3131 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003132 if (!Impl)
3133 return 0;
3134
3135 ToIface->setImplementation(Impl);
3136 }
3137
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003138 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003139}
3140
Douglas Gregor3daef292010-12-07 15:32:12 +00003141Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3142 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3143 Importer.Import(D->getCategoryDecl()));
3144 if (!Category)
3145 return 0;
3146
3147 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3148 if (!ToImpl) {
3149 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3150 if (!DC)
3151 return 0;
3152
3153 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3154 Importer.Import(D->getLocation()),
3155 Importer.Import(D->getIdentifier()),
3156 Category->getClassInterface());
3157
3158 DeclContext *LexicalDC = DC;
3159 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3160 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3161 if (!LexicalDC)
3162 return 0;
3163
3164 ToImpl->setLexicalDeclContext(LexicalDC);
3165 }
3166
3167 LexicalDC->addDecl(ToImpl);
3168 Category->setImplementation(ToImpl);
3169 }
3170
3171 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003172 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003173 return ToImpl;
3174}
3175
Douglas Gregordd182ff2010-12-07 01:26:03 +00003176Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3177 // Find the corresponding interface.
3178 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3179 Importer.Import(D->getClassInterface()));
3180 if (!Iface)
3181 return 0;
3182
3183 // Import the superclass, if any.
3184 ObjCInterfaceDecl *Super = 0;
3185 if (D->getSuperClass()) {
3186 Super = cast_or_null<ObjCInterfaceDecl>(
3187 Importer.Import(D->getSuperClass()));
3188 if (!Super)
3189 return 0;
3190 }
3191
3192 ObjCImplementationDecl *Impl = Iface->getImplementation();
3193 if (!Impl) {
3194 // We haven't imported an implementation yet. Create a new @implementation
3195 // now.
3196 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3197 Importer.ImportContext(D->getDeclContext()),
3198 Importer.Import(D->getLocation()),
3199 Iface, Super);
3200
3201 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3202 DeclContext *LexicalDC
3203 = Importer.ImportContext(D->getLexicalDeclContext());
3204 if (!LexicalDC)
3205 return 0;
3206 Impl->setLexicalDeclContext(LexicalDC);
3207 }
3208
3209 // Associate the implementation with the class it implements.
3210 Iface->setImplementation(Impl);
3211 Importer.Imported(D, Iface->getImplementation());
3212 } else {
3213 Importer.Imported(D, Iface->getImplementation());
3214
3215 // Verify that the existing @implementation has the same superclass.
3216 if ((Super && !Impl->getSuperClass()) ||
3217 (!Super && Impl->getSuperClass()) ||
3218 (Super && Impl->getSuperClass() &&
3219 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3220 Importer.ToDiag(Impl->getLocation(),
3221 diag::err_odr_objc_superclass_inconsistent)
3222 << Iface->getDeclName();
3223 // FIXME: It would be nice to have the location of the superclass
3224 // below.
3225 if (Impl->getSuperClass())
3226 Importer.ToDiag(Impl->getLocation(),
3227 diag::note_odr_objc_superclass)
3228 << Impl->getSuperClass()->getDeclName();
3229 else
3230 Importer.ToDiag(Impl->getLocation(),
3231 diag::note_odr_objc_missing_superclass);
3232 if (D->getSuperClass())
3233 Importer.FromDiag(D->getLocation(),
3234 diag::note_odr_objc_superclass)
3235 << D->getSuperClass()->getDeclName();
3236 else
3237 Importer.FromDiag(D->getLocation(),
3238 diag::note_odr_objc_missing_superclass);
3239 return 0;
3240 }
3241 }
3242
3243 // Import all of the members of this @implementation.
3244 ImportDeclContext(D);
3245
3246 return Impl;
3247}
3248
Douglas Gregore3261622010-02-17 18:02:10 +00003249Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3250 // Import the major distinguishing characteristics of an @property.
3251 DeclContext *DC, *LexicalDC;
3252 DeclarationName Name;
3253 SourceLocation Loc;
3254 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3255 return 0;
3256
3257 // Check whether we have already imported this property.
3258 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3259 Lookup.first != Lookup.second;
3260 ++Lookup.first) {
3261 if (ObjCPropertyDecl *FoundProp
3262 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3263 // Check property types.
3264 if (!Importer.IsStructurallyEquivalent(D->getType(),
3265 FoundProp->getType())) {
3266 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3267 << Name << D->getType() << FoundProp->getType();
3268 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3269 << FoundProp->getType();
3270 return 0;
3271 }
3272
3273 // FIXME: Check property attributes, getters, setters, etc.?
3274
3275 // Consider these properties to be equivalent.
3276 Importer.Imported(D, FoundProp);
3277 return FoundProp;
3278 }
3279 }
3280
3281 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003282 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3283 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003284 return 0;
3285
3286 // Create the new property.
3287 ObjCPropertyDecl *ToProperty
3288 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3289 Name.getAsIdentifierInfo(),
3290 Importer.Import(D->getAtLoc()),
3291 T,
3292 D->getPropertyImplementation());
3293 Importer.Imported(D, ToProperty);
3294 ToProperty->setLexicalDeclContext(LexicalDC);
3295 LexicalDC->addDecl(ToProperty);
3296
3297 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003298 ToProperty->setPropertyAttributesAsWritten(
3299 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003300 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3301 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3302 ToProperty->setGetterMethodDecl(
3303 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3304 ToProperty->setSetterMethodDecl(
3305 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3306 ToProperty->setPropertyIvarDecl(
3307 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3308 return ToProperty;
3309}
3310
Douglas Gregor954e0c72010-12-07 18:32:03 +00003311Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3312 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3313 Importer.Import(D->getPropertyDecl()));
3314 if (!Property)
3315 return 0;
3316
3317 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3318 if (!DC)
3319 return 0;
3320
3321 // Import the lexical declaration context.
3322 DeclContext *LexicalDC = DC;
3323 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3324 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3325 if (!LexicalDC)
3326 return 0;
3327 }
3328
3329 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3330 if (!InImpl)
3331 return 0;
3332
3333 // Import the ivar (for an @synthesize).
3334 ObjCIvarDecl *Ivar = 0;
3335 if (D->getPropertyIvarDecl()) {
3336 Ivar = cast_or_null<ObjCIvarDecl>(
3337 Importer.Import(D->getPropertyIvarDecl()));
3338 if (!Ivar)
3339 return 0;
3340 }
3341
3342 ObjCPropertyImplDecl *ToImpl
3343 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3344 if (!ToImpl) {
3345 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3346 Importer.Import(D->getLocStart()),
3347 Importer.Import(D->getLocation()),
3348 Property,
3349 D->getPropertyImplementation(),
3350 Ivar,
3351 Importer.Import(D->getPropertyIvarDeclLoc()));
3352 ToImpl->setLexicalDeclContext(LexicalDC);
3353 Importer.Imported(D, ToImpl);
3354 LexicalDC->addDecl(ToImpl);
3355 } else {
3356 // Check that we have the same kind of property implementation (@synthesize
3357 // vs. @dynamic).
3358 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3359 Importer.ToDiag(ToImpl->getLocation(),
3360 diag::err_odr_objc_property_impl_kind_inconsistent)
3361 << Property->getDeclName()
3362 << (ToImpl->getPropertyImplementation()
3363 == ObjCPropertyImplDecl::Dynamic);
3364 Importer.FromDiag(D->getLocation(),
3365 diag::note_odr_objc_property_impl_kind)
3366 << D->getPropertyDecl()->getDeclName()
3367 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3368 return 0;
3369 }
3370
3371 // For @synthesize, check that we have the same
3372 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3373 Ivar != ToImpl->getPropertyIvarDecl()) {
3374 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3375 diag::err_odr_objc_synthesize_ivar_inconsistent)
3376 << Property->getDeclName()
3377 << ToImpl->getPropertyIvarDecl()->getDeclName()
3378 << Ivar->getDeclName();
3379 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3380 diag::note_odr_objc_synthesize_ivar_here)
3381 << D->getPropertyIvarDecl()->getDeclName();
3382 return 0;
3383 }
3384
3385 // Merge the existing implementation with the new implementation.
3386 Importer.Imported(D, ToImpl);
3387 }
3388
3389 return ToImpl;
3390}
3391
Douglas Gregor2b785022010-02-18 02:12:22 +00003392Decl *
3393ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3394 // Import the context of this declaration.
3395 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3396 if (!DC)
3397 return 0;
3398
3399 DeclContext *LexicalDC = DC;
3400 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3401 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3402 if (!LexicalDC)
3403 return 0;
3404 }
3405
3406 // Import the location of this declaration.
3407 SourceLocation Loc = Importer.Import(D->getLocation());
3408
3409 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3410 llvm::SmallVector<SourceLocation, 4> Locations;
3411 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3412 = D->protocol_loc_begin();
3413 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3414 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3415 FromProto != FromProtoEnd;
3416 ++FromProto, ++FromProtoLoc) {
3417 ObjCProtocolDecl *ToProto
3418 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3419 if (!ToProto)
3420 continue;
3421
3422 Protocols.push_back(ToProto);
3423 Locations.push_back(Importer.Import(*FromProtoLoc));
3424 }
3425
3426 ObjCForwardProtocolDecl *ToForward
3427 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3428 Protocols.data(), Protocols.size(),
3429 Locations.data());
3430 ToForward->setLexicalDeclContext(LexicalDC);
3431 LexicalDC->addDecl(ToForward);
3432 Importer.Imported(D, ToForward);
3433 return ToForward;
3434}
3435
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003436Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3437 // Import the context of this declaration.
3438 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3439 if (!DC)
3440 return 0;
3441
3442 DeclContext *LexicalDC = DC;
3443 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3444 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3445 if (!LexicalDC)
3446 return 0;
3447 }
3448
3449 // Import the location of this declaration.
3450 SourceLocation Loc = Importer.Import(D->getLocation());
3451
3452 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3453 llvm::SmallVector<SourceLocation, 4> Locations;
3454 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3455 From != FromEnd; ++From) {
3456 ObjCInterfaceDecl *ToIface
3457 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3458 if (!ToIface)
3459 continue;
3460
3461 Interfaces.push_back(ToIface);
3462 Locations.push_back(Importer.Import(From->getLocation()));
3463 }
3464
3465 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3466 Loc,
3467 Interfaces.data(),
3468 Locations.data(),
3469 Interfaces.size());
3470 ToClass->setLexicalDeclContext(LexicalDC);
3471 LexicalDC->addDecl(ToClass);
3472 Importer.Imported(D, ToClass);
3473 return ToClass;
3474}
3475
Douglas Gregor040afae2010-11-30 19:14:50 +00003476Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3477 // For template arguments, we adopt the translation unit as our declaration
3478 // context. This context will be fixed when the actual template declaration
3479 // is created.
3480
3481 // FIXME: Import default argument.
3482 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3483 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003484 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003485 Importer.Import(D->getLocation()),
3486 D->getDepth(),
3487 D->getIndex(),
3488 Importer.Import(D->getIdentifier()),
3489 D->wasDeclaredWithTypename(),
3490 D->isParameterPack());
3491}
3492
3493Decl *
3494ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3495 // Import the name of this declaration.
3496 DeclarationName Name = Importer.Import(D->getDeclName());
3497 if (D->getDeclName() && !Name)
3498 return 0;
3499
3500 // Import the location of this declaration.
3501 SourceLocation Loc = Importer.Import(D->getLocation());
3502
3503 // Import the type of this declaration.
3504 QualType T = Importer.Import(D->getType());
3505 if (T.isNull())
3506 return 0;
3507
3508 // Import type-source information.
3509 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3510 if (D->getTypeSourceInfo() && !TInfo)
3511 return 0;
3512
3513 // FIXME: Import default argument.
3514
3515 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3516 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003517 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003518 Loc, D->getDepth(), D->getPosition(),
3519 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003520 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003521}
3522
3523Decl *
3524ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3525 // Import the name of this declaration.
3526 DeclarationName Name = Importer.Import(D->getDeclName());
3527 if (D->getDeclName() && !Name)
3528 return 0;
3529
3530 // Import the location of this declaration.
3531 SourceLocation Loc = Importer.Import(D->getLocation());
3532
3533 // Import template parameters.
3534 TemplateParameterList *TemplateParams
3535 = ImportTemplateParameterList(D->getTemplateParameters());
3536 if (!TemplateParams)
3537 return 0;
3538
3539 // FIXME: Import default argument.
3540
3541 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3542 Importer.getToContext().getTranslationUnitDecl(),
3543 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003544 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003545 Name.getAsIdentifierInfo(),
3546 TemplateParams);
3547}
3548
3549Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3550 // If this record has a definition in the translation unit we're coming from,
3551 // but this particular declaration is not that definition, import the
3552 // definition and map to that.
3553 CXXRecordDecl *Definition
3554 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3555 if (Definition && Definition != D->getTemplatedDecl()) {
3556 Decl *ImportedDef
3557 = Importer.Import(Definition->getDescribedClassTemplate());
3558 if (!ImportedDef)
3559 return 0;
3560
3561 return Importer.Imported(D, ImportedDef);
3562 }
3563
3564 // Import the major distinguishing characteristics of this class template.
3565 DeclContext *DC, *LexicalDC;
3566 DeclarationName Name;
3567 SourceLocation Loc;
3568 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3569 return 0;
3570
3571 // We may already have a template of the same name; try to find and match it.
3572 if (!DC->isFunctionOrMethod()) {
3573 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3574 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3575 Lookup.first != Lookup.second;
3576 ++Lookup.first) {
3577 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3578 continue;
3579
3580 Decl *Found = *Lookup.first;
3581 if (ClassTemplateDecl *FoundTemplate
3582 = dyn_cast<ClassTemplateDecl>(Found)) {
3583 if (IsStructuralMatch(D, FoundTemplate)) {
3584 // The class templates structurally match; call it the same template.
3585 // FIXME: We may be filling in a forward declaration here. Handle
3586 // this case!
3587 Importer.Imported(D->getTemplatedDecl(),
3588 FoundTemplate->getTemplatedDecl());
3589 return Importer.Imported(D, FoundTemplate);
3590 }
3591 }
3592
3593 ConflictingDecls.push_back(*Lookup.first);
3594 }
3595
3596 if (!ConflictingDecls.empty()) {
3597 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3598 ConflictingDecls.data(),
3599 ConflictingDecls.size());
3600 }
3601
3602 if (!Name)
3603 return 0;
3604 }
3605
3606 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3607
3608 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003609 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3610 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003611 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3612 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003613 DC, StartLoc, IdLoc,
3614 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003615 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003616 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003617 D2Templated->setLexicalDeclContext(LexicalDC);
3618
3619 // Create the class template declaration itself.
3620 TemplateParameterList *TemplateParams
3621 = ImportTemplateParameterList(D->getTemplateParameters());
3622 if (!TemplateParams)
3623 return 0;
3624
3625 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3626 Loc, Name, TemplateParams,
3627 D2Templated,
3628 /*PrevDecl=*/0);
3629 D2Templated->setDescribedClassTemplate(D2);
3630
3631 D2->setAccess(D->getAccess());
3632 D2->setLexicalDeclContext(LexicalDC);
3633 LexicalDC->addDecl(D2);
3634
3635 // Note the relationship between the class templates.
3636 Importer.Imported(D, D2);
3637 Importer.Imported(DTemplated, D2Templated);
3638
3639 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3640 // FIXME: Import definition!
3641 }
3642
3643 return D2;
3644}
3645
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003646Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3647 ClassTemplateSpecializationDecl *D) {
3648 // If this record has a definition in the translation unit we're coming from,
3649 // but this particular declaration is not that definition, import the
3650 // definition and map to that.
3651 TagDecl *Definition = D->getDefinition();
3652 if (Definition && Definition != D) {
3653 Decl *ImportedDef = Importer.Import(Definition);
3654 if (!ImportedDef)
3655 return 0;
3656
3657 return Importer.Imported(D, ImportedDef);
3658 }
3659
3660 ClassTemplateDecl *ClassTemplate
3661 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3662 D->getSpecializedTemplate()));
3663 if (!ClassTemplate)
3664 return 0;
3665
3666 // Import the context of this declaration.
3667 DeclContext *DC = ClassTemplate->getDeclContext();
3668 if (!DC)
3669 return 0;
3670
3671 DeclContext *LexicalDC = DC;
3672 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3673 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3674 if (!LexicalDC)
3675 return 0;
3676 }
3677
3678 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003679 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3680 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003681
3682 // Import template arguments.
3683 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3684 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3685 D->getTemplateArgs().size(),
3686 TemplateArgs))
3687 return 0;
3688
3689 // Try to find an existing specialization with these template arguments.
3690 void *InsertPos = 0;
3691 ClassTemplateSpecializationDecl *D2
3692 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3693 TemplateArgs.size(), InsertPos);
3694 if (D2) {
3695 // We already have a class template specialization with these template
3696 // arguments.
3697
3698 // FIXME: Check for specialization vs. instantiation errors.
3699
3700 if (RecordDecl *FoundDef = D2->getDefinition()) {
3701 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3702 // The record types structurally match, or the "from" translation
3703 // unit only had a forward declaration anyway; call it the same
3704 // function.
3705 return Importer.Imported(D, FoundDef);
3706 }
3707 }
3708 } else {
3709 // Create a new specialization.
3710 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3711 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003712 StartLoc, IdLoc,
3713 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003714 TemplateArgs.data(),
3715 TemplateArgs.size(),
3716 /*PrevDecl=*/0);
3717 D2->setSpecializationKind(D->getSpecializationKind());
3718
3719 // Add this specialization to the class template.
3720 ClassTemplate->AddSpecialization(D2, InsertPos);
3721
3722 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003723 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003724
3725 // Add the specialization to this context.
3726 D2->setLexicalDeclContext(LexicalDC);
3727 LexicalDC->addDecl(D2);
3728 }
3729 Importer.Imported(D, D2);
3730
3731 if (D->isDefinition() && ImportDefinition(D, D2))
3732 return 0;
3733
3734 return D2;
3735}
3736
Douglas Gregor4800d952010-02-11 19:21:55 +00003737//----------------------------------------------------------------------------
3738// Import Statements
3739//----------------------------------------------------------------------------
3740
3741Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3742 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3743 << S->getStmtClassName();
3744 return 0;
3745}
3746
3747//----------------------------------------------------------------------------
3748// Import Expressions
3749//----------------------------------------------------------------------------
3750Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3751 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3752 << E->getStmtClassName();
3753 return 0;
3754}
3755
Douglas Gregor44080632010-02-19 01:17:02 +00003756Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3757 NestedNameSpecifier *Qualifier = 0;
3758 if (E->getQualifier()) {
3759 Qualifier = Importer.Import(E->getQualifier());
3760 if (!E->getQualifier())
3761 return 0;
3762 }
3763
3764 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3765 if (!ToD)
3766 return 0;
3767
3768 QualType T = Importer.Import(E->getType());
3769 if (T.isNull())
3770 return 0;
3771
Douglas Gregor40d96a62011-02-28 21:54:11 +00003772 return DeclRefExpr::Create(Importer.getToContext(),
3773 Importer.Import(E->getQualifierLoc()),
Douglas Gregor44080632010-02-19 01:17:02 +00003774 ToD,
3775 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003776 T, E->getValueKind(),
Douglas Gregor44080632010-02-19 01:17:02 +00003777 /*FIXME:TemplateArgs=*/0);
3778}
3779
Douglas Gregor4800d952010-02-11 19:21:55 +00003780Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3781 QualType T = Importer.Import(E->getType());
3782 if (T.isNull())
3783 return 0;
3784
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003785 return IntegerLiteral::Create(Importer.getToContext(),
3786 E->getValue(), T,
3787 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003788}
3789
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003790Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3791 QualType T = Importer.Import(E->getType());
3792 if (T.isNull())
3793 return 0;
3794
3795 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3796 E->isWide(), T,
3797 Importer.Import(E->getLocation()));
3798}
3799
Douglas Gregorf638f952010-02-19 01:07:06 +00003800Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3801 Expr *SubExpr = Importer.Import(E->getSubExpr());
3802 if (!SubExpr)
3803 return 0;
3804
3805 return new (Importer.getToContext())
3806 ParenExpr(Importer.Import(E->getLParen()),
3807 Importer.Import(E->getRParen()),
3808 SubExpr);
3809}
3810
3811Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3812 QualType T = Importer.Import(E->getType());
3813 if (T.isNull())
3814 return 0;
3815
3816 Expr *SubExpr = Importer.Import(E->getSubExpr());
3817 if (!SubExpr)
3818 return 0;
3819
3820 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003821 T, E->getValueKind(),
3822 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003823 Importer.Import(E->getOperatorLoc()));
3824}
3825
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003826Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3827 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003828 QualType ResultType = Importer.Import(E->getType());
3829
3830 if (E->isArgumentType()) {
3831 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3832 if (!TInfo)
3833 return 0;
3834
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003835 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3836 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003837 Importer.Import(E->getOperatorLoc()),
3838 Importer.Import(E->getRParenLoc()));
3839 }
3840
3841 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3842 if (!SubExpr)
3843 return 0;
3844
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003845 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3846 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003847 Importer.Import(E->getOperatorLoc()),
3848 Importer.Import(E->getRParenLoc()));
3849}
3850
Douglas Gregorf638f952010-02-19 01:07:06 +00003851Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3852 QualType T = Importer.Import(E->getType());
3853 if (T.isNull())
3854 return 0;
3855
3856 Expr *LHS = Importer.Import(E->getLHS());
3857 if (!LHS)
3858 return 0;
3859
3860 Expr *RHS = Importer.Import(E->getRHS());
3861 if (!RHS)
3862 return 0;
3863
3864 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003865 T, E->getValueKind(),
3866 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003867 Importer.Import(E->getOperatorLoc()));
3868}
3869
3870Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3871 QualType T = Importer.Import(E->getType());
3872 if (T.isNull())
3873 return 0;
3874
3875 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3876 if (CompLHSType.isNull())
3877 return 0;
3878
3879 QualType CompResultType = Importer.Import(E->getComputationResultType());
3880 if (CompResultType.isNull())
3881 return 0;
3882
3883 Expr *LHS = Importer.Import(E->getLHS());
3884 if (!LHS)
3885 return 0;
3886
3887 Expr *RHS = Importer.Import(E->getRHS());
3888 if (!RHS)
3889 return 0;
3890
3891 return new (Importer.getToContext())
3892 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003893 T, E->getValueKind(),
3894 E->getObjectKind(),
3895 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003896 Importer.Import(E->getOperatorLoc()));
3897}
3898
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003899static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003900 if (E->path_empty()) return false;
3901
3902 // TODO: import cast paths
3903 return true;
3904}
3905
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003906Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3907 QualType T = Importer.Import(E->getType());
3908 if (T.isNull())
3909 return 0;
3910
3911 Expr *SubExpr = Importer.Import(E->getSubExpr());
3912 if (!SubExpr)
3913 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003914
3915 CXXCastPath BasePath;
3916 if (ImportCastPath(E, BasePath))
3917 return 0;
3918
3919 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003920 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003921}
3922
Douglas Gregor008847a2010-02-19 01:32:14 +00003923Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3924 QualType T = Importer.Import(E->getType());
3925 if (T.isNull())
3926 return 0;
3927
3928 Expr *SubExpr = Importer.Import(E->getSubExpr());
3929 if (!SubExpr)
3930 return 0;
3931
3932 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3933 if (!TInfo && E->getTypeInfoAsWritten())
3934 return 0;
3935
John McCallf871d0c2010-08-07 06:22:56 +00003936 CXXCastPath BasePath;
3937 if (ImportCastPath(E, BasePath))
3938 return 0;
3939
John McCallf89e55a2010-11-18 06:31:45 +00003940 return CStyleCastExpr::Create(Importer.getToContext(), T,
3941 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00003942 SubExpr, &BasePath, TInfo,
3943 Importer.Import(E->getLParenLoc()),
3944 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00003945}
3946
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00003947ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00003948 ASTContext &FromContext, FileManager &FromFileManager,
3949 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003950 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00003951 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3952 Minimal(MinimalImport)
3953{
Douglas Gregor9bed8792010-02-09 19:21:46 +00003954 ImportedDecls[FromContext.getTranslationUnitDecl()]
3955 = ToContext.getTranslationUnitDecl();
3956}
3957
3958ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003959
3960QualType ASTImporter::Import(QualType FromT) {
3961 if (FromT.isNull())
3962 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00003963
3964 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003965
Douglas Gregor169fba52010-02-08 15:18:58 +00003966 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00003967 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3968 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00003969 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00003970 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003971
Douglas Gregor169fba52010-02-08 15:18:58 +00003972 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003973 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00003974 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003975 if (ToT.isNull())
3976 return ToT;
3977
Douglas Gregor169fba52010-02-08 15:18:58 +00003978 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00003979 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00003980
John McCallf4c73712011-01-19 06:33:43 +00003981 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00003982}
3983
Douglas Gregor9bed8792010-02-09 19:21:46 +00003984TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003985 if (!FromTSI)
3986 return FromTSI;
3987
3988 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00003989 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00003990 QualType T = Import(FromTSI->getType());
3991 if (T.isNull())
3992 return 0;
3993
3994 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00003995 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00003996}
3997
3998Decl *ASTImporter::Import(Decl *FromD) {
3999 if (!FromD)
4000 return 0;
4001
4002 // Check whether we've already imported this declaration.
4003 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
4004 if (Pos != ImportedDecls.end())
4005 return Pos->second;
4006
4007 // Import the type
4008 ASTNodeImporter Importer(*this);
4009 Decl *ToD = Importer.Visit(FromD);
4010 if (!ToD)
4011 return 0;
4012
4013 // Record the imported declaration.
4014 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004015
4016 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4017 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004018 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004019 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004020 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004021 // When we've finished transforming a typedef, see whether it was the
4022 // typedef for an anonymous tag.
4023 for (llvm::SmallVector<TagDecl *, 4>::iterator
4024 FromTag = AnonTagsWithPendingTypedefs.begin(),
4025 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4026 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004027 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004028 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4029 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004030 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004031 AnonTagsWithPendingTypedefs.erase(FromTag);
4032 break;
4033 }
4034 }
4035 }
4036 }
4037
Douglas Gregor9bed8792010-02-09 19:21:46 +00004038 return ToD;
4039}
4040
4041DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4042 if (!FromDC)
4043 return FromDC;
4044
4045 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4046}
4047
4048Expr *ASTImporter::Import(Expr *FromE) {
4049 if (!FromE)
4050 return 0;
4051
4052 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4053}
4054
4055Stmt *ASTImporter::Import(Stmt *FromS) {
4056 if (!FromS)
4057 return 0;
4058
Douglas Gregor4800d952010-02-11 19:21:55 +00004059 // Check whether we've already imported this declaration.
4060 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4061 if (Pos != ImportedStmts.end())
4062 return Pos->second;
4063
4064 // Import the type
4065 ASTNodeImporter Importer(*this);
4066 Stmt *ToS = Importer.Visit(FromS);
4067 if (!ToS)
4068 return 0;
4069
4070 // Record the imported declaration.
4071 ImportedStmts[FromS] = ToS;
4072 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004073}
4074
4075NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4076 if (!FromNNS)
4077 return 0;
4078
4079 // FIXME: Implement!
4080 return 0;
4081}
4082
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004083NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4084 // FIXME: Implement!
4085 return NestedNameSpecifierLoc();
4086}
4087
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004088TemplateName ASTImporter::Import(TemplateName From) {
4089 switch (From.getKind()) {
4090 case TemplateName::Template:
4091 if (TemplateDecl *ToTemplate
4092 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4093 return TemplateName(ToTemplate);
4094
4095 return TemplateName();
4096
4097 case TemplateName::OverloadedTemplate: {
4098 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4099 UnresolvedSet<2> ToTemplates;
4100 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4101 E = FromStorage->end();
4102 I != E; ++I) {
4103 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4104 ToTemplates.addDecl(To);
4105 else
4106 return TemplateName();
4107 }
4108 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4109 ToTemplates.end());
4110 }
4111
4112 case TemplateName::QualifiedTemplate: {
4113 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4114 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4115 if (!Qualifier)
4116 return TemplateName();
4117
4118 if (TemplateDecl *ToTemplate
4119 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4120 return ToContext.getQualifiedTemplateName(Qualifier,
4121 QTN->hasTemplateKeyword(),
4122 ToTemplate);
4123
4124 return TemplateName();
4125 }
4126
4127 case TemplateName::DependentTemplate: {
4128 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4129 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4130 if (!Qualifier)
4131 return TemplateName();
4132
4133 if (DTN->isIdentifier()) {
4134 return ToContext.getDependentTemplateName(Qualifier,
4135 Import(DTN->getIdentifier()));
4136 }
4137
4138 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4139 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004140
4141 case TemplateName::SubstTemplateTemplateParmPack: {
4142 SubstTemplateTemplateParmPackStorage *SubstPack
4143 = From.getAsSubstTemplateTemplateParmPack();
4144 TemplateTemplateParmDecl *Param
4145 = cast_or_null<TemplateTemplateParmDecl>(
4146 Import(SubstPack->getParameterPack()));
4147 if (!Param)
4148 return TemplateName();
4149
4150 ASTNodeImporter Importer(*this);
4151 TemplateArgument ArgPack
4152 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4153 if (ArgPack.isNull())
4154 return TemplateName();
4155
4156 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4157 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004158 }
4159
4160 llvm_unreachable("Invalid template name kind");
4161 return TemplateName();
4162}
4163
Douglas Gregor9bed8792010-02-09 19:21:46 +00004164SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4165 if (FromLoc.isInvalid())
4166 return SourceLocation();
4167
Douglas Gregor88523732010-02-10 00:15:17 +00004168 SourceManager &FromSM = FromContext.getSourceManager();
4169
4170 // For now, map everything down to its spelling location, so that we
4171 // don't have to import macro instantiations.
4172 // FIXME: Import macro instantiations!
4173 FromLoc = FromSM.getSpellingLoc(FromLoc);
4174 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4175 SourceManager &ToSM = ToContext.getSourceManager();
4176 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4177 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004178}
4179
4180SourceRange ASTImporter::Import(SourceRange FromRange) {
4181 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4182}
4183
Douglas Gregor88523732010-02-10 00:15:17 +00004184FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004185 llvm::DenseMap<FileID, FileID>::iterator Pos
4186 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004187 if (Pos != ImportedFileIDs.end())
4188 return Pos->second;
4189
4190 SourceManager &FromSM = FromContext.getSourceManager();
4191 SourceManager &ToSM = ToContext.getSourceManager();
4192 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4193 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4194
4195 // Include location of this file.
4196 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4197
4198 // Map the FileID for to the "to" source manager.
4199 FileID ToID;
4200 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004201 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004202 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4203 // disk again
4204 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4205 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004206 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004207 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4208 FromSLoc.getFile().getFileCharacteristic());
4209 } else {
4210 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004211 const llvm::MemoryBuffer *
4212 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004213 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004214 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004215 FromBuf->getBufferIdentifier());
4216 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4217 }
4218
4219
Sebastian Redl535a3e22010-09-30 01:03:06 +00004220 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004221 return ToID;
4222}
4223
Douglas Gregord8868a62011-01-18 03:11:38 +00004224void ASTImporter::ImportDefinition(Decl *From) {
4225 Decl *To = Import(From);
4226 if (!To)
4227 return;
4228
4229 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4230 ASTNodeImporter Importer(*this);
4231 Importer.ImportDeclContext(FromDC, true);
4232 }
4233}
4234
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004235DeclarationName ASTImporter::Import(DeclarationName FromName) {
4236 if (!FromName)
4237 return DeclarationName();
4238
4239 switch (FromName.getNameKind()) {
4240 case DeclarationName::Identifier:
4241 return Import(FromName.getAsIdentifierInfo());
4242
4243 case DeclarationName::ObjCZeroArgSelector:
4244 case DeclarationName::ObjCOneArgSelector:
4245 case DeclarationName::ObjCMultiArgSelector:
4246 return Import(FromName.getObjCSelector());
4247
4248 case DeclarationName::CXXConstructorName: {
4249 QualType T = Import(FromName.getCXXNameType());
4250 if (T.isNull())
4251 return DeclarationName();
4252
4253 return ToContext.DeclarationNames.getCXXConstructorName(
4254 ToContext.getCanonicalType(T));
4255 }
4256
4257 case DeclarationName::CXXDestructorName: {
4258 QualType T = Import(FromName.getCXXNameType());
4259 if (T.isNull())
4260 return DeclarationName();
4261
4262 return ToContext.DeclarationNames.getCXXDestructorName(
4263 ToContext.getCanonicalType(T));
4264 }
4265
4266 case DeclarationName::CXXConversionFunctionName: {
4267 QualType T = Import(FromName.getCXXNameType());
4268 if (T.isNull())
4269 return DeclarationName();
4270
4271 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4272 ToContext.getCanonicalType(T));
4273 }
4274
4275 case DeclarationName::CXXOperatorName:
4276 return ToContext.DeclarationNames.getCXXOperatorName(
4277 FromName.getCXXOverloadedOperator());
4278
4279 case DeclarationName::CXXLiteralOperatorName:
4280 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4281 Import(FromName.getCXXLiteralIdentifier()));
4282
4283 case DeclarationName::CXXUsingDirective:
4284 // FIXME: STATICS!
4285 return DeclarationName::getUsingDirectiveName();
4286 }
4287
4288 // Silence bogus GCC warning
4289 return DeclarationName();
4290}
4291
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004292IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004293 if (!FromId)
4294 return 0;
4295
4296 return &ToContext.Idents.get(FromId->getName());
4297}
Douglas Gregor089459a2010-02-08 21:09:39 +00004298
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004299Selector ASTImporter::Import(Selector FromSel) {
4300 if (FromSel.isNull())
4301 return Selector();
4302
4303 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4304 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4305 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4306 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4307 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4308}
4309
Douglas Gregor089459a2010-02-08 21:09:39 +00004310DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4311 DeclContext *DC,
4312 unsigned IDNS,
4313 NamedDecl **Decls,
4314 unsigned NumDecls) {
4315 return Name;
4316}
4317
4318DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004319 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004320}
4321
4322DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004323 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004324}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004325
4326Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4327 ImportedDecls[From] = To;
4328 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004329}
Douglas Gregorea35d112010-02-15 23:54:17 +00004330
4331bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004332 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004333 = ImportedTypes.find(From.getTypePtr());
4334 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4335 return true;
4336
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004337 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004338 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004339}