blob: abcb2ef94ff5ec962de0087e90a5605ed22dd150 [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);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000142 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *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;
524 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
525 return false;
526 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
527 return false;
528 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
529 return false;
530 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
531 if (!IsStructurallyEquivalent(Context,
532 Proto1->getExceptionType(I),
533 Proto2->getExceptionType(I)))
534 return false;
535 }
536 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
537 return false;
538
539 // Fall through to check the bits common with FunctionNoProtoType.
540 }
541
542 case Type::FunctionNoProto: {
543 const FunctionType *Function1 = cast<FunctionType>(T1);
544 const FunctionType *Function2 = cast<FunctionType>(T2);
545 if (!IsStructurallyEquivalent(Context,
546 Function1->getResultType(),
547 Function2->getResultType()))
548 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000549 if (Function1->getExtInfo() != Function2->getExtInfo())
550 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000551 break;
552 }
553
554 case Type::UnresolvedUsing:
555 if (!IsStructurallyEquivalent(Context,
556 cast<UnresolvedUsingType>(T1)->getDecl(),
557 cast<UnresolvedUsingType>(T2)->getDecl()))
558 return false;
559
560 break;
John McCall81904512011-01-06 01:58:22 +0000561
562 case Type::Attributed:
563 if (!IsStructurallyEquivalent(Context,
564 cast<AttributedType>(T1)->getModifiedType(),
565 cast<AttributedType>(T2)->getModifiedType()))
566 return false;
567 if (!IsStructurallyEquivalent(Context,
568 cast<AttributedType>(T1)->getEquivalentType(),
569 cast<AttributedType>(T2)->getEquivalentType()))
570 return false;
571 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000572
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000573 case Type::Paren:
574 if (!IsStructurallyEquivalent(Context,
575 cast<ParenType>(T1)->getInnerType(),
576 cast<ParenType>(T2)->getInnerType()))
577 return false;
578 break;
579
Douglas Gregor3996e242010-02-15 22:01:00 +0000580 case Type::Typedef:
581 if (!IsStructurallyEquivalent(Context,
582 cast<TypedefType>(T1)->getDecl(),
583 cast<TypedefType>(T2)->getDecl()))
584 return false;
585 break;
586
587 case Type::TypeOfExpr:
588 if (!IsStructurallyEquivalent(Context,
589 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
590 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
591 return false;
592 break;
593
594 case Type::TypeOf:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypeOfType>(T1)->getUnderlyingType(),
597 cast<TypeOfType>(T2)->getUnderlyingType()))
598 return false;
599 break;
600
601 case Type::Decltype:
602 if (!IsStructurallyEquivalent(Context,
603 cast<DecltypeType>(T1)->getUnderlyingExpr(),
604 cast<DecltypeType>(T2)->getUnderlyingExpr()))
605 return false;
606 break;
607
Richard Smith30482bc2011-02-20 03:19:35 +0000608 case Type::Auto:
609 if (!IsStructurallyEquivalent(Context,
610 cast<AutoType>(T1)->getDeducedType(),
611 cast<AutoType>(T2)->getDeducedType()))
612 return false;
613 break;
614
Douglas Gregor3996e242010-02-15 22:01:00 +0000615 case Type::Record:
616 case Type::Enum:
617 if (!IsStructurallyEquivalent(Context,
618 cast<TagType>(T1)->getDecl(),
619 cast<TagType>(T2)->getDecl()))
620 return false;
621 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000622
Douglas Gregor3996e242010-02-15 22:01:00 +0000623 case Type::TemplateTypeParm: {
624 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
625 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
626 if (Parm1->getDepth() != Parm2->getDepth())
627 return false;
628 if (Parm1->getIndex() != Parm2->getIndex())
629 return false;
630 if (Parm1->isParameterPack() != Parm2->isParameterPack())
631 return false;
632
633 // Names of template type parameters are never significant.
634 break;
635 }
636
637 case Type::SubstTemplateTypeParm: {
638 const SubstTemplateTypeParmType *Subst1
639 = cast<SubstTemplateTypeParmType>(T1);
640 const SubstTemplateTypeParmType *Subst2
641 = cast<SubstTemplateTypeParmType>(T2);
642 if (!IsStructurallyEquivalent(Context,
643 QualType(Subst1->getReplacedParameter(), 0),
644 QualType(Subst2->getReplacedParameter(), 0)))
645 return false;
646 if (!IsStructurallyEquivalent(Context,
647 Subst1->getReplacementType(),
648 Subst2->getReplacementType()))
649 return false;
650 break;
651 }
652
Douglas Gregorfb322d82011-01-14 05:11:40 +0000653 case Type::SubstTemplateTypeParmPack: {
654 const SubstTemplateTypeParmPackType *Subst1
655 = cast<SubstTemplateTypeParmPackType>(T1);
656 const SubstTemplateTypeParmPackType *Subst2
657 = cast<SubstTemplateTypeParmPackType>(T2);
658 if (!IsStructurallyEquivalent(Context,
659 QualType(Subst1->getReplacedParameter(), 0),
660 QualType(Subst2->getReplacedParameter(), 0)))
661 return false;
662 if (!IsStructurallyEquivalent(Context,
663 Subst1->getArgumentPack(),
664 Subst2->getArgumentPack()))
665 return false;
666 break;
667 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000668 case Type::TemplateSpecialization: {
669 const TemplateSpecializationType *Spec1
670 = cast<TemplateSpecializationType>(T1);
671 const TemplateSpecializationType *Spec2
672 = cast<TemplateSpecializationType>(T2);
673 if (!IsStructurallyEquivalent(Context,
674 Spec1->getTemplateName(),
675 Spec2->getTemplateName()))
676 return false;
677 if (Spec1->getNumArgs() != Spec2->getNumArgs())
678 return false;
679 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
680 if (!IsStructurallyEquivalent(Context,
681 Spec1->getArg(I), Spec2->getArg(I)))
682 return false;
683 }
684 break;
685 }
686
Abramo Bagnara6150c882010-05-11 21:36:43 +0000687 case Type::Elaborated: {
688 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
689 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
690 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
691 if (Elab1->getKeyword() != Elab2->getKeyword())
692 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000693 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000694 Elab1->getQualifier(),
695 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000696 return false;
697 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000698 Elab1->getNamedType(),
699 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000700 return false;
701 break;
702 }
703
John McCalle78aac42010-03-10 03:28:59 +0000704 case Type::InjectedClassName: {
705 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
706 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
707 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000708 Inj1->getInjectedSpecializationType(),
709 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000710 return false;
711 break;
712 }
713
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000714 case Type::DependentName: {
715 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
716 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000717 if (!IsStructurallyEquivalent(Context,
718 Typename1->getQualifier(),
719 Typename2->getQualifier()))
720 return false;
721 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
722 Typename2->getIdentifier()))
723 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000724
725 break;
726 }
727
John McCallc392f372010-06-11 00:33:02 +0000728 case Type::DependentTemplateSpecialization: {
729 const DependentTemplateSpecializationType *Spec1 =
730 cast<DependentTemplateSpecializationType>(T1);
731 const DependentTemplateSpecializationType *Spec2 =
732 cast<DependentTemplateSpecializationType>(T2);
733 if (!IsStructurallyEquivalent(Context,
734 Spec1->getQualifier(),
735 Spec2->getQualifier()))
736 return false;
737 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
738 Spec2->getIdentifier()))
739 return false;
740 if (Spec1->getNumArgs() != Spec2->getNumArgs())
741 return false;
742 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
743 if (!IsStructurallyEquivalent(Context,
744 Spec1->getArg(I), Spec2->getArg(I)))
745 return false;
746 }
747 break;
748 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000749
750 case Type::PackExpansion:
751 if (!IsStructurallyEquivalent(Context,
752 cast<PackExpansionType>(T1)->getPattern(),
753 cast<PackExpansionType>(T2)->getPattern()))
754 return false;
755 break;
756
Douglas Gregor3996e242010-02-15 22:01:00 +0000757 case Type::ObjCInterface: {
758 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
759 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
760 if (!IsStructurallyEquivalent(Context,
761 Iface1->getDecl(), Iface2->getDecl()))
762 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000763 break;
764 }
765
766 case Type::ObjCObject: {
767 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
768 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
769 if (!IsStructurallyEquivalent(Context,
770 Obj1->getBaseType(),
771 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000772 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000773 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
774 return false;
775 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000777 Obj1->getProtocol(I),
778 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000779 return false;
780 }
781 break;
782 }
783
784 case Type::ObjCObjectPointer: {
785 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
786 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
787 if (!IsStructurallyEquivalent(Context,
788 Ptr1->getPointeeType(),
789 Ptr2->getPointeeType()))
790 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 break;
792 }
793
794 } // end switch
795
796 return true;
797}
798
799/// \brief Determine structural equivalence of two records.
800static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
801 RecordDecl *D1, RecordDecl *D2) {
802 if (D1->isUnion() != D2->isUnion()) {
803 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
804 << Context.C2.getTypeDeclType(D2);
805 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
806 << D1->getDeclName() << (unsigned)D1->getTagKind();
807 return false;
808 }
809
Douglas Gregore2e50d332010-12-01 01:36:18 +0000810 // If both declarations are class template specializations, we know
811 // the ODR applies, so check the template and template arguments.
812 ClassTemplateSpecializationDecl *Spec1
813 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
814 ClassTemplateSpecializationDecl *Spec2
815 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
816 if (Spec1 && Spec2) {
817 // Check that the specialized templates are the same.
818 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
819 Spec2->getSpecializedTemplate()))
820 return false;
821
822 // Check that the template arguments are the same.
823 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
824 return false;
825
826 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
827 if (!IsStructurallyEquivalent(Context,
828 Spec1->getTemplateArgs().get(I),
829 Spec2->getTemplateArgs().get(I)))
830 return false;
831 }
832 // If one is a class template specialization and the other is not, these
833 // structures are diferent.
834 else if (Spec1 || Spec2)
835 return false;
836
Douglas Gregorb4964f72010-02-15 23:54:17 +0000837 // Compare the definitions of these two records. If either or both are
838 // incomplete, we assume that they are equivalent.
839 D1 = D1->getDefinition();
840 D2 = D2->getDefinition();
841 if (!D1 || !D2)
842 return true;
843
Douglas Gregor3996e242010-02-15 22:01:00 +0000844 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
845 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
846 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
847 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000848 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000849 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000850 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000851 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000852 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000853 return false;
854 }
855
856 // Check the base classes.
857 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
858 BaseEnd1 = D1CXX->bases_end(),
859 Base2 = D2CXX->bases_begin();
860 Base1 != BaseEnd1;
861 ++Base1, ++Base2) {
862 if (!IsStructurallyEquivalent(Context,
863 Base1->getType(), Base2->getType())) {
864 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
865 << Context.C2.getTypeDeclType(D2);
866 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
867 << Base2->getType()
868 << Base2->getSourceRange();
869 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
870 << Base1->getType()
871 << Base1->getSourceRange();
872 return false;
873 }
874
875 // Check virtual vs. non-virtual inheritance mismatch.
876 if (Base1->isVirtual() != Base2->isVirtual()) {
877 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
878 << Context.C2.getTypeDeclType(D2);
879 Context.Diag2(Base2->getSourceRange().getBegin(),
880 diag::note_odr_virtual_base)
881 << Base2->isVirtual() << Base2->getSourceRange();
882 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
883 << Base1->isVirtual()
884 << Base1->getSourceRange();
885 return false;
886 }
887 }
888 } else if (D1CXX->getNumBases() > 0) {
889 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
890 << Context.C2.getTypeDeclType(D2);
891 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
892 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
893 << Base1->getType()
894 << Base1->getSourceRange();
895 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
896 return false;
897 }
898 }
899
900 // Check the fields for consistency.
901 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
902 Field2End = D2->field_end();
903 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
904 Field1End = D1->field_end();
905 Field1 != Field1End;
906 ++Field1, ++Field2) {
907 if (Field2 == Field2End) {
908 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
909 << Context.C2.getTypeDeclType(D2);
910 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
911 << Field1->getDeclName() << Field1->getType();
912 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
913 return false;
914 }
915
916 if (!IsStructurallyEquivalent(Context,
917 Field1->getType(), Field2->getType())) {
918 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
919 << Context.C2.getTypeDeclType(D2);
920 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
921 << Field2->getDeclName() << Field2->getType();
922 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
923 << Field1->getDeclName() << Field1->getType();
924 return false;
925 }
926
927 if (Field1->isBitField() != Field2->isBitField()) {
928 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
929 << Context.C2.getTypeDeclType(D2);
930 if (Field1->isBitField()) {
931 llvm::APSInt Bits;
932 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
933 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
934 << Field1->getDeclName() << Field1->getType()
935 << Bits.toString(10, false);
936 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
937 << Field2->getDeclName();
938 } else {
939 llvm::APSInt Bits;
940 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
941 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
942 << Field2->getDeclName() << Field2->getType()
943 << Bits.toString(10, false);
944 Context.Diag1(Field1->getLocation(),
945 diag::note_odr_not_bit_field)
946 << Field1->getDeclName();
947 }
948 return false;
949 }
950
951 if (Field1->isBitField()) {
952 // Make sure that the bit-fields are the same length.
953 llvm::APSInt Bits1, Bits2;
954 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
955 return false;
956 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
957 return false;
958
959 if (!IsSameValue(Bits1, Bits2)) {
960 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
961 << Context.C2.getTypeDeclType(D2);
962 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
963 << Field2->getDeclName() << Field2->getType()
964 << Bits2.toString(10, false);
965 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
966 << Field1->getDeclName() << Field1->getType()
967 << Bits1.toString(10, false);
968 return false;
969 }
970 }
971 }
972
973 if (Field2 != Field2End) {
974 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
975 << Context.C2.getTypeDeclType(D2);
976 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
977 << Field2->getDeclName() << Field2->getType();
978 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
979 return false;
980 }
981
982 return true;
983}
984
985/// \brief Determine structural equivalence of two enums.
986static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
987 EnumDecl *D1, EnumDecl *D2) {
988 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
989 EC2End = D2->enumerator_end();
990 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
991 EC1End = D1->enumerator_end();
992 EC1 != EC1End; ++EC1, ++EC2) {
993 if (EC2 == EC2End) {
994 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
995 << Context.C2.getTypeDeclType(D2);
996 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
997 << EC1->getDeclName()
998 << EC1->getInitVal().toString(10);
999 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1000 return false;
1001 }
1002
1003 llvm::APSInt Val1 = EC1->getInitVal();
1004 llvm::APSInt Val2 = EC2->getInitVal();
1005 if (!IsSameValue(Val1, Val2) ||
1006 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1007 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1008 << Context.C2.getTypeDeclType(D2);
1009 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1010 << EC2->getDeclName()
1011 << EC2->getInitVal().toString(10);
1012 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1013 << EC1->getDeclName()
1014 << EC1->getInitVal().toString(10);
1015 return false;
1016 }
1017 }
1018
1019 if (EC2 != EC2End) {
1020 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1021 << Context.C2.getTypeDeclType(D2);
1022 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1023 << EC2->getDeclName()
1024 << EC2->getInitVal().toString(10);
1025 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1026 return false;
1027 }
1028
1029 return true;
1030}
Douglas Gregora082a492010-11-30 19:14:50 +00001031
1032static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1033 TemplateParameterList *Params1,
1034 TemplateParameterList *Params2) {
1035 if (Params1->size() != Params2->size()) {
1036 Context.Diag2(Params2->getTemplateLoc(),
1037 diag::err_odr_different_num_template_parameters)
1038 << Params1->size() << Params2->size();
1039 Context.Diag1(Params1->getTemplateLoc(),
1040 diag::note_odr_template_parameter_list);
1041 return false;
1042 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001043
Douglas Gregora082a492010-11-30 19:14:50 +00001044 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1045 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1046 Context.Diag2(Params2->getParam(I)->getLocation(),
1047 diag::err_odr_different_template_parameter_kind);
1048 Context.Diag1(Params1->getParam(I)->getLocation(),
1049 diag::note_odr_template_parameter_here);
1050 return false;
1051 }
1052
1053 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1054 Params2->getParam(I))) {
1055
1056 return false;
1057 }
1058 }
1059
1060 return true;
1061}
1062
1063static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1064 TemplateTypeParmDecl *D1,
1065 TemplateTypeParmDecl *D2) {
1066 if (D1->isParameterPack() != D2->isParameterPack()) {
1067 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1068 << D2->isParameterPack();
1069 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1070 << D1->isParameterPack();
1071 return false;
1072 }
1073
1074 return true;
1075}
1076
1077static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1078 NonTypeTemplateParmDecl *D1,
1079 NonTypeTemplateParmDecl *D2) {
1080 // FIXME: Enable once we have variadic templates.
1081#if 0
1082 if (D1->isParameterPack() != D2->isParameterPack()) {
1083 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1084 << D2->isParameterPack();
1085 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1086 << D1->isParameterPack();
1087 return false;
1088 }
1089#endif
1090
1091 // Check types.
1092 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1093 Context.Diag2(D2->getLocation(),
1094 diag::err_odr_non_type_parameter_type_inconsistent)
1095 << D2->getType() << D1->getType();
1096 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1097 << D1->getType();
1098 return false;
1099 }
1100
1101 return true;
1102}
1103
1104static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1105 TemplateTemplateParmDecl *D1,
1106 TemplateTemplateParmDecl *D2) {
1107 // FIXME: Enable once we have variadic templates.
1108#if 0
1109 if (D1->isParameterPack() != D2->isParameterPack()) {
1110 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1111 << D2->isParameterPack();
1112 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1113 << D1->isParameterPack();
1114 return false;
1115 }
1116#endif
1117
1118 // Check template parameter lists.
1119 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1120 D2->getTemplateParameters());
1121}
1122
1123static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1124 ClassTemplateDecl *D1,
1125 ClassTemplateDecl *D2) {
1126 // Check template parameters.
1127 if (!IsStructurallyEquivalent(Context,
1128 D1->getTemplateParameters(),
1129 D2->getTemplateParameters()))
1130 return false;
1131
1132 // Check the templated declaration.
1133 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1134 D2->getTemplatedDecl());
1135}
1136
Douglas Gregor3996e242010-02-15 22:01:00 +00001137/// \brief Determine structural equivalence of two declarations.
1138static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1139 Decl *D1, Decl *D2) {
1140 // FIXME: Check for known structural equivalences via a callback of some sort.
1141
Douglas Gregorb4964f72010-02-15 23:54:17 +00001142 // Check whether we already know that these two declarations are not
1143 // structurally equivalent.
1144 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1145 D2->getCanonicalDecl())))
1146 return false;
1147
Douglas Gregor3996e242010-02-15 22:01:00 +00001148 // Determine whether we've already produced a tentative equivalence for D1.
1149 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1150 if (EquivToD1)
1151 return EquivToD1 == D2->getCanonicalDecl();
1152
1153 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1154 EquivToD1 = D2->getCanonicalDecl();
1155 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1156 return true;
1157}
1158
1159bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1160 Decl *D2) {
1161 if (!::IsStructurallyEquivalent(*this, D1, D2))
1162 return false;
1163
1164 return !Finish();
1165}
1166
1167bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1168 QualType T2) {
1169 if (!::IsStructurallyEquivalent(*this, T1, T2))
1170 return false;
1171
1172 return !Finish();
1173}
1174
1175bool StructuralEquivalenceContext::Finish() {
1176 while (!DeclsToCheck.empty()) {
1177 // Check the next declaration.
1178 Decl *D1 = DeclsToCheck.front();
1179 DeclsToCheck.pop_front();
1180
1181 Decl *D2 = TentativeEquivalences[D1];
1182 assert(D2 && "Unrecorded tentative equivalence?");
1183
Douglas Gregorb4964f72010-02-15 23:54:17 +00001184 bool Equivalent = true;
1185
Douglas Gregor3996e242010-02-15 22:01:00 +00001186 // FIXME: Switch on all declaration kinds. For now, we're just going to
1187 // check the obvious ones.
1188 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1189 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1190 // Check for equivalent structure names.
1191 IdentifierInfo *Name1 = Record1->getIdentifier();
1192 if (!Name1 && Record1->getTypedefForAnonDecl())
1193 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1194 IdentifierInfo *Name2 = Record2->getIdentifier();
1195 if (!Name2 && Record2->getTypedefForAnonDecl())
1196 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001197 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1198 !::IsStructurallyEquivalent(*this, Record1, Record2))
1199 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001200 } else {
1201 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001202 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001203 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001204 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001205 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1206 // Check for equivalent enum names.
1207 IdentifierInfo *Name1 = Enum1->getIdentifier();
1208 if (!Name1 && Enum1->getTypedefForAnonDecl())
1209 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1210 IdentifierInfo *Name2 = Enum2->getIdentifier();
1211 if (!Name2 && Enum2->getTypedefForAnonDecl())
1212 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001213 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1214 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1215 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001216 } else {
1217 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001218 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001219 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001220 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001221 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1222 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001223 Typedef2->getIdentifier()) ||
1224 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001225 Typedef1->getUnderlyingType(),
1226 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001227 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 } else {
1229 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001230 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001231 }
Douglas Gregora082a492010-11-30 19:14:50 +00001232 } else if (ClassTemplateDecl *ClassTemplate1
1233 = dyn_cast<ClassTemplateDecl>(D1)) {
1234 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1235 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1236 ClassTemplate2->getIdentifier()) ||
1237 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1238 Equivalent = false;
1239 } else {
1240 // Class template/non-class-template mismatch.
1241 Equivalent = false;
1242 }
1243 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1244 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1245 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1246 Equivalent = false;
1247 } else {
1248 // Kind mismatch.
1249 Equivalent = false;
1250 }
1251 } else if (NonTypeTemplateParmDecl *NTTP1
1252 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1253 if (NonTypeTemplateParmDecl *NTTP2
1254 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1255 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1256 Equivalent = false;
1257 } else {
1258 // Kind mismatch.
1259 Equivalent = false;
1260 }
1261 } else if (TemplateTemplateParmDecl *TTP1
1262 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1263 if (TemplateTemplateParmDecl *TTP2
1264 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1265 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1266 Equivalent = false;
1267 } else {
1268 // Kind mismatch.
1269 Equivalent = false;
1270 }
1271 }
1272
Douglas Gregorb4964f72010-02-15 23:54:17 +00001273 if (!Equivalent) {
1274 // Note that these two declarations are not equivalent (and we already
1275 // know about it).
1276 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1277 D2->getCanonicalDecl()));
1278 return true;
1279 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001280 // FIXME: Check other declaration kinds!
1281 }
1282
1283 return false;
1284}
1285
1286//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001287// Import Types
1288//----------------------------------------------------------------------------
1289
John McCall424cec92011-01-19 06:33:43 +00001290QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001291 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1292 << T->getTypeClassName();
1293 return QualType();
1294}
1295
John McCall424cec92011-01-19 06:33:43 +00001296QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001297 switch (T->getKind()) {
1298 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1299 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1300
1301 case BuiltinType::Char_U:
1302 // The context we're importing from has an unsigned 'char'. If we're
1303 // importing into a context with a signed 'char', translate to
1304 // 'unsigned char' instead.
1305 if (Importer.getToContext().getLangOptions().CharIsSigned)
1306 return Importer.getToContext().UnsignedCharTy;
1307
1308 return Importer.getToContext().CharTy;
1309
1310 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1311
1312 case BuiltinType::Char16:
1313 // FIXME: Make sure that the "to" context supports C++!
1314 return Importer.getToContext().Char16Ty;
1315
1316 case BuiltinType::Char32:
1317 // FIXME: Make sure that the "to" context supports C++!
1318 return Importer.getToContext().Char32Ty;
1319
1320 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1321 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1322 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1323 case BuiltinType::ULongLong:
1324 return Importer.getToContext().UnsignedLongLongTy;
1325 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1326
1327 case BuiltinType::Char_S:
1328 // The context we're importing from has an unsigned 'char'. If we're
1329 // importing into a context with a signed 'char', translate to
1330 // 'unsigned char' instead.
1331 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1332 return Importer.getToContext().SignedCharTy;
1333
1334 return Importer.getToContext().CharTy;
1335
1336 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001337 case BuiltinType::WChar_S:
1338 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001339 // FIXME: If not in C++, shall we translate to the C equivalent of
1340 // wchar_t?
1341 return Importer.getToContext().WCharTy;
1342
1343 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1344 case BuiltinType::Int : return Importer.getToContext().IntTy;
1345 case BuiltinType::Long : return Importer.getToContext().LongTy;
1346 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1347 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1348 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1349 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1350 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1351
1352 case BuiltinType::NullPtr:
1353 // FIXME: Make sure that the "to" context supports C++0x!
1354 return Importer.getToContext().NullPtrTy;
1355
1356 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1357 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001358
1359 case BuiltinType::ObjCId:
1360 // FIXME: Make sure that the "to" context supports Objective-C!
1361 return Importer.getToContext().ObjCBuiltinIdTy;
1362
1363 case BuiltinType::ObjCClass:
1364 return Importer.getToContext().ObjCBuiltinClassTy;
1365
1366 case BuiltinType::ObjCSel:
1367 return Importer.getToContext().ObjCBuiltinSelTy;
1368 }
1369
1370 return QualType();
1371}
1372
John McCall424cec92011-01-19 06:33:43 +00001373QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001374 QualType ToElementType = Importer.Import(T->getElementType());
1375 if (ToElementType.isNull())
1376 return QualType();
1377
1378 return Importer.getToContext().getComplexType(ToElementType);
1379}
1380
John McCall424cec92011-01-19 06:33:43 +00001381QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001382 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1383 if (ToPointeeType.isNull())
1384 return QualType();
1385
1386 return Importer.getToContext().getPointerType(ToPointeeType);
1387}
1388
John McCall424cec92011-01-19 06:33:43 +00001389QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001390 // FIXME: Check for blocks support in "to" context.
1391 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1392 if (ToPointeeType.isNull())
1393 return QualType();
1394
1395 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1396}
1397
John McCall424cec92011-01-19 06:33:43 +00001398QualType
1399ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001400 // FIXME: Check for C++ support in "to" context.
1401 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1402 if (ToPointeeType.isNull())
1403 return QualType();
1404
1405 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1406}
1407
John McCall424cec92011-01-19 06:33:43 +00001408QualType
1409ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001410 // FIXME: Check for C++0x support in "to" context.
1411 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1412 if (ToPointeeType.isNull())
1413 return QualType();
1414
1415 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1416}
1417
John McCall424cec92011-01-19 06:33:43 +00001418QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001419 // FIXME: Check for C++ support in "to" context.
1420 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1421 if (ToPointeeType.isNull())
1422 return QualType();
1423
1424 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1425 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1426 ClassType.getTypePtr());
1427}
1428
John McCall424cec92011-01-19 06:33:43 +00001429QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430 QualType ToElementType = Importer.Import(T->getElementType());
1431 if (ToElementType.isNull())
1432 return QualType();
1433
1434 return Importer.getToContext().getConstantArrayType(ToElementType,
1435 T->getSize(),
1436 T->getSizeModifier(),
1437 T->getIndexTypeCVRQualifiers());
1438}
1439
John McCall424cec92011-01-19 06:33:43 +00001440QualType
1441ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001442 QualType ToElementType = Importer.Import(T->getElementType());
1443 if (ToElementType.isNull())
1444 return QualType();
1445
1446 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1447 T->getSizeModifier(),
1448 T->getIndexTypeCVRQualifiers());
1449}
1450
John McCall424cec92011-01-19 06:33:43 +00001451QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001452 QualType ToElementType = Importer.Import(T->getElementType());
1453 if (ToElementType.isNull())
1454 return QualType();
1455
1456 Expr *Size = Importer.Import(T->getSizeExpr());
1457 if (!Size)
1458 return QualType();
1459
1460 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1461 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1462 T->getSizeModifier(),
1463 T->getIndexTypeCVRQualifiers(),
1464 Brackets);
1465}
1466
John McCall424cec92011-01-19 06:33:43 +00001467QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001468 QualType ToElementType = Importer.Import(T->getElementType());
1469 if (ToElementType.isNull())
1470 return QualType();
1471
1472 return Importer.getToContext().getVectorType(ToElementType,
1473 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001474 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001475}
1476
John McCall424cec92011-01-19 06:33:43 +00001477QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001478 QualType ToElementType = Importer.Import(T->getElementType());
1479 if (ToElementType.isNull())
1480 return QualType();
1481
1482 return Importer.getToContext().getExtVectorType(ToElementType,
1483 T->getNumElements());
1484}
1485
John McCall424cec92011-01-19 06:33:43 +00001486QualType
1487ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001488 // FIXME: What happens if we're importing a function without a prototype
1489 // into C++? Should we make it variadic?
1490 QualType ToResultType = Importer.Import(T->getResultType());
1491 if (ToResultType.isNull())
1492 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001493
Douglas Gregor96e578d2010-02-05 17:54:41 +00001494 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001495 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001496}
1497
John McCall424cec92011-01-19 06:33:43 +00001498QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001499 QualType ToResultType = Importer.Import(T->getResultType());
1500 if (ToResultType.isNull())
1501 return QualType();
1502
1503 // Import argument types
1504 llvm::SmallVector<QualType, 4> ArgTypes;
1505 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1506 AEnd = T->arg_type_end();
1507 A != AEnd; ++A) {
1508 QualType ArgType = Importer.Import(*A);
1509 if (ArgType.isNull())
1510 return QualType();
1511 ArgTypes.push_back(ArgType);
1512 }
1513
1514 // Import exception types
1515 llvm::SmallVector<QualType, 4> ExceptionTypes;
1516 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1517 EEnd = T->exception_end();
1518 E != EEnd; ++E) {
1519 QualType ExceptionType = Importer.Import(*E);
1520 if (ExceptionType.isNull())
1521 return QualType();
1522 ExceptionTypes.push_back(ExceptionType);
1523 }
John McCalldb40c7f2010-12-14 08:05:40 +00001524
1525 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1526 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001527
1528 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001529 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001530}
1531
John McCall424cec92011-01-19 06:33:43 +00001532QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001533 TypedefDecl *ToDecl
1534 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1535 if (!ToDecl)
1536 return QualType();
1537
1538 return Importer.getToContext().getTypeDeclType(ToDecl);
1539}
1540
John McCall424cec92011-01-19 06:33:43 +00001541QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001542 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1543 if (!ToExpr)
1544 return QualType();
1545
1546 return Importer.getToContext().getTypeOfExprType(ToExpr);
1547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1551 if (ToUnderlyingType.isNull())
1552 return QualType();
1553
1554 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1555}
1556
John McCall424cec92011-01-19 06:33:43 +00001557QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001558 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001559 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1560 if (!ToExpr)
1561 return QualType();
1562
1563 return Importer.getToContext().getDecltypeType(ToExpr);
1564}
1565
Richard Smith30482bc2011-02-20 03:19:35 +00001566QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1567 // FIXME: Make sure that the "to" context supports C++0x!
1568 QualType FromDeduced = T->getDeducedType();
1569 QualType ToDeduced;
1570 if (!FromDeduced.isNull()) {
1571 ToDeduced = Importer.Import(FromDeduced);
1572 if (ToDeduced.isNull())
1573 return QualType();
1574 }
1575
1576 return Importer.getToContext().getAutoType(ToDeduced);
1577}
1578
John McCall424cec92011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001580 RecordDecl *ToDecl
1581 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1582 if (!ToDecl)
1583 return QualType();
1584
1585 return Importer.getToContext().getTagDeclType(ToDecl);
1586}
1587
John McCall424cec92011-01-19 06:33:43 +00001588QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001589 EnumDecl *ToDecl
1590 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1591 if (!ToDecl)
1592 return QualType();
1593
1594 return Importer.getToContext().getTagDeclType(ToDecl);
1595}
1596
Douglas Gregore2e50d332010-12-01 01:36:18 +00001597QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001598 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001599 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1600 if (ToTemplate.isNull())
1601 return QualType();
1602
1603 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1604 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1605 return QualType();
1606
1607 QualType ToCanonType;
1608 if (!QualType(T, 0).isCanonical()) {
1609 QualType FromCanonType
1610 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1611 ToCanonType =Importer.Import(FromCanonType);
1612 if (ToCanonType.isNull())
1613 return QualType();
1614 }
1615 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1616 ToTemplateArgs.data(),
1617 ToTemplateArgs.size(),
1618 ToCanonType);
1619}
1620
John McCall424cec92011-01-19 06:33:43 +00001621QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001622 NestedNameSpecifier *ToQualifier = 0;
1623 // Note: the qualifier in an ElaboratedType is optional.
1624 if (T->getQualifier()) {
1625 ToQualifier = Importer.Import(T->getQualifier());
1626 if (!ToQualifier)
1627 return QualType();
1628 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001629
1630 QualType ToNamedType = Importer.Import(T->getNamedType());
1631 if (ToNamedType.isNull())
1632 return QualType();
1633
Abramo Bagnara6150c882010-05-11 21:36:43 +00001634 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1635 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001636}
1637
John McCall424cec92011-01-19 06:33:43 +00001638QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001639 ObjCInterfaceDecl *Class
1640 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1641 if (!Class)
1642 return QualType();
1643
John McCall8b07ec22010-05-15 11:32:37 +00001644 return Importer.getToContext().getObjCInterfaceType(Class);
1645}
1646
John McCall424cec92011-01-19 06:33:43 +00001647QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001648 QualType ToBaseType = Importer.Import(T->getBaseType());
1649 if (ToBaseType.isNull())
1650 return QualType();
1651
Douglas Gregor96e578d2010-02-05 17:54:41 +00001652 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001653 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001654 PEnd = T->qual_end();
1655 P != PEnd; ++P) {
1656 ObjCProtocolDecl *Protocol
1657 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1658 if (!Protocol)
1659 return QualType();
1660 Protocols.push_back(Protocol);
1661 }
1662
John McCall8b07ec22010-05-15 11:32:37 +00001663 return Importer.getToContext().getObjCObjectType(ToBaseType,
1664 Protocols.data(),
1665 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001666}
1667
John McCall424cec92011-01-19 06:33:43 +00001668QualType
1669ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001670 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1671 if (ToPointeeType.isNull())
1672 return QualType();
1673
John McCall8b07ec22010-05-15 11:32:37 +00001674 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001675}
1676
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001677//----------------------------------------------------------------------------
1678// Import Declarations
1679//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001680bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1681 DeclContext *&LexicalDC,
1682 DeclarationName &Name,
1683 SourceLocation &Loc) {
1684 // Import the context of this declaration.
1685 DC = Importer.ImportContext(D->getDeclContext());
1686 if (!DC)
1687 return true;
1688
1689 LexicalDC = DC;
1690 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1691 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1692 if (!LexicalDC)
1693 return true;
1694 }
1695
1696 // Import the name of this declaration.
1697 Name = Importer.Import(D->getDeclName());
1698 if (D->getDeclName() && !Name)
1699 return true;
1700
1701 // Import the location of this declaration.
1702 Loc = Importer.Import(D->getLocation());
1703 return false;
1704}
1705
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001706void
1707ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1708 DeclarationNameInfo& To) {
1709 // NOTE: To.Name and To.Loc are already imported.
1710 // We only have to import To.LocInfo.
1711 switch (To.getName().getNameKind()) {
1712 case DeclarationName::Identifier:
1713 case DeclarationName::ObjCZeroArgSelector:
1714 case DeclarationName::ObjCOneArgSelector:
1715 case DeclarationName::ObjCMultiArgSelector:
1716 case DeclarationName::CXXUsingDirective:
1717 return;
1718
1719 case DeclarationName::CXXOperatorName: {
1720 SourceRange Range = From.getCXXOperatorNameRange();
1721 To.setCXXOperatorNameRange(Importer.Import(Range));
1722 return;
1723 }
1724 case DeclarationName::CXXLiteralOperatorName: {
1725 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1726 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1727 return;
1728 }
1729 case DeclarationName::CXXConstructorName:
1730 case DeclarationName::CXXDestructorName:
1731 case DeclarationName::CXXConversionFunctionName: {
1732 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1733 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1734 return;
1735 }
1736 assert(0 && "Unknown name kind.");
1737 }
1738}
1739
Douglas Gregor0a791672011-01-18 03:11:38 +00001740void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1741 if (Importer.isMinimalImport() && !ForceImport) {
1742 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1743 ToDC->setHasExternalLexicalStorage();
1744 ToDC->setHasExternalVisibleStorage();
1745 }
1746 return;
1747 }
1748
Douglas Gregor968d6332010-02-21 18:24:45 +00001749 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1750 FromEnd = FromDC->decls_end();
1751 From != FromEnd;
1752 ++From)
1753 Importer.Import(*From);
1754}
1755
Douglas Gregore2e50d332010-12-01 01:36:18 +00001756bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1757 if (To->getDefinition())
1758 return false;
1759
1760 To->startDefinition();
1761
1762 // Add base classes.
1763 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1764 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1765
1766 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1767 for (CXXRecordDecl::base_class_iterator
1768 Base1 = FromCXX->bases_begin(),
1769 FromBaseEnd = FromCXX->bases_end();
1770 Base1 != FromBaseEnd;
1771 ++Base1) {
1772 QualType T = Importer.Import(Base1->getType());
1773 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001774 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001775
1776 SourceLocation EllipsisLoc;
1777 if (Base1->isPackExpansion())
1778 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001779
1780 Bases.push_back(
1781 new (Importer.getToContext())
1782 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1783 Base1->isVirtual(),
1784 Base1->isBaseOfClass(),
1785 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001786 Importer.Import(Base1->getTypeSourceInfo()),
1787 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001788 }
1789 if (!Bases.empty())
1790 ToCXX->setBases(Bases.data(), Bases.size());
1791 }
1792
1793 ImportDeclContext(From);
1794 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001795 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001796}
1797
Douglas Gregora082a492010-11-30 19:14:50 +00001798TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1799 TemplateParameterList *Params) {
1800 llvm::SmallVector<NamedDecl *, 4> ToParams;
1801 ToParams.reserve(Params->size());
1802 for (TemplateParameterList::iterator P = Params->begin(),
1803 PEnd = Params->end();
1804 P != PEnd; ++P) {
1805 Decl *To = Importer.Import(*P);
1806 if (!To)
1807 return 0;
1808
1809 ToParams.push_back(cast<NamedDecl>(To));
1810 }
1811
1812 return TemplateParameterList::Create(Importer.getToContext(),
1813 Importer.Import(Params->getTemplateLoc()),
1814 Importer.Import(Params->getLAngleLoc()),
1815 ToParams.data(), ToParams.size(),
1816 Importer.Import(Params->getRAngleLoc()));
1817}
1818
Douglas Gregore2e50d332010-12-01 01:36:18 +00001819TemplateArgument
1820ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1821 switch (From.getKind()) {
1822 case TemplateArgument::Null:
1823 return TemplateArgument();
1824
1825 case TemplateArgument::Type: {
1826 QualType ToType = Importer.Import(From.getAsType());
1827 if (ToType.isNull())
1828 return TemplateArgument();
1829 return TemplateArgument(ToType);
1830 }
1831
1832 case TemplateArgument::Integral: {
1833 QualType ToType = Importer.Import(From.getIntegralType());
1834 if (ToType.isNull())
1835 return TemplateArgument();
1836 return TemplateArgument(*From.getAsIntegral(), ToType);
1837 }
1838
1839 case TemplateArgument::Declaration:
1840 if (Decl *To = Importer.Import(From.getAsDecl()))
1841 return TemplateArgument(To);
1842 return TemplateArgument();
1843
1844 case TemplateArgument::Template: {
1845 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1846 if (ToTemplate.isNull())
1847 return TemplateArgument();
1848
1849 return TemplateArgument(ToTemplate);
1850 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001851
1852 case TemplateArgument::TemplateExpansion: {
1853 TemplateName ToTemplate
1854 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1855 if (ToTemplate.isNull())
1856 return TemplateArgument();
1857
Douglas Gregore1d60df2011-01-14 23:41:42 +00001858 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001859 }
1860
Douglas Gregore2e50d332010-12-01 01:36:18 +00001861 case TemplateArgument::Expression:
1862 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1863 return TemplateArgument(ToExpr);
1864 return TemplateArgument();
1865
1866 case TemplateArgument::Pack: {
1867 llvm::SmallVector<TemplateArgument, 2> ToPack;
1868 ToPack.reserve(From.pack_size());
1869 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1870 return TemplateArgument();
1871
1872 TemplateArgument *ToArgs
1873 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1874 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1875 return TemplateArgument(ToArgs, ToPack.size());
1876 }
1877 }
1878
1879 llvm_unreachable("Invalid template argument kind");
1880 return TemplateArgument();
1881}
1882
1883bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1884 unsigned NumFromArgs,
1885 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1886 for (unsigned I = 0; I != NumFromArgs; ++I) {
1887 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1888 if (To.isNull() && !FromArgs[I].isNull())
1889 return true;
1890
1891 ToArgs.push_back(To);
1892 }
1893
1894 return false;
1895}
1896
Douglas Gregor5c73e912010-02-11 00:48:18 +00001897bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001898 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001899 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001900 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001901 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001902 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001903}
1904
Douglas Gregor98c10182010-02-12 22:17:39 +00001905bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001906 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001907 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001908 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001909 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001910}
1911
Douglas Gregora082a492010-11-30 19:14:50 +00001912bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1913 ClassTemplateDecl *To) {
1914 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1915 Importer.getToContext(),
1916 Importer.getNonEquivalentDecls());
1917 return Ctx.IsStructurallyEquivalent(From, To);
1918}
1919
Douglas Gregore4c83e42010-02-09 22:48:33 +00001920Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001921 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001922 << D->getDeclKindName();
1923 return 0;
1924}
1925
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001926Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1927 // Import the major distinguishing characteristics of this namespace.
1928 DeclContext *DC, *LexicalDC;
1929 DeclarationName Name;
1930 SourceLocation Loc;
1931 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1932 return 0;
1933
1934 NamespaceDecl *MergeWithNamespace = 0;
1935 if (!Name) {
1936 // This is an anonymous namespace. Adopt an existing anonymous
1937 // namespace if we can.
1938 // FIXME: Not testable.
1939 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1940 MergeWithNamespace = TU->getAnonymousNamespace();
1941 else
1942 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1943 } else {
1944 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1945 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1946 Lookup.first != Lookup.second;
1947 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001948 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001949 continue;
1950
1951 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1952 MergeWithNamespace = FoundNS;
1953 ConflictingDecls.clear();
1954 break;
1955 }
1956
1957 ConflictingDecls.push_back(*Lookup.first);
1958 }
1959
1960 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001961 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001962 ConflictingDecls.data(),
1963 ConflictingDecls.size());
1964 }
1965 }
1966
1967 // Create the "to" namespace, if needed.
1968 NamespaceDecl *ToNamespace = MergeWithNamespace;
1969 if (!ToNamespace) {
1970 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1971 Name.getAsIdentifierInfo());
1972 ToNamespace->setLexicalDeclContext(LexicalDC);
1973 LexicalDC->addDecl(ToNamespace);
1974
1975 // If this is an anonymous namespace, register it as the anonymous
1976 // namespace within its context.
1977 if (!Name) {
1978 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1979 TU->setAnonymousNamespace(ToNamespace);
1980 else
1981 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1982 }
1983 }
1984 Importer.Imported(D, ToNamespace);
1985
1986 ImportDeclContext(D);
1987
1988 return ToNamespace;
1989}
1990
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001991Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1992 // Import the major distinguishing characteristics of this typedef.
1993 DeclContext *DC, *LexicalDC;
1994 DeclarationName Name;
1995 SourceLocation Loc;
1996 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1997 return 0;
1998
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001999 // If this typedef is not in block scope, determine whether we've
2000 // seen a typedef with the same name (that we can merge with) or any
2001 // other entity by that name (which name lookup could conflict with).
2002 if (!DC->isFunctionOrMethod()) {
2003 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2004 unsigned IDNS = Decl::IDNS_Ordinary;
2005 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2006 Lookup.first != Lookup.second;
2007 ++Lookup.first) {
2008 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2009 continue;
2010 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002011 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2012 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002013 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002014 }
2015
2016 ConflictingDecls.push_back(*Lookup.first);
2017 }
2018
2019 if (!ConflictingDecls.empty()) {
2020 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2021 ConflictingDecls.data(),
2022 ConflictingDecls.size());
2023 if (!Name)
2024 return 0;
2025 }
2026 }
2027
Douglas Gregorb4964f72010-02-15 23:54:17 +00002028 // Import the underlying type of this typedef;
2029 QualType T = Importer.Import(D->getUnderlyingType());
2030 if (T.isNull())
2031 return 0;
2032
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002033 // Create the new typedef node.
2034 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002035 SourceLocation StartL = Importer.Import(D->getLocStart());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002036 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002037 StartL, Loc,
2038 Name.getAsIdentifierInfo(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002039 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002040 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002041 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002042 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002043 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002044
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002045 return ToTypedef;
2046}
2047
Douglas Gregor98c10182010-02-12 22:17:39 +00002048Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2049 // Import the major distinguishing characteristics of this enum.
2050 DeclContext *DC, *LexicalDC;
2051 DeclarationName Name;
2052 SourceLocation Loc;
2053 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2054 return 0;
2055
2056 // Figure out what enum name we're looking for.
2057 unsigned IDNS = Decl::IDNS_Tag;
2058 DeclarationName SearchName = Name;
2059 if (!SearchName && D->getTypedefForAnonDecl()) {
2060 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2061 IDNS = Decl::IDNS_Ordinary;
2062 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2063 IDNS |= Decl::IDNS_Ordinary;
2064
2065 // We may already have an enum of the same name; try to find and match it.
2066 if (!DC->isFunctionOrMethod() && SearchName) {
2067 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2068 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2069 Lookup.first != Lookup.second;
2070 ++Lookup.first) {
2071 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2072 continue;
2073
2074 Decl *Found = *Lookup.first;
2075 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2076 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2077 Found = Tag->getDecl();
2078 }
2079
2080 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002081 if (IsStructuralMatch(D, FoundEnum))
2082 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002083 }
2084
2085 ConflictingDecls.push_back(*Lookup.first);
2086 }
2087
2088 if (!ConflictingDecls.empty()) {
2089 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2090 ConflictingDecls.data(),
2091 ConflictingDecls.size());
2092 }
2093 }
2094
2095 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002096 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002097 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002098 Importer.Import(D->getLocStart()), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002099 D->isScoped(), D->isScopedUsingClassTag(),
2100 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002101 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002102 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002103 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002104 D2->setLexicalDeclContext(LexicalDC);
2105 Importer.Imported(D, D2);
2106 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002107
2108 // Import the integer type.
2109 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2110 if (ToIntegerType.isNull())
2111 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002112 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002113
2114 // Import the definition
2115 if (D->isDefinition()) {
2116 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2117 if (T.isNull())
2118 return 0;
2119
2120 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2121 if (ToPromotionType.isNull())
2122 return 0;
2123
Douglas Gregor3996e242010-02-15 22:01:00 +00002124 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002125 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002126
2127 // FIXME: we might need to merge the number of positive or negative bits
2128 // if the enumerator lists don't match.
2129 D2->completeDefinition(T, ToPromotionType,
2130 D->getNumPositiveBits(),
2131 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002132 }
2133
Douglas Gregor3996e242010-02-15 22:01:00 +00002134 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002135}
2136
Douglas Gregor5c73e912010-02-11 00:48:18 +00002137Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2138 // If this record has a definition in the translation unit we're coming from,
2139 // but this particular declaration is not that definition, import the
2140 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002141 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002142 if (Definition && Definition != D) {
2143 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002144 if (!ImportedDef)
2145 return 0;
2146
2147 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002148 }
2149
2150 // Import the major distinguishing characteristics of this record.
2151 DeclContext *DC, *LexicalDC;
2152 DeclarationName Name;
2153 SourceLocation Loc;
2154 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2155 return 0;
2156
2157 // Figure out what structure name we're looking for.
2158 unsigned IDNS = Decl::IDNS_Tag;
2159 DeclarationName SearchName = Name;
2160 if (!SearchName && D->getTypedefForAnonDecl()) {
2161 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2162 IDNS = Decl::IDNS_Ordinary;
2163 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2164 IDNS |= Decl::IDNS_Ordinary;
2165
2166 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002167 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002168 if (!DC->isFunctionOrMethod() && SearchName) {
2169 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2170 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2171 Lookup.first != Lookup.second;
2172 ++Lookup.first) {
2173 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2174 continue;
2175
2176 Decl *Found = *Lookup.first;
2177 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2178 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2179 Found = Tag->getDecl();
2180 }
2181
2182 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002183 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2184 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2185 // The record types structurally match, or the "from" translation
2186 // unit only had a forward declaration anyway; call it the same
2187 // function.
2188 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002189 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002190 }
2191 } else {
2192 // We have a forward declaration of this type, so adopt that forward
2193 // declaration rather than building a new one.
2194 AdoptDecl = FoundRecord;
2195 continue;
2196 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002197 }
2198
2199 ConflictingDecls.push_back(*Lookup.first);
2200 }
2201
2202 if (!ConflictingDecls.empty()) {
2203 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2204 ConflictingDecls.data(),
2205 ConflictingDecls.size());
2206 }
2207 }
2208
2209 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002210 RecordDecl *D2 = AdoptDecl;
2211 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002212 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002213 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002214 D->getTagKind(),
2215 DC, Loc,
2216 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002217 Importer.Import(D->getLocStart()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002218 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002219 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002220 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002221 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002222 DC, Loc,
2223 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002224 Importer.Import(D->getLocStart()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002225 }
Douglas Gregor14454802011-02-25 02:25:35 +00002226
2227 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002228 D2->setLexicalDeclContext(LexicalDC);
2229 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002230 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002231
Douglas Gregor3996e242010-02-15 22:01:00 +00002232 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002233
Douglas Gregore2e50d332010-12-01 01:36:18 +00002234 if (D->isDefinition() && ImportDefinition(D, D2))
2235 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002236
Douglas Gregor3996e242010-02-15 22:01:00 +00002237 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002238}
2239
Douglas Gregor98c10182010-02-12 22:17:39 +00002240Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2241 // Import the major distinguishing characteristics of this enumerator.
2242 DeclContext *DC, *LexicalDC;
2243 DeclarationName Name;
2244 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002245 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002246 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002247
2248 QualType T = Importer.Import(D->getType());
2249 if (T.isNull())
2250 return 0;
2251
Douglas Gregor98c10182010-02-12 22:17:39 +00002252 // Determine whether there are any other declarations with the same name and
2253 // in the same context.
2254 if (!LexicalDC->isFunctionOrMethod()) {
2255 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2256 unsigned IDNS = Decl::IDNS_Ordinary;
2257 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2258 Lookup.first != Lookup.second;
2259 ++Lookup.first) {
2260 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2261 continue;
2262
2263 ConflictingDecls.push_back(*Lookup.first);
2264 }
2265
2266 if (!ConflictingDecls.empty()) {
2267 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2268 ConflictingDecls.data(),
2269 ConflictingDecls.size());
2270 if (!Name)
2271 return 0;
2272 }
2273 }
2274
2275 Expr *Init = Importer.Import(D->getInitExpr());
2276 if (D->getInitExpr() && !Init)
2277 return 0;
2278
2279 EnumConstantDecl *ToEnumerator
2280 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2281 Name.getAsIdentifierInfo(), T,
2282 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002283 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002284 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002285 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002286 LexicalDC->addDecl(ToEnumerator);
2287 return ToEnumerator;
2288}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002289
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002290Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2291 // Import the major distinguishing characteristics of this function.
2292 DeclContext *DC, *LexicalDC;
2293 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002294 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002295 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002296 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002297
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002298 // Try to find a function in our own ("to") context with the same name, same
2299 // type, and in the same context as the function we're importing.
2300 if (!LexicalDC->isFunctionOrMethod()) {
2301 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2302 unsigned IDNS = Decl::IDNS_Ordinary;
2303 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2304 Lookup.first != Lookup.second;
2305 ++Lookup.first) {
2306 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2307 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002308
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002309 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2310 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2311 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002312 if (Importer.IsStructurallyEquivalent(D->getType(),
2313 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002314 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002315 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002316 }
2317
2318 // FIXME: Check for overloading more carefully, e.g., by boosting
2319 // Sema::IsOverload out to the AST library.
2320
2321 // Function overloading is okay in C++.
2322 if (Importer.getToContext().getLangOptions().CPlusPlus)
2323 continue;
2324
2325 // Complain about inconsistent function types.
2326 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002327 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002328 Importer.ToDiag(FoundFunction->getLocation(),
2329 diag::note_odr_value_here)
2330 << FoundFunction->getType();
2331 }
2332 }
2333
2334 ConflictingDecls.push_back(*Lookup.first);
2335 }
2336
2337 if (!ConflictingDecls.empty()) {
2338 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2339 ConflictingDecls.data(),
2340 ConflictingDecls.size());
2341 if (!Name)
2342 return 0;
2343 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002344 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002345
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002346 DeclarationNameInfo NameInfo(Name, Loc);
2347 // Import additional name location/type info.
2348 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2349
Douglas Gregorb4964f72010-02-15 23:54:17 +00002350 // Import the type.
2351 QualType T = Importer.Import(D->getType());
2352 if (T.isNull())
2353 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002354
2355 // Import the function parameters.
2356 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2357 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2358 P != PEnd; ++P) {
2359 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2360 if (!ToP)
2361 return 0;
2362
2363 Parameters.push_back(ToP);
2364 }
2365
2366 // Create the imported function.
2367 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002368 FunctionDecl *ToFunction = 0;
2369 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2370 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2371 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002372 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002373 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002374 FromConstructor->isExplicit(),
2375 D->isInlineSpecified(),
2376 D->isImplicit());
2377 } else if (isa<CXXDestructorDecl>(D)) {
2378 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2379 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002380 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002381 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002382 D->isInlineSpecified(),
2383 D->isImplicit());
2384 } else if (CXXConversionDecl *FromConversion
2385 = dyn_cast<CXXConversionDecl>(D)) {
2386 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2387 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002388 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002389 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002390 D->isInlineSpecified(),
2391 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002392 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2393 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2394 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002395 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002396 NameInfo, T, TInfo,
2397 Method->isStatic(),
2398 Method->getStorageClassAsWritten(),
2399 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002400 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002401 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002402 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002403 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002404 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002405 D->isInlineSpecified(),
2406 D->hasWrittenPrototype());
2407 }
John McCall3e11ebe2010-03-15 10:12:16 +00002408
2409 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002410 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002411 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002412 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002413 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2414 ToFunction->setTrivial(D->isTrivial());
2415 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002416 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002417
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002418 // Set the parameters.
2419 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002420 Parameters[I]->setOwningFunction(ToFunction);
2421 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002422 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002423 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002424
2425 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002426
2427 // Add this function to the lexical context.
2428 LexicalDC->addDecl(ToFunction);
2429
Douglas Gregor43f54792010-02-17 02:12:47 +00002430 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002431}
2432
Douglas Gregor00eace12010-02-21 18:29:16 +00002433Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2434 return VisitFunctionDecl(D);
2435}
2436
2437Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2438 return VisitCXXMethodDecl(D);
2439}
2440
2441Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2442 return VisitCXXMethodDecl(D);
2443}
2444
2445Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2446 return VisitCXXMethodDecl(D);
2447}
2448
Douglas Gregor5c73e912010-02-11 00:48:18 +00002449Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2450 // Import the major distinguishing characteristics of a variable.
2451 DeclContext *DC, *LexicalDC;
2452 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002453 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002454 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2455 return 0;
2456
2457 // Import the type.
2458 QualType T = Importer.Import(D->getType());
2459 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002460 return 0;
2461
2462 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2463 Expr *BitWidth = Importer.Import(D->getBitWidth());
2464 if (!BitWidth && D->getBitWidth())
2465 return 0;
2466
Abramo Bagnaradff19302011-03-08 08:55:46 +00002467 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2468 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002469 Loc, Name.getAsIdentifierInfo(),
2470 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002471 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002472 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002473 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002474 LexicalDC->addDecl(ToField);
2475 return ToField;
2476}
2477
Francois Pichet783dd6e2010-11-21 06:08:52 +00002478Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2479 // Import the major distinguishing characteristics of a variable.
2480 DeclContext *DC, *LexicalDC;
2481 DeclarationName Name;
2482 SourceLocation Loc;
2483 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2484 return 0;
2485
2486 // Import the type.
2487 QualType T = Importer.Import(D->getType());
2488 if (T.isNull())
2489 return 0;
2490
2491 NamedDecl **NamedChain =
2492 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2493
2494 unsigned i = 0;
2495 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2496 PE = D->chain_end(); PI != PE; ++PI) {
2497 Decl* D = Importer.Import(*PI);
2498 if (!D)
2499 return 0;
2500 NamedChain[i++] = cast<NamedDecl>(D);
2501 }
2502
2503 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2504 Importer.getToContext(), DC,
2505 Loc, Name.getAsIdentifierInfo(), T,
2506 NamedChain, D->getChainingSize());
2507 ToIndirectField->setAccess(D->getAccess());
2508 ToIndirectField->setLexicalDeclContext(LexicalDC);
2509 Importer.Imported(D, ToIndirectField);
2510 LexicalDC->addDecl(ToIndirectField);
2511 return ToIndirectField;
2512}
2513
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002514Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2515 // Import the major distinguishing characteristics of an ivar.
2516 DeclContext *DC, *LexicalDC;
2517 DeclarationName Name;
2518 SourceLocation Loc;
2519 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2520 return 0;
2521
2522 // Determine whether we've already imported this ivar
2523 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2524 Lookup.first != Lookup.second;
2525 ++Lookup.first) {
2526 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2527 if (Importer.IsStructurallyEquivalent(D->getType(),
2528 FoundIvar->getType())) {
2529 Importer.Imported(D, FoundIvar);
2530 return FoundIvar;
2531 }
2532
2533 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2534 << Name << D->getType() << FoundIvar->getType();
2535 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2536 << FoundIvar->getType();
2537 return 0;
2538 }
2539 }
2540
2541 // Import the type.
2542 QualType T = Importer.Import(D->getType());
2543 if (T.isNull())
2544 return 0;
2545
2546 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2547 Expr *BitWidth = Importer.Import(D->getBitWidth());
2548 if (!BitWidth && D->getBitWidth())
2549 return 0;
2550
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002551 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2552 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002553 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002554 Loc, Name.getAsIdentifierInfo(),
2555 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002556 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002557 ToIvar->setLexicalDeclContext(LexicalDC);
2558 Importer.Imported(D, ToIvar);
2559 LexicalDC->addDecl(ToIvar);
2560 return ToIvar;
2561
2562}
2563
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002564Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2565 // Import the major distinguishing characteristics of a variable.
2566 DeclContext *DC, *LexicalDC;
2567 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002568 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002569 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002570 return 0;
2571
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002572 // Try to find a variable in our own ("to") context with the same name and
2573 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002574 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002575 VarDecl *MergeWithVar = 0;
2576 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2577 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002578 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002579 Lookup.first != Lookup.second;
2580 ++Lookup.first) {
2581 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2582 continue;
2583
2584 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2585 // We have found a variable that we may need to merge with. Check it.
2586 if (isExternalLinkage(FoundVar->getLinkage()) &&
2587 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002588 if (Importer.IsStructurallyEquivalent(D->getType(),
2589 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002590 MergeWithVar = FoundVar;
2591 break;
2592 }
2593
Douglas Gregor56521c52010-02-12 17:23:39 +00002594 const ArrayType *FoundArray
2595 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2596 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002597 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002598 if (FoundArray && TArray) {
2599 if (isa<IncompleteArrayType>(FoundArray) &&
2600 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002601 // Import the type.
2602 QualType T = Importer.Import(D->getType());
2603 if (T.isNull())
2604 return 0;
2605
Douglas Gregor56521c52010-02-12 17:23:39 +00002606 FoundVar->setType(T);
2607 MergeWithVar = FoundVar;
2608 break;
2609 } else if (isa<IncompleteArrayType>(TArray) &&
2610 isa<ConstantArrayType>(FoundArray)) {
2611 MergeWithVar = FoundVar;
2612 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002613 }
2614 }
2615
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002616 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002617 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002618 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2619 << FoundVar->getType();
2620 }
2621 }
2622
2623 ConflictingDecls.push_back(*Lookup.first);
2624 }
2625
2626 if (MergeWithVar) {
2627 // An equivalent variable with external linkage has been found. Link
2628 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002629 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002630
2631 if (VarDecl *DDef = D->getDefinition()) {
2632 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2633 Importer.ToDiag(ExistingDef->getLocation(),
2634 diag::err_odr_variable_multiple_def)
2635 << Name;
2636 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2637 } else {
2638 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002639 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002640 }
2641 }
2642
2643 return MergeWithVar;
2644 }
2645
2646 if (!ConflictingDecls.empty()) {
2647 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2648 ConflictingDecls.data(),
2649 ConflictingDecls.size());
2650 if (!Name)
2651 return 0;
2652 }
2653 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002654
Douglas Gregorb4964f72010-02-15 23:54:17 +00002655 // Import the type.
2656 QualType T = Importer.Import(D->getType());
2657 if (T.isNull())
2658 return 0;
2659
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002660 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002661 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002662 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2663 Importer.Import(D->getInnerLocStart()),
2664 Loc, Name.getAsIdentifierInfo(),
2665 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002666 D->getStorageClass(),
2667 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002668 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002669 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002670 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002671 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002672 LexicalDC->addDecl(ToVar);
2673
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002674 // Merge the initializer.
2675 // FIXME: Can we really import any initializer? Alternatively, we could force
2676 // ourselves to import every declaration of a variable and then only use
2677 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002678 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002679
2680 // FIXME: Other bits to merge?
2681
2682 return ToVar;
2683}
2684
Douglas Gregor8b228d72010-02-17 21:22:52 +00002685Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2686 // Parameters are created in the translation unit's context, then moved
2687 // into the function declaration's context afterward.
2688 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2689
2690 // Import the name of this declaration.
2691 DeclarationName Name = Importer.Import(D->getDeclName());
2692 if (D->getDeclName() && !Name)
2693 return 0;
2694
2695 // Import the location of this declaration.
2696 SourceLocation Loc = Importer.Import(D->getLocation());
2697
2698 // Import the parameter's type.
2699 QualType T = Importer.Import(D->getType());
2700 if (T.isNull())
2701 return 0;
2702
2703 // Create the imported parameter.
2704 ImplicitParamDecl *ToParm
2705 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2706 Loc, Name.getAsIdentifierInfo(),
2707 T);
2708 return Importer.Imported(D, ToParm);
2709}
2710
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002711Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2712 // Parameters are created in the translation unit's context, then moved
2713 // into the function declaration's context afterward.
2714 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2715
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002716 // Import the name of this declaration.
2717 DeclarationName Name = Importer.Import(D->getDeclName());
2718 if (D->getDeclName() && !Name)
2719 return 0;
2720
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002721 // Import the location of this declaration.
2722 SourceLocation Loc = Importer.Import(D->getLocation());
2723
2724 // Import the parameter's type.
2725 QualType T = Importer.Import(D->getType());
2726 if (T.isNull())
2727 return 0;
2728
2729 // Create the imported parameter.
2730 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2731 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002732 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002733 Loc, Name.getAsIdentifierInfo(),
2734 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002735 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002736 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002737 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002738 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002739}
2740
Douglas Gregor43f54792010-02-17 02:12:47 +00002741Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2742 // Import the major distinguishing characteristics of a method.
2743 DeclContext *DC, *LexicalDC;
2744 DeclarationName Name;
2745 SourceLocation Loc;
2746 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2747 return 0;
2748
2749 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2750 Lookup.first != Lookup.second;
2751 ++Lookup.first) {
2752 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2753 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2754 continue;
2755
2756 // Check return types.
2757 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2758 FoundMethod->getResultType())) {
2759 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2760 << D->isInstanceMethod() << Name
2761 << D->getResultType() << FoundMethod->getResultType();
2762 Importer.ToDiag(FoundMethod->getLocation(),
2763 diag::note_odr_objc_method_here)
2764 << D->isInstanceMethod() << Name;
2765 return 0;
2766 }
2767
2768 // Check the number of parameters.
2769 if (D->param_size() != FoundMethod->param_size()) {
2770 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2771 << D->isInstanceMethod() << Name
2772 << D->param_size() << FoundMethod->param_size();
2773 Importer.ToDiag(FoundMethod->getLocation(),
2774 diag::note_odr_objc_method_here)
2775 << D->isInstanceMethod() << Name;
2776 return 0;
2777 }
2778
2779 // Check parameter types.
2780 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2781 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2782 P != PEnd; ++P, ++FoundP) {
2783 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2784 (*FoundP)->getType())) {
2785 Importer.FromDiag((*P)->getLocation(),
2786 diag::err_odr_objc_method_param_type_inconsistent)
2787 << D->isInstanceMethod() << Name
2788 << (*P)->getType() << (*FoundP)->getType();
2789 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2790 << (*FoundP)->getType();
2791 return 0;
2792 }
2793 }
2794
2795 // Check variadic/non-variadic.
2796 // Check the number of parameters.
2797 if (D->isVariadic() != FoundMethod->isVariadic()) {
2798 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2799 << D->isInstanceMethod() << Name;
2800 Importer.ToDiag(FoundMethod->getLocation(),
2801 diag::note_odr_objc_method_here)
2802 << D->isInstanceMethod() << Name;
2803 return 0;
2804 }
2805
2806 // FIXME: Any other bits we need to merge?
2807 return Importer.Imported(D, FoundMethod);
2808 }
2809 }
2810
2811 // Import the result type.
2812 QualType ResultTy = Importer.Import(D->getResultType());
2813 if (ResultTy.isNull())
2814 return 0;
2815
Douglas Gregor12852d92010-03-08 14:59:44 +00002816 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2817
Douglas Gregor43f54792010-02-17 02:12:47 +00002818 ObjCMethodDecl *ToMethod
2819 = ObjCMethodDecl::Create(Importer.getToContext(),
2820 Loc,
2821 Importer.Import(D->getLocEnd()),
2822 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002823 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002824 D->isInstanceMethod(),
2825 D->isVariadic(),
2826 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002827 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002828 D->getImplementationControl());
2829
2830 // FIXME: When we decide to merge method definitions, we'll need to
2831 // deal with implicit parameters.
2832
2833 // Import the parameters
2834 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2835 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2836 FromPEnd = D->param_end();
2837 FromP != FromPEnd;
2838 ++FromP) {
2839 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2840 if (!ToP)
2841 return 0;
2842
2843 ToParams.push_back(ToP);
2844 }
2845
2846 // Set the parameters.
2847 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2848 ToParams[I]->setOwningFunction(ToMethod);
2849 ToMethod->addDecl(ToParams[I]);
2850 }
2851 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002852 ToParams.data(), ToParams.size(),
2853 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002854
2855 ToMethod->setLexicalDeclContext(LexicalDC);
2856 Importer.Imported(D, ToMethod);
2857 LexicalDC->addDecl(ToMethod);
2858 return ToMethod;
2859}
2860
Douglas Gregor84c51c32010-02-18 01:47:50 +00002861Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2862 // Import the major distinguishing characteristics of a category.
2863 DeclContext *DC, *LexicalDC;
2864 DeclarationName Name;
2865 SourceLocation Loc;
2866 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2867 return 0;
2868
2869 ObjCInterfaceDecl *ToInterface
2870 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2871 if (!ToInterface)
2872 return 0;
2873
2874 // Determine if we've already encountered this category.
2875 ObjCCategoryDecl *MergeWithCategory
2876 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2877 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2878 if (!ToCategory) {
2879 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2880 Importer.Import(D->getAtLoc()),
2881 Loc,
2882 Importer.Import(D->getCategoryNameLoc()),
2883 Name.getAsIdentifierInfo());
2884 ToCategory->setLexicalDeclContext(LexicalDC);
2885 LexicalDC->addDecl(ToCategory);
2886 Importer.Imported(D, ToCategory);
2887
2888 // Link this category into its class's category list.
2889 ToCategory->setClassInterface(ToInterface);
2890 ToCategory->insertNextClassCategory();
2891
2892 // Import protocols
2893 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2894 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2895 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2896 = D->protocol_loc_begin();
2897 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2898 FromProtoEnd = D->protocol_end();
2899 FromProto != FromProtoEnd;
2900 ++FromProto, ++FromProtoLoc) {
2901 ObjCProtocolDecl *ToProto
2902 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2903 if (!ToProto)
2904 return 0;
2905 Protocols.push_back(ToProto);
2906 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2907 }
2908
2909 // FIXME: If we're merging, make sure that the protocol list is the same.
2910 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2911 ProtocolLocs.data(), Importer.getToContext());
2912
2913 } else {
2914 Importer.Imported(D, ToCategory);
2915 }
2916
2917 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002918 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002919
2920 // If we have an implementation, import it as well.
2921 if (D->getImplementation()) {
2922 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002923 = cast_or_null<ObjCCategoryImplDecl>(
2924 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002925 if (!Impl)
2926 return 0;
2927
2928 ToCategory->setImplementation(Impl);
2929 }
2930
2931 return ToCategory;
2932}
2933
Douglas Gregor98d156a2010-02-17 16:12:00 +00002934Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002935 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002936 DeclContext *DC, *LexicalDC;
2937 DeclarationName Name;
2938 SourceLocation Loc;
2939 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2940 return 0;
2941
2942 ObjCProtocolDecl *MergeWithProtocol = 0;
2943 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2944 Lookup.first != Lookup.second;
2945 ++Lookup.first) {
2946 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2947 continue;
2948
2949 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2950 break;
2951 }
2952
2953 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2954 if (!ToProto || ToProto->isForwardDecl()) {
2955 if (!ToProto) {
2956 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2957 Name.getAsIdentifierInfo());
2958 ToProto->setForwardDecl(D->isForwardDecl());
2959 ToProto->setLexicalDeclContext(LexicalDC);
2960 LexicalDC->addDecl(ToProto);
2961 }
2962 Importer.Imported(D, ToProto);
2963
2964 // Import protocols
2965 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2966 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2967 ObjCProtocolDecl::protocol_loc_iterator
2968 FromProtoLoc = D->protocol_loc_begin();
2969 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2970 FromProtoEnd = D->protocol_end();
2971 FromProto != FromProtoEnd;
2972 ++FromProto, ++FromProtoLoc) {
2973 ObjCProtocolDecl *ToProto
2974 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2975 if (!ToProto)
2976 return 0;
2977 Protocols.push_back(ToProto);
2978 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2979 }
2980
2981 // FIXME: If we're merging, make sure that the protocol list is the same.
2982 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2983 ProtocolLocs.data(), Importer.getToContext());
2984 } else {
2985 Importer.Imported(D, ToProto);
2986 }
2987
Douglas Gregor84c51c32010-02-18 01:47:50 +00002988 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002989 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002990
2991 return ToProto;
2992}
2993
Douglas Gregor45635322010-02-16 01:20:57 +00002994Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2995 // Import the major distinguishing characteristics of an @interface.
2996 DeclContext *DC, *LexicalDC;
2997 DeclarationName Name;
2998 SourceLocation Loc;
2999 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3000 return 0;
3001
3002 ObjCInterfaceDecl *MergeWithIface = 0;
3003 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3004 Lookup.first != Lookup.second;
3005 ++Lookup.first) {
3006 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3007 continue;
3008
3009 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3010 break;
3011 }
3012
3013 ObjCInterfaceDecl *ToIface = MergeWithIface;
3014 if (!ToIface || ToIface->isForwardDecl()) {
3015 if (!ToIface) {
3016 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3017 DC, Loc,
3018 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003019 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003020 D->isForwardDecl(),
3021 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003022 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003023 ToIface->setLexicalDeclContext(LexicalDC);
3024 LexicalDC->addDecl(ToIface);
3025 }
3026 Importer.Imported(D, ToIface);
3027
Douglas Gregor45635322010-02-16 01:20:57 +00003028 if (D->getSuperClass()) {
3029 ObjCInterfaceDecl *Super
3030 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3031 if (!Super)
3032 return 0;
3033
3034 ToIface->setSuperClass(Super);
3035 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3036 }
3037
3038 // Import protocols
3039 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3040 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3041 ObjCInterfaceDecl::protocol_loc_iterator
3042 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003043
3044 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003045 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3046 FromProtoEnd = D->protocol_end();
3047 FromProto != FromProtoEnd;
3048 ++FromProto, ++FromProtoLoc) {
3049 ObjCProtocolDecl *ToProto
3050 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3051 if (!ToProto)
3052 return 0;
3053 Protocols.push_back(ToProto);
3054 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3055 }
3056
3057 // FIXME: If we're merging, make sure that the protocol list is the same.
3058 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3059 ProtocolLocs.data(), Importer.getToContext());
3060
Douglas Gregor45635322010-02-16 01:20:57 +00003061 // Import @end range
3062 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3063 } else {
3064 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003065
3066 // Check for consistency of superclasses.
3067 DeclarationName FromSuperName, ToSuperName;
3068 if (D->getSuperClass())
3069 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3070 if (ToIface->getSuperClass())
3071 ToSuperName = ToIface->getSuperClass()->getDeclName();
3072 if (FromSuperName != ToSuperName) {
3073 Importer.ToDiag(ToIface->getLocation(),
3074 diag::err_odr_objc_superclass_inconsistent)
3075 << ToIface->getDeclName();
3076 if (ToIface->getSuperClass())
3077 Importer.ToDiag(ToIface->getSuperClassLoc(),
3078 diag::note_odr_objc_superclass)
3079 << ToIface->getSuperClass()->getDeclName();
3080 else
3081 Importer.ToDiag(ToIface->getLocation(),
3082 diag::note_odr_objc_missing_superclass);
3083 if (D->getSuperClass())
3084 Importer.FromDiag(D->getSuperClassLoc(),
3085 diag::note_odr_objc_superclass)
3086 << D->getSuperClass()->getDeclName();
3087 else
3088 Importer.FromDiag(D->getLocation(),
3089 diag::note_odr_objc_missing_superclass);
3090 return 0;
3091 }
Douglas Gregor45635322010-02-16 01:20:57 +00003092 }
3093
Douglas Gregor84c51c32010-02-18 01:47:50 +00003094 // Import categories. When the categories themselves are imported, they'll
3095 // hook themselves into this interface.
3096 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3097 FromCat = FromCat->getNextClassCategory())
3098 Importer.Import(FromCat);
3099
Douglas Gregor45635322010-02-16 01:20:57 +00003100 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003101 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003102
3103 // If we have an @implementation, import it as well.
3104 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003105 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3106 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003107 if (!Impl)
3108 return 0;
3109
3110 ToIface->setImplementation(Impl);
3111 }
3112
Douglas Gregor98d156a2010-02-17 16:12:00 +00003113 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003114}
3115
Douglas Gregor4da9d682010-12-07 15:32:12 +00003116Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3117 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3118 Importer.Import(D->getCategoryDecl()));
3119 if (!Category)
3120 return 0;
3121
3122 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3123 if (!ToImpl) {
3124 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3125 if (!DC)
3126 return 0;
3127
3128 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3129 Importer.Import(D->getLocation()),
3130 Importer.Import(D->getIdentifier()),
3131 Category->getClassInterface());
3132
3133 DeclContext *LexicalDC = DC;
3134 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3135 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3136 if (!LexicalDC)
3137 return 0;
3138
3139 ToImpl->setLexicalDeclContext(LexicalDC);
3140 }
3141
3142 LexicalDC->addDecl(ToImpl);
3143 Category->setImplementation(ToImpl);
3144 }
3145
3146 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003147 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003148 return ToImpl;
3149}
3150
Douglas Gregorda8025c2010-12-07 01:26:03 +00003151Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3152 // Find the corresponding interface.
3153 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3154 Importer.Import(D->getClassInterface()));
3155 if (!Iface)
3156 return 0;
3157
3158 // Import the superclass, if any.
3159 ObjCInterfaceDecl *Super = 0;
3160 if (D->getSuperClass()) {
3161 Super = cast_or_null<ObjCInterfaceDecl>(
3162 Importer.Import(D->getSuperClass()));
3163 if (!Super)
3164 return 0;
3165 }
3166
3167 ObjCImplementationDecl *Impl = Iface->getImplementation();
3168 if (!Impl) {
3169 // We haven't imported an implementation yet. Create a new @implementation
3170 // now.
3171 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3172 Importer.ImportContext(D->getDeclContext()),
3173 Importer.Import(D->getLocation()),
3174 Iface, Super);
3175
3176 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3177 DeclContext *LexicalDC
3178 = Importer.ImportContext(D->getLexicalDeclContext());
3179 if (!LexicalDC)
3180 return 0;
3181 Impl->setLexicalDeclContext(LexicalDC);
3182 }
3183
3184 // Associate the implementation with the class it implements.
3185 Iface->setImplementation(Impl);
3186 Importer.Imported(D, Iface->getImplementation());
3187 } else {
3188 Importer.Imported(D, Iface->getImplementation());
3189
3190 // Verify that the existing @implementation has the same superclass.
3191 if ((Super && !Impl->getSuperClass()) ||
3192 (!Super && Impl->getSuperClass()) ||
3193 (Super && Impl->getSuperClass() &&
3194 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3195 Importer.ToDiag(Impl->getLocation(),
3196 diag::err_odr_objc_superclass_inconsistent)
3197 << Iface->getDeclName();
3198 // FIXME: It would be nice to have the location of the superclass
3199 // below.
3200 if (Impl->getSuperClass())
3201 Importer.ToDiag(Impl->getLocation(),
3202 diag::note_odr_objc_superclass)
3203 << Impl->getSuperClass()->getDeclName();
3204 else
3205 Importer.ToDiag(Impl->getLocation(),
3206 diag::note_odr_objc_missing_superclass);
3207 if (D->getSuperClass())
3208 Importer.FromDiag(D->getLocation(),
3209 diag::note_odr_objc_superclass)
3210 << D->getSuperClass()->getDeclName();
3211 else
3212 Importer.FromDiag(D->getLocation(),
3213 diag::note_odr_objc_missing_superclass);
3214 return 0;
3215 }
3216 }
3217
3218 // Import all of the members of this @implementation.
3219 ImportDeclContext(D);
3220
3221 return Impl;
3222}
3223
Douglas Gregora11c4582010-02-17 18:02:10 +00003224Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3225 // Import the major distinguishing characteristics of an @property.
3226 DeclContext *DC, *LexicalDC;
3227 DeclarationName Name;
3228 SourceLocation Loc;
3229 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3230 return 0;
3231
3232 // Check whether we have already imported this property.
3233 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3234 Lookup.first != Lookup.second;
3235 ++Lookup.first) {
3236 if (ObjCPropertyDecl *FoundProp
3237 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3238 // Check property types.
3239 if (!Importer.IsStructurallyEquivalent(D->getType(),
3240 FoundProp->getType())) {
3241 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3242 << Name << D->getType() << FoundProp->getType();
3243 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3244 << FoundProp->getType();
3245 return 0;
3246 }
3247
3248 // FIXME: Check property attributes, getters, setters, etc.?
3249
3250 // Consider these properties to be equivalent.
3251 Importer.Imported(D, FoundProp);
3252 return FoundProp;
3253 }
3254 }
3255
3256 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003257 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3258 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003259 return 0;
3260
3261 // Create the new property.
3262 ObjCPropertyDecl *ToProperty
3263 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3264 Name.getAsIdentifierInfo(),
3265 Importer.Import(D->getAtLoc()),
3266 T,
3267 D->getPropertyImplementation());
3268 Importer.Imported(D, ToProperty);
3269 ToProperty->setLexicalDeclContext(LexicalDC);
3270 LexicalDC->addDecl(ToProperty);
3271
3272 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003273 ToProperty->setPropertyAttributesAsWritten(
3274 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003275 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3276 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3277 ToProperty->setGetterMethodDecl(
3278 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3279 ToProperty->setSetterMethodDecl(
3280 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3281 ToProperty->setPropertyIvarDecl(
3282 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3283 return ToProperty;
3284}
3285
Douglas Gregor14a49e22010-12-07 18:32:03 +00003286Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3287 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3288 Importer.Import(D->getPropertyDecl()));
3289 if (!Property)
3290 return 0;
3291
3292 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3293 if (!DC)
3294 return 0;
3295
3296 // Import the lexical declaration context.
3297 DeclContext *LexicalDC = DC;
3298 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3299 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3300 if (!LexicalDC)
3301 return 0;
3302 }
3303
3304 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3305 if (!InImpl)
3306 return 0;
3307
3308 // Import the ivar (for an @synthesize).
3309 ObjCIvarDecl *Ivar = 0;
3310 if (D->getPropertyIvarDecl()) {
3311 Ivar = cast_or_null<ObjCIvarDecl>(
3312 Importer.Import(D->getPropertyIvarDecl()));
3313 if (!Ivar)
3314 return 0;
3315 }
3316
3317 ObjCPropertyImplDecl *ToImpl
3318 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3319 if (!ToImpl) {
3320 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3321 Importer.Import(D->getLocStart()),
3322 Importer.Import(D->getLocation()),
3323 Property,
3324 D->getPropertyImplementation(),
3325 Ivar,
3326 Importer.Import(D->getPropertyIvarDeclLoc()));
3327 ToImpl->setLexicalDeclContext(LexicalDC);
3328 Importer.Imported(D, ToImpl);
3329 LexicalDC->addDecl(ToImpl);
3330 } else {
3331 // Check that we have the same kind of property implementation (@synthesize
3332 // vs. @dynamic).
3333 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3334 Importer.ToDiag(ToImpl->getLocation(),
3335 diag::err_odr_objc_property_impl_kind_inconsistent)
3336 << Property->getDeclName()
3337 << (ToImpl->getPropertyImplementation()
3338 == ObjCPropertyImplDecl::Dynamic);
3339 Importer.FromDiag(D->getLocation(),
3340 diag::note_odr_objc_property_impl_kind)
3341 << D->getPropertyDecl()->getDeclName()
3342 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3343 return 0;
3344 }
3345
3346 // For @synthesize, check that we have the same
3347 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3348 Ivar != ToImpl->getPropertyIvarDecl()) {
3349 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3350 diag::err_odr_objc_synthesize_ivar_inconsistent)
3351 << Property->getDeclName()
3352 << ToImpl->getPropertyIvarDecl()->getDeclName()
3353 << Ivar->getDeclName();
3354 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3355 diag::note_odr_objc_synthesize_ivar_here)
3356 << D->getPropertyIvarDecl()->getDeclName();
3357 return 0;
3358 }
3359
3360 // Merge the existing implementation with the new implementation.
3361 Importer.Imported(D, ToImpl);
3362 }
3363
3364 return ToImpl;
3365}
3366
Douglas Gregor8661a722010-02-18 02:12:22 +00003367Decl *
3368ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3369 // Import the context of this declaration.
3370 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3371 if (!DC)
3372 return 0;
3373
3374 DeclContext *LexicalDC = DC;
3375 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3376 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3377 if (!LexicalDC)
3378 return 0;
3379 }
3380
3381 // Import the location of this declaration.
3382 SourceLocation Loc = Importer.Import(D->getLocation());
3383
3384 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3385 llvm::SmallVector<SourceLocation, 4> Locations;
3386 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3387 = D->protocol_loc_begin();
3388 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3389 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3390 FromProto != FromProtoEnd;
3391 ++FromProto, ++FromProtoLoc) {
3392 ObjCProtocolDecl *ToProto
3393 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3394 if (!ToProto)
3395 continue;
3396
3397 Protocols.push_back(ToProto);
3398 Locations.push_back(Importer.Import(*FromProtoLoc));
3399 }
3400
3401 ObjCForwardProtocolDecl *ToForward
3402 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3403 Protocols.data(), Protocols.size(),
3404 Locations.data());
3405 ToForward->setLexicalDeclContext(LexicalDC);
3406 LexicalDC->addDecl(ToForward);
3407 Importer.Imported(D, ToForward);
3408 return ToForward;
3409}
3410
Douglas Gregor06537af2010-02-18 02:04:09 +00003411Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3412 // Import the context of this declaration.
3413 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3414 if (!DC)
3415 return 0;
3416
3417 DeclContext *LexicalDC = DC;
3418 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3419 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3420 if (!LexicalDC)
3421 return 0;
3422 }
3423
3424 // Import the location of this declaration.
3425 SourceLocation Loc = Importer.Import(D->getLocation());
3426
3427 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3428 llvm::SmallVector<SourceLocation, 4> Locations;
3429 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3430 From != FromEnd; ++From) {
3431 ObjCInterfaceDecl *ToIface
3432 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3433 if (!ToIface)
3434 continue;
3435
3436 Interfaces.push_back(ToIface);
3437 Locations.push_back(Importer.Import(From->getLocation()));
3438 }
3439
3440 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3441 Loc,
3442 Interfaces.data(),
3443 Locations.data(),
3444 Interfaces.size());
3445 ToClass->setLexicalDeclContext(LexicalDC);
3446 LexicalDC->addDecl(ToClass);
3447 Importer.Imported(D, ToClass);
3448 return ToClass;
3449}
3450
Douglas Gregora082a492010-11-30 19:14:50 +00003451Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3452 // For template arguments, we adopt the translation unit as our declaration
3453 // context. This context will be fixed when the actual template declaration
3454 // is created.
3455
3456 // FIXME: Import default argument.
3457 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3458 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003459 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003460 Importer.Import(D->getLocation()),
3461 D->getDepth(),
3462 D->getIndex(),
3463 Importer.Import(D->getIdentifier()),
3464 D->wasDeclaredWithTypename(),
3465 D->isParameterPack());
3466}
3467
3468Decl *
3469ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3470 // Import the name of this declaration.
3471 DeclarationName Name = Importer.Import(D->getDeclName());
3472 if (D->getDeclName() && !Name)
3473 return 0;
3474
3475 // Import the location of this declaration.
3476 SourceLocation Loc = Importer.Import(D->getLocation());
3477
3478 // Import the type of this declaration.
3479 QualType T = Importer.Import(D->getType());
3480 if (T.isNull())
3481 return 0;
3482
3483 // Import type-source information.
3484 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3485 if (D->getTypeSourceInfo() && !TInfo)
3486 return 0;
3487
3488 // FIXME: Import default argument.
3489
3490 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3491 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003492 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003493 Loc, D->getDepth(), D->getPosition(),
3494 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003495 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003496}
3497
3498Decl *
3499ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3500 // Import the name of this declaration.
3501 DeclarationName Name = Importer.Import(D->getDeclName());
3502 if (D->getDeclName() && !Name)
3503 return 0;
3504
3505 // Import the location of this declaration.
3506 SourceLocation Loc = Importer.Import(D->getLocation());
3507
3508 // Import template parameters.
3509 TemplateParameterList *TemplateParams
3510 = ImportTemplateParameterList(D->getTemplateParameters());
3511 if (!TemplateParams)
3512 return 0;
3513
3514 // FIXME: Import default argument.
3515
3516 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3517 Importer.getToContext().getTranslationUnitDecl(),
3518 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003519 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003520 Name.getAsIdentifierInfo(),
3521 TemplateParams);
3522}
3523
3524Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3525 // If this record has a definition in the translation unit we're coming from,
3526 // but this particular declaration is not that definition, import the
3527 // definition and map to that.
3528 CXXRecordDecl *Definition
3529 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3530 if (Definition && Definition != D->getTemplatedDecl()) {
3531 Decl *ImportedDef
3532 = Importer.Import(Definition->getDescribedClassTemplate());
3533 if (!ImportedDef)
3534 return 0;
3535
3536 return Importer.Imported(D, ImportedDef);
3537 }
3538
3539 // Import the major distinguishing characteristics of this class template.
3540 DeclContext *DC, *LexicalDC;
3541 DeclarationName Name;
3542 SourceLocation Loc;
3543 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3544 return 0;
3545
3546 // We may already have a template of the same name; try to find and match it.
3547 if (!DC->isFunctionOrMethod()) {
3548 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3549 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3550 Lookup.first != Lookup.second;
3551 ++Lookup.first) {
3552 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3553 continue;
3554
3555 Decl *Found = *Lookup.first;
3556 if (ClassTemplateDecl *FoundTemplate
3557 = dyn_cast<ClassTemplateDecl>(Found)) {
3558 if (IsStructuralMatch(D, FoundTemplate)) {
3559 // The class templates structurally match; call it the same template.
3560 // FIXME: We may be filling in a forward declaration here. Handle
3561 // this case!
3562 Importer.Imported(D->getTemplatedDecl(),
3563 FoundTemplate->getTemplatedDecl());
3564 return Importer.Imported(D, FoundTemplate);
3565 }
3566 }
3567
3568 ConflictingDecls.push_back(*Lookup.first);
3569 }
3570
3571 if (!ConflictingDecls.empty()) {
3572 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3573 ConflictingDecls.data(),
3574 ConflictingDecls.size());
3575 }
3576
3577 if (!Name)
3578 return 0;
3579 }
3580
3581 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3582
3583 // Create the declaration that is being templated.
3584 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3585 DTemplated->getTagKind(),
3586 DC,
3587 Importer.Import(DTemplated->getLocation()),
3588 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00003589 Importer.Import(DTemplated->getLocStart()));
Douglas Gregora082a492010-11-30 19:14:50 +00003590 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003591 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003592 D2Templated->setLexicalDeclContext(LexicalDC);
3593
3594 // Create the class template declaration itself.
3595 TemplateParameterList *TemplateParams
3596 = ImportTemplateParameterList(D->getTemplateParameters());
3597 if (!TemplateParams)
3598 return 0;
3599
3600 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3601 Loc, Name, TemplateParams,
3602 D2Templated,
3603 /*PrevDecl=*/0);
3604 D2Templated->setDescribedClassTemplate(D2);
3605
3606 D2->setAccess(D->getAccess());
3607 D2->setLexicalDeclContext(LexicalDC);
3608 LexicalDC->addDecl(D2);
3609
3610 // Note the relationship between the class templates.
3611 Importer.Imported(D, D2);
3612 Importer.Imported(DTemplated, D2Templated);
3613
3614 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3615 // FIXME: Import definition!
3616 }
3617
3618 return D2;
3619}
3620
Douglas Gregore2e50d332010-12-01 01:36:18 +00003621Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3622 ClassTemplateSpecializationDecl *D) {
3623 // If this record has a definition in the translation unit we're coming from,
3624 // but this particular declaration is not that definition, import the
3625 // definition and map to that.
3626 TagDecl *Definition = D->getDefinition();
3627 if (Definition && Definition != D) {
3628 Decl *ImportedDef = Importer.Import(Definition);
3629 if (!ImportedDef)
3630 return 0;
3631
3632 return Importer.Imported(D, ImportedDef);
3633 }
3634
3635 ClassTemplateDecl *ClassTemplate
3636 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3637 D->getSpecializedTemplate()));
3638 if (!ClassTemplate)
3639 return 0;
3640
3641 // Import the context of this declaration.
3642 DeclContext *DC = ClassTemplate->getDeclContext();
3643 if (!DC)
3644 return 0;
3645
3646 DeclContext *LexicalDC = DC;
3647 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3648 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3649 if (!LexicalDC)
3650 return 0;
3651 }
3652
3653 // Import the location of this declaration.
3654 SourceLocation Loc = Importer.Import(D->getLocation());
3655
3656 // Import template arguments.
3657 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3658 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3659 D->getTemplateArgs().size(),
3660 TemplateArgs))
3661 return 0;
3662
3663 // Try to find an existing specialization with these template arguments.
3664 void *InsertPos = 0;
3665 ClassTemplateSpecializationDecl *D2
3666 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3667 TemplateArgs.size(), InsertPos);
3668 if (D2) {
3669 // We already have a class template specialization with these template
3670 // arguments.
3671
3672 // FIXME: Check for specialization vs. instantiation errors.
3673
3674 if (RecordDecl *FoundDef = D2->getDefinition()) {
3675 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3676 // The record types structurally match, or the "from" translation
3677 // unit only had a forward declaration anyway; call it the same
3678 // function.
3679 return Importer.Imported(D, FoundDef);
3680 }
3681 }
3682 } else {
3683 // Create a new specialization.
3684 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3685 D->getTagKind(), DC,
3686 Loc, ClassTemplate,
3687 TemplateArgs.data(),
3688 TemplateArgs.size(),
3689 /*PrevDecl=*/0);
3690 D2->setSpecializationKind(D->getSpecializationKind());
3691
3692 // Add this specialization to the class template.
3693 ClassTemplate->AddSpecialization(D2, InsertPos);
3694
3695 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003696 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003697
3698 // Add the specialization to this context.
3699 D2->setLexicalDeclContext(LexicalDC);
3700 LexicalDC->addDecl(D2);
3701 }
3702 Importer.Imported(D, D2);
3703
3704 if (D->isDefinition() && ImportDefinition(D, D2))
3705 return 0;
3706
3707 return D2;
3708}
3709
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003710//----------------------------------------------------------------------------
3711// Import Statements
3712//----------------------------------------------------------------------------
3713
3714Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3715 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3716 << S->getStmtClassName();
3717 return 0;
3718}
3719
3720//----------------------------------------------------------------------------
3721// Import Expressions
3722//----------------------------------------------------------------------------
3723Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3724 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3725 << E->getStmtClassName();
3726 return 0;
3727}
3728
Douglas Gregor52f820e2010-02-19 01:17:02 +00003729Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3730 NestedNameSpecifier *Qualifier = 0;
3731 if (E->getQualifier()) {
3732 Qualifier = Importer.Import(E->getQualifier());
3733 if (!E->getQualifier())
3734 return 0;
3735 }
3736
3737 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3738 if (!ToD)
3739 return 0;
3740
3741 QualType T = Importer.Import(E->getType());
3742 if (T.isNull())
3743 return 0;
3744
Douglas Gregorea972d32011-02-28 21:54:11 +00003745 return DeclRefExpr::Create(Importer.getToContext(),
3746 Importer.Import(E->getQualifierLoc()),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003747 ToD,
3748 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003749 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003750 /*FIXME:TemplateArgs=*/0);
3751}
3752
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003753Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3754 QualType T = Importer.Import(E->getType());
3755 if (T.isNull())
3756 return 0;
3757
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003758 return IntegerLiteral::Create(Importer.getToContext(),
3759 E->getValue(), T,
3760 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003761}
3762
Douglas Gregor623421d2010-02-18 02:21:22 +00003763Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3764 QualType T = Importer.Import(E->getType());
3765 if (T.isNull())
3766 return 0;
3767
3768 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3769 E->isWide(), T,
3770 Importer.Import(E->getLocation()));
3771}
3772
Douglas Gregorc74247e2010-02-19 01:07:06 +00003773Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3774 Expr *SubExpr = Importer.Import(E->getSubExpr());
3775 if (!SubExpr)
3776 return 0;
3777
3778 return new (Importer.getToContext())
3779 ParenExpr(Importer.Import(E->getLParen()),
3780 Importer.Import(E->getRParen()),
3781 SubExpr);
3782}
3783
3784Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3785 QualType T = Importer.Import(E->getType());
3786 if (T.isNull())
3787 return 0;
3788
3789 Expr *SubExpr = Importer.Import(E->getSubExpr());
3790 if (!SubExpr)
3791 return 0;
3792
3793 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003794 T, E->getValueKind(),
3795 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003796 Importer.Import(E->getOperatorLoc()));
3797}
3798
Douglas Gregord8552cd2010-02-19 01:24:23 +00003799Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3800 QualType ResultType = Importer.Import(E->getType());
3801
3802 if (E->isArgumentType()) {
3803 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3804 if (!TInfo)
3805 return 0;
3806
3807 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3808 TInfo, ResultType,
3809 Importer.Import(E->getOperatorLoc()),
3810 Importer.Import(E->getRParenLoc()));
3811 }
3812
3813 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3814 if (!SubExpr)
3815 return 0;
3816
3817 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3818 SubExpr, ResultType,
3819 Importer.Import(E->getOperatorLoc()),
3820 Importer.Import(E->getRParenLoc()));
3821}
3822
Douglas Gregorc74247e2010-02-19 01:07:06 +00003823Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3824 QualType T = Importer.Import(E->getType());
3825 if (T.isNull())
3826 return 0;
3827
3828 Expr *LHS = Importer.Import(E->getLHS());
3829 if (!LHS)
3830 return 0;
3831
3832 Expr *RHS = Importer.Import(E->getRHS());
3833 if (!RHS)
3834 return 0;
3835
3836 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003837 T, E->getValueKind(),
3838 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003839 Importer.Import(E->getOperatorLoc()));
3840}
3841
3842Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3843 QualType T = Importer.Import(E->getType());
3844 if (T.isNull())
3845 return 0;
3846
3847 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3848 if (CompLHSType.isNull())
3849 return 0;
3850
3851 QualType CompResultType = Importer.Import(E->getComputationResultType());
3852 if (CompResultType.isNull())
3853 return 0;
3854
3855 Expr *LHS = Importer.Import(E->getLHS());
3856 if (!LHS)
3857 return 0;
3858
3859 Expr *RHS = Importer.Import(E->getRHS());
3860 if (!RHS)
3861 return 0;
3862
3863 return new (Importer.getToContext())
3864 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003865 T, E->getValueKind(),
3866 E->getObjectKind(),
3867 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003868 Importer.Import(E->getOperatorLoc()));
3869}
3870
John McCallcf142162010-08-07 06:22:56 +00003871bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3872 if (E->path_empty()) return false;
3873
3874 // TODO: import cast paths
3875 return true;
3876}
3877
Douglas Gregor98c10182010-02-12 22:17:39 +00003878Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3879 QualType T = Importer.Import(E->getType());
3880 if (T.isNull())
3881 return 0;
3882
3883 Expr *SubExpr = Importer.Import(E->getSubExpr());
3884 if (!SubExpr)
3885 return 0;
John McCallcf142162010-08-07 06:22:56 +00003886
3887 CXXCastPath BasePath;
3888 if (ImportCastPath(E, BasePath))
3889 return 0;
3890
3891 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003892 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003893}
3894
Douglas Gregor5481d322010-02-19 01:32:14 +00003895Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3896 QualType T = Importer.Import(E->getType());
3897 if (T.isNull())
3898 return 0;
3899
3900 Expr *SubExpr = Importer.Import(E->getSubExpr());
3901 if (!SubExpr)
3902 return 0;
3903
3904 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3905 if (!TInfo && E->getTypeInfoAsWritten())
3906 return 0;
3907
John McCallcf142162010-08-07 06:22:56 +00003908 CXXCastPath BasePath;
3909 if (ImportCastPath(E, BasePath))
3910 return 0;
3911
John McCall7decc9e2010-11-18 06:31:45 +00003912 return CStyleCastExpr::Create(Importer.getToContext(), T,
3913 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003914 SubExpr, &BasePath, TInfo,
3915 Importer.Import(E->getLParenLoc()),
3916 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003917}
3918
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003919ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003920 ASTContext &FromContext, FileManager &FromFileManager,
3921 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003922 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003923 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3924 Minimal(MinimalImport)
3925{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003926 ImportedDecls[FromContext.getTranslationUnitDecl()]
3927 = ToContext.getTranslationUnitDecl();
3928}
3929
3930ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003931
3932QualType ASTImporter::Import(QualType FromT) {
3933 if (FromT.isNull())
3934 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003935
3936 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003937
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003938 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003939 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3940 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003941 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003942 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003943
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003944 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003945 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003946 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003947 if (ToT.isNull())
3948 return ToT;
3949
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003950 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003951 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003952
John McCall424cec92011-01-19 06:33:43 +00003953 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003954}
3955
Douglas Gregor62d311f2010-02-09 19:21:46 +00003956TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003957 if (!FromTSI)
3958 return FromTSI;
3959
3960 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003961 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003962 QualType T = Import(FromTSI->getType());
3963 if (T.isNull())
3964 return 0;
3965
3966 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003967 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003968}
3969
3970Decl *ASTImporter::Import(Decl *FromD) {
3971 if (!FromD)
3972 return 0;
3973
3974 // Check whether we've already imported this declaration.
3975 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3976 if (Pos != ImportedDecls.end())
3977 return Pos->second;
3978
3979 // Import the type
3980 ASTNodeImporter Importer(*this);
3981 Decl *ToD = Importer.Visit(FromD);
3982 if (!ToD)
3983 return 0;
3984
3985 // Record the imported declaration.
3986 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003987
3988 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3989 // Keep track of anonymous tags that have an associated typedef.
3990 if (FromTag->getTypedefForAnonDecl())
3991 AnonTagsWithPendingTypedefs.push_back(FromTag);
3992 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3993 // When we've finished transforming a typedef, see whether it was the
3994 // typedef for an anonymous tag.
3995 for (llvm::SmallVector<TagDecl *, 4>::iterator
3996 FromTag = AnonTagsWithPendingTypedefs.begin(),
3997 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3998 FromTag != FromTagEnd; ++FromTag) {
3999 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
4000 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4001 // We found the typedef for an anonymous tag; link them.
4002 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
4003 AnonTagsWithPendingTypedefs.erase(FromTag);
4004 break;
4005 }
4006 }
4007 }
4008 }
4009
Douglas Gregor62d311f2010-02-09 19:21:46 +00004010 return ToD;
4011}
4012
4013DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4014 if (!FromDC)
4015 return FromDC;
4016
4017 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4018}
4019
4020Expr *ASTImporter::Import(Expr *FromE) {
4021 if (!FromE)
4022 return 0;
4023
4024 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4025}
4026
4027Stmt *ASTImporter::Import(Stmt *FromS) {
4028 if (!FromS)
4029 return 0;
4030
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004031 // Check whether we've already imported this declaration.
4032 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4033 if (Pos != ImportedStmts.end())
4034 return Pos->second;
4035
4036 // Import the type
4037 ASTNodeImporter Importer(*this);
4038 Stmt *ToS = Importer.Visit(FromS);
4039 if (!ToS)
4040 return 0;
4041
4042 // Record the imported declaration.
4043 ImportedStmts[FromS] = ToS;
4044 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004045}
4046
4047NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4048 if (!FromNNS)
4049 return 0;
4050
4051 // FIXME: Implement!
4052 return 0;
4053}
4054
Douglas Gregor14454802011-02-25 02:25:35 +00004055NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4056 // FIXME: Implement!
4057 return NestedNameSpecifierLoc();
4058}
4059
Douglas Gregore2e50d332010-12-01 01:36:18 +00004060TemplateName ASTImporter::Import(TemplateName From) {
4061 switch (From.getKind()) {
4062 case TemplateName::Template:
4063 if (TemplateDecl *ToTemplate
4064 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4065 return TemplateName(ToTemplate);
4066
4067 return TemplateName();
4068
4069 case TemplateName::OverloadedTemplate: {
4070 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4071 UnresolvedSet<2> ToTemplates;
4072 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4073 E = FromStorage->end();
4074 I != E; ++I) {
4075 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4076 ToTemplates.addDecl(To);
4077 else
4078 return TemplateName();
4079 }
4080 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4081 ToTemplates.end());
4082 }
4083
4084 case TemplateName::QualifiedTemplate: {
4085 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4086 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4087 if (!Qualifier)
4088 return TemplateName();
4089
4090 if (TemplateDecl *ToTemplate
4091 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4092 return ToContext.getQualifiedTemplateName(Qualifier,
4093 QTN->hasTemplateKeyword(),
4094 ToTemplate);
4095
4096 return TemplateName();
4097 }
4098
4099 case TemplateName::DependentTemplate: {
4100 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4101 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4102 if (!Qualifier)
4103 return TemplateName();
4104
4105 if (DTN->isIdentifier()) {
4106 return ToContext.getDependentTemplateName(Qualifier,
4107 Import(DTN->getIdentifier()));
4108 }
4109
4110 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4111 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004112
4113 case TemplateName::SubstTemplateTemplateParmPack: {
4114 SubstTemplateTemplateParmPackStorage *SubstPack
4115 = From.getAsSubstTemplateTemplateParmPack();
4116 TemplateTemplateParmDecl *Param
4117 = cast_or_null<TemplateTemplateParmDecl>(
4118 Import(SubstPack->getParameterPack()));
4119 if (!Param)
4120 return TemplateName();
4121
4122 ASTNodeImporter Importer(*this);
4123 TemplateArgument ArgPack
4124 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4125 if (ArgPack.isNull())
4126 return TemplateName();
4127
4128 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4129 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004130 }
4131
4132 llvm_unreachable("Invalid template name kind");
4133 return TemplateName();
4134}
4135
Douglas Gregor62d311f2010-02-09 19:21:46 +00004136SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4137 if (FromLoc.isInvalid())
4138 return SourceLocation();
4139
Douglas Gregor811663e2010-02-10 00:15:17 +00004140 SourceManager &FromSM = FromContext.getSourceManager();
4141
4142 // For now, map everything down to its spelling location, so that we
4143 // don't have to import macro instantiations.
4144 // FIXME: Import macro instantiations!
4145 FromLoc = FromSM.getSpellingLoc(FromLoc);
4146 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4147 SourceManager &ToSM = ToContext.getSourceManager();
4148 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4149 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004150}
4151
4152SourceRange ASTImporter::Import(SourceRange FromRange) {
4153 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4154}
4155
Douglas Gregor811663e2010-02-10 00:15:17 +00004156FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004157 llvm::DenseMap<FileID, FileID>::iterator Pos
4158 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004159 if (Pos != ImportedFileIDs.end())
4160 return Pos->second;
4161
4162 SourceManager &FromSM = FromContext.getSourceManager();
4163 SourceManager &ToSM = ToContext.getSourceManager();
4164 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4165 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4166
4167 // Include location of this file.
4168 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4169
4170 // Map the FileID for to the "to" source manager.
4171 FileID ToID;
4172 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004173 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004174 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4175 // disk again
4176 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4177 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004178 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004179 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4180 FromSLoc.getFile().getFileCharacteristic());
4181 } else {
4182 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004183 const llvm::MemoryBuffer *
4184 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004185 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004186 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004187 FromBuf->getBufferIdentifier());
4188 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4189 }
4190
4191
Sebastian Redl99219f12010-09-30 01:03:06 +00004192 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004193 return ToID;
4194}
4195
Douglas Gregor0a791672011-01-18 03:11:38 +00004196void ASTImporter::ImportDefinition(Decl *From) {
4197 Decl *To = Import(From);
4198 if (!To)
4199 return;
4200
4201 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4202 ASTNodeImporter Importer(*this);
4203 Importer.ImportDeclContext(FromDC, true);
4204 }
4205}
4206
Douglas Gregor96e578d2010-02-05 17:54:41 +00004207DeclarationName ASTImporter::Import(DeclarationName FromName) {
4208 if (!FromName)
4209 return DeclarationName();
4210
4211 switch (FromName.getNameKind()) {
4212 case DeclarationName::Identifier:
4213 return Import(FromName.getAsIdentifierInfo());
4214
4215 case DeclarationName::ObjCZeroArgSelector:
4216 case DeclarationName::ObjCOneArgSelector:
4217 case DeclarationName::ObjCMultiArgSelector:
4218 return Import(FromName.getObjCSelector());
4219
4220 case DeclarationName::CXXConstructorName: {
4221 QualType T = Import(FromName.getCXXNameType());
4222 if (T.isNull())
4223 return DeclarationName();
4224
4225 return ToContext.DeclarationNames.getCXXConstructorName(
4226 ToContext.getCanonicalType(T));
4227 }
4228
4229 case DeclarationName::CXXDestructorName: {
4230 QualType T = Import(FromName.getCXXNameType());
4231 if (T.isNull())
4232 return DeclarationName();
4233
4234 return ToContext.DeclarationNames.getCXXDestructorName(
4235 ToContext.getCanonicalType(T));
4236 }
4237
4238 case DeclarationName::CXXConversionFunctionName: {
4239 QualType T = Import(FromName.getCXXNameType());
4240 if (T.isNull())
4241 return DeclarationName();
4242
4243 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4244 ToContext.getCanonicalType(T));
4245 }
4246
4247 case DeclarationName::CXXOperatorName:
4248 return ToContext.DeclarationNames.getCXXOperatorName(
4249 FromName.getCXXOverloadedOperator());
4250
4251 case DeclarationName::CXXLiteralOperatorName:
4252 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4253 Import(FromName.getCXXLiteralIdentifier()));
4254
4255 case DeclarationName::CXXUsingDirective:
4256 // FIXME: STATICS!
4257 return DeclarationName::getUsingDirectiveName();
4258 }
4259
4260 // Silence bogus GCC warning
4261 return DeclarationName();
4262}
4263
Douglas Gregore2e50d332010-12-01 01:36:18 +00004264IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004265 if (!FromId)
4266 return 0;
4267
4268 return &ToContext.Idents.get(FromId->getName());
4269}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004270
Douglas Gregor43f54792010-02-17 02:12:47 +00004271Selector ASTImporter::Import(Selector FromSel) {
4272 if (FromSel.isNull())
4273 return Selector();
4274
4275 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4276 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4277 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4278 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4279 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4280}
4281
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004282DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4283 DeclContext *DC,
4284 unsigned IDNS,
4285 NamedDecl **Decls,
4286 unsigned NumDecls) {
4287 return Name;
4288}
4289
4290DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004291 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004292}
4293
4294DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004295 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004296}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004297
4298Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4299 ImportedDecls[From] = To;
4300 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004301}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004302
4303bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004304 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004305 = ImportedTypes.find(From.getTypePtr());
4306 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4307 return true;
4308
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004309 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004310 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004311}