blob: 2179c950a476d7207a87985617892fb7ed388090 [file] [log] [blame]
Douglas Gregor96e578d2010-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 Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-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 Gregor62d311f2010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000042
43 // Importing types
John McCall424cec92011-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 Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-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 Gregor96e578d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
John McCall424cec92011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000084 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000085 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
86 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000087 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregore2e50d332010-12-01 01:36:18 +000088 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +000089 TemplateParameterList *ImportTemplateParameterList(
90 TemplateParameterList *Params);
Douglas Gregore2e50d332010-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 Gregor5c73e912010-02-11 00:48:18 +000095 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000096 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000097 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000098 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000099 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000100 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000101 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000102 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000103 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000104 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000105 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
106 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
107 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
108 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000109 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000110 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000111 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000112 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000113 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000114 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000115 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000116 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000117 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000118 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000119 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000120 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000121 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000122 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000123 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000124 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000125 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
126 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
127 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
128 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000129 Decl *VisitClassTemplateSpecializationDecl(
130 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000131
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000132 // Importing statements
133 Stmt *VisitStmt(Stmt *S);
134
135 // Importing expressions
136 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000137 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000138 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000139 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000140 Expr *VisitParenExpr(ParenExpr *E);
141 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000142 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000143 Expr *VisitBinaryOperator(BinaryOperator *E);
144 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000145 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000146 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000147 };
148}
149
150//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000151// Structural Equivalence
152//----------------------------------------------------------------------------
153
154namespace {
155 struct StructuralEquivalenceContext {
156 /// \brief AST contexts for which we are checking structural equivalence.
157 ASTContext &C1, &C2;
158
Douglas Gregor3996e242010-02-15 22:01:00 +0000159 /// \brief The set of "tentative" equivalences between two canonical
160 /// declarations, mapping from a declaration in the first context to the
161 /// declaration in the second context that we believe to be equivalent.
162 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
163
164 /// \brief Queue of declarations in the first context whose equivalence
165 /// with a declaration in the second context still needs to be verified.
166 std::deque<Decl *> DeclsToCheck;
167
Douglas Gregorb4964f72010-02-15 23:54:17 +0000168 /// \brief Declaration (from, to) pairs that are known not to be equivalent
169 /// (which we have already complained about).
170 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
171
Douglas Gregor3996e242010-02-15 22:01:00 +0000172 /// \brief Whether we're being strict about the spelling of types when
173 /// unifying two types.
174 bool StrictTypeSpelling;
175
176 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000177 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000178 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000179 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000180 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000181
182 /// \brief Determine whether the two declarations are structurally
183 /// equivalent.
184 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
185
186 /// \brief Determine whether the two types are structurally equivalent.
187 bool IsStructurallyEquivalent(QualType T1, QualType T2);
188
189 private:
190 /// \brief Finish checking all of the structural equivalences.
191 ///
192 /// \returns true if an error occurred, false otherwise.
193 bool Finish();
194
195 public:
196 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000197 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000198 }
199
200 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000201 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000202 }
203 };
204}
205
206static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
207 QualType T1, QualType T2);
208static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
209 Decl *D1, Decl *D2);
210
211/// \brief Determine if two APInts have the same value, after zero-extending
212/// one of them (if needed!) to ensure that the bit-widths match.
213static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
214 if (I1.getBitWidth() == I2.getBitWidth())
215 return I1 == I2;
216
217 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000218 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor3996e242010-02-15 22:01:00 +0000219
Jay Foad6d4db0c2010-12-07 08:25:34 +0000220 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor3996e242010-02-15 22:01:00 +0000221}
222
223/// \brief Determine if two APSInts have the same value, zero- or sign-extending
224/// as needed.
225static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
226 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
227 return I1 == I2;
228
229 // Check for a bit-width mismatch.
230 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000231 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor3996e242010-02-15 22:01:00 +0000232 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000233 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000234
235 // We have a signedness mismatch. Turn the signed value into an unsigned
236 // value.
237 if (I1.isSigned()) {
238 if (I1.isNegative())
239 return false;
240
241 return llvm::APSInt(I1, true) == I2;
242 }
243
244 if (I2.isNegative())
245 return false;
246
247 return I1 == llvm::APSInt(I2, true);
248}
249
250/// \brief Determine structural equivalence of two expressions.
251static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
252 Expr *E1, Expr *E2) {
253 if (!E1 || !E2)
254 return E1 == E2;
255
256 // FIXME: Actually perform a structural comparison!
257 return true;
258}
259
260/// \brief Determine whether two identifiers are equivalent.
261static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
262 const IdentifierInfo *Name2) {
263 if (!Name1 || !Name2)
264 return Name1 == Name2;
265
266 return Name1->getName() == Name2->getName();
267}
268
269/// \brief Determine whether two nested-name-specifiers are equivalent.
270static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
271 NestedNameSpecifier *NNS1,
272 NestedNameSpecifier *NNS2) {
273 // FIXME: Implement!
274 return true;
275}
276
277/// \brief Determine whether two template arguments are equivalent.
278static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
279 const TemplateArgument &Arg1,
280 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000281 if (Arg1.getKind() != Arg2.getKind())
282 return false;
283
284 switch (Arg1.getKind()) {
285 case TemplateArgument::Null:
286 return true;
287
288 case TemplateArgument::Type:
289 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
290
291 case TemplateArgument::Integral:
292 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
293 Arg2.getIntegralType()))
294 return false;
295
296 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
297
298 case TemplateArgument::Declaration:
299 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
300
301 case TemplateArgument::Template:
302 return IsStructurallyEquivalent(Context,
303 Arg1.getAsTemplate(),
304 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000305
306 case TemplateArgument::TemplateExpansion:
307 return IsStructurallyEquivalent(Context,
308 Arg1.getAsTemplateOrTemplatePattern(),
309 Arg2.getAsTemplateOrTemplatePattern());
310
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311 case TemplateArgument::Expression:
312 return IsStructurallyEquivalent(Context,
313 Arg1.getAsExpr(), Arg2.getAsExpr());
314
315 case TemplateArgument::Pack:
316 if (Arg1.pack_size() != Arg2.pack_size())
317 return false;
318
319 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
320 if (!IsStructurallyEquivalent(Context,
321 Arg1.pack_begin()[I],
322 Arg2.pack_begin()[I]))
323 return false;
324
325 return true;
326 }
327
328 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000329 return true;
330}
331
332/// \brief Determine structural equivalence for the common part of array
333/// types.
334static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
335 const ArrayType *Array1,
336 const ArrayType *Array2) {
337 if (!IsStructurallyEquivalent(Context,
338 Array1->getElementType(),
339 Array2->getElementType()))
340 return false;
341 if (Array1->getSizeModifier() != Array2->getSizeModifier())
342 return false;
343 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
344 return false;
345
346 return true;
347}
348
349/// \brief Determine structural equivalence of two types.
350static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
351 QualType T1, QualType T2) {
352 if (T1.isNull() || T2.isNull())
353 return T1.isNull() && T2.isNull();
354
355 if (!Context.StrictTypeSpelling) {
356 // We aren't being strict about token-to-token equivalence of types,
357 // so map down to the canonical type.
358 T1 = Context.C1.getCanonicalType(T1);
359 T2 = Context.C2.getCanonicalType(T2);
360 }
361
362 if (T1.getQualifiers() != T2.getQualifiers())
363 return false;
364
Douglas Gregorb4964f72010-02-15 23:54:17 +0000365 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000366
Douglas Gregorb4964f72010-02-15 23:54:17 +0000367 if (T1->getTypeClass() != T2->getTypeClass()) {
368 // Compare function types with prototypes vs. without prototypes as if
369 // both did not have prototypes.
370 if (T1->getTypeClass() == Type::FunctionProto &&
371 T2->getTypeClass() == Type::FunctionNoProto)
372 TC = Type::FunctionNoProto;
373 else if (T1->getTypeClass() == Type::FunctionNoProto &&
374 T2->getTypeClass() == Type::FunctionProto)
375 TC = Type::FunctionNoProto;
376 else
377 return false;
378 }
379
380 switch (TC) {
381 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000382 // FIXME: Deal with Char_S/Char_U.
383 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
384 return false;
385 break;
386
387 case Type::Complex:
388 if (!IsStructurallyEquivalent(Context,
389 cast<ComplexType>(T1)->getElementType(),
390 cast<ComplexType>(T2)->getElementType()))
391 return false;
392 break;
393
394 case Type::Pointer:
395 if (!IsStructurallyEquivalent(Context,
396 cast<PointerType>(T1)->getPointeeType(),
397 cast<PointerType>(T2)->getPointeeType()))
398 return false;
399 break;
400
401 case Type::BlockPointer:
402 if (!IsStructurallyEquivalent(Context,
403 cast<BlockPointerType>(T1)->getPointeeType(),
404 cast<BlockPointerType>(T2)->getPointeeType()))
405 return false;
406 break;
407
408 case Type::LValueReference:
409 case Type::RValueReference: {
410 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
411 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
412 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
413 return false;
414 if (Ref1->isInnerRef() != Ref2->isInnerRef())
415 return false;
416 if (!IsStructurallyEquivalent(Context,
417 Ref1->getPointeeTypeAsWritten(),
418 Ref2->getPointeeTypeAsWritten()))
419 return false;
420 break;
421 }
422
423 case Type::MemberPointer: {
424 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
425 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
426 if (!IsStructurallyEquivalent(Context,
427 MemPtr1->getPointeeType(),
428 MemPtr2->getPointeeType()))
429 return false;
430 if (!IsStructurallyEquivalent(Context,
431 QualType(MemPtr1->getClass(), 0),
432 QualType(MemPtr2->getClass(), 0)))
433 return false;
434 break;
435 }
436
437 case Type::ConstantArray: {
438 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
439 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
440 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
441 return false;
442
443 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
444 return false;
445 break;
446 }
447
448 case Type::IncompleteArray:
449 if (!IsArrayStructurallyEquivalent(Context,
450 cast<ArrayType>(T1),
451 cast<ArrayType>(T2)))
452 return false;
453 break;
454
455 case Type::VariableArray: {
456 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
457 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
458 if (!IsStructurallyEquivalent(Context,
459 Array1->getSizeExpr(), Array2->getSizeExpr()))
460 return false;
461
462 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
463 return false;
464
465 break;
466 }
467
468 case Type::DependentSizedArray: {
469 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
470 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
471 if (!IsStructurallyEquivalent(Context,
472 Array1->getSizeExpr(), Array2->getSizeExpr()))
473 return false;
474
475 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
476 return false;
477
478 break;
479 }
480
481 case Type::DependentSizedExtVector: {
482 const DependentSizedExtVectorType *Vec1
483 = cast<DependentSizedExtVectorType>(T1);
484 const DependentSizedExtVectorType *Vec2
485 = cast<DependentSizedExtVectorType>(T2);
486 if (!IsStructurallyEquivalent(Context,
487 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
488 return false;
489 if (!IsStructurallyEquivalent(Context,
490 Vec1->getElementType(),
491 Vec2->getElementType()))
492 return false;
493 break;
494 }
495
496 case Type::Vector:
497 case Type::ExtVector: {
498 const VectorType *Vec1 = cast<VectorType>(T1);
499 const VectorType *Vec2 = cast<VectorType>(T2);
500 if (!IsStructurallyEquivalent(Context,
501 Vec1->getElementType(),
502 Vec2->getElementType()))
503 return false;
504 if (Vec1->getNumElements() != Vec2->getNumElements())
505 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000506 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000507 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000508 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000509 }
510
511 case Type::FunctionProto: {
512 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
513 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
514 if (Proto1->getNumArgs() != Proto2->getNumArgs())
515 return false;
516 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
517 if (!IsStructurallyEquivalent(Context,
518 Proto1->getArgType(I),
519 Proto2->getArgType(I)))
520 return false;
521 }
522 if (Proto1->isVariadic() != Proto2->isVariadic())
523 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000524 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000525 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
527 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
528 return false;
529 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
530 if (!IsStructurallyEquivalent(Context,
531 Proto1->getExceptionType(I),
532 Proto2->getExceptionType(I)))
533 return false;
534 }
535 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000536 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000537 Proto1->getNoexceptExpr(),
538 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 return false;
540 }
541 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
542 return false;
543
544 // Fall through to check the bits common with FunctionNoProtoType.
545 }
546
547 case Type::FunctionNoProto: {
548 const FunctionType *Function1 = cast<FunctionType>(T1);
549 const FunctionType *Function2 = cast<FunctionType>(T2);
550 if (!IsStructurallyEquivalent(Context,
551 Function1->getResultType(),
552 Function2->getResultType()))
553 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000554 if (Function1->getExtInfo() != Function2->getExtInfo())
555 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000556 break;
557 }
558
559 case Type::UnresolvedUsing:
560 if (!IsStructurallyEquivalent(Context,
561 cast<UnresolvedUsingType>(T1)->getDecl(),
562 cast<UnresolvedUsingType>(T2)->getDecl()))
563 return false;
564
565 break;
John McCall81904512011-01-06 01:58:22 +0000566
567 case Type::Attributed:
568 if (!IsStructurallyEquivalent(Context,
569 cast<AttributedType>(T1)->getModifiedType(),
570 cast<AttributedType>(T2)->getModifiedType()))
571 return false;
572 if (!IsStructurallyEquivalent(Context,
573 cast<AttributedType>(T1)->getEquivalentType(),
574 cast<AttributedType>(T2)->getEquivalentType()))
575 return false;
576 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000577
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000578 case Type::Paren:
579 if (!IsStructurallyEquivalent(Context,
580 cast<ParenType>(T1)->getInnerType(),
581 cast<ParenType>(T2)->getInnerType()))
582 return false;
583 break;
584
Douglas Gregor3996e242010-02-15 22:01:00 +0000585 case Type::Typedef:
586 if (!IsStructurallyEquivalent(Context,
587 cast<TypedefType>(T1)->getDecl(),
588 cast<TypedefType>(T2)->getDecl()))
589 return false;
590 break;
591
592 case Type::TypeOfExpr:
593 if (!IsStructurallyEquivalent(Context,
594 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
595 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
596 return false;
597 break;
598
599 case Type::TypeOf:
600 if (!IsStructurallyEquivalent(Context,
601 cast<TypeOfType>(T1)->getUnderlyingType(),
602 cast<TypeOfType>(T2)->getUnderlyingType()))
603 return false;
604 break;
605
606 case Type::Decltype:
607 if (!IsStructurallyEquivalent(Context,
608 cast<DecltypeType>(T1)->getUnderlyingExpr(),
609 cast<DecltypeType>(T2)->getUnderlyingExpr()))
610 return false;
611 break;
612
Richard Smith30482bc2011-02-20 03:19:35 +0000613 case Type::Auto:
614 if (!IsStructurallyEquivalent(Context,
615 cast<AutoType>(T1)->getDeducedType(),
616 cast<AutoType>(T2)->getDeducedType()))
617 return false;
618 break;
619
Douglas Gregor3996e242010-02-15 22:01:00 +0000620 case Type::Record:
621 case Type::Enum:
622 if (!IsStructurallyEquivalent(Context,
623 cast<TagType>(T1)->getDecl(),
624 cast<TagType>(T2)->getDecl()))
625 return false;
626 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000627
Douglas Gregor3996e242010-02-15 22:01:00 +0000628 case Type::TemplateTypeParm: {
629 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
630 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
631 if (Parm1->getDepth() != Parm2->getDepth())
632 return false;
633 if (Parm1->getIndex() != Parm2->getIndex())
634 return false;
635 if (Parm1->isParameterPack() != Parm2->isParameterPack())
636 return false;
637
638 // Names of template type parameters are never significant.
639 break;
640 }
641
642 case Type::SubstTemplateTypeParm: {
643 const SubstTemplateTypeParmType *Subst1
644 = cast<SubstTemplateTypeParmType>(T1);
645 const SubstTemplateTypeParmType *Subst2
646 = cast<SubstTemplateTypeParmType>(T2);
647 if (!IsStructurallyEquivalent(Context,
648 QualType(Subst1->getReplacedParameter(), 0),
649 QualType(Subst2->getReplacedParameter(), 0)))
650 return false;
651 if (!IsStructurallyEquivalent(Context,
652 Subst1->getReplacementType(),
653 Subst2->getReplacementType()))
654 return false;
655 break;
656 }
657
Douglas Gregorfb322d82011-01-14 05:11:40 +0000658 case Type::SubstTemplateTypeParmPack: {
659 const SubstTemplateTypeParmPackType *Subst1
660 = cast<SubstTemplateTypeParmPackType>(T1);
661 const SubstTemplateTypeParmPackType *Subst2
662 = cast<SubstTemplateTypeParmPackType>(T2);
663 if (!IsStructurallyEquivalent(Context,
664 QualType(Subst1->getReplacedParameter(), 0),
665 QualType(Subst2->getReplacedParameter(), 0)))
666 return false;
667 if (!IsStructurallyEquivalent(Context,
668 Subst1->getArgumentPack(),
669 Subst2->getArgumentPack()))
670 return false;
671 break;
672 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000673 case Type::TemplateSpecialization: {
674 const TemplateSpecializationType *Spec1
675 = cast<TemplateSpecializationType>(T1);
676 const TemplateSpecializationType *Spec2
677 = cast<TemplateSpecializationType>(T2);
678 if (!IsStructurallyEquivalent(Context,
679 Spec1->getTemplateName(),
680 Spec2->getTemplateName()))
681 return false;
682 if (Spec1->getNumArgs() != Spec2->getNumArgs())
683 return false;
684 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
685 if (!IsStructurallyEquivalent(Context,
686 Spec1->getArg(I), Spec2->getArg(I)))
687 return false;
688 }
689 break;
690 }
691
Abramo Bagnara6150c882010-05-11 21:36:43 +0000692 case Type::Elaborated: {
693 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
694 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
695 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
696 if (Elab1->getKeyword() != Elab2->getKeyword())
697 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000698 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000699 Elab1->getQualifier(),
700 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000701 return false;
702 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000703 Elab1->getNamedType(),
704 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000705 return false;
706 break;
707 }
708
John McCalle78aac42010-03-10 03:28:59 +0000709 case Type::InjectedClassName: {
710 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
711 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
712 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000713 Inj1->getInjectedSpecializationType(),
714 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000715 return false;
716 break;
717 }
718
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000719 case Type::DependentName: {
720 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
721 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000722 if (!IsStructurallyEquivalent(Context,
723 Typename1->getQualifier(),
724 Typename2->getQualifier()))
725 return false;
726 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
727 Typename2->getIdentifier()))
728 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000729
730 break;
731 }
732
John McCallc392f372010-06-11 00:33:02 +0000733 case Type::DependentTemplateSpecialization: {
734 const DependentTemplateSpecializationType *Spec1 =
735 cast<DependentTemplateSpecializationType>(T1);
736 const DependentTemplateSpecializationType *Spec2 =
737 cast<DependentTemplateSpecializationType>(T2);
738 if (!IsStructurallyEquivalent(Context,
739 Spec1->getQualifier(),
740 Spec2->getQualifier()))
741 return false;
742 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
743 Spec2->getIdentifier()))
744 return false;
745 if (Spec1->getNumArgs() != Spec2->getNumArgs())
746 return false;
747 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
748 if (!IsStructurallyEquivalent(Context,
749 Spec1->getArg(I), Spec2->getArg(I)))
750 return false;
751 }
752 break;
753 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000754
755 case Type::PackExpansion:
756 if (!IsStructurallyEquivalent(Context,
757 cast<PackExpansionType>(T1)->getPattern(),
758 cast<PackExpansionType>(T2)->getPattern()))
759 return false;
760 break;
761
Douglas Gregor3996e242010-02-15 22:01:00 +0000762 case Type::ObjCInterface: {
763 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
764 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
765 if (!IsStructurallyEquivalent(Context,
766 Iface1->getDecl(), Iface2->getDecl()))
767 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000768 break;
769 }
770
771 case Type::ObjCObject: {
772 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
773 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
774 if (!IsStructurallyEquivalent(Context,
775 Obj1->getBaseType(),
776 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000777 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000778 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
779 return false;
780 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000781 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000782 Obj1->getProtocol(I),
783 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000784 return false;
785 }
786 break;
787 }
788
789 case Type::ObjCObjectPointer: {
790 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
791 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
792 if (!IsStructurallyEquivalent(Context,
793 Ptr1->getPointeeType(),
794 Ptr2->getPointeeType()))
795 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000796 break;
797 }
798
799 } // end switch
800
801 return true;
802}
803
804/// \brief Determine structural equivalence of two records.
805static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
806 RecordDecl *D1, RecordDecl *D2) {
807 if (D1->isUnion() != D2->isUnion()) {
808 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
809 << Context.C2.getTypeDeclType(D2);
810 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
811 << D1->getDeclName() << (unsigned)D1->getTagKind();
812 return false;
813 }
814
Douglas Gregore2e50d332010-12-01 01:36:18 +0000815 // If both declarations are class template specializations, we know
816 // the ODR applies, so check the template and template arguments.
817 ClassTemplateSpecializationDecl *Spec1
818 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
819 ClassTemplateSpecializationDecl *Spec2
820 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
821 if (Spec1 && Spec2) {
822 // Check that the specialized templates are the same.
823 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
824 Spec2->getSpecializedTemplate()))
825 return false;
826
827 // Check that the template arguments are the same.
828 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
829 return false;
830
831 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
832 if (!IsStructurallyEquivalent(Context,
833 Spec1->getTemplateArgs().get(I),
834 Spec2->getTemplateArgs().get(I)))
835 return false;
836 }
837 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000838 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000839 else if (Spec1 || Spec2)
840 return false;
841
Douglas Gregorb4964f72010-02-15 23:54:17 +0000842 // Compare the definitions of these two records. If either or both are
843 // incomplete, we assume that they are equivalent.
844 D1 = D1->getDefinition();
845 D2 = D2->getDefinition();
846 if (!D1 || !D2)
847 return true;
848
Douglas Gregor3996e242010-02-15 22:01:00 +0000849 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
850 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
851 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
852 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000853 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000854 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000855 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000856 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000857 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000858 return false;
859 }
860
861 // Check the base classes.
862 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
863 BaseEnd1 = D1CXX->bases_end(),
864 Base2 = D2CXX->bases_begin();
865 Base1 != BaseEnd1;
866 ++Base1, ++Base2) {
867 if (!IsStructurallyEquivalent(Context,
868 Base1->getType(), Base2->getType())) {
869 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
870 << Context.C2.getTypeDeclType(D2);
871 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
872 << Base2->getType()
873 << Base2->getSourceRange();
874 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
875 << Base1->getType()
876 << Base1->getSourceRange();
877 return false;
878 }
879
880 // Check virtual vs. non-virtual inheritance mismatch.
881 if (Base1->isVirtual() != Base2->isVirtual()) {
882 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
883 << Context.C2.getTypeDeclType(D2);
884 Context.Diag2(Base2->getSourceRange().getBegin(),
885 diag::note_odr_virtual_base)
886 << Base2->isVirtual() << Base2->getSourceRange();
887 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
888 << Base1->isVirtual()
889 << Base1->getSourceRange();
890 return false;
891 }
892 }
893 } else if (D1CXX->getNumBases() > 0) {
894 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
895 << Context.C2.getTypeDeclType(D2);
896 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
897 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
898 << Base1->getType()
899 << Base1->getSourceRange();
900 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
901 return false;
902 }
903 }
904
905 // Check the fields for consistency.
906 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
907 Field2End = D2->field_end();
908 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
909 Field1End = D1->field_end();
910 Field1 != Field1End;
911 ++Field1, ++Field2) {
912 if (Field2 == Field2End) {
913 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
914 << Context.C2.getTypeDeclType(D2);
915 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
916 << Field1->getDeclName() << Field1->getType();
917 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
918 return false;
919 }
920
921 if (!IsStructurallyEquivalent(Context,
922 Field1->getType(), Field2->getType())) {
923 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
924 << Context.C2.getTypeDeclType(D2);
925 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
926 << Field2->getDeclName() << Field2->getType();
927 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
928 << Field1->getDeclName() << Field1->getType();
929 return false;
930 }
931
932 if (Field1->isBitField() != Field2->isBitField()) {
933 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
934 << Context.C2.getTypeDeclType(D2);
935 if (Field1->isBitField()) {
936 llvm::APSInt Bits;
937 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
938 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
939 << Field1->getDeclName() << Field1->getType()
940 << Bits.toString(10, false);
941 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
942 << Field2->getDeclName();
943 } else {
944 llvm::APSInt Bits;
945 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
946 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
947 << Field2->getDeclName() << Field2->getType()
948 << Bits.toString(10, false);
949 Context.Diag1(Field1->getLocation(),
950 diag::note_odr_not_bit_field)
951 << Field1->getDeclName();
952 }
953 return false;
954 }
955
956 if (Field1->isBitField()) {
957 // Make sure that the bit-fields are the same length.
958 llvm::APSInt Bits1, Bits2;
959 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
960 return false;
961 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
962 return false;
963
964 if (!IsSameValue(Bits1, Bits2)) {
965 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
966 << Context.C2.getTypeDeclType(D2);
967 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
968 << Field2->getDeclName() << Field2->getType()
969 << Bits2.toString(10, false);
970 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
971 << Field1->getDeclName() << Field1->getType()
972 << Bits1.toString(10, false);
973 return false;
974 }
975 }
976 }
977
978 if (Field2 != Field2End) {
979 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
980 << Context.C2.getTypeDeclType(D2);
981 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
982 << Field2->getDeclName() << Field2->getType();
983 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
984 return false;
985 }
986
987 return true;
988}
989
990/// \brief Determine structural equivalence of two enums.
991static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
992 EnumDecl *D1, EnumDecl *D2) {
993 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
994 EC2End = D2->enumerator_end();
995 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
996 EC1End = D1->enumerator_end();
997 EC1 != EC1End; ++EC1, ++EC2) {
998 if (EC2 == EC2End) {
999 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1000 << Context.C2.getTypeDeclType(D2);
1001 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1002 << EC1->getDeclName()
1003 << EC1->getInitVal().toString(10);
1004 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1005 return false;
1006 }
1007
1008 llvm::APSInt Val1 = EC1->getInitVal();
1009 llvm::APSInt Val2 = EC2->getInitVal();
1010 if (!IsSameValue(Val1, Val2) ||
1011 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1012 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1013 << Context.C2.getTypeDeclType(D2);
1014 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1015 << EC2->getDeclName()
1016 << EC2->getInitVal().toString(10);
1017 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1018 << EC1->getDeclName()
1019 << EC1->getInitVal().toString(10);
1020 return false;
1021 }
1022 }
1023
1024 if (EC2 != EC2End) {
1025 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1026 << Context.C2.getTypeDeclType(D2);
1027 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1028 << EC2->getDeclName()
1029 << EC2->getInitVal().toString(10);
1030 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1031 return false;
1032 }
1033
1034 return true;
1035}
Douglas Gregora082a492010-11-30 19:14:50 +00001036
1037static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1038 TemplateParameterList *Params1,
1039 TemplateParameterList *Params2) {
1040 if (Params1->size() != Params2->size()) {
1041 Context.Diag2(Params2->getTemplateLoc(),
1042 diag::err_odr_different_num_template_parameters)
1043 << Params1->size() << Params2->size();
1044 Context.Diag1(Params1->getTemplateLoc(),
1045 diag::note_odr_template_parameter_list);
1046 return false;
1047 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001048
Douglas Gregora082a492010-11-30 19:14:50 +00001049 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1050 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1051 Context.Diag2(Params2->getParam(I)->getLocation(),
1052 diag::err_odr_different_template_parameter_kind);
1053 Context.Diag1(Params1->getParam(I)->getLocation(),
1054 diag::note_odr_template_parameter_here);
1055 return false;
1056 }
1057
1058 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1059 Params2->getParam(I))) {
1060
1061 return false;
1062 }
1063 }
1064
1065 return true;
1066}
1067
1068static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1069 TemplateTypeParmDecl *D1,
1070 TemplateTypeParmDecl *D2) {
1071 if (D1->isParameterPack() != D2->isParameterPack()) {
1072 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1073 << D2->isParameterPack();
1074 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1075 << D1->isParameterPack();
1076 return false;
1077 }
1078
1079 return true;
1080}
1081
1082static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083 NonTypeTemplateParmDecl *D1,
1084 NonTypeTemplateParmDecl *D2) {
1085 // FIXME: Enable once we have variadic templates.
1086#if 0
1087 if (D1->isParameterPack() != D2->isParameterPack()) {
1088 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1089 << D2->isParameterPack();
1090 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1091 << D1->isParameterPack();
1092 return false;
1093 }
1094#endif
1095
1096 // Check types.
1097 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1098 Context.Diag2(D2->getLocation(),
1099 diag::err_odr_non_type_parameter_type_inconsistent)
1100 << D2->getType() << D1->getType();
1101 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1102 << D1->getType();
1103 return false;
1104 }
1105
1106 return true;
1107}
1108
1109static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1110 TemplateTemplateParmDecl *D1,
1111 TemplateTemplateParmDecl *D2) {
1112 // FIXME: Enable once we have variadic templates.
1113#if 0
1114 if (D1->isParameterPack() != D2->isParameterPack()) {
1115 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1116 << D2->isParameterPack();
1117 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1118 << D1->isParameterPack();
1119 return false;
1120 }
1121#endif
1122
1123 // Check template parameter lists.
1124 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1125 D2->getTemplateParameters());
1126}
1127
1128static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1129 ClassTemplateDecl *D1,
1130 ClassTemplateDecl *D2) {
1131 // Check template parameters.
1132 if (!IsStructurallyEquivalent(Context,
1133 D1->getTemplateParameters(),
1134 D2->getTemplateParameters()))
1135 return false;
1136
1137 // Check the templated declaration.
1138 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1139 D2->getTemplatedDecl());
1140}
1141
Douglas Gregor3996e242010-02-15 22:01:00 +00001142/// \brief Determine structural equivalence of two declarations.
1143static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1144 Decl *D1, Decl *D2) {
1145 // FIXME: Check for known structural equivalences via a callback of some sort.
1146
Douglas Gregorb4964f72010-02-15 23:54:17 +00001147 // Check whether we already know that these two declarations are not
1148 // structurally equivalent.
1149 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1150 D2->getCanonicalDecl())))
1151 return false;
1152
Douglas Gregor3996e242010-02-15 22:01:00 +00001153 // Determine whether we've already produced a tentative equivalence for D1.
1154 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1155 if (EquivToD1)
1156 return EquivToD1 == D2->getCanonicalDecl();
1157
1158 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1159 EquivToD1 = D2->getCanonicalDecl();
1160 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1161 return true;
1162}
1163
1164bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1165 Decl *D2) {
1166 if (!::IsStructurallyEquivalent(*this, D1, D2))
1167 return false;
1168
1169 return !Finish();
1170}
1171
1172bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1173 QualType T2) {
1174 if (!::IsStructurallyEquivalent(*this, T1, T2))
1175 return false;
1176
1177 return !Finish();
1178}
1179
1180bool StructuralEquivalenceContext::Finish() {
1181 while (!DeclsToCheck.empty()) {
1182 // Check the next declaration.
1183 Decl *D1 = DeclsToCheck.front();
1184 DeclsToCheck.pop_front();
1185
1186 Decl *D2 = TentativeEquivalences[D1];
1187 assert(D2 && "Unrecorded tentative equivalence?");
1188
Douglas Gregorb4964f72010-02-15 23:54:17 +00001189 bool Equivalent = true;
1190
Douglas Gregor3996e242010-02-15 22:01:00 +00001191 // FIXME: Switch on all declaration kinds. For now, we're just going to
1192 // check the obvious ones.
1193 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1194 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1195 // Check for equivalent structure names.
1196 IdentifierInfo *Name1 = Record1->getIdentifier();
1197 if (!Name1 && Record1->getTypedefForAnonDecl())
1198 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1199 IdentifierInfo *Name2 = Record2->getIdentifier();
1200 if (!Name2 && Record2->getTypedefForAnonDecl())
1201 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001202 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1203 !::IsStructurallyEquivalent(*this, Record1, Record2))
1204 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001205 } else {
1206 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001207 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001208 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001209 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001210 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1211 // Check for equivalent enum names.
1212 IdentifierInfo *Name1 = Enum1->getIdentifier();
1213 if (!Name1 && Enum1->getTypedefForAnonDecl())
1214 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1215 IdentifierInfo *Name2 = Enum2->getIdentifier();
1216 if (!Name2 && Enum2->getTypedefForAnonDecl())
1217 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001218 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1219 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1220 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001221 } else {
1222 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001223 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001224 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001225 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001226 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1227 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001228 Typedef2->getIdentifier()) ||
1229 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001230 Typedef1->getUnderlyingType(),
1231 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001232 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001233 } else {
1234 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001235 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001236 }
Douglas Gregora082a492010-11-30 19:14:50 +00001237 } else if (ClassTemplateDecl *ClassTemplate1
1238 = dyn_cast<ClassTemplateDecl>(D1)) {
1239 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1240 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1241 ClassTemplate2->getIdentifier()) ||
1242 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1243 Equivalent = false;
1244 } else {
1245 // Class template/non-class-template mismatch.
1246 Equivalent = false;
1247 }
1248 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1249 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1250 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1251 Equivalent = false;
1252 } else {
1253 // Kind mismatch.
1254 Equivalent = false;
1255 }
1256 } else if (NonTypeTemplateParmDecl *NTTP1
1257 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1258 if (NonTypeTemplateParmDecl *NTTP2
1259 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1260 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1261 Equivalent = false;
1262 } else {
1263 // Kind mismatch.
1264 Equivalent = false;
1265 }
1266 } else if (TemplateTemplateParmDecl *TTP1
1267 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1268 if (TemplateTemplateParmDecl *TTP2
1269 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1270 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1271 Equivalent = false;
1272 } else {
1273 // Kind mismatch.
1274 Equivalent = false;
1275 }
1276 }
1277
Douglas Gregorb4964f72010-02-15 23:54:17 +00001278 if (!Equivalent) {
1279 // Note that these two declarations are not equivalent (and we already
1280 // know about it).
1281 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1282 D2->getCanonicalDecl()));
1283 return true;
1284 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001285 // FIXME: Check other declaration kinds!
1286 }
1287
1288 return false;
1289}
1290
1291//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001292// Import Types
1293//----------------------------------------------------------------------------
1294
John McCall424cec92011-01-19 06:33:43 +00001295QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001296 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1297 << T->getTypeClassName();
1298 return QualType();
1299}
1300
John McCall424cec92011-01-19 06:33:43 +00001301QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001302 switch (T->getKind()) {
1303 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1304 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1305
1306 case BuiltinType::Char_U:
1307 // The context we're importing from has an unsigned 'char'. If we're
1308 // importing into a context with a signed 'char', translate to
1309 // 'unsigned char' instead.
1310 if (Importer.getToContext().getLangOptions().CharIsSigned)
1311 return Importer.getToContext().UnsignedCharTy;
1312
1313 return Importer.getToContext().CharTy;
1314
1315 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1316
1317 case BuiltinType::Char16:
1318 // FIXME: Make sure that the "to" context supports C++!
1319 return Importer.getToContext().Char16Ty;
1320
1321 case BuiltinType::Char32:
1322 // FIXME: Make sure that the "to" context supports C++!
1323 return Importer.getToContext().Char32Ty;
1324
1325 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1326 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1327 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1328 case BuiltinType::ULongLong:
1329 return Importer.getToContext().UnsignedLongLongTy;
1330 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1331
1332 case BuiltinType::Char_S:
1333 // The context we're importing from has an unsigned 'char'. If we're
1334 // importing into a context with a signed 'char', translate to
1335 // 'unsigned char' instead.
1336 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1337 return Importer.getToContext().SignedCharTy;
1338
1339 return Importer.getToContext().CharTy;
1340
1341 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001342 case BuiltinType::WChar_S:
1343 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001344 // FIXME: If not in C++, shall we translate to the C equivalent of
1345 // wchar_t?
1346 return Importer.getToContext().WCharTy;
1347
1348 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1349 case BuiltinType::Int : return Importer.getToContext().IntTy;
1350 case BuiltinType::Long : return Importer.getToContext().LongTy;
1351 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1352 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1353 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1354 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1355 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1356
1357 case BuiltinType::NullPtr:
1358 // FIXME: Make sure that the "to" context supports C++0x!
1359 return Importer.getToContext().NullPtrTy;
1360
1361 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1362 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall31996342011-04-07 08:22:57 +00001363 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001364
1365 case BuiltinType::ObjCId:
1366 // FIXME: Make sure that the "to" context supports Objective-C!
1367 return Importer.getToContext().ObjCBuiltinIdTy;
1368
1369 case BuiltinType::ObjCClass:
1370 return Importer.getToContext().ObjCBuiltinClassTy;
1371
1372 case BuiltinType::ObjCSel:
1373 return Importer.getToContext().ObjCBuiltinSelTy;
1374 }
1375
1376 return QualType();
1377}
1378
John McCall424cec92011-01-19 06:33:43 +00001379QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001380 QualType ToElementType = Importer.Import(T->getElementType());
1381 if (ToElementType.isNull())
1382 return QualType();
1383
1384 return Importer.getToContext().getComplexType(ToElementType);
1385}
1386
John McCall424cec92011-01-19 06:33:43 +00001387QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001388 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1389 if (ToPointeeType.isNull())
1390 return QualType();
1391
1392 return Importer.getToContext().getPointerType(ToPointeeType);
1393}
1394
John McCall424cec92011-01-19 06:33:43 +00001395QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001396 // FIXME: Check for blocks support in "to" context.
1397 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1398 if (ToPointeeType.isNull())
1399 return QualType();
1400
1401 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1402}
1403
John McCall424cec92011-01-19 06:33:43 +00001404QualType
1405ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001406 // FIXME: Check for C++ support in "to" context.
1407 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1408 if (ToPointeeType.isNull())
1409 return QualType();
1410
1411 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1412}
1413
John McCall424cec92011-01-19 06:33:43 +00001414QualType
1415ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001416 // FIXME: Check for C++0x support in "to" context.
1417 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1418 if (ToPointeeType.isNull())
1419 return QualType();
1420
1421 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1422}
1423
John McCall424cec92011-01-19 06:33:43 +00001424QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001425 // FIXME: Check for C++ support in "to" context.
1426 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1427 if (ToPointeeType.isNull())
1428 return QualType();
1429
1430 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1431 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1432 ClassType.getTypePtr());
1433}
1434
John McCall424cec92011-01-19 06:33:43 +00001435QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001436 QualType ToElementType = Importer.Import(T->getElementType());
1437 if (ToElementType.isNull())
1438 return QualType();
1439
1440 return Importer.getToContext().getConstantArrayType(ToElementType,
1441 T->getSize(),
1442 T->getSizeModifier(),
1443 T->getIndexTypeCVRQualifiers());
1444}
1445
John McCall424cec92011-01-19 06:33:43 +00001446QualType
1447ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001448 QualType ToElementType = Importer.Import(T->getElementType());
1449 if (ToElementType.isNull())
1450 return QualType();
1451
1452 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1453 T->getSizeModifier(),
1454 T->getIndexTypeCVRQualifiers());
1455}
1456
John McCall424cec92011-01-19 06:33:43 +00001457QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001458 QualType ToElementType = Importer.Import(T->getElementType());
1459 if (ToElementType.isNull())
1460 return QualType();
1461
1462 Expr *Size = Importer.Import(T->getSizeExpr());
1463 if (!Size)
1464 return QualType();
1465
1466 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1467 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1468 T->getSizeModifier(),
1469 T->getIndexTypeCVRQualifiers(),
1470 Brackets);
1471}
1472
John McCall424cec92011-01-19 06:33:43 +00001473QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001474 QualType ToElementType = Importer.Import(T->getElementType());
1475 if (ToElementType.isNull())
1476 return QualType();
1477
1478 return Importer.getToContext().getVectorType(ToElementType,
1479 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001480 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001481}
1482
John McCall424cec92011-01-19 06:33:43 +00001483QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001484 QualType ToElementType = Importer.Import(T->getElementType());
1485 if (ToElementType.isNull())
1486 return QualType();
1487
1488 return Importer.getToContext().getExtVectorType(ToElementType,
1489 T->getNumElements());
1490}
1491
John McCall424cec92011-01-19 06:33:43 +00001492QualType
1493ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494 // FIXME: What happens if we're importing a function without a prototype
1495 // into C++? Should we make it variadic?
1496 QualType ToResultType = Importer.Import(T->getResultType());
1497 if (ToResultType.isNull())
1498 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001499
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001501 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001502}
1503
John McCall424cec92011-01-19 06:33:43 +00001504QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001505 QualType ToResultType = Importer.Import(T->getResultType());
1506 if (ToResultType.isNull())
1507 return QualType();
1508
1509 // Import argument types
1510 llvm::SmallVector<QualType, 4> ArgTypes;
1511 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1512 AEnd = T->arg_type_end();
1513 A != AEnd; ++A) {
1514 QualType ArgType = Importer.Import(*A);
1515 if (ArgType.isNull())
1516 return QualType();
1517 ArgTypes.push_back(ArgType);
1518 }
1519
1520 // Import exception types
1521 llvm::SmallVector<QualType, 4> ExceptionTypes;
1522 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1523 EEnd = T->exception_end();
1524 E != EEnd; ++E) {
1525 QualType ExceptionType = Importer.Import(*E);
1526 if (ExceptionType.isNull())
1527 return QualType();
1528 ExceptionTypes.push_back(ExceptionType);
1529 }
John McCalldb40c7f2010-12-14 08:05:40 +00001530
1531 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1532 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001533
1534 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001535 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536}
1537
John McCall424cec92011-01-19 06:33:43 +00001538QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001539 TypedefDecl *ToDecl
1540 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1541 if (!ToDecl)
1542 return QualType();
1543
1544 return Importer.getToContext().getTypeDeclType(ToDecl);
1545}
1546
John McCall424cec92011-01-19 06:33:43 +00001547QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001548 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1549 if (!ToExpr)
1550 return QualType();
1551
1552 return Importer.getToContext().getTypeOfExprType(ToExpr);
1553}
1554
John McCall424cec92011-01-19 06:33:43 +00001555QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001556 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1557 if (ToUnderlyingType.isNull())
1558 return QualType();
1559
1560 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1561}
1562
John McCall424cec92011-01-19 06:33:43 +00001563QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001564 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001565 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1566 if (!ToExpr)
1567 return QualType();
1568
1569 return Importer.getToContext().getDecltypeType(ToExpr);
1570}
1571
Richard Smith30482bc2011-02-20 03:19:35 +00001572QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1573 // FIXME: Make sure that the "to" context supports C++0x!
1574 QualType FromDeduced = T->getDeducedType();
1575 QualType ToDeduced;
1576 if (!FromDeduced.isNull()) {
1577 ToDeduced = Importer.Import(FromDeduced);
1578 if (ToDeduced.isNull())
1579 return QualType();
1580 }
1581
1582 return Importer.getToContext().getAutoType(ToDeduced);
1583}
1584
John McCall424cec92011-01-19 06:33:43 +00001585QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001586 RecordDecl *ToDecl
1587 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1588 if (!ToDecl)
1589 return QualType();
1590
1591 return Importer.getToContext().getTagDeclType(ToDecl);
1592}
1593
John McCall424cec92011-01-19 06:33:43 +00001594QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001595 EnumDecl *ToDecl
1596 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1597 if (!ToDecl)
1598 return QualType();
1599
1600 return Importer.getToContext().getTagDeclType(ToDecl);
1601}
1602
Douglas Gregore2e50d332010-12-01 01:36:18 +00001603QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001604 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001605 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1606 if (ToTemplate.isNull())
1607 return QualType();
1608
1609 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1610 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1611 return QualType();
1612
1613 QualType ToCanonType;
1614 if (!QualType(T, 0).isCanonical()) {
1615 QualType FromCanonType
1616 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1617 ToCanonType =Importer.Import(FromCanonType);
1618 if (ToCanonType.isNull())
1619 return QualType();
1620 }
1621 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1622 ToTemplateArgs.data(),
1623 ToTemplateArgs.size(),
1624 ToCanonType);
1625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001628 NestedNameSpecifier *ToQualifier = 0;
1629 // Note: the qualifier in an ElaboratedType is optional.
1630 if (T->getQualifier()) {
1631 ToQualifier = Importer.Import(T->getQualifier());
1632 if (!ToQualifier)
1633 return QualType();
1634 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001635
1636 QualType ToNamedType = Importer.Import(T->getNamedType());
1637 if (ToNamedType.isNull())
1638 return QualType();
1639
Abramo Bagnara6150c882010-05-11 21:36:43 +00001640 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1641 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001642}
1643
John McCall424cec92011-01-19 06:33:43 +00001644QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001645 ObjCInterfaceDecl *Class
1646 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1647 if (!Class)
1648 return QualType();
1649
John McCall8b07ec22010-05-15 11:32:37 +00001650 return Importer.getToContext().getObjCInterfaceType(Class);
1651}
1652
John McCall424cec92011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001654 QualType ToBaseType = Importer.Import(T->getBaseType());
1655 if (ToBaseType.isNull())
1656 return QualType();
1657
Douglas Gregor96e578d2010-02-05 17:54:41 +00001658 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001659 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001660 PEnd = T->qual_end();
1661 P != PEnd; ++P) {
1662 ObjCProtocolDecl *Protocol
1663 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1664 if (!Protocol)
1665 return QualType();
1666 Protocols.push_back(Protocol);
1667 }
1668
John McCall8b07ec22010-05-15 11:32:37 +00001669 return Importer.getToContext().getObjCObjectType(ToBaseType,
1670 Protocols.data(),
1671 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001672}
1673
John McCall424cec92011-01-19 06:33:43 +00001674QualType
1675ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001676 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1677 if (ToPointeeType.isNull())
1678 return QualType();
1679
John McCall8b07ec22010-05-15 11:32:37 +00001680 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001681}
1682
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001683//----------------------------------------------------------------------------
1684// Import Declarations
1685//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001686bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1687 DeclContext *&LexicalDC,
1688 DeclarationName &Name,
1689 SourceLocation &Loc) {
1690 // Import the context of this declaration.
1691 DC = Importer.ImportContext(D->getDeclContext());
1692 if (!DC)
1693 return true;
1694
1695 LexicalDC = DC;
1696 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1697 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1698 if (!LexicalDC)
1699 return true;
1700 }
1701
1702 // Import the name of this declaration.
1703 Name = Importer.Import(D->getDeclName());
1704 if (D->getDeclName() && !Name)
1705 return true;
1706
1707 // Import the location of this declaration.
1708 Loc = Importer.Import(D->getLocation());
1709 return false;
1710}
1711
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001712void
1713ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1714 DeclarationNameInfo& To) {
1715 // NOTE: To.Name and To.Loc are already imported.
1716 // We only have to import To.LocInfo.
1717 switch (To.getName().getNameKind()) {
1718 case DeclarationName::Identifier:
1719 case DeclarationName::ObjCZeroArgSelector:
1720 case DeclarationName::ObjCOneArgSelector:
1721 case DeclarationName::ObjCMultiArgSelector:
1722 case DeclarationName::CXXUsingDirective:
1723 return;
1724
1725 case DeclarationName::CXXOperatorName: {
1726 SourceRange Range = From.getCXXOperatorNameRange();
1727 To.setCXXOperatorNameRange(Importer.Import(Range));
1728 return;
1729 }
1730 case DeclarationName::CXXLiteralOperatorName: {
1731 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1732 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1733 return;
1734 }
1735 case DeclarationName::CXXConstructorName:
1736 case DeclarationName::CXXDestructorName:
1737 case DeclarationName::CXXConversionFunctionName: {
1738 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1739 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1740 return;
1741 }
1742 assert(0 && "Unknown name kind.");
1743 }
1744}
1745
Douglas Gregor0a791672011-01-18 03:11:38 +00001746void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1747 if (Importer.isMinimalImport() && !ForceImport) {
1748 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1749 ToDC->setHasExternalLexicalStorage();
1750 ToDC->setHasExternalVisibleStorage();
1751 }
1752 return;
1753 }
1754
Douglas Gregor968d6332010-02-21 18:24:45 +00001755 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1756 FromEnd = FromDC->decls_end();
1757 From != FromEnd;
1758 ++From)
1759 Importer.Import(*From);
1760}
1761
Douglas Gregore2e50d332010-12-01 01:36:18 +00001762bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1763 if (To->getDefinition())
1764 return false;
1765
1766 To->startDefinition();
1767
1768 // Add base classes.
1769 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1770 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1771
1772 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1773 for (CXXRecordDecl::base_class_iterator
1774 Base1 = FromCXX->bases_begin(),
1775 FromBaseEnd = FromCXX->bases_end();
1776 Base1 != FromBaseEnd;
1777 ++Base1) {
1778 QualType T = Importer.Import(Base1->getType());
1779 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001780 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001781
1782 SourceLocation EllipsisLoc;
1783 if (Base1->isPackExpansion())
1784 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001785
1786 Bases.push_back(
1787 new (Importer.getToContext())
1788 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1789 Base1->isVirtual(),
1790 Base1->isBaseOfClass(),
1791 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001792 Importer.Import(Base1->getTypeSourceInfo()),
1793 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001794 }
1795 if (!Bases.empty())
1796 ToCXX->setBases(Bases.data(), Bases.size());
1797 }
1798
1799 ImportDeclContext(From);
1800 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001801 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001802}
1803
Douglas Gregora082a492010-11-30 19:14:50 +00001804TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1805 TemplateParameterList *Params) {
1806 llvm::SmallVector<NamedDecl *, 4> ToParams;
1807 ToParams.reserve(Params->size());
1808 for (TemplateParameterList::iterator P = Params->begin(),
1809 PEnd = Params->end();
1810 P != PEnd; ++P) {
1811 Decl *To = Importer.Import(*P);
1812 if (!To)
1813 return 0;
1814
1815 ToParams.push_back(cast<NamedDecl>(To));
1816 }
1817
1818 return TemplateParameterList::Create(Importer.getToContext(),
1819 Importer.Import(Params->getTemplateLoc()),
1820 Importer.Import(Params->getLAngleLoc()),
1821 ToParams.data(), ToParams.size(),
1822 Importer.Import(Params->getRAngleLoc()));
1823}
1824
Douglas Gregore2e50d332010-12-01 01:36:18 +00001825TemplateArgument
1826ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1827 switch (From.getKind()) {
1828 case TemplateArgument::Null:
1829 return TemplateArgument();
1830
1831 case TemplateArgument::Type: {
1832 QualType ToType = Importer.Import(From.getAsType());
1833 if (ToType.isNull())
1834 return TemplateArgument();
1835 return TemplateArgument(ToType);
1836 }
1837
1838 case TemplateArgument::Integral: {
1839 QualType ToType = Importer.Import(From.getIntegralType());
1840 if (ToType.isNull())
1841 return TemplateArgument();
1842 return TemplateArgument(*From.getAsIntegral(), ToType);
1843 }
1844
1845 case TemplateArgument::Declaration:
1846 if (Decl *To = Importer.Import(From.getAsDecl()))
1847 return TemplateArgument(To);
1848 return TemplateArgument();
1849
1850 case TemplateArgument::Template: {
1851 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1852 if (ToTemplate.isNull())
1853 return TemplateArgument();
1854
1855 return TemplateArgument(ToTemplate);
1856 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001857
1858 case TemplateArgument::TemplateExpansion: {
1859 TemplateName ToTemplate
1860 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1861 if (ToTemplate.isNull())
1862 return TemplateArgument();
1863
Douglas Gregore1d60df2011-01-14 23:41:42 +00001864 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001865 }
1866
Douglas Gregore2e50d332010-12-01 01:36:18 +00001867 case TemplateArgument::Expression:
1868 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1869 return TemplateArgument(ToExpr);
1870 return TemplateArgument();
1871
1872 case TemplateArgument::Pack: {
1873 llvm::SmallVector<TemplateArgument, 2> ToPack;
1874 ToPack.reserve(From.pack_size());
1875 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1876 return TemplateArgument();
1877
1878 TemplateArgument *ToArgs
1879 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1880 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1881 return TemplateArgument(ToArgs, ToPack.size());
1882 }
1883 }
1884
1885 llvm_unreachable("Invalid template argument kind");
1886 return TemplateArgument();
1887}
1888
1889bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1890 unsigned NumFromArgs,
1891 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1892 for (unsigned I = 0; I != NumFromArgs; ++I) {
1893 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1894 if (To.isNull() && !FromArgs[I].isNull())
1895 return true;
1896
1897 ToArgs.push_back(To);
1898 }
1899
1900 return false;
1901}
1902
Douglas Gregor5c73e912010-02-11 00:48:18 +00001903bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001904 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001905 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001906 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001907 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001908 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001909}
1910
Douglas Gregor98c10182010-02-12 22:17:39 +00001911bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001912 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001913 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001914 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001915 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001916}
1917
Douglas Gregora082a492010-11-30 19:14:50 +00001918bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1919 ClassTemplateDecl *To) {
1920 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1921 Importer.getToContext(),
1922 Importer.getNonEquivalentDecls());
1923 return Ctx.IsStructurallyEquivalent(From, To);
1924}
1925
Douglas Gregore4c83e42010-02-09 22:48:33 +00001926Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001927 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001928 << D->getDeclKindName();
1929 return 0;
1930}
1931
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001932Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1933 // Import the major distinguishing characteristics of this namespace.
1934 DeclContext *DC, *LexicalDC;
1935 DeclarationName Name;
1936 SourceLocation Loc;
1937 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1938 return 0;
1939
1940 NamespaceDecl *MergeWithNamespace = 0;
1941 if (!Name) {
1942 // This is an anonymous namespace. Adopt an existing anonymous
1943 // namespace if we can.
1944 // FIXME: Not testable.
1945 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1946 MergeWithNamespace = TU->getAnonymousNamespace();
1947 else
1948 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1949 } else {
1950 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1951 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1952 Lookup.first != Lookup.second;
1953 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001954 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001955 continue;
1956
1957 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1958 MergeWithNamespace = FoundNS;
1959 ConflictingDecls.clear();
1960 break;
1961 }
1962
1963 ConflictingDecls.push_back(*Lookup.first);
1964 }
1965
1966 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001967 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001968 ConflictingDecls.data(),
1969 ConflictingDecls.size());
1970 }
1971 }
1972
1973 // Create the "to" namespace, if needed.
1974 NamespaceDecl *ToNamespace = MergeWithNamespace;
1975 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001976 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1977 Importer.Import(D->getLocStart()),
1978 Loc, Name.getAsIdentifierInfo());
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001979 ToNamespace->setLexicalDeclContext(LexicalDC);
1980 LexicalDC->addDecl(ToNamespace);
1981
1982 // If this is an anonymous namespace, register it as the anonymous
1983 // namespace within its context.
1984 if (!Name) {
1985 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1986 TU->setAnonymousNamespace(ToNamespace);
1987 else
1988 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1989 }
1990 }
1991 Importer.Imported(D, ToNamespace);
1992
1993 ImportDeclContext(D);
1994
1995 return ToNamespace;
1996}
1997
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001998Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1999 // Import the major distinguishing characteristics of this typedef.
2000 DeclContext *DC, *LexicalDC;
2001 DeclarationName Name;
2002 SourceLocation Loc;
2003 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2004 return 0;
2005
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002006 // If this typedef is not in block scope, determine whether we've
2007 // seen a typedef with the same name (that we can merge with) or any
2008 // other entity by that name (which name lookup could conflict with).
2009 if (!DC->isFunctionOrMethod()) {
2010 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2011 unsigned IDNS = Decl::IDNS_Ordinary;
2012 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2013 Lookup.first != Lookup.second;
2014 ++Lookup.first) {
2015 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2016 continue;
2017 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002018 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2019 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002020 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002021 }
2022
2023 ConflictingDecls.push_back(*Lookup.first);
2024 }
2025
2026 if (!ConflictingDecls.empty()) {
2027 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2028 ConflictingDecls.data(),
2029 ConflictingDecls.size());
2030 if (!Name)
2031 return 0;
2032 }
2033 }
2034
Douglas Gregorb4964f72010-02-15 23:54:17 +00002035 // Import the underlying type of this typedef;
2036 QualType T = Importer.Import(D->getUnderlyingType());
2037 if (T.isNull())
2038 return 0;
2039
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002040 // Create the new typedef node.
2041 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002042 SourceLocation StartL = Importer.Import(D->getLocStart());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002043 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002044 StartL, Loc,
2045 Name.getAsIdentifierInfo(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002046 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002047 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002048 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002049 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002050 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002051
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002052 return ToTypedef;
2053}
2054
Douglas Gregor98c10182010-02-12 22:17:39 +00002055Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2056 // Import the major distinguishing characteristics of this enum.
2057 DeclContext *DC, *LexicalDC;
2058 DeclarationName Name;
2059 SourceLocation Loc;
2060 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2061 return 0;
2062
2063 // Figure out what enum name we're looking for.
2064 unsigned IDNS = Decl::IDNS_Tag;
2065 DeclarationName SearchName = Name;
2066 if (!SearchName && D->getTypedefForAnonDecl()) {
2067 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2068 IDNS = Decl::IDNS_Ordinary;
2069 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2070 IDNS |= Decl::IDNS_Ordinary;
2071
2072 // We may already have an enum of the same name; try to find and match it.
2073 if (!DC->isFunctionOrMethod() && SearchName) {
2074 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2075 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2076 Lookup.first != Lookup.second;
2077 ++Lookup.first) {
2078 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2079 continue;
2080
2081 Decl *Found = *Lookup.first;
2082 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2083 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2084 Found = Tag->getDecl();
2085 }
2086
2087 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002088 if (IsStructuralMatch(D, FoundEnum))
2089 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002090 }
2091
2092 ConflictingDecls.push_back(*Lookup.first);
2093 }
2094
2095 if (!ConflictingDecls.empty()) {
2096 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2097 ConflictingDecls.data(),
2098 ConflictingDecls.size());
2099 }
2100 }
2101
2102 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002103 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2104 Importer.Import(D->getLocStart()),
2105 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002106 D->isScoped(), D->isScopedUsingClassTag(),
2107 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002108 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002109 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002110 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002111 D2->setLexicalDeclContext(LexicalDC);
2112 Importer.Imported(D, D2);
2113 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002114
2115 // Import the integer type.
2116 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2117 if (ToIntegerType.isNull())
2118 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002119 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002120
2121 // Import the definition
2122 if (D->isDefinition()) {
2123 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2124 if (T.isNull())
2125 return 0;
2126
2127 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2128 if (ToPromotionType.isNull())
2129 return 0;
2130
Douglas Gregor3996e242010-02-15 22:01:00 +00002131 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002132 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002133
2134 // FIXME: we might need to merge the number of positive or negative bits
2135 // if the enumerator lists don't match.
2136 D2->completeDefinition(T, ToPromotionType,
2137 D->getNumPositiveBits(),
2138 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002139 }
2140
Douglas Gregor3996e242010-02-15 22:01:00 +00002141 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002142}
2143
Douglas Gregor5c73e912010-02-11 00:48:18 +00002144Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2145 // If this record has a definition in the translation unit we're coming from,
2146 // but this particular declaration is not that definition, import the
2147 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002148 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002149 if (Definition && Definition != D) {
2150 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002151 if (!ImportedDef)
2152 return 0;
2153
2154 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002155 }
2156
2157 // Import the major distinguishing characteristics of this record.
2158 DeclContext *DC, *LexicalDC;
2159 DeclarationName Name;
2160 SourceLocation Loc;
2161 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2162 return 0;
2163
2164 // Figure out what structure name we're looking for.
2165 unsigned IDNS = Decl::IDNS_Tag;
2166 DeclarationName SearchName = Name;
2167 if (!SearchName && D->getTypedefForAnonDecl()) {
2168 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2169 IDNS = Decl::IDNS_Ordinary;
2170 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2171 IDNS |= Decl::IDNS_Ordinary;
2172
2173 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002174 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002175 if (!DC->isFunctionOrMethod() && SearchName) {
2176 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2177 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2178 Lookup.first != Lookup.second;
2179 ++Lookup.first) {
2180 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2181 continue;
2182
2183 Decl *Found = *Lookup.first;
2184 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2185 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2186 Found = Tag->getDecl();
2187 }
2188
2189 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002190 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2191 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2192 // The record types structurally match, or the "from" translation
2193 // unit only had a forward declaration anyway; call it the same
2194 // function.
2195 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002196 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002197 }
2198 } else {
2199 // We have a forward declaration of this type, so adopt that forward
2200 // declaration rather than building a new one.
2201 AdoptDecl = FoundRecord;
2202 continue;
2203 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002204 }
2205
2206 ConflictingDecls.push_back(*Lookup.first);
2207 }
2208
2209 if (!ConflictingDecls.empty()) {
2210 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2211 ConflictingDecls.data(),
2212 ConflictingDecls.size());
2213 }
2214 }
2215
2216 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002217 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002218 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002219 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002220 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002221 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002222 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002223 DC, StartLoc, Loc,
2224 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002225 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002226 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002227 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002228 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002229 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002230 }
Douglas Gregor14454802011-02-25 02:25:35 +00002231
2232 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002233 D2->setLexicalDeclContext(LexicalDC);
2234 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002235 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002236
Douglas Gregor3996e242010-02-15 22:01:00 +00002237 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002238
Douglas Gregore2e50d332010-12-01 01:36:18 +00002239 if (D->isDefinition() && ImportDefinition(D, D2))
2240 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002241
Douglas Gregor3996e242010-02-15 22:01:00 +00002242 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002243}
2244
Douglas Gregor98c10182010-02-12 22:17:39 +00002245Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2246 // Import the major distinguishing characteristics of this enumerator.
2247 DeclContext *DC, *LexicalDC;
2248 DeclarationName Name;
2249 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002250 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002251 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002252
2253 QualType T = Importer.Import(D->getType());
2254 if (T.isNull())
2255 return 0;
2256
Douglas Gregor98c10182010-02-12 22:17:39 +00002257 // Determine whether there are any other declarations with the same name and
2258 // in the same context.
2259 if (!LexicalDC->isFunctionOrMethod()) {
2260 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2261 unsigned IDNS = Decl::IDNS_Ordinary;
2262 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2263 Lookup.first != Lookup.second;
2264 ++Lookup.first) {
2265 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2266 continue;
2267
2268 ConflictingDecls.push_back(*Lookup.first);
2269 }
2270
2271 if (!ConflictingDecls.empty()) {
2272 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2273 ConflictingDecls.data(),
2274 ConflictingDecls.size());
2275 if (!Name)
2276 return 0;
2277 }
2278 }
2279
2280 Expr *Init = Importer.Import(D->getInitExpr());
2281 if (D->getInitExpr() && !Init)
2282 return 0;
2283
2284 EnumConstantDecl *ToEnumerator
2285 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2286 Name.getAsIdentifierInfo(), T,
2287 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002288 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002289 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002290 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002291 LexicalDC->addDecl(ToEnumerator);
2292 return ToEnumerator;
2293}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002294
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002295Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2296 // Import the major distinguishing characteristics of this function.
2297 DeclContext *DC, *LexicalDC;
2298 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002299 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002300 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002301 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002302
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002303 // Try to find a function in our own ("to") context with the same name, same
2304 // type, and in the same context as the function we're importing.
2305 if (!LexicalDC->isFunctionOrMethod()) {
2306 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2307 unsigned IDNS = Decl::IDNS_Ordinary;
2308 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2309 Lookup.first != Lookup.second;
2310 ++Lookup.first) {
2311 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2312 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002313
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002314 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2315 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2316 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002317 if (Importer.IsStructurallyEquivalent(D->getType(),
2318 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002319 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002320 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002321 }
2322
2323 // FIXME: Check for overloading more carefully, e.g., by boosting
2324 // Sema::IsOverload out to the AST library.
2325
2326 // Function overloading is okay in C++.
2327 if (Importer.getToContext().getLangOptions().CPlusPlus)
2328 continue;
2329
2330 // Complain about inconsistent function types.
2331 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002332 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002333 Importer.ToDiag(FoundFunction->getLocation(),
2334 diag::note_odr_value_here)
2335 << FoundFunction->getType();
2336 }
2337 }
2338
2339 ConflictingDecls.push_back(*Lookup.first);
2340 }
2341
2342 if (!ConflictingDecls.empty()) {
2343 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2344 ConflictingDecls.data(),
2345 ConflictingDecls.size());
2346 if (!Name)
2347 return 0;
2348 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002349 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002350
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002351 DeclarationNameInfo NameInfo(Name, Loc);
2352 // Import additional name location/type info.
2353 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2354
Douglas Gregorb4964f72010-02-15 23:54:17 +00002355 // Import the type.
2356 QualType T = Importer.Import(D->getType());
2357 if (T.isNull())
2358 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002359
2360 // Import the function parameters.
2361 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2362 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2363 P != PEnd; ++P) {
2364 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2365 if (!ToP)
2366 return 0;
2367
2368 Parameters.push_back(ToP);
2369 }
2370
2371 // Create the imported function.
2372 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002373 FunctionDecl *ToFunction = 0;
2374 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2375 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2376 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002377 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002378 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002379 FromConstructor->isExplicit(),
2380 D->isInlineSpecified(),
2381 D->isImplicit());
2382 } else if (isa<CXXDestructorDecl>(D)) {
2383 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2384 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002385 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002386 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002387 D->isInlineSpecified(),
2388 D->isImplicit());
2389 } else if (CXXConversionDecl *FromConversion
2390 = dyn_cast<CXXConversionDecl>(D)) {
2391 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2392 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002393 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002394 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002395 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002396 FromConversion->isExplicit(),
2397 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002398 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2399 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2400 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002401 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002402 NameInfo, T, TInfo,
2403 Method->isStatic(),
2404 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002405 Method->isInlineSpecified(),
2406 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002407 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002408 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002409 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002410 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002411 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002412 D->isInlineSpecified(),
2413 D->hasWrittenPrototype());
2414 }
John McCall3e11ebe2010-03-15 10:12:16 +00002415
2416 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002417 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002418 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002419 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002420 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2421 ToFunction->setTrivial(D->isTrivial());
2422 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002423 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002424
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002425 // Set the parameters.
2426 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002427 Parameters[I]->setOwningFunction(ToFunction);
2428 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002429 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002430 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002431
2432 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002433
2434 // Add this function to the lexical context.
2435 LexicalDC->addDecl(ToFunction);
2436
Douglas Gregor43f54792010-02-17 02:12:47 +00002437 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002438}
2439
Douglas Gregor00eace12010-02-21 18:29:16 +00002440Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2441 return VisitFunctionDecl(D);
2442}
2443
2444Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2445 return VisitCXXMethodDecl(D);
2446}
2447
2448Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2449 return VisitCXXMethodDecl(D);
2450}
2451
2452Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2453 return VisitCXXMethodDecl(D);
2454}
2455
Douglas Gregor5c73e912010-02-11 00:48:18 +00002456Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2457 // Import the major distinguishing characteristics of a variable.
2458 DeclContext *DC, *LexicalDC;
2459 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002460 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002461 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2462 return 0;
2463
2464 // Import the type.
2465 QualType T = Importer.Import(D->getType());
2466 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002467 return 0;
2468
2469 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2470 Expr *BitWidth = Importer.Import(D->getBitWidth());
2471 if (!BitWidth && D->getBitWidth())
2472 return 0;
2473
Abramo Bagnaradff19302011-03-08 08:55:46 +00002474 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2475 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002476 Loc, Name.getAsIdentifierInfo(),
2477 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002478 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002479 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002480 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002481 LexicalDC->addDecl(ToField);
2482 return ToField;
2483}
2484
Francois Pichet783dd6e2010-11-21 06:08:52 +00002485Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2486 // Import the major distinguishing characteristics of a variable.
2487 DeclContext *DC, *LexicalDC;
2488 DeclarationName Name;
2489 SourceLocation Loc;
2490 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2491 return 0;
2492
2493 // Import the type.
2494 QualType T = Importer.Import(D->getType());
2495 if (T.isNull())
2496 return 0;
2497
2498 NamedDecl **NamedChain =
2499 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2500
2501 unsigned i = 0;
2502 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2503 PE = D->chain_end(); PI != PE; ++PI) {
2504 Decl* D = Importer.Import(*PI);
2505 if (!D)
2506 return 0;
2507 NamedChain[i++] = cast<NamedDecl>(D);
2508 }
2509
2510 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2511 Importer.getToContext(), DC,
2512 Loc, Name.getAsIdentifierInfo(), T,
2513 NamedChain, D->getChainingSize());
2514 ToIndirectField->setAccess(D->getAccess());
2515 ToIndirectField->setLexicalDeclContext(LexicalDC);
2516 Importer.Imported(D, ToIndirectField);
2517 LexicalDC->addDecl(ToIndirectField);
2518 return ToIndirectField;
2519}
2520
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002521Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2522 // Import the major distinguishing characteristics of an ivar.
2523 DeclContext *DC, *LexicalDC;
2524 DeclarationName Name;
2525 SourceLocation Loc;
2526 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2527 return 0;
2528
2529 // Determine whether we've already imported this ivar
2530 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2531 Lookup.first != Lookup.second;
2532 ++Lookup.first) {
2533 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2534 if (Importer.IsStructurallyEquivalent(D->getType(),
2535 FoundIvar->getType())) {
2536 Importer.Imported(D, FoundIvar);
2537 return FoundIvar;
2538 }
2539
2540 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2541 << Name << D->getType() << FoundIvar->getType();
2542 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2543 << FoundIvar->getType();
2544 return 0;
2545 }
2546 }
2547
2548 // Import the type.
2549 QualType T = Importer.Import(D->getType());
2550 if (T.isNull())
2551 return 0;
2552
2553 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2554 Expr *BitWidth = Importer.Import(D->getBitWidth());
2555 if (!BitWidth && D->getBitWidth())
2556 return 0;
2557
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002558 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2559 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002560 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002561 Loc, Name.getAsIdentifierInfo(),
2562 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002563 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002564 ToIvar->setLexicalDeclContext(LexicalDC);
2565 Importer.Imported(D, ToIvar);
2566 LexicalDC->addDecl(ToIvar);
2567 return ToIvar;
2568
2569}
2570
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002571Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2572 // Import the major distinguishing characteristics of a variable.
2573 DeclContext *DC, *LexicalDC;
2574 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002575 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002576 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002577 return 0;
2578
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002579 // Try to find a variable in our own ("to") context with the same name and
2580 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002581 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002582 VarDecl *MergeWithVar = 0;
2583 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2584 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002585 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002586 Lookup.first != Lookup.second;
2587 ++Lookup.first) {
2588 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2589 continue;
2590
2591 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2592 // We have found a variable that we may need to merge with. Check it.
2593 if (isExternalLinkage(FoundVar->getLinkage()) &&
2594 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002595 if (Importer.IsStructurallyEquivalent(D->getType(),
2596 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002597 MergeWithVar = FoundVar;
2598 break;
2599 }
2600
Douglas Gregor56521c52010-02-12 17:23:39 +00002601 const ArrayType *FoundArray
2602 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2603 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002604 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002605 if (FoundArray && TArray) {
2606 if (isa<IncompleteArrayType>(FoundArray) &&
2607 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002608 // Import the type.
2609 QualType T = Importer.Import(D->getType());
2610 if (T.isNull())
2611 return 0;
2612
Douglas Gregor56521c52010-02-12 17:23:39 +00002613 FoundVar->setType(T);
2614 MergeWithVar = FoundVar;
2615 break;
2616 } else if (isa<IncompleteArrayType>(TArray) &&
2617 isa<ConstantArrayType>(FoundArray)) {
2618 MergeWithVar = FoundVar;
2619 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002620 }
2621 }
2622
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002623 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002624 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002625 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2626 << FoundVar->getType();
2627 }
2628 }
2629
2630 ConflictingDecls.push_back(*Lookup.first);
2631 }
2632
2633 if (MergeWithVar) {
2634 // An equivalent variable with external linkage has been found. Link
2635 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002636 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002637
2638 if (VarDecl *DDef = D->getDefinition()) {
2639 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2640 Importer.ToDiag(ExistingDef->getLocation(),
2641 diag::err_odr_variable_multiple_def)
2642 << Name;
2643 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2644 } else {
2645 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002646 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002647 }
2648 }
2649
2650 return MergeWithVar;
2651 }
2652
2653 if (!ConflictingDecls.empty()) {
2654 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2655 ConflictingDecls.data(),
2656 ConflictingDecls.size());
2657 if (!Name)
2658 return 0;
2659 }
2660 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002661
Douglas Gregorb4964f72010-02-15 23:54:17 +00002662 // Import the type.
2663 QualType T = Importer.Import(D->getType());
2664 if (T.isNull())
2665 return 0;
2666
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002667 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002668 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002669 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2670 Importer.Import(D->getInnerLocStart()),
2671 Loc, Name.getAsIdentifierInfo(),
2672 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002673 D->getStorageClass(),
2674 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002675 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002676 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002677 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002678 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002679 LexicalDC->addDecl(ToVar);
2680
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002681 // Merge the initializer.
2682 // FIXME: Can we really import any initializer? Alternatively, we could force
2683 // ourselves to import every declaration of a variable and then only use
2684 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002685 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002686
2687 // FIXME: Other bits to merge?
2688
2689 return ToVar;
2690}
2691
Douglas Gregor8b228d72010-02-17 21:22:52 +00002692Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2693 // Parameters are created in the translation unit's context, then moved
2694 // into the function declaration's context afterward.
2695 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2696
2697 // Import the name of this declaration.
2698 DeclarationName Name = Importer.Import(D->getDeclName());
2699 if (D->getDeclName() && !Name)
2700 return 0;
2701
2702 // Import the location of this declaration.
2703 SourceLocation Loc = Importer.Import(D->getLocation());
2704
2705 // Import the parameter's type.
2706 QualType T = Importer.Import(D->getType());
2707 if (T.isNull())
2708 return 0;
2709
2710 // Create the imported parameter.
2711 ImplicitParamDecl *ToParm
2712 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2713 Loc, Name.getAsIdentifierInfo(),
2714 T);
2715 return Importer.Imported(D, ToParm);
2716}
2717
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002718Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2719 // Parameters are created in the translation unit's context, then moved
2720 // into the function declaration's context afterward.
2721 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2722
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002723 // Import the name of this declaration.
2724 DeclarationName Name = Importer.Import(D->getDeclName());
2725 if (D->getDeclName() && !Name)
2726 return 0;
2727
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002728 // Import the location of this declaration.
2729 SourceLocation Loc = Importer.Import(D->getLocation());
2730
2731 // Import the parameter's type.
2732 QualType T = Importer.Import(D->getType());
2733 if (T.isNull())
2734 return 0;
2735
2736 // Create the imported parameter.
2737 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2738 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002739 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002740 Loc, Name.getAsIdentifierInfo(),
2741 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002742 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002743 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002744 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002745 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002746}
2747
Douglas Gregor43f54792010-02-17 02:12:47 +00002748Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2749 // Import the major distinguishing characteristics of a method.
2750 DeclContext *DC, *LexicalDC;
2751 DeclarationName Name;
2752 SourceLocation Loc;
2753 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2754 return 0;
2755
2756 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2757 Lookup.first != Lookup.second;
2758 ++Lookup.first) {
2759 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2760 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2761 continue;
2762
2763 // Check return types.
2764 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2765 FoundMethod->getResultType())) {
2766 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2767 << D->isInstanceMethod() << Name
2768 << D->getResultType() << FoundMethod->getResultType();
2769 Importer.ToDiag(FoundMethod->getLocation(),
2770 diag::note_odr_objc_method_here)
2771 << D->isInstanceMethod() << Name;
2772 return 0;
2773 }
2774
2775 // Check the number of parameters.
2776 if (D->param_size() != FoundMethod->param_size()) {
2777 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2778 << D->isInstanceMethod() << Name
2779 << D->param_size() << FoundMethod->param_size();
2780 Importer.ToDiag(FoundMethod->getLocation(),
2781 diag::note_odr_objc_method_here)
2782 << D->isInstanceMethod() << Name;
2783 return 0;
2784 }
2785
2786 // Check parameter types.
2787 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2788 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2789 P != PEnd; ++P, ++FoundP) {
2790 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2791 (*FoundP)->getType())) {
2792 Importer.FromDiag((*P)->getLocation(),
2793 diag::err_odr_objc_method_param_type_inconsistent)
2794 << D->isInstanceMethod() << Name
2795 << (*P)->getType() << (*FoundP)->getType();
2796 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2797 << (*FoundP)->getType();
2798 return 0;
2799 }
2800 }
2801
2802 // Check variadic/non-variadic.
2803 // Check the number of parameters.
2804 if (D->isVariadic() != FoundMethod->isVariadic()) {
2805 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2806 << D->isInstanceMethod() << Name;
2807 Importer.ToDiag(FoundMethod->getLocation(),
2808 diag::note_odr_objc_method_here)
2809 << D->isInstanceMethod() << Name;
2810 return 0;
2811 }
2812
2813 // FIXME: Any other bits we need to merge?
2814 return Importer.Imported(D, FoundMethod);
2815 }
2816 }
2817
2818 // Import the result type.
2819 QualType ResultTy = Importer.Import(D->getResultType());
2820 if (ResultTy.isNull())
2821 return 0;
2822
Douglas Gregor12852d92010-03-08 14:59:44 +00002823 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2824
Douglas Gregor43f54792010-02-17 02:12:47 +00002825 ObjCMethodDecl *ToMethod
2826 = ObjCMethodDecl::Create(Importer.getToContext(),
2827 Loc,
2828 Importer.Import(D->getLocEnd()),
2829 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002830 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002831 D->isInstanceMethod(),
2832 D->isVariadic(),
2833 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002834 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002835 D->getImplementationControl());
2836
2837 // FIXME: When we decide to merge method definitions, we'll need to
2838 // deal with implicit parameters.
2839
2840 // Import the parameters
2841 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2842 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2843 FromPEnd = D->param_end();
2844 FromP != FromPEnd;
2845 ++FromP) {
2846 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2847 if (!ToP)
2848 return 0;
2849
2850 ToParams.push_back(ToP);
2851 }
2852
2853 // Set the parameters.
2854 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2855 ToParams[I]->setOwningFunction(ToMethod);
2856 ToMethod->addDecl(ToParams[I]);
2857 }
2858 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002859 ToParams.data(), ToParams.size(),
2860 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002861
2862 ToMethod->setLexicalDeclContext(LexicalDC);
2863 Importer.Imported(D, ToMethod);
2864 LexicalDC->addDecl(ToMethod);
2865 return ToMethod;
2866}
2867
Douglas Gregor84c51c32010-02-18 01:47:50 +00002868Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2869 // Import the major distinguishing characteristics of a category.
2870 DeclContext *DC, *LexicalDC;
2871 DeclarationName Name;
2872 SourceLocation Loc;
2873 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2874 return 0;
2875
2876 ObjCInterfaceDecl *ToInterface
2877 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2878 if (!ToInterface)
2879 return 0;
2880
2881 // Determine if we've already encountered this category.
2882 ObjCCategoryDecl *MergeWithCategory
2883 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2884 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2885 if (!ToCategory) {
2886 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2887 Importer.Import(D->getAtLoc()),
2888 Loc,
2889 Importer.Import(D->getCategoryNameLoc()),
2890 Name.getAsIdentifierInfo());
2891 ToCategory->setLexicalDeclContext(LexicalDC);
2892 LexicalDC->addDecl(ToCategory);
2893 Importer.Imported(D, ToCategory);
2894
2895 // Link this category into its class's category list.
2896 ToCategory->setClassInterface(ToInterface);
2897 ToCategory->insertNextClassCategory();
2898
2899 // Import protocols
2900 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2901 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2902 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2903 = D->protocol_loc_begin();
2904 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2905 FromProtoEnd = D->protocol_end();
2906 FromProto != FromProtoEnd;
2907 ++FromProto, ++FromProtoLoc) {
2908 ObjCProtocolDecl *ToProto
2909 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2910 if (!ToProto)
2911 return 0;
2912 Protocols.push_back(ToProto);
2913 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2914 }
2915
2916 // FIXME: If we're merging, make sure that the protocol list is the same.
2917 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2918 ProtocolLocs.data(), Importer.getToContext());
2919
2920 } else {
2921 Importer.Imported(D, ToCategory);
2922 }
2923
2924 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002925 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002926
2927 // If we have an implementation, import it as well.
2928 if (D->getImplementation()) {
2929 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002930 = cast_or_null<ObjCCategoryImplDecl>(
2931 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002932 if (!Impl)
2933 return 0;
2934
2935 ToCategory->setImplementation(Impl);
2936 }
2937
2938 return ToCategory;
2939}
2940
Douglas Gregor98d156a2010-02-17 16:12:00 +00002941Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002942 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002943 DeclContext *DC, *LexicalDC;
2944 DeclarationName Name;
2945 SourceLocation Loc;
2946 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2947 return 0;
2948
2949 ObjCProtocolDecl *MergeWithProtocol = 0;
2950 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2951 Lookup.first != Lookup.second;
2952 ++Lookup.first) {
2953 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2954 continue;
2955
2956 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2957 break;
2958 }
2959
2960 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2961 if (!ToProto || ToProto->isForwardDecl()) {
2962 if (!ToProto) {
2963 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2964 Name.getAsIdentifierInfo());
2965 ToProto->setForwardDecl(D->isForwardDecl());
2966 ToProto->setLexicalDeclContext(LexicalDC);
2967 LexicalDC->addDecl(ToProto);
2968 }
2969 Importer.Imported(D, ToProto);
2970
2971 // Import protocols
2972 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2973 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2974 ObjCProtocolDecl::protocol_loc_iterator
2975 FromProtoLoc = D->protocol_loc_begin();
2976 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2977 FromProtoEnd = D->protocol_end();
2978 FromProto != FromProtoEnd;
2979 ++FromProto, ++FromProtoLoc) {
2980 ObjCProtocolDecl *ToProto
2981 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2982 if (!ToProto)
2983 return 0;
2984 Protocols.push_back(ToProto);
2985 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2986 }
2987
2988 // FIXME: If we're merging, make sure that the protocol list is the same.
2989 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2990 ProtocolLocs.data(), Importer.getToContext());
2991 } else {
2992 Importer.Imported(D, ToProto);
2993 }
2994
Douglas Gregor84c51c32010-02-18 01:47:50 +00002995 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002996 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002997
2998 return ToProto;
2999}
3000
Douglas Gregor45635322010-02-16 01:20:57 +00003001Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3002 // Import the major distinguishing characteristics of an @interface.
3003 DeclContext *DC, *LexicalDC;
3004 DeclarationName Name;
3005 SourceLocation Loc;
3006 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3007 return 0;
3008
3009 ObjCInterfaceDecl *MergeWithIface = 0;
3010 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3011 Lookup.first != Lookup.second;
3012 ++Lookup.first) {
3013 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3014 continue;
3015
3016 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3017 break;
3018 }
3019
3020 ObjCInterfaceDecl *ToIface = MergeWithIface;
3021 if (!ToIface || ToIface->isForwardDecl()) {
3022 if (!ToIface) {
3023 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3024 DC, Loc,
3025 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003026 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003027 D->isForwardDecl(),
3028 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003029 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003030 ToIface->setLexicalDeclContext(LexicalDC);
3031 LexicalDC->addDecl(ToIface);
3032 }
3033 Importer.Imported(D, ToIface);
3034
Douglas Gregor45635322010-02-16 01:20:57 +00003035 if (D->getSuperClass()) {
3036 ObjCInterfaceDecl *Super
3037 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3038 if (!Super)
3039 return 0;
3040
3041 ToIface->setSuperClass(Super);
3042 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3043 }
3044
3045 // Import protocols
3046 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3047 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3048 ObjCInterfaceDecl::protocol_loc_iterator
3049 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003050
3051 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003052 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3053 FromProtoEnd = D->protocol_end();
3054 FromProto != FromProtoEnd;
3055 ++FromProto, ++FromProtoLoc) {
3056 ObjCProtocolDecl *ToProto
3057 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3058 if (!ToProto)
3059 return 0;
3060 Protocols.push_back(ToProto);
3061 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3062 }
3063
3064 // FIXME: If we're merging, make sure that the protocol list is the same.
3065 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3066 ProtocolLocs.data(), Importer.getToContext());
3067
Douglas Gregor45635322010-02-16 01:20:57 +00003068 // Import @end range
3069 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3070 } else {
3071 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003072
3073 // Check for consistency of superclasses.
3074 DeclarationName FromSuperName, ToSuperName;
3075 if (D->getSuperClass())
3076 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3077 if (ToIface->getSuperClass())
3078 ToSuperName = ToIface->getSuperClass()->getDeclName();
3079 if (FromSuperName != ToSuperName) {
3080 Importer.ToDiag(ToIface->getLocation(),
3081 diag::err_odr_objc_superclass_inconsistent)
3082 << ToIface->getDeclName();
3083 if (ToIface->getSuperClass())
3084 Importer.ToDiag(ToIface->getSuperClassLoc(),
3085 diag::note_odr_objc_superclass)
3086 << ToIface->getSuperClass()->getDeclName();
3087 else
3088 Importer.ToDiag(ToIface->getLocation(),
3089 diag::note_odr_objc_missing_superclass);
3090 if (D->getSuperClass())
3091 Importer.FromDiag(D->getSuperClassLoc(),
3092 diag::note_odr_objc_superclass)
3093 << D->getSuperClass()->getDeclName();
3094 else
3095 Importer.FromDiag(D->getLocation(),
3096 diag::note_odr_objc_missing_superclass);
3097 return 0;
3098 }
Douglas Gregor45635322010-02-16 01:20:57 +00003099 }
3100
Douglas Gregor84c51c32010-02-18 01:47:50 +00003101 // Import categories. When the categories themselves are imported, they'll
3102 // hook themselves into this interface.
3103 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3104 FromCat = FromCat->getNextClassCategory())
3105 Importer.Import(FromCat);
3106
Douglas Gregor45635322010-02-16 01:20:57 +00003107 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003108 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003109
3110 // If we have an @implementation, import it as well.
3111 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003112 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3113 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003114 if (!Impl)
3115 return 0;
3116
3117 ToIface->setImplementation(Impl);
3118 }
3119
Douglas Gregor98d156a2010-02-17 16:12:00 +00003120 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003121}
3122
Douglas Gregor4da9d682010-12-07 15:32:12 +00003123Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3124 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3125 Importer.Import(D->getCategoryDecl()));
3126 if (!Category)
3127 return 0;
3128
3129 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3130 if (!ToImpl) {
3131 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3132 if (!DC)
3133 return 0;
3134
3135 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3136 Importer.Import(D->getLocation()),
3137 Importer.Import(D->getIdentifier()),
3138 Category->getClassInterface());
3139
3140 DeclContext *LexicalDC = DC;
3141 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3142 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3143 if (!LexicalDC)
3144 return 0;
3145
3146 ToImpl->setLexicalDeclContext(LexicalDC);
3147 }
3148
3149 LexicalDC->addDecl(ToImpl);
3150 Category->setImplementation(ToImpl);
3151 }
3152
3153 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003154 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003155 return ToImpl;
3156}
3157
Douglas Gregorda8025c2010-12-07 01:26:03 +00003158Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3159 // Find the corresponding interface.
3160 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3161 Importer.Import(D->getClassInterface()));
3162 if (!Iface)
3163 return 0;
3164
3165 // Import the superclass, if any.
3166 ObjCInterfaceDecl *Super = 0;
3167 if (D->getSuperClass()) {
3168 Super = cast_or_null<ObjCInterfaceDecl>(
3169 Importer.Import(D->getSuperClass()));
3170 if (!Super)
3171 return 0;
3172 }
3173
3174 ObjCImplementationDecl *Impl = Iface->getImplementation();
3175 if (!Impl) {
3176 // We haven't imported an implementation yet. Create a new @implementation
3177 // now.
3178 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3179 Importer.ImportContext(D->getDeclContext()),
3180 Importer.Import(D->getLocation()),
3181 Iface, Super);
3182
3183 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3184 DeclContext *LexicalDC
3185 = Importer.ImportContext(D->getLexicalDeclContext());
3186 if (!LexicalDC)
3187 return 0;
3188 Impl->setLexicalDeclContext(LexicalDC);
3189 }
3190
3191 // Associate the implementation with the class it implements.
3192 Iface->setImplementation(Impl);
3193 Importer.Imported(D, Iface->getImplementation());
3194 } else {
3195 Importer.Imported(D, Iface->getImplementation());
3196
3197 // Verify that the existing @implementation has the same superclass.
3198 if ((Super && !Impl->getSuperClass()) ||
3199 (!Super && Impl->getSuperClass()) ||
3200 (Super && Impl->getSuperClass() &&
3201 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3202 Importer.ToDiag(Impl->getLocation(),
3203 diag::err_odr_objc_superclass_inconsistent)
3204 << Iface->getDeclName();
3205 // FIXME: It would be nice to have the location of the superclass
3206 // below.
3207 if (Impl->getSuperClass())
3208 Importer.ToDiag(Impl->getLocation(),
3209 diag::note_odr_objc_superclass)
3210 << Impl->getSuperClass()->getDeclName();
3211 else
3212 Importer.ToDiag(Impl->getLocation(),
3213 diag::note_odr_objc_missing_superclass);
3214 if (D->getSuperClass())
3215 Importer.FromDiag(D->getLocation(),
3216 diag::note_odr_objc_superclass)
3217 << D->getSuperClass()->getDeclName();
3218 else
3219 Importer.FromDiag(D->getLocation(),
3220 diag::note_odr_objc_missing_superclass);
3221 return 0;
3222 }
3223 }
3224
3225 // Import all of the members of this @implementation.
3226 ImportDeclContext(D);
3227
3228 return Impl;
3229}
3230
Douglas Gregora11c4582010-02-17 18:02:10 +00003231Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3232 // Import the major distinguishing characteristics of an @property.
3233 DeclContext *DC, *LexicalDC;
3234 DeclarationName Name;
3235 SourceLocation Loc;
3236 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3237 return 0;
3238
3239 // Check whether we have already imported this property.
3240 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3241 Lookup.first != Lookup.second;
3242 ++Lookup.first) {
3243 if (ObjCPropertyDecl *FoundProp
3244 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3245 // Check property types.
3246 if (!Importer.IsStructurallyEquivalent(D->getType(),
3247 FoundProp->getType())) {
3248 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3249 << Name << D->getType() << FoundProp->getType();
3250 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3251 << FoundProp->getType();
3252 return 0;
3253 }
3254
3255 // FIXME: Check property attributes, getters, setters, etc.?
3256
3257 // Consider these properties to be equivalent.
3258 Importer.Imported(D, FoundProp);
3259 return FoundProp;
3260 }
3261 }
3262
3263 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003264 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3265 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003266 return 0;
3267
3268 // Create the new property.
3269 ObjCPropertyDecl *ToProperty
3270 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3271 Name.getAsIdentifierInfo(),
3272 Importer.Import(D->getAtLoc()),
3273 T,
3274 D->getPropertyImplementation());
3275 Importer.Imported(D, ToProperty);
3276 ToProperty->setLexicalDeclContext(LexicalDC);
3277 LexicalDC->addDecl(ToProperty);
3278
3279 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003280 ToProperty->setPropertyAttributesAsWritten(
3281 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003282 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3283 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3284 ToProperty->setGetterMethodDecl(
3285 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3286 ToProperty->setSetterMethodDecl(
3287 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3288 ToProperty->setPropertyIvarDecl(
3289 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3290 return ToProperty;
3291}
3292
Douglas Gregor14a49e22010-12-07 18:32:03 +00003293Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3294 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3295 Importer.Import(D->getPropertyDecl()));
3296 if (!Property)
3297 return 0;
3298
3299 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3300 if (!DC)
3301 return 0;
3302
3303 // Import the lexical declaration context.
3304 DeclContext *LexicalDC = DC;
3305 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3306 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3307 if (!LexicalDC)
3308 return 0;
3309 }
3310
3311 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3312 if (!InImpl)
3313 return 0;
3314
3315 // Import the ivar (for an @synthesize).
3316 ObjCIvarDecl *Ivar = 0;
3317 if (D->getPropertyIvarDecl()) {
3318 Ivar = cast_or_null<ObjCIvarDecl>(
3319 Importer.Import(D->getPropertyIvarDecl()));
3320 if (!Ivar)
3321 return 0;
3322 }
3323
3324 ObjCPropertyImplDecl *ToImpl
3325 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3326 if (!ToImpl) {
3327 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3328 Importer.Import(D->getLocStart()),
3329 Importer.Import(D->getLocation()),
3330 Property,
3331 D->getPropertyImplementation(),
3332 Ivar,
3333 Importer.Import(D->getPropertyIvarDeclLoc()));
3334 ToImpl->setLexicalDeclContext(LexicalDC);
3335 Importer.Imported(D, ToImpl);
3336 LexicalDC->addDecl(ToImpl);
3337 } else {
3338 // Check that we have the same kind of property implementation (@synthesize
3339 // vs. @dynamic).
3340 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3341 Importer.ToDiag(ToImpl->getLocation(),
3342 diag::err_odr_objc_property_impl_kind_inconsistent)
3343 << Property->getDeclName()
3344 << (ToImpl->getPropertyImplementation()
3345 == ObjCPropertyImplDecl::Dynamic);
3346 Importer.FromDiag(D->getLocation(),
3347 diag::note_odr_objc_property_impl_kind)
3348 << D->getPropertyDecl()->getDeclName()
3349 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3350 return 0;
3351 }
3352
3353 // For @synthesize, check that we have the same
3354 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3355 Ivar != ToImpl->getPropertyIvarDecl()) {
3356 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3357 diag::err_odr_objc_synthesize_ivar_inconsistent)
3358 << Property->getDeclName()
3359 << ToImpl->getPropertyIvarDecl()->getDeclName()
3360 << Ivar->getDeclName();
3361 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3362 diag::note_odr_objc_synthesize_ivar_here)
3363 << D->getPropertyIvarDecl()->getDeclName();
3364 return 0;
3365 }
3366
3367 // Merge the existing implementation with the new implementation.
3368 Importer.Imported(D, ToImpl);
3369 }
3370
3371 return ToImpl;
3372}
3373
Douglas Gregor8661a722010-02-18 02:12:22 +00003374Decl *
3375ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3376 // Import the context of this declaration.
3377 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3378 if (!DC)
3379 return 0;
3380
3381 DeclContext *LexicalDC = DC;
3382 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3383 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3384 if (!LexicalDC)
3385 return 0;
3386 }
3387
3388 // Import the location of this declaration.
3389 SourceLocation Loc = Importer.Import(D->getLocation());
3390
3391 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3392 llvm::SmallVector<SourceLocation, 4> Locations;
3393 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3394 = D->protocol_loc_begin();
3395 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3396 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3397 FromProto != FromProtoEnd;
3398 ++FromProto, ++FromProtoLoc) {
3399 ObjCProtocolDecl *ToProto
3400 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3401 if (!ToProto)
3402 continue;
3403
3404 Protocols.push_back(ToProto);
3405 Locations.push_back(Importer.Import(*FromProtoLoc));
3406 }
3407
3408 ObjCForwardProtocolDecl *ToForward
3409 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3410 Protocols.data(), Protocols.size(),
3411 Locations.data());
3412 ToForward->setLexicalDeclContext(LexicalDC);
3413 LexicalDC->addDecl(ToForward);
3414 Importer.Imported(D, ToForward);
3415 return ToForward;
3416}
3417
Douglas Gregor06537af2010-02-18 02:04:09 +00003418Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3419 // Import the context of this declaration.
3420 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3421 if (!DC)
3422 return 0;
3423
3424 DeclContext *LexicalDC = DC;
3425 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3426 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3427 if (!LexicalDC)
3428 return 0;
3429 }
3430
3431 // Import the location of this declaration.
3432 SourceLocation Loc = Importer.Import(D->getLocation());
3433
3434 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3435 llvm::SmallVector<SourceLocation, 4> Locations;
3436 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3437 From != FromEnd; ++From) {
3438 ObjCInterfaceDecl *ToIface
3439 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3440 if (!ToIface)
3441 continue;
3442
3443 Interfaces.push_back(ToIface);
3444 Locations.push_back(Importer.Import(From->getLocation()));
3445 }
3446
3447 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3448 Loc,
3449 Interfaces.data(),
3450 Locations.data(),
3451 Interfaces.size());
3452 ToClass->setLexicalDeclContext(LexicalDC);
3453 LexicalDC->addDecl(ToClass);
3454 Importer.Imported(D, ToClass);
3455 return ToClass;
3456}
3457
Douglas Gregora082a492010-11-30 19:14:50 +00003458Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3459 // For template arguments, we adopt the translation unit as our declaration
3460 // context. This context will be fixed when the actual template declaration
3461 // is created.
3462
3463 // FIXME: Import default argument.
3464 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3465 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003466 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003467 Importer.Import(D->getLocation()),
3468 D->getDepth(),
3469 D->getIndex(),
3470 Importer.Import(D->getIdentifier()),
3471 D->wasDeclaredWithTypename(),
3472 D->isParameterPack());
3473}
3474
3475Decl *
3476ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3477 // Import the name of this declaration.
3478 DeclarationName Name = Importer.Import(D->getDeclName());
3479 if (D->getDeclName() && !Name)
3480 return 0;
3481
3482 // Import the location of this declaration.
3483 SourceLocation Loc = Importer.Import(D->getLocation());
3484
3485 // Import the type of this declaration.
3486 QualType T = Importer.Import(D->getType());
3487 if (T.isNull())
3488 return 0;
3489
3490 // Import type-source information.
3491 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3492 if (D->getTypeSourceInfo() && !TInfo)
3493 return 0;
3494
3495 // FIXME: Import default argument.
3496
3497 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3498 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003499 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003500 Loc, D->getDepth(), D->getPosition(),
3501 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003502 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003503}
3504
3505Decl *
3506ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3507 // Import the name of this declaration.
3508 DeclarationName Name = Importer.Import(D->getDeclName());
3509 if (D->getDeclName() && !Name)
3510 return 0;
3511
3512 // Import the location of this declaration.
3513 SourceLocation Loc = Importer.Import(D->getLocation());
3514
3515 // Import template parameters.
3516 TemplateParameterList *TemplateParams
3517 = ImportTemplateParameterList(D->getTemplateParameters());
3518 if (!TemplateParams)
3519 return 0;
3520
3521 // FIXME: Import default argument.
3522
3523 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3524 Importer.getToContext().getTranslationUnitDecl(),
3525 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003526 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003527 Name.getAsIdentifierInfo(),
3528 TemplateParams);
3529}
3530
3531Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3532 // If this record has a definition in the translation unit we're coming from,
3533 // but this particular declaration is not that definition, import the
3534 // definition and map to that.
3535 CXXRecordDecl *Definition
3536 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3537 if (Definition && Definition != D->getTemplatedDecl()) {
3538 Decl *ImportedDef
3539 = Importer.Import(Definition->getDescribedClassTemplate());
3540 if (!ImportedDef)
3541 return 0;
3542
3543 return Importer.Imported(D, ImportedDef);
3544 }
3545
3546 // Import the major distinguishing characteristics of this class template.
3547 DeclContext *DC, *LexicalDC;
3548 DeclarationName Name;
3549 SourceLocation Loc;
3550 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3551 return 0;
3552
3553 // We may already have a template of the same name; try to find and match it.
3554 if (!DC->isFunctionOrMethod()) {
3555 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3556 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3557 Lookup.first != Lookup.second;
3558 ++Lookup.first) {
3559 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3560 continue;
3561
3562 Decl *Found = *Lookup.first;
3563 if (ClassTemplateDecl *FoundTemplate
3564 = dyn_cast<ClassTemplateDecl>(Found)) {
3565 if (IsStructuralMatch(D, FoundTemplate)) {
3566 // The class templates structurally match; call it the same template.
3567 // FIXME: We may be filling in a forward declaration here. Handle
3568 // this case!
3569 Importer.Imported(D->getTemplatedDecl(),
3570 FoundTemplate->getTemplatedDecl());
3571 return Importer.Imported(D, FoundTemplate);
3572 }
3573 }
3574
3575 ConflictingDecls.push_back(*Lookup.first);
3576 }
3577
3578 if (!ConflictingDecls.empty()) {
3579 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3580 ConflictingDecls.data(),
3581 ConflictingDecls.size());
3582 }
3583
3584 if (!Name)
3585 return 0;
3586 }
3587
3588 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3589
3590 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003591 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3592 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003593 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3594 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003595 DC, StartLoc, IdLoc,
3596 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003597 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003598 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003599 D2Templated->setLexicalDeclContext(LexicalDC);
3600
3601 // Create the class template declaration itself.
3602 TemplateParameterList *TemplateParams
3603 = ImportTemplateParameterList(D->getTemplateParameters());
3604 if (!TemplateParams)
3605 return 0;
3606
3607 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3608 Loc, Name, TemplateParams,
3609 D2Templated,
3610 /*PrevDecl=*/0);
3611 D2Templated->setDescribedClassTemplate(D2);
3612
3613 D2->setAccess(D->getAccess());
3614 D2->setLexicalDeclContext(LexicalDC);
3615 LexicalDC->addDecl(D2);
3616
3617 // Note the relationship between the class templates.
3618 Importer.Imported(D, D2);
3619 Importer.Imported(DTemplated, D2Templated);
3620
3621 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3622 // FIXME: Import definition!
3623 }
3624
3625 return D2;
3626}
3627
Douglas Gregore2e50d332010-12-01 01:36:18 +00003628Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3629 ClassTemplateSpecializationDecl *D) {
3630 // If this record has a definition in the translation unit we're coming from,
3631 // but this particular declaration is not that definition, import the
3632 // definition and map to that.
3633 TagDecl *Definition = D->getDefinition();
3634 if (Definition && Definition != D) {
3635 Decl *ImportedDef = Importer.Import(Definition);
3636 if (!ImportedDef)
3637 return 0;
3638
3639 return Importer.Imported(D, ImportedDef);
3640 }
3641
3642 ClassTemplateDecl *ClassTemplate
3643 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3644 D->getSpecializedTemplate()));
3645 if (!ClassTemplate)
3646 return 0;
3647
3648 // Import the context of this declaration.
3649 DeclContext *DC = ClassTemplate->getDeclContext();
3650 if (!DC)
3651 return 0;
3652
3653 DeclContext *LexicalDC = DC;
3654 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3655 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3656 if (!LexicalDC)
3657 return 0;
3658 }
3659
3660 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003661 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3662 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003663
3664 // Import template arguments.
3665 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3666 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3667 D->getTemplateArgs().size(),
3668 TemplateArgs))
3669 return 0;
3670
3671 // Try to find an existing specialization with these template arguments.
3672 void *InsertPos = 0;
3673 ClassTemplateSpecializationDecl *D2
3674 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3675 TemplateArgs.size(), InsertPos);
3676 if (D2) {
3677 // We already have a class template specialization with these template
3678 // arguments.
3679
3680 // FIXME: Check for specialization vs. instantiation errors.
3681
3682 if (RecordDecl *FoundDef = D2->getDefinition()) {
3683 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3684 // The record types structurally match, or the "from" translation
3685 // unit only had a forward declaration anyway; call it the same
3686 // function.
3687 return Importer.Imported(D, FoundDef);
3688 }
3689 }
3690 } else {
3691 // Create a new specialization.
3692 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3693 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003694 StartLoc, IdLoc,
3695 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003696 TemplateArgs.data(),
3697 TemplateArgs.size(),
3698 /*PrevDecl=*/0);
3699 D2->setSpecializationKind(D->getSpecializationKind());
3700
3701 // Add this specialization to the class template.
3702 ClassTemplate->AddSpecialization(D2, InsertPos);
3703
3704 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003705 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003706
3707 // Add the specialization to this context.
3708 D2->setLexicalDeclContext(LexicalDC);
3709 LexicalDC->addDecl(D2);
3710 }
3711 Importer.Imported(D, D2);
3712
3713 if (D->isDefinition() && ImportDefinition(D, D2))
3714 return 0;
3715
3716 return D2;
3717}
3718
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003719//----------------------------------------------------------------------------
3720// Import Statements
3721//----------------------------------------------------------------------------
3722
3723Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3724 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3725 << S->getStmtClassName();
3726 return 0;
3727}
3728
3729//----------------------------------------------------------------------------
3730// Import Expressions
3731//----------------------------------------------------------------------------
3732Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3733 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3734 << E->getStmtClassName();
3735 return 0;
3736}
3737
Douglas Gregor52f820e2010-02-19 01:17:02 +00003738Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3739 NestedNameSpecifier *Qualifier = 0;
3740 if (E->getQualifier()) {
3741 Qualifier = Importer.Import(E->getQualifier());
3742 if (!E->getQualifier())
3743 return 0;
3744 }
3745
3746 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3747 if (!ToD)
3748 return 0;
3749
3750 QualType T = Importer.Import(E->getType());
3751 if (T.isNull())
3752 return 0;
3753
Douglas Gregorea972d32011-02-28 21:54:11 +00003754 return DeclRefExpr::Create(Importer.getToContext(),
3755 Importer.Import(E->getQualifierLoc()),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003756 ToD,
3757 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003758 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003759 /*FIXME:TemplateArgs=*/0);
3760}
3761
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003762Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3763 QualType T = Importer.Import(E->getType());
3764 if (T.isNull())
3765 return 0;
3766
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003767 return IntegerLiteral::Create(Importer.getToContext(),
3768 E->getValue(), T,
3769 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003770}
3771
Douglas Gregor623421d2010-02-18 02:21:22 +00003772Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3773 QualType T = Importer.Import(E->getType());
3774 if (T.isNull())
3775 return 0;
3776
3777 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3778 E->isWide(), T,
3779 Importer.Import(E->getLocation()));
3780}
3781
Douglas Gregorc74247e2010-02-19 01:07:06 +00003782Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3783 Expr *SubExpr = Importer.Import(E->getSubExpr());
3784 if (!SubExpr)
3785 return 0;
3786
3787 return new (Importer.getToContext())
3788 ParenExpr(Importer.Import(E->getLParen()),
3789 Importer.Import(E->getRParen()),
3790 SubExpr);
3791}
3792
3793Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3794 QualType T = Importer.Import(E->getType());
3795 if (T.isNull())
3796 return 0;
3797
3798 Expr *SubExpr = Importer.Import(E->getSubExpr());
3799 if (!SubExpr)
3800 return 0;
3801
3802 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003803 T, E->getValueKind(),
3804 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003805 Importer.Import(E->getOperatorLoc()));
3806}
3807
Peter Collingbournee190dee2011-03-11 19:24:49 +00003808Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3809 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00003810 QualType ResultType = Importer.Import(E->getType());
3811
3812 if (E->isArgumentType()) {
3813 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3814 if (!TInfo)
3815 return 0;
3816
Peter Collingbournee190dee2011-03-11 19:24:49 +00003817 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3818 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003819 Importer.Import(E->getOperatorLoc()),
3820 Importer.Import(E->getRParenLoc()));
3821 }
3822
3823 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3824 if (!SubExpr)
3825 return 0;
3826
Peter Collingbournee190dee2011-03-11 19:24:49 +00003827 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3828 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003829 Importer.Import(E->getOperatorLoc()),
3830 Importer.Import(E->getRParenLoc()));
3831}
3832
Douglas Gregorc74247e2010-02-19 01:07:06 +00003833Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3834 QualType T = Importer.Import(E->getType());
3835 if (T.isNull())
3836 return 0;
3837
3838 Expr *LHS = Importer.Import(E->getLHS());
3839 if (!LHS)
3840 return 0;
3841
3842 Expr *RHS = Importer.Import(E->getRHS());
3843 if (!RHS)
3844 return 0;
3845
3846 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003847 T, E->getValueKind(),
3848 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003849 Importer.Import(E->getOperatorLoc()));
3850}
3851
3852Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3853 QualType T = Importer.Import(E->getType());
3854 if (T.isNull())
3855 return 0;
3856
3857 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3858 if (CompLHSType.isNull())
3859 return 0;
3860
3861 QualType CompResultType = Importer.Import(E->getComputationResultType());
3862 if (CompResultType.isNull())
3863 return 0;
3864
3865 Expr *LHS = Importer.Import(E->getLHS());
3866 if (!LHS)
3867 return 0;
3868
3869 Expr *RHS = Importer.Import(E->getRHS());
3870 if (!RHS)
3871 return 0;
3872
3873 return new (Importer.getToContext())
3874 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003875 T, E->getValueKind(),
3876 E->getObjectKind(),
3877 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003878 Importer.Import(E->getOperatorLoc()));
3879}
3880
Benjamin Kramer8aef5962011-03-26 12:38:21 +00003881static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00003882 if (E->path_empty()) return false;
3883
3884 // TODO: import cast paths
3885 return true;
3886}
3887
Douglas Gregor98c10182010-02-12 22:17:39 +00003888Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3889 QualType T = Importer.Import(E->getType());
3890 if (T.isNull())
3891 return 0;
3892
3893 Expr *SubExpr = Importer.Import(E->getSubExpr());
3894 if (!SubExpr)
3895 return 0;
John McCallcf142162010-08-07 06:22:56 +00003896
3897 CXXCastPath BasePath;
3898 if (ImportCastPath(E, BasePath))
3899 return 0;
3900
3901 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003902 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003903}
3904
Douglas Gregor5481d322010-02-19 01:32:14 +00003905Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3906 QualType T = Importer.Import(E->getType());
3907 if (T.isNull())
3908 return 0;
3909
3910 Expr *SubExpr = Importer.Import(E->getSubExpr());
3911 if (!SubExpr)
3912 return 0;
3913
3914 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3915 if (!TInfo && E->getTypeInfoAsWritten())
3916 return 0;
3917
John McCallcf142162010-08-07 06:22:56 +00003918 CXXCastPath BasePath;
3919 if (ImportCastPath(E, BasePath))
3920 return 0;
3921
John McCall7decc9e2010-11-18 06:31:45 +00003922 return CStyleCastExpr::Create(Importer.getToContext(), T,
3923 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003924 SubExpr, &BasePath, TInfo,
3925 Importer.Import(E->getLParenLoc()),
3926 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003927}
3928
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003929ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003930 ASTContext &FromContext, FileManager &FromFileManager,
3931 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003932 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003933 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3934 Minimal(MinimalImport)
3935{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003936 ImportedDecls[FromContext.getTranslationUnitDecl()]
3937 = ToContext.getTranslationUnitDecl();
3938}
3939
3940ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003941
3942QualType ASTImporter::Import(QualType FromT) {
3943 if (FromT.isNull())
3944 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003945
3946 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003947
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003948 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003949 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3950 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003951 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003952 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003953
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003954 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003955 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003956 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003957 if (ToT.isNull())
3958 return ToT;
3959
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003960 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003961 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003962
John McCall424cec92011-01-19 06:33:43 +00003963 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003964}
3965
Douglas Gregor62d311f2010-02-09 19:21:46 +00003966TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003967 if (!FromTSI)
3968 return FromTSI;
3969
3970 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003971 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003972 QualType T = Import(FromTSI->getType());
3973 if (T.isNull())
3974 return 0;
3975
3976 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003977 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003978}
3979
3980Decl *ASTImporter::Import(Decl *FromD) {
3981 if (!FromD)
3982 return 0;
3983
3984 // Check whether we've already imported this declaration.
3985 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3986 if (Pos != ImportedDecls.end())
3987 return Pos->second;
3988
3989 // Import the type
3990 ASTNodeImporter Importer(*this);
3991 Decl *ToD = Importer.Visit(FromD);
3992 if (!ToD)
3993 return 0;
3994
3995 // Record the imported declaration.
3996 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003997
3998 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3999 // Keep track of anonymous tags that have an associated typedef.
4000 if (FromTag->getTypedefForAnonDecl())
4001 AnonTagsWithPendingTypedefs.push_back(FromTag);
4002 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
4003 // When we've finished transforming a typedef, see whether it was the
4004 // typedef for an anonymous tag.
4005 for (llvm::SmallVector<TagDecl *, 4>::iterator
4006 FromTag = AnonTagsWithPendingTypedefs.begin(),
4007 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4008 FromTag != FromTagEnd; ++FromTag) {
4009 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
4010 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4011 // We found the typedef for an anonymous tag; link them.
4012 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
4013 AnonTagsWithPendingTypedefs.erase(FromTag);
4014 break;
4015 }
4016 }
4017 }
4018 }
4019
Douglas Gregor62d311f2010-02-09 19:21:46 +00004020 return ToD;
4021}
4022
4023DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4024 if (!FromDC)
4025 return FromDC;
4026
4027 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4028}
4029
4030Expr *ASTImporter::Import(Expr *FromE) {
4031 if (!FromE)
4032 return 0;
4033
4034 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4035}
4036
4037Stmt *ASTImporter::Import(Stmt *FromS) {
4038 if (!FromS)
4039 return 0;
4040
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004041 // Check whether we've already imported this declaration.
4042 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4043 if (Pos != ImportedStmts.end())
4044 return Pos->second;
4045
4046 // Import the type
4047 ASTNodeImporter Importer(*this);
4048 Stmt *ToS = Importer.Visit(FromS);
4049 if (!ToS)
4050 return 0;
4051
4052 // Record the imported declaration.
4053 ImportedStmts[FromS] = ToS;
4054 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004055}
4056
4057NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4058 if (!FromNNS)
4059 return 0;
4060
4061 // FIXME: Implement!
4062 return 0;
4063}
4064
Douglas Gregor14454802011-02-25 02:25:35 +00004065NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4066 // FIXME: Implement!
4067 return NestedNameSpecifierLoc();
4068}
4069
Douglas Gregore2e50d332010-12-01 01:36:18 +00004070TemplateName ASTImporter::Import(TemplateName From) {
4071 switch (From.getKind()) {
4072 case TemplateName::Template:
4073 if (TemplateDecl *ToTemplate
4074 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4075 return TemplateName(ToTemplate);
4076
4077 return TemplateName();
4078
4079 case TemplateName::OverloadedTemplate: {
4080 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4081 UnresolvedSet<2> ToTemplates;
4082 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4083 E = FromStorage->end();
4084 I != E; ++I) {
4085 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4086 ToTemplates.addDecl(To);
4087 else
4088 return TemplateName();
4089 }
4090 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4091 ToTemplates.end());
4092 }
4093
4094 case TemplateName::QualifiedTemplate: {
4095 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4096 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4097 if (!Qualifier)
4098 return TemplateName();
4099
4100 if (TemplateDecl *ToTemplate
4101 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4102 return ToContext.getQualifiedTemplateName(Qualifier,
4103 QTN->hasTemplateKeyword(),
4104 ToTemplate);
4105
4106 return TemplateName();
4107 }
4108
4109 case TemplateName::DependentTemplate: {
4110 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4111 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4112 if (!Qualifier)
4113 return TemplateName();
4114
4115 if (DTN->isIdentifier()) {
4116 return ToContext.getDependentTemplateName(Qualifier,
4117 Import(DTN->getIdentifier()));
4118 }
4119
4120 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4121 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004122
4123 case TemplateName::SubstTemplateTemplateParmPack: {
4124 SubstTemplateTemplateParmPackStorage *SubstPack
4125 = From.getAsSubstTemplateTemplateParmPack();
4126 TemplateTemplateParmDecl *Param
4127 = cast_or_null<TemplateTemplateParmDecl>(
4128 Import(SubstPack->getParameterPack()));
4129 if (!Param)
4130 return TemplateName();
4131
4132 ASTNodeImporter Importer(*this);
4133 TemplateArgument ArgPack
4134 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4135 if (ArgPack.isNull())
4136 return TemplateName();
4137
4138 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4139 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004140 }
4141
4142 llvm_unreachable("Invalid template name kind");
4143 return TemplateName();
4144}
4145
Douglas Gregor62d311f2010-02-09 19:21:46 +00004146SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4147 if (FromLoc.isInvalid())
4148 return SourceLocation();
4149
Douglas Gregor811663e2010-02-10 00:15:17 +00004150 SourceManager &FromSM = FromContext.getSourceManager();
4151
4152 // For now, map everything down to its spelling location, so that we
4153 // don't have to import macro instantiations.
4154 // FIXME: Import macro instantiations!
4155 FromLoc = FromSM.getSpellingLoc(FromLoc);
4156 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4157 SourceManager &ToSM = ToContext.getSourceManager();
4158 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4159 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004160}
4161
4162SourceRange ASTImporter::Import(SourceRange FromRange) {
4163 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4164}
4165
Douglas Gregor811663e2010-02-10 00:15:17 +00004166FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004167 llvm::DenseMap<FileID, FileID>::iterator Pos
4168 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004169 if (Pos != ImportedFileIDs.end())
4170 return Pos->second;
4171
4172 SourceManager &FromSM = FromContext.getSourceManager();
4173 SourceManager &ToSM = ToContext.getSourceManager();
4174 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4175 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4176
4177 // Include location of this file.
4178 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4179
4180 // Map the FileID for to the "to" source manager.
4181 FileID ToID;
4182 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004183 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004184 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4185 // disk again
4186 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4187 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004188 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004189 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4190 FromSLoc.getFile().getFileCharacteristic());
4191 } else {
4192 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004193 const llvm::MemoryBuffer *
4194 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004195 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004196 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004197 FromBuf->getBufferIdentifier());
4198 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4199 }
4200
4201
Sebastian Redl99219f12010-09-30 01:03:06 +00004202 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004203 return ToID;
4204}
4205
Douglas Gregor0a791672011-01-18 03:11:38 +00004206void ASTImporter::ImportDefinition(Decl *From) {
4207 Decl *To = Import(From);
4208 if (!To)
4209 return;
4210
4211 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4212 ASTNodeImporter Importer(*this);
4213 Importer.ImportDeclContext(FromDC, true);
4214 }
4215}
4216
Douglas Gregor96e578d2010-02-05 17:54:41 +00004217DeclarationName ASTImporter::Import(DeclarationName FromName) {
4218 if (!FromName)
4219 return DeclarationName();
4220
4221 switch (FromName.getNameKind()) {
4222 case DeclarationName::Identifier:
4223 return Import(FromName.getAsIdentifierInfo());
4224
4225 case DeclarationName::ObjCZeroArgSelector:
4226 case DeclarationName::ObjCOneArgSelector:
4227 case DeclarationName::ObjCMultiArgSelector:
4228 return Import(FromName.getObjCSelector());
4229
4230 case DeclarationName::CXXConstructorName: {
4231 QualType T = Import(FromName.getCXXNameType());
4232 if (T.isNull())
4233 return DeclarationName();
4234
4235 return ToContext.DeclarationNames.getCXXConstructorName(
4236 ToContext.getCanonicalType(T));
4237 }
4238
4239 case DeclarationName::CXXDestructorName: {
4240 QualType T = Import(FromName.getCXXNameType());
4241 if (T.isNull())
4242 return DeclarationName();
4243
4244 return ToContext.DeclarationNames.getCXXDestructorName(
4245 ToContext.getCanonicalType(T));
4246 }
4247
4248 case DeclarationName::CXXConversionFunctionName: {
4249 QualType T = Import(FromName.getCXXNameType());
4250 if (T.isNull())
4251 return DeclarationName();
4252
4253 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4254 ToContext.getCanonicalType(T));
4255 }
4256
4257 case DeclarationName::CXXOperatorName:
4258 return ToContext.DeclarationNames.getCXXOperatorName(
4259 FromName.getCXXOverloadedOperator());
4260
4261 case DeclarationName::CXXLiteralOperatorName:
4262 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4263 Import(FromName.getCXXLiteralIdentifier()));
4264
4265 case DeclarationName::CXXUsingDirective:
4266 // FIXME: STATICS!
4267 return DeclarationName::getUsingDirectiveName();
4268 }
4269
4270 // Silence bogus GCC warning
4271 return DeclarationName();
4272}
4273
Douglas Gregore2e50d332010-12-01 01:36:18 +00004274IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004275 if (!FromId)
4276 return 0;
4277
4278 return &ToContext.Idents.get(FromId->getName());
4279}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004280
Douglas Gregor43f54792010-02-17 02:12:47 +00004281Selector ASTImporter::Import(Selector FromSel) {
4282 if (FromSel.isNull())
4283 return Selector();
4284
4285 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4286 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4287 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4288 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4289 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4290}
4291
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004292DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4293 DeclContext *DC,
4294 unsigned IDNS,
4295 NamedDecl **Decls,
4296 unsigned NumDecls) {
4297 return Name;
4298}
4299
4300DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004301 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004302}
4303
4304DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004305 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004306}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004307
4308Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4309 ImportedDecls[From] = To;
4310 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004311}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004312
4313bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004314 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004315 = ImportedTypes.find(From.getTypePtr());
4316 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4317 return true;
4318
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004319 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004320 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004321}