blob: f3bb5d1415ec8e7640974218b1de2d03c3e0e523 [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) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001970 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1971 Importer.Import(D->getLocStart()),
1972 Loc, Name.getAsIdentifierInfo());
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001973 ToNamespace->setLexicalDeclContext(LexicalDC);
1974 LexicalDC->addDecl(ToNamespace);
1975
1976 // If this is an anonymous namespace, register it as the anonymous
1977 // namespace within its context.
1978 if (!Name) {
1979 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1980 TU->setAnonymousNamespace(ToNamespace);
1981 else
1982 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1983 }
1984 }
1985 Importer.Imported(D, ToNamespace);
1986
1987 ImportDeclContext(D);
1988
1989 return ToNamespace;
1990}
1991
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001992Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1993 // Import the major distinguishing characteristics of this typedef.
1994 DeclContext *DC, *LexicalDC;
1995 DeclarationName Name;
1996 SourceLocation Loc;
1997 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1998 return 0;
1999
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002000 // If this typedef is not in block scope, determine whether we've
2001 // seen a typedef with the same name (that we can merge with) or any
2002 // other entity by that name (which name lookup could conflict with).
2003 if (!DC->isFunctionOrMethod()) {
2004 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2005 unsigned IDNS = Decl::IDNS_Ordinary;
2006 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2007 Lookup.first != Lookup.second;
2008 ++Lookup.first) {
2009 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2010 continue;
2011 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002012 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2013 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002014 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002015 }
2016
2017 ConflictingDecls.push_back(*Lookup.first);
2018 }
2019
2020 if (!ConflictingDecls.empty()) {
2021 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2022 ConflictingDecls.data(),
2023 ConflictingDecls.size());
2024 if (!Name)
2025 return 0;
2026 }
2027 }
2028
Douglas Gregorb4964f72010-02-15 23:54:17 +00002029 // Import the underlying type of this typedef;
2030 QualType T = Importer.Import(D->getUnderlyingType());
2031 if (T.isNull())
2032 return 0;
2033
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002034 // Create the new typedef node.
2035 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002036 SourceLocation StartL = Importer.Import(D->getLocStart());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002037 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002038 StartL, Loc,
2039 Name.getAsIdentifierInfo(),
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002040 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002041 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002042 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002043 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002044 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002045
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002046 return ToTypedef;
2047}
2048
Douglas Gregor98c10182010-02-12 22:17:39 +00002049Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2050 // Import the major distinguishing characteristics of this enum.
2051 DeclContext *DC, *LexicalDC;
2052 DeclarationName Name;
2053 SourceLocation Loc;
2054 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2055 return 0;
2056
2057 // Figure out what enum name we're looking for.
2058 unsigned IDNS = Decl::IDNS_Tag;
2059 DeclarationName SearchName = Name;
2060 if (!SearchName && D->getTypedefForAnonDecl()) {
2061 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2062 IDNS = Decl::IDNS_Ordinary;
2063 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2064 IDNS |= Decl::IDNS_Ordinary;
2065
2066 // We may already have an enum of the same name; try to find and match it.
2067 if (!DC->isFunctionOrMethod() && SearchName) {
2068 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2069 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2070 Lookup.first != Lookup.second;
2071 ++Lookup.first) {
2072 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2073 continue;
2074
2075 Decl *Found = *Lookup.first;
2076 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2077 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2078 Found = Tag->getDecl();
2079 }
2080
2081 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002082 if (IsStructuralMatch(D, FoundEnum))
2083 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002084 }
2085
2086 ConflictingDecls.push_back(*Lookup.first);
2087 }
2088
2089 if (!ConflictingDecls.empty()) {
2090 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2091 ConflictingDecls.data(),
2092 ConflictingDecls.size());
2093 }
2094 }
2095
2096 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002097 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002098 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002099 Importer.Import(D->getLocStart()), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002100 D->isScoped(), D->isScopedUsingClassTag(),
2101 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002102 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002103 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002104 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002105 D2->setLexicalDeclContext(LexicalDC);
2106 Importer.Imported(D, D2);
2107 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002108
2109 // Import the integer type.
2110 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2111 if (ToIntegerType.isNull())
2112 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002113 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002114
2115 // Import the definition
2116 if (D->isDefinition()) {
2117 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2118 if (T.isNull())
2119 return 0;
2120
2121 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2122 if (ToPromotionType.isNull())
2123 return 0;
2124
Douglas Gregor3996e242010-02-15 22:01:00 +00002125 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002126 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002127
2128 // FIXME: we might need to merge the number of positive or negative bits
2129 // if the enumerator lists don't match.
2130 D2->completeDefinition(T, ToPromotionType,
2131 D->getNumPositiveBits(),
2132 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002133 }
2134
Douglas Gregor3996e242010-02-15 22:01:00 +00002135 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002136}
2137
Douglas Gregor5c73e912010-02-11 00:48:18 +00002138Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2139 // If this record has a definition in the translation unit we're coming from,
2140 // but this particular declaration is not that definition, import the
2141 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002142 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002143 if (Definition && Definition != D) {
2144 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002145 if (!ImportedDef)
2146 return 0;
2147
2148 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002149 }
2150
2151 // Import the major distinguishing characteristics of this record.
2152 DeclContext *DC, *LexicalDC;
2153 DeclarationName Name;
2154 SourceLocation Loc;
2155 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2156 return 0;
2157
2158 // Figure out what structure name we're looking for.
2159 unsigned IDNS = Decl::IDNS_Tag;
2160 DeclarationName SearchName = Name;
2161 if (!SearchName && D->getTypedefForAnonDecl()) {
2162 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2163 IDNS = Decl::IDNS_Ordinary;
2164 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2165 IDNS |= Decl::IDNS_Ordinary;
2166
2167 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002168 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002169 if (!DC->isFunctionOrMethod() && SearchName) {
2170 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2171 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2172 Lookup.first != Lookup.second;
2173 ++Lookup.first) {
2174 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2175 continue;
2176
2177 Decl *Found = *Lookup.first;
2178 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2179 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2180 Found = Tag->getDecl();
2181 }
2182
2183 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002184 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2185 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2186 // The record types structurally match, or the "from" translation
2187 // unit only had a forward declaration anyway; call it the same
2188 // function.
2189 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002190 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002191 }
2192 } else {
2193 // We have a forward declaration of this type, so adopt that forward
2194 // declaration rather than building a new one.
2195 AdoptDecl = FoundRecord;
2196 continue;
2197 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002198 }
2199
2200 ConflictingDecls.push_back(*Lookup.first);
2201 }
2202
2203 if (!ConflictingDecls.empty()) {
2204 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2205 ConflictingDecls.data(),
2206 ConflictingDecls.size());
2207 }
2208 }
2209
2210 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002211 RecordDecl *D2 = AdoptDecl;
2212 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002213 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002214 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002215 D->getTagKind(),
2216 DC, Loc,
2217 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002218 Importer.Import(D->getLocStart()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002219 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002220 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002221 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002222 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002223 DC, Loc,
2224 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00002225 Importer.Import(D->getLocStart()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002226 }
Douglas Gregor14454802011-02-25 02:25:35 +00002227
2228 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002229 D2->setLexicalDeclContext(LexicalDC);
2230 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002231 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002232
Douglas Gregor3996e242010-02-15 22:01:00 +00002233 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002234
Douglas Gregore2e50d332010-12-01 01:36:18 +00002235 if (D->isDefinition() && ImportDefinition(D, D2))
2236 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002237
Douglas Gregor3996e242010-02-15 22:01:00 +00002238 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002239}
2240
Douglas Gregor98c10182010-02-12 22:17:39 +00002241Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2242 // Import the major distinguishing characteristics of this enumerator.
2243 DeclContext *DC, *LexicalDC;
2244 DeclarationName Name;
2245 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002246 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002247 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002248
2249 QualType T = Importer.Import(D->getType());
2250 if (T.isNull())
2251 return 0;
2252
Douglas Gregor98c10182010-02-12 22:17:39 +00002253 // Determine whether there are any other declarations with the same name and
2254 // in the same context.
2255 if (!LexicalDC->isFunctionOrMethod()) {
2256 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2257 unsigned IDNS = Decl::IDNS_Ordinary;
2258 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2259 Lookup.first != Lookup.second;
2260 ++Lookup.first) {
2261 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2262 continue;
2263
2264 ConflictingDecls.push_back(*Lookup.first);
2265 }
2266
2267 if (!ConflictingDecls.empty()) {
2268 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2269 ConflictingDecls.data(),
2270 ConflictingDecls.size());
2271 if (!Name)
2272 return 0;
2273 }
2274 }
2275
2276 Expr *Init = Importer.Import(D->getInitExpr());
2277 if (D->getInitExpr() && !Init)
2278 return 0;
2279
2280 EnumConstantDecl *ToEnumerator
2281 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2282 Name.getAsIdentifierInfo(), T,
2283 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002284 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002285 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002286 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002287 LexicalDC->addDecl(ToEnumerator);
2288 return ToEnumerator;
2289}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002290
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002291Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2292 // Import the major distinguishing characteristics of this function.
2293 DeclContext *DC, *LexicalDC;
2294 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002295 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002296 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002297 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002298
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002299 // Try to find a function in our own ("to") context with the same name, same
2300 // type, and in the same context as the function we're importing.
2301 if (!LexicalDC->isFunctionOrMethod()) {
2302 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2303 unsigned IDNS = Decl::IDNS_Ordinary;
2304 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2305 Lookup.first != Lookup.second;
2306 ++Lookup.first) {
2307 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2308 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002309
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002310 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2311 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2312 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002313 if (Importer.IsStructurallyEquivalent(D->getType(),
2314 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002315 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002316 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002317 }
2318
2319 // FIXME: Check for overloading more carefully, e.g., by boosting
2320 // Sema::IsOverload out to the AST library.
2321
2322 // Function overloading is okay in C++.
2323 if (Importer.getToContext().getLangOptions().CPlusPlus)
2324 continue;
2325
2326 // Complain about inconsistent function types.
2327 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002328 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002329 Importer.ToDiag(FoundFunction->getLocation(),
2330 diag::note_odr_value_here)
2331 << FoundFunction->getType();
2332 }
2333 }
2334
2335 ConflictingDecls.push_back(*Lookup.first);
2336 }
2337
2338 if (!ConflictingDecls.empty()) {
2339 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2340 ConflictingDecls.data(),
2341 ConflictingDecls.size());
2342 if (!Name)
2343 return 0;
2344 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002345 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002346
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002347 DeclarationNameInfo NameInfo(Name, Loc);
2348 // Import additional name location/type info.
2349 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2350
Douglas Gregorb4964f72010-02-15 23:54:17 +00002351 // Import the type.
2352 QualType T = Importer.Import(D->getType());
2353 if (T.isNull())
2354 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002355
2356 // Import the function parameters.
2357 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2358 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2359 P != PEnd; ++P) {
2360 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2361 if (!ToP)
2362 return 0;
2363
2364 Parameters.push_back(ToP);
2365 }
2366
2367 // Create the imported function.
2368 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002369 FunctionDecl *ToFunction = 0;
2370 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2371 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2372 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002373 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002374 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002375 FromConstructor->isExplicit(),
2376 D->isInlineSpecified(),
2377 D->isImplicit());
2378 } else if (isa<CXXDestructorDecl>(D)) {
2379 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2380 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002381 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002382 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002383 D->isInlineSpecified(),
2384 D->isImplicit());
2385 } else if (CXXConversionDecl *FromConversion
2386 = dyn_cast<CXXConversionDecl>(D)) {
2387 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2388 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002389 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002390 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002391 D->isInlineSpecified(),
2392 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002393 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2394 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2395 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002396 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002397 NameInfo, T, TInfo,
2398 Method->isStatic(),
2399 Method->getStorageClassAsWritten(),
2400 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002401 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002402 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002403 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002404 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002405 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002406 D->isInlineSpecified(),
2407 D->hasWrittenPrototype());
2408 }
John McCall3e11ebe2010-03-15 10:12:16 +00002409
2410 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002411 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002412 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002413 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002414 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2415 ToFunction->setTrivial(D->isTrivial());
2416 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002417 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002418
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002419 // Set the parameters.
2420 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002421 Parameters[I]->setOwningFunction(ToFunction);
2422 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002423 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002424 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002425
2426 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002427
2428 // Add this function to the lexical context.
2429 LexicalDC->addDecl(ToFunction);
2430
Douglas Gregor43f54792010-02-17 02:12:47 +00002431 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002432}
2433
Douglas Gregor00eace12010-02-21 18:29:16 +00002434Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2435 return VisitFunctionDecl(D);
2436}
2437
2438Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2439 return VisitCXXMethodDecl(D);
2440}
2441
2442Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2443 return VisitCXXMethodDecl(D);
2444}
2445
2446Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2447 return VisitCXXMethodDecl(D);
2448}
2449
Douglas Gregor5c73e912010-02-11 00:48:18 +00002450Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2451 // Import the major distinguishing characteristics of a variable.
2452 DeclContext *DC, *LexicalDC;
2453 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002454 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002455 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2456 return 0;
2457
2458 // Import the type.
2459 QualType T = Importer.Import(D->getType());
2460 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002461 return 0;
2462
2463 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2464 Expr *BitWidth = Importer.Import(D->getBitWidth());
2465 if (!BitWidth && D->getBitWidth())
2466 return 0;
2467
Abramo Bagnaradff19302011-03-08 08:55:46 +00002468 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2469 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002470 Loc, Name.getAsIdentifierInfo(),
2471 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002472 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002473 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002474 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002475 LexicalDC->addDecl(ToField);
2476 return ToField;
2477}
2478
Francois Pichet783dd6e2010-11-21 06:08:52 +00002479Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2480 // Import the major distinguishing characteristics of a variable.
2481 DeclContext *DC, *LexicalDC;
2482 DeclarationName Name;
2483 SourceLocation Loc;
2484 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2485 return 0;
2486
2487 // Import the type.
2488 QualType T = Importer.Import(D->getType());
2489 if (T.isNull())
2490 return 0;
2491
2492 NamedDecl **NamedChain =
2493 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2494
2495 unsigned i = 0;
2496 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2497 PE = D->chain_end(); PI != PE; ++PI) {
2498 Decl* D = Importer.Import(*PI);
2499 if (!D)
2500 return 0;
2501 NamedChain[i++] = cast<NamedDecl>(D);
2502 }
2503
2504 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2505 Importer.getToContext(), DC,
2506 Loc, Name.getAsIdentifierInfo(), T,
2507 NamedChain, D->getChainingSize());
2508 ToIndirectField->setAccess(D->getAccess());
2509 ToIndirectField->setLexicalDeclContext(LexicalDC);
2510 Importer.Imported(D, ToIndirectField);
2511 LexicalDC->addDecl(ToIndirectField);
2512 return ToIndirectField;
2513}
2514
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002515Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2516 // Import the major distinguishing characteristics of an ivar.
2517 DeclContext *DC, *LexicalDC;
2518 DeclarationName Name;
2519 SourceLocation Loc;
2520 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2521 return 0;
2522
2523 // Determine whether we've already imported this ivar
2524 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2525 Lookup.first != Lookup.second;
2526 ++Lookup.first) {
2527 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2528 if (Importer.IsStructurallyEquivalent(D->getType(),
2529 FoundIvar->getType())) {
2530 Importer.Imported(D, FoundIvar);
2531 return FoundIvar;
2532 }
2533
2534 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2535 << Name << D->getType() << FoundIvar->getType();
2536 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2537 << FoundIvar->getType();
2538 return 0;
2539 }
2540 }
2541
2542 // Import the type.
2543 QualType T = Importer.Import(D->getType());
2544 if (T.isNull())
2545 return 0;
2546
2547 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2548 Expr *BitWidth = Importer.Import(D->getBitWidth());
2549 if (!BitWidth && D->getBitWidth())
2550 return 0;
2551
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002552 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2553 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002554 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002555 Loc, Name.getAsIdentifierInfo(),
2556 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002557 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002558 ToIvar->setLexicalDeclContext(LexicalDC);
2559 Importer.Imported(D, ToIvar);
2560 LexicalDC->addDecl(ToIvar);
2561 return ToIvar;
2562
2563}
2564
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002565Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2566 // Import the major distinguishing characteristics of a variable.
2567 DeclContext *DC, *LexicalDC;
2568 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002569 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002570 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002571 return 0;
2572
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002573 // Try to find a variable in our own ("to") context with the same name and
2574 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002575 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002576 VarDecl *MergeWithVar = 0;
2577 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2578 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002579 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002580 Lookup.first != Lookup.second;
2581 ++Lookup.first) {
2582 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2583 continue;
2584
2585 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2586 // We have found a variable that we may need to merge with. Check it.
2587 if (isExternalLinkage(FoundVar->getLinkage()) &&
2588 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002589 if (Importer.IsStructurallyEquivalent(D->getType(),
2590 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002591 MergeWithVar = FoundVar;
2592 break;
2593 }
2594
Douglas Gregor56521c52010-02-12 17:23:39 +00002595 const ArrayType *FoundArray
2596 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2597 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002598 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002599 if (FoundArray && TArray) {
2600 if (isa<IncompleteArrayType>(FoundArray) &&
2601 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002602 // Import the type.
2603 QualType T = Importer.Import(D->getType());
2604 if (T.isNull())
2605 return 0;
2606
Douglas Gregor56521c52010-02-12 17:23:39 +00002607 FoundVar->setType(T);
2608 MergeWithVar = FoundVar;
2609 break;
2610 } else if (isa<IncompleteArrayType>(TArray) &&
2611 isa<ConstantArrayType>(FoundArray)) {
2612 MergeWithVar = FoundVar;
2613 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002614 }
2615 }
2616
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002617 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002618 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002619 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2620 << FoundVar->getType();
2621 }
2622 }
2623
2624 ConflictingDecls.push_back(*Lookup.first);
2625 }
2626
2627 if (MergeWithVar) {
2628 // An equivalent variable with external linkage has been found. Link
2629 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002630 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002631
2632 if (VarDecl *DDef = D->getDefinition()) {
2633 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2634 Importer.ToDiag(ExistingDef->getLocation(),
2635 diag::err_odr_variable_multiple_def)
2636 << Name;
2637 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2638 } else {
2639 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002640 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002641 }
2642 }
2643
2644 return MergeWithVar;
2645 }
2646
2647 if (!ConflictingDecls.empty()) {
2648 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2649 ConflictingDecls.data(),
2650 ConflictingDecls.size());
2651 if (!Name)
2652 return 0;
2653 }
2654 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002655
Douglas Gregorb4964f72010-02-15 23:54:17 +00002656 // Import the type.
2657 QualType T = Importer.Import(D->getType());
2658 if (T.isNull())
2659 return 0;
2660
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002661 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002662 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002663 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2664 Importer.Import(D->getInnerLocStart()),
2665 Loc, Name.getAsIdentifierInfo(),
2666 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002667 D->getStorageClass(),
2668 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002669 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002670 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002671 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002672 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002673 LexicalDC->addDecl(ToVar);
2674
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002675 // Merge the initializer.
2676 // FIXME: Can we really import any initializer? Alternatively, we could force
2677 // ourselves to import every declaration of a variable and then only use
2678 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002679 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002680
2681 // FIXME: Other bits to merge?
2682
2683 return ToVar;
2684}
2685
Douglas Gregor8b228d72010-02-17 21:22:52 +00002686Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2687 // Parameters are created in the translation unit's context, then moved
2688 // into the function declaration's context afterward.
2689 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2690
2691 // Import the name of this declaration.
2692 DeclarationName Name = Importer.Import(D->getDeclName());
2693 if (D->getDeclName() && !Name)
2694 return 0;
2695
2696 // Import the location of this declaration.
2697 SourceLocation Loc = Importer.Import(D->getLocation());
2698
2699 // Import the parameter's type.
2700 QualType T = Importer.Import(D->getType());
2701 if (T.isNull())
2702 return 0;
2703
2704 // Create the imported parameter.
2705 ImplicitParamDecl *ToParm
2706 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2707 Loc, Name.getAsIdentifierInfo(),
2708 T);
2709 return Importer.Imported(D, ToParm);
2710}
2711
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002712Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2713 // Parameters are created in the translation unit's context, then moved
2714 // into the function declaration's context afterward.
2715 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2716
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002717 // Import the name of this declaration.
2718 DeclarationName Name = Importer.Import(D->getDeclName());
2719 if (D->getDeclName() && !Name)
2720 return 0;
2721
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002722 // Import the location of this declaration.
2723 SourceLocation Loc = Importer.Import(D->getLocation());
2724
2725 // Import the parameter's type.
2726 QualType T = Importer.Import(D->getType());
2727 if (T.isNull())
2728 return 0;
2729
2730 // Create the imported parameter.
2731 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2732 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002733 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002734 Loc, Name.getAsIdentifierInfo(),
2735 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002736 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002737 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002738 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002739 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002740}
2741
Douglas Gregor43f54792010-02-17 02:12:47 +00002742Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2743 // Import the major distinguishing characteristics of a method.
2744 DeclContext *DC, *LexicalDC;
2745 DeclarationName Name;
2746 SourceLocation Loc;
2747 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2748 return 0;
2749
2750 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2751 Lookup.first != Lookup.second;
2752 ++Lookup.first) {
2753 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2754 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2755 continue;
2756
2757 // Check return types.
2758 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2759 FoundMethod->getResultType())) {
2760 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2761 << D->isInstanceMethod() << Name
2762 << D->getResultType() << FoundMethod->getResultType();
2763 Importer.ToDiag(FoundMethod->getLocation(),
2764 diag::note_odr_objc_method_here)
2765 << D->isInstanceMethod() << Name;
2766 return 0;
2767 }
2768
2769 // Check the number of parameters.
2770 if (D->param_size() != FoundMethod->param_size()) {
2771 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2772 << D->isInstanceMethod() << Name
2773 << D->param_size() << FoundMethod->param_size();
2774 Importer.ToDiag(FoundMethod->getLocation(),
2775 diag::note_odr_objc_method_here)
2776 << D->isInstanceMethod() << Name;
2777 return 0;
2778 }
2779
2780 // Check parameter types.
2781 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2782 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2783 P != PEnd; ++P, ++FoundP) {
2784 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2785 (*FoundP)->getType())) {
2786 Importer.FromDiag((*P)->getLocation(),
2787 diag::err_odr_objc_method_param_type_inconsistent)
2788 << D->isInstanceMethod() << Name
2789 << (*P)->getType() << (*FoundP)->getType();
2790 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2791 << (*FoundP)->getType();
2792 return 0;
2793 }
2794 }
2795
2796 // Check variadic/non-variadic.
2797 // Check the number of parameters.
2798 if (D->isVariadic() != FoundMethod->isVariadic()) {
2799 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2800 << D->isInstanceMethod() << Name;
2801 Importer.ToDiag(FoundMethod->getLocation(),
2802 diag::note_odr_objc_method_here)
2803 << D->isInstanceMethod() << Name;
2804 return 0;
2805 }
2806
2807 // FIXME: Any other bits we need to merge?
2808 return Importer.Imported(D, FoundMethod);
2809 }
2810 }
2811
2812 // Import the result type.
2813 QualType ResultTy = Importer.Import(D->getResultType());
2814 if (ResultTy.isNull())
2815 return 0;
2816
Douglas Gregor12852d92010-03-08 14:59:44 +00002817 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2818
Douglas Gregor43f54792010-02-17 02:12:47 +00002819 ObjCMethodDecl *ToMethod
2820 = ObjCMethodDecl::Create(Importer.getToContext(),
2821 Loc,
2822 Importer.Import(D->getLocEnd()),
2823 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002824 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002825 D->isInstanceMethod(),
2826 D->isVariadic(),
2827 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002828 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002829 D->getImplementationControl());
2830
2831 // FIXME: When we decide to merge method definitions, we'll need to
2832 // deal with implicit parameters.
2833
2834 // Import the parameters
2835 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2836 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2837 FromPEnd = D->param_end();
2838 FromP != FromPEnd;
2839 ++FromP) {
2840 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2841 if (!ToP)
2842 return 0;
2843
2844 ToParams.push_back(ToP);
2845 }
2846
2847 // Set the parameters.
2848 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2849 ToParams[I]->setOwningFunction(ToMethod);
2850 ToMethod->addDecl(ToParams[I]);
2851 }
2852 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002853 ToParams.data(), ToParams.size(),
2854 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002855
2856 ToMethod->setLexicalDeclContext(LexicalDC);
2857 Importer.Imported(D, ToMethod);
2858 LexicalDC->addDecl(ToMethod);
2859 return ToMethod;
2860}
2861
Douglas Gregor84c51c32010-02-18 01:47:50 +00002862Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2863 // Import the major distinguishing characteristics of a category.
2864 DeclContext *DC, *LexicalDC;
2865 DeclarationName Name;
2866 SourceLocation Loc;
2867 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2868 return 0;
2869
2870 ObjCInterfaceDecl *ToInterface
2871 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2872 if (!ToInterface)
2873 return 0;
2874
2875 // Determine if we've already encountered this category.
2876 ObjCCategoryDecl *MergeWithCategory
2877 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2878 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2879 if (!ToCategory) {
2880 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2881 Importer.Import(D->getAtLoc()),
2882 Loc,
2883 Importer.Import(D->getCategoryNameLoc()),
2884 Name.getAsIdentifierInfo());
2885 ToCategory->setLexicalDeclContext(LexicalDC);
2886 LexicalDC->addDecl(ToCategory);
2887 Importer.Imported(D, ToCategory);
2888
2889 // Link this category into its class's category list.
2890 ToCategory->setClassInterface(ToInterface);
2891 ToCategory->insertNextClassCategory();
2892
2893 // Import protocols
2894 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2895 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2896 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2897 = D->protocol_loc_begin();
2898 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2899 FromProtoEnd = D->protocol_end();
2900 FromProto != FromProtoEnd;
2901 ++FromProto, ++FromProtoLoc) {
2902 ObjCProtocolDecl *ToProto
2903 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2904 if (!ToProto)
2905 return 0;
2906 Protocols.push_back(ToProto);
2907 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2908 }
2909
2910 // FIXME: If we're merging, make sure that the protocol list is the same.
2911 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2912 ProtocolLocs.data(), Importer.getToContext());
2913
2914 } else {
2915 Importer.Imported(D, ToCategory);
2916 }
2917
2918 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002919 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002920
2921 // If we have an implementation, import it as well.
2922 if (D->getImplementation()) {
2923 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002924 = cast_or_null<ObjCCategoryImplDecl>(
2925 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002926 if (!Impl)
2927 return 0;
2928
2929 ToCategory->setImplementation(Impl);
2930 }
2931
2932 return ToCategory;
2933}
2934
Douglas Gregor98d156a2010-02-17 16:12:00 +00002935Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002936 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002937 DeclContext *DC, *LexicalDC;
2938 DeclarationName Name;
2939 SourceLocation Loc;
2940 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2941 return 0;
2942
2943 ObjCProtocolDecl *MergeWithProtocol = 0;
2944 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2945 Lookup.first != Lookup.second;
2946 ++Lookup.first) {
2947 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2948 continue;
2949
2950 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2951 break;
2952 }
2953
2954 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2955 if (!ToProto || ToProto->isForwardDecl()) {
2956 if (!ToProto) {
2957 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2958 Name.getAsIdentifierInfo());
2959 ToProto->setForwardDecl(D->isForwardDecl());
2960 ToProto->setLexicalDeclContext(LexicalDC);
2961 LexicalDC->addDecl(ToProto);
2962 }
2963 Importer.Imported(D, ToProto);
2964
2965 // Import protocols
2966 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2967 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2968 ObjCProtocolDecl::protocol_loc_iterator
2969 FromProtoLoc = D->protocol_loc_begin();
2970 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2971 FromProtoEnd = D->protocol_end();
2972 FromProto != FromProtoEnd;
2973 ++FromProto, ++FromProtoLoc) {
2974 ObjCProtocolDecl *ToProto
2975 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2976 if (!ToProto)
2977 return 0;
2978 Protocols.push_back(ToProto);
2979 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2980 }
2981
2982 // FIXME: If we're merging, make sure that the protocol list is the same.
2983 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2984 ProtocolLocs.data(), Importer.getToContext());
2985 } else {
2986 Importer.Imported(D, ToProto);
2987 }
2988
Douglas Gregor84c51c32010-02-18 01:47:50 +00002989 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002990 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002991
2992 return ToProto;
2993}
2994
Douglas Gregor45635322010-02-16 01:20:57 +00002995Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2996 // Import the major distinguishing characteristics of an @interface.
2997 DeclContext *DC, *LexicalDC;
2998 DeclarationName Name;
2999 SourceLocation Loc;
3000 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3001 return 0;
3002
3003 ObjCInterfaceDecl *MergeWithIface = 0;
3004 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3005 Lookup.first != Lookup.second;
3006 ++Lookup.first) {
3007 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3008 continue;
3009
3010 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3011 break;
3012 }
3013
3014 ObjCInterfaceDecl *ToIface = MergeWithIface;
3015 if (!ToIface || ToIface->isForwardDecl()) {
3016 if (!ToIface) {
3017 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3018 DC, Loc,
3019 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003020 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003021 D->isForwardDecl(),
3022 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003023 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003024 ToIface->setLexicalDeclContext(LexicalDC);
3025 LexicalDC->addDecl(ToIface);
3026 }
3027 Importer.Imported(D, ToIface);
3028
Douglas Gregor45635322010-02-16 01:20:57 +00003029 if (D->getSuperClass()) {
3030 ObjCInterfaceDecl *Super
3031 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3032 if (!Super)
3033 return 0;
3034
3035 ToIface->setSuperClass(Super);
3036 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3037 }
3038
3039 // Import protocols
3040 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3041 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3042 ObjCInterfaceDecl::protocol_loc_iterator
3043 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003044
3045 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003046 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3047 FromProtoEnd = D->protocol_end();
3048 FromProto != FromProtoEnd;
3049 ++FromProto, ++FromProtoLoc) {
3050 ObjCProtocolDecl *ToProto
3051 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3052 if (!ToProto)
3053 return 0;
3054 Protocols.push_back(ToProto);
3055 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3056 }
3057
3058 // FIXME: If we're merging, make sure that the protocol list is the same.
3059 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3060 ProtocolLocs.data(), Importer.getToContext());
3061
Douglas Gregor45635322010-02-16 01:20:57 +00003062 // Import @end range
3063 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3064 } else {
3065 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003066
3067 // Check for consistency of superclasses.
3068 DeclarationName FromSuperName, ToSuperName;
3069 if (D->getSuperClass())
3070 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3071 if (ToIface->getSuperClass())
3072 ToSuperName = ToIface->getSuperClass()->getDeclName();
3073 if (FromSuperName != ToSuperName) {
3074 Importer.ToDiag(ToIface->getLocation(),
3075 diag::err_odr_objc_superclass_inconsistent)
3076 << ToIface->getDeclName();
3077 if (ToIface->getSuperClass())
3078 Importer.ToDiag(ToIface->getSuperClassLoc(),
3079 diag::note_odr_objc_superclass)
3080 << ToIface->getSuperClass()->getDeclName();
3081 else
3082 Importer.ToDiag(ToIface->getLocation(),
3083 diag::note_odr_objc_missing_superclass);
3084 if (D->getSuperClass())
3085 Importer.FromDiag(D->getSuperClassLoc(),
3086 diag::note_odr_objc_superclass)
3087 << D->getSuperClass()->getDeclName();
3088 else
3089 Importer.FromDiag(D->getLocation(),
3090 diag::note_odr_objc_missing_superclass);
3091 return 0;
3092 }
Douglas Gregor45635322010-02-16 01:20:57 +00003093 }
3094
Douglas Gregor84c51c32010-02-18 01:47:50 +00003095 // Import categories. When the categories themselves are imported, they'll
3096 // hook themselves into this interface.
3097 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3098 FromCat = FromCat->getNextClassCategory())
3099 Importer.Import(FromCat);
3100
Douglas Gregor45635322010-02-16 01:20:57 +00003101 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003102 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003103
3104 // If we have an @implementation, import it as well.
3105 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003106 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3107 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003108 if (!Impl)
3109 return 0;
3110
3111 ToIface->setImplementation(Impl);
3112 }
3113
Douglas Gregor98d156a2010-02-17 16:12:00 +00003114 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003115}
3116
Douglas Gregor4da9d682010-12-07 15:32:12 +00003117Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3118 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3119 Importer.Import(D->getCategoryDecl()));
3120 if (!Category)
3121 return 0;
3122
3123 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3124 if (!ToImpl) {
3125 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3126 if (!DC)
3127 return 0;
3128
3129 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3130 Importer.Import(D->getLocation()),
3131 Importer.Import(D->getIdentifier()),
3132 Category->getClassInterface());
3133
3134 DeclContext *LexicalDC = DC;
3135 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3136 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3137 if (!LexicalDC)
3138 return 0;
3139
3140 ToImpl->setLexicalDeclContext(LexicalDC);
3141 }
3142
3143 LexicalDC->addDecl(ToImpl);
3144 Category->setImplementation(ToImpl);
3145 }
3146
3147 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003148 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003149 return ToImpl;
3150}
3151
Douglas Gregorda8025c2010-12-07 01:26:03 +00003152Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3153 // Find the corresponding interface.
3154 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3155 Importer.Import(D->getClassInterface()));
3156 if (!Iface)
3157 return 0;
3158
3159 // Import the superclass, if any.
3160 ObjCInterfaceDecl *Super = 0;
3161 if (D->getSuperClass()) {
3162 Super = cast_or_null<ObjCInterfaceDecl>(
3163 Importer.Import(D->getSuperClass()));
3164 if (!Super)
3165 return 0;
3166 }
3167
3168 ObjCImplementationDecl *Impl = Iface->getImplementation();
3169 if (!Impl) {
3170 // We haven't imported an implementation yet. Create a new @implementation
3171 // now.
3172 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3173 Importer.ImportContext(D->getDeclContext()),
3174 Importer.Import(D->getLocation()),
3175 Iface, Super);
3176
3177 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3178 DeclContext *LexicalDC
3179 = Importer.ImportContext(D->getLexicalDeclContext());
3180 if (!LexicalDC)
3181 return 0;
3182 Impl->setLexicalDeclContext(LexicalDC);
3183 }
3184
3185 // Associate the implementation with the class it implements.
3186 Iface->setImplementation(Impl);
3187 Importer.Imported(D, Iface->getImplementation());
3188 } else {
3189 Importer.Imported(D, Iface->getImplementation());
3190
3191 // Verify that the existing @implementation has the same superclass.
3192 if ((Super && !Impl->getSuperClass()) ||
3193 (!Super && Impl->getSuperClass()) ||
3194 (Super && Impl->getSuperClass() &&
3195 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3196 Importer.ToDiag(Impl->getLocation(),
3197 diag::err_odr_objc_superclass_inconsistent)
3198 << Iface->getDeclName();
3199 // FIXME: It would be nice to have the location of the superclass
3200 // below.
3201 if (Impl->getSuperClass())
3202 Importer.ToDiag(Impl->getLocation(),
3203 diag::note_odr_objc_superclass)
3204 << Impl->getSuperClass()->getDeclName();
3205 else
3206 Importer.ToDiag(Impl->getLocation(),
3207 diag::note_odr_objc_missing_superclass);
3208 if (D->getSuperClass())
3209 Importer.FromDiag(D->getLocation(),
3210 diag::note_odr_objc_superclass)
3211 << D->getSuperClass()->getDeclName();
3212 else
3213 Importer.FromDiag(D->getLocation(),
3214 diag::note_odr_objc_missing_superclass);
3215 return 0;
3216 }
3217 }
3218
3219 // Import all of the members of this @implementation.
3220 ImportDeclContext(D);
3221
3222 return Impl;
3223}
3224
Douglas Gregora11c4582010-02-17 18:02:10 +00003225Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3226 // Import the major distinguishing characteristics of an @property.
3227 DeclContext *DC, *LexicalDC;
3228 DeclarationName Name;
3229 SourceLocation Loc;
3230 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3231 return 0;
3232
3233 // Check whether we have already imported this property.
3234 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3235 Lookup.first != Lookup.second;
3236 ++Lookup.first) {
3237 if (ObjCPropertyDecl *FoundProp
3238 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3239 // Check property types.
3240 if (!Importer.IsStructurallyEquivalent(D->getType(),
3241 FoundProp->getType())) {
3242 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3243 << Name << D->getType() << FoundProp->getType();
3244 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3245 << FoundProp->getType();
3246 return 0;
3247 }
3248
3249 // FIXME: Check property attributes, getters, setters, etc.?
3250
3251 // Consider these properties to be equivalent.
3252 Importer.Imported(D, FoundProp);
3253 return FoundProp;
3254 }
3255 }
3256
3257 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003258 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3259 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003260 return 0;
3261
3262 // Create the new property.
3263 ObjCPropertyDecl *ToProperty
3264 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3265 Name.getAsIdentifierInfo(),
3266 Importer.Import(D->getAtLoc()),
3267 T,
3268 D->getPropertyImplementation());
3269 Importer.Imported(D, ToProperty);
3270 ToProperty->setLexicalDeclContext(LexicalDC);
3271 LexicalDC->addDecl(ToProperty);
3272
3273 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003274 ToProperty->setPropertyAttributesAsWritten(
3275 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003276 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3277 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3278 ToProperty->setGetterMethodDecl(
3279 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3280 ToProperty->setSetterMethodDecl(
3281 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3282 ToProperty->setPropertyIvarDecl(
3283 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3284 return ToProperty;
3285}
3286
Douglas Gregor14a49e22010-12-07 18:32:03 +00003287Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3288 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3289 Importer.Import(D->getPropertyDecl()));
3290 if (!Property)
3291 return 0;
3292
3293 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3294 if (!DC)
3295 return 0;
3296
3297 // Import the lexical declaration context.
3298 DeclContext *LexicalDC = DC;
3299 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3300 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3301 if (!LexicalDC)
3302 return 0;
3303 }
3304
3305 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3306 if (!InImpl)
3307 return 0;
3308
3309 // Import the ivar (for an @synthesize).
3310 ObjCIvarDecl *Ivar = 0;
3311 if (D->getPropertyIvarDecl()) {
3312 Ivar = cast_or_null<ObjCIvarDecl>(
3313 Importer.Import(D->getPropertyIvarDecl()));
3314 if (!Ivar)
3315 return 0;
3316 }
3317
3318 ObjCPropertyImplDecl *ToImpl
3319 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3320 if (!ToImpl) {
3321 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3322 Importer.Import(D->getLocStart()),
3323 Importer.Import(D->getLocation()),
3324 Property,
3325 D->getPropertyImplementation(),
3326 Ivar,
3327 Importer.Import(D->getPropertyIvarDeclLoc()));
3328 ToImpl->setLexicalDeclContext(LexicalDC);
3329 Importer.Imported(D, ToImpl);
3330 LexicalDC->addDecl(ToImpl);
3331 } else {
3332 // Check that we have the same kind of property implementation (@synthesize
3333 // vs. @dynamic).
3334 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3335 Importer.ToDiag(ToImpl->getLocation(),
3336 diag::err_odr_objc_property_impl_kind_inconsistent)
3337 << Property->getDeclName()
3338 << (ToImpl->getPropertyImplementation()
3339 == ObjCPropertyImplDecl::Dynamic);
3340 Importer.FromDiag(D->getLocation(),
3341 diag::note_odr_objc_property_impl_kind)
3342 << D->getPropertyDecl()->getDeclName()
3343 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3344 return 0;
3345 }
3346
3347 // For @synthesize, check that we have the same
3348 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3349 Ivar != ToImpl->getPropertyIvarDecl()) {
3350 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3351 diag::err_odr_objc_synthesize_ivar_inconsistent)
3352 << Property->getDeclName()
3353 << ToImpl->getPropertyIvarDecl()->getDeclName()
3354 << Ivar->getDeclName();
3355 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3356 diag::note_odr_objc_synthesize_ivar_here)
3357 << D->getPropertyIvarDecl()->getDeclName();
3358 return 0;
3359 }
3360
3361 // Merge the existing implementation with the new implementation.
3362 Importer.Imported(D, ToImpl);
3363 }
3364
3365 return ToImpl;
3366}
3367
Douglas Gregor8661a722010-02-18 02:12:22 +00003368Decl *
3369ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3370 // Import the context of this declaration.
3371 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3372 if (!DC)
3373 return 0;
3374
3375 DeclContext *LexicalDC = DC;
3376 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3377 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3378 if (!LexicalDC)
3379 return 0;
3380 }
3381
3382 // Import the location of this declaration.
3383 SourceLocation Loc = Importer.Import(D->getLocation());
3384
3385 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3386 llvm::SmallVector<SourceLocation, 4> Locations;
3387 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3388 = D->protocol_loc_begin();
3389 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3390 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3391 FromProto != FromProtoEnd;
3392 ++FromProto, ++FromProtoLoc) {
3393 ObjCProtocolDecl *ToProto
3394 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3395 if (!ToProto)
3396 continue;
3397
3398 Protocols.push_back(ToProto);
3399 Locations.push_back(Importer.Import(*FromProtoLoc));
3400 }
3401
3402 ObjCForwardProtocolDecl *ToForward
3403 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3404 Protocols.data(), Protocols.size(),
3405 Locations.data());
3406 ToForward->setLexicalDeclContext(LexicalDC);
3407 LexicalDC->addDecl(ToForward);
3408 Importer.Imported(D, ToForward);
3409 return ToForward;
3410}
3411
Douglas Gregor06537af2010-02-18 02:04:09 +00003412Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3413 // Import the context of this declaration.
3414 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3415 if (!DC)
3416 return 0;
3417
3418 DeclContext *LexicalDC = DC;
3419 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3420 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3421 if (!LexicalDC)
3422 return 0;
3423 }
3424
3425 // Import the location of this declaration.
3426 SourceLocation Loc = Importer.Import(D->getLocation());
3427
3428 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3429 llvm::SmallVector<SourceLocation, 4> Locations;
3430 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3431 From != FromEnd; ++From) {
3432 ObjCInterfaceDecl *ToIface
3433 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3434 if (!ToIface)
3435 continue;
3436
3437 Interfaces.push_back(ToIface);
3438 Locations.push_back(Importer.Import(From->getLocation()));
3439 }
3440
3441 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3442 Loc,
3443 Interfaces.data(),
3444 Locations.data(),
3445 Interfaces.size());
3446 ToClass->setLexicalDeclContext(LexicalDC);
3447 LexicalDC->addDecl(ToClass);
3448 Importer.Imported(D, ToClass);
3449 return ToClass;
3450}
3451
Douglas Gregora082a492010-11-30 19:14:50 +00003452Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3453 // For template arguments, we adopt the translation unit as our declaration
3454 // context. This context will be fixed when the actual template declaration
3455 // is created.
3456
3457 // FIXME: Import default argument.
3458 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3459 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003460 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003461 Importer.Import(D->getLocation()),
3462 D->getDepth(),
3463 D->getIndex(),
3464 Importer.Import(D->getIdentifier()),
3465 D->wasDeclaredWithTypename(),
3466 D->isParameterPack());
3467}
3468
3469Decl *
3470ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3471 // Import the name of this declaration.
3472 DeclarationName Name = Importer.Import(D->getDeclName());
3473 if (D->getDeclName() && !Name)
3474 return 0;
3475
3476 // Import the location of this declaration.
3477 SourceLocation Loc = Importer.Import(D->getLocation());
3478
3479 // Import the type of this declaration.
3480 QualType T = Importer.Import(D->getType());
3481 if (T.isNull())
3482 return 0;
3483
3484 // Import type-source information.
3485 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3486 if (D->getTypeSourceInfo() && !TInfo)
3487 return 0;
3488
3489 // FIXME: Import default argument.
3490
3491 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3492 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003493 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003494 Loc, D->getDepth(), D->getPosition(),
3495 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003496 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003497}
3498
3499Decl *
3500ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3501 // Import the name of this declaration.
3502 DeclarationName Name = Importer.Import(D->getDeclName());
3503 if (D->getDeclName() && !Name)
3504 return 0;
3505
3506 // Import the location of this declaration.
3507 SourceLocation Loc = Importer.Import(D->getLocation());
3508
3509 // Import template parameters.
3510 TemplateParameterList *TemplateParams
3511 = ImportTemplateParameterList(D->getTemplateParameters());
3512 if (!TemplateParams)
3513 return 0;
3514
3515 // FIXME: Import default argument.
3516
3517 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3518 Importer.getToContext().getTranslationUnitDecl(),
3519 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003520 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003521 Name.getAsIdentifierInfo(),
3522 TemplateParams);
3523}
3524
3525Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3526 // If this record has a definition in the translation unit we're coming from,
3527 // but this particular declaration is not that definition, import the
3528 // definition and map to that.
3529 CXXRecordDecl *Definition
3530 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3531 if (Definition && Definition != D->getTemplatedDecl()) {
3532 Decl *ImportedDef
3533 = Importer.Import(Definition->getDescribedClassTemplate());
3534 if (!ImportedDef)
3535 return 0;
3536
3537 return Importer.Imported(D, ImportedDef);
3538 }
3539
3540 // Import the major distinguishing characteristics of this class template.
3541 DeclContext *DC, *LexicalDC;
3542 DeclarationName Name;
3543 SourceLocation Loc;
3544 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3545 return 0;
3546
3547 // We may already have a template of the same name; try to find and match it.
3548 if (!DC->isFunctionOrMethod()) {
3549 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3550 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3551 Lookup.first != Lookup.second;
3552 ++Lookup.first) {
3553 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3554 continue;
3555
3556 Decl *Found = *Lookup.first;
3557 if (ClassTemplateDecl *FoundTemplate
3558 = dyn_cast<ClassTemplateDecl>(Found)) {
3559 if (IsStructuralMatch(D, FoundTemplate)) {
3560 // The class templates structurally match; call it the same template.
3561 // FIXME: We may be filling in a forward declaration here. Handle
3562 // this case!
3563 Importer.Imported(D->getTemplatedDecl(),
3564 FoundTemplate->getTemplatedDecl());
3565 return Importer.Imported(D, FoundTemplate);
3566 }
3567 }
3568
3569 ConflictingDecls.push_back(*Lookup.first);
3570 }
3571
3572 if (!ConflictingDecls.empty()) {
3573 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3574 ConflictingDecls.data(),
3575 ConflictingDecls.size());
3576 }
3577
3578 if (!Name)
3579 return 0;
3580 }
3581
3582 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3583
3584 // Create the declaration that is being templated.
3585 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3586 DTemplated->getTagKind(),
3587 DC,
3588 Importer.Import(DTemplated->getLocation()),
3589 Name.getAsIdentifierInfo(),
Abramo Bagnara50228632011-03-06 16:09:14 +00003590 Importer.Import(DTemplated->getLocStart()));
Douglas Gregora082a492010-11-30 19:14:50 +00003591 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003592 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003593 D2Templated->setLexicalDeclContext(LexicalDC);
3594
3595 // Create the class template declaration itself.
3596 TemplateParameterList *TemplateParams
3597 = ImportTemplateParameterList(D->getTemplateParameters());
3598 if (!TemplateParams)
3599 return 0;
3600
3601 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3602 Loc, Name, TemplateParams,
3603 D2Templated,
3604 /*PrevDecl=*/0);
3605 D2Templated->setDescribedClassTemplate(D2);
3606
3607 D2->setAccess(D->getAccess());
3608 D2->setLexicalDeclContext(LexicalDC);
3609 LexicalDC->addDecl(D2);
3610
3611 // Note the relationship between the class templates.
3612 Importer.Imported(D, D2);
3613 Importer.Imported(DTemplated, D2Templated);
3614
3615 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3616 // FIXME: Import definition!
3617 }
3618
3619 return D2;
3620}
3621
Douglas Gregore2e50d332010-12-01 01:36:18 +00003622Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3623 ClassTemplateSpecializationDecl *D) {
3624 // If this record has a definition in the translation unit we're coming from,
3625 // but this particular declaration is not that definition, import the
3626 // definition and map to that.
3627 TagDecl *Definition = D->getDefinition();
3628 if (Definition && Definition != D) {
3629 Decl *ImportedDef = Importer.Import(Definition);
3630 if (!ImportedDef)
3631 return 0;
3632
3633 return Importer.Imported(D, ImportedDef);
3634 }
3635
3636 ClassTemplateDecl *ClassTemplate
3637 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3638 D->getSpecializedTemplate()));
3639 if (!ClassTemplate)
3640 return 0;
3641
3642 // Import the context of this declaration.
3643 DeclContext *DC = ClassTemplate->getDeclContext();
3644 if (!DC)
3645 return 0;
3646
3647 DeclContext *LexicalDC = DC;
3648 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3649 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3650 if (!LexicalDC)
3651 return 0;
3652 }
3653
3654 // Import the location of this declaration.
3655 SourceLocation Loc = Importer.Import(D->getLocation());
3656
3657 // Import template arguments.
3658 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3659 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3660 D->getTemplateArgs().size(),
3661 TemplateArgs))
3662 return 0;
3663
3664 // Try to find an existing specialization with these template arguments.
3665 void *InsertPos = 0;
3666 ClassTemplateSpecializationDecl *D2
3667 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3668 TemplateArgs.size(), InsertPos);
3669 if (D2) {
3670 // We already have a class template specialization with these template
3671 // arguments.
3672
3673 // FIXME: Check for specialization vs. instantiation errors.
3674
3675 if (RecordDecl *FoundDef = D2->getDefinition()) {
3676 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3677 // The record types structurally match, or the "from" translation
3678 // unit only had a forward declaration anyway; call it the same
3679 // function.
3680 return Importer.Imported(D, FoundDef);
3681 }
3682 }
3683 } else {
3684 // Create a new specialization.
3685 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3686 D->getTagKind(), DC,
3687 Loc, ClassTemplate,
3688 TemplateArgs.data(),
3689 TemplateArgs.size(),
3690 /*PrevDecl=*/0);
3691 D2->setSpecializationKind(D->getSpecializationKind());
3692
3693 // Add this specialization to the class template.
3694 ClassTemplate->AddSpecialization(D2, InsertPos);
3695
3696 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003697 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003698
3699 // Add the specialization to this context.
3700 D2->setLexicalDeclContext(LexicalDC);
3701 LexicalDC->addDecl(D2);
3702 }
3703 Importer.Imported(D, D2);
3704
3705 if (D->isDefinition() && ImportDefinition(D, D2))
3706 return 0;
3707
3708 return D2;
3709}
3710
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003711//----------------------------------------------------------------------------
3712// Import Statements
3713//----------------------------------------------------------------------------
3714
3715Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3716 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3717 << S->getStmtClassName();
3718 return 0;
3719}
3720
3721//----------------------------------------------------------------------------
3722// Import Expressions
3723//----------------------------------------------------------------------------
3724Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3725 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3726 << E->getStmtClassName();
3727 return 0;
3728}
3729
Douglas Gregor52f820e2010-02-19 01:17:02 +00003730Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3731 NestedNameSpecifier *Qualifier = 0;
3732 if (E->getQualifier()) {
3733 Qualifier = Importer.Import(E->getQualifier());
3734 if (!E->getQualifier())
3735 return 0;
3736 }
3737
3738 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3739 if (!ToD)
3740 return 0;
3741
3742 QualType T = Importer.Import(E->getType());
3743 if (T.isNull())
3744 return 0;
3745
Douglas Gregorea972d32011-02-28 21:54:11 +00003746 return DeclRefExpr::Create(Importer.getToContext(),
3747 Importer.Import(E->getQualifierLoc()),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003748 ToD,
3749 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003750 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003751 /*FIXME:TemplateArgs=*/0);
3752}
3753
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003754Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3755 QualType T = Importer.Import(E->getType());
3756 if (T.isNull())
3757 return 0;
3758
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003759 return IntegerLiteral::Create(Importer.getToContext(),
3760 E->getValue(), T,
3761 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003762}
3763
Douglas Gregor623421d2010-02-18 02:21:22 +00003764Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3765 QualType T = Importer.Import(E->getType());
3766 if (T.isNull())
3767 return 0;
3768
3769 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3770 E->isWide(), T,
3771 Importer.Import(E->getLocation()));
3772}
3773
Douglas Gregorc74247e2010-02-19 01:07:06 +00003774Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3775 Expr *SubExpr = Importer.Import(E->getSubExpr());
3776 if (!SubExpr)
3777 return 0;
3778
3779 return new (Importer.getToContext())
3780 ParenExpr(Importer.Import(E->getLParen()),
3781 Importer.Import(E->getRParen()),
3782 SubExpr);
3783}
3784
3785Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3786 QualType T = Importer.Import(E->getType());
3787 if (T.isNull())
3788 return 0;
3789
3790 Expr *SubExpr = Importer.Import(E->getSubExpr());
3791 if (!SubExpr)
3792 return 0;
3793
3794 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003795 T, E->getValueKind(),
3796 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003797 Importer.Import(E->getOperatorLoc()));
3798}
3799
Douglas Gregord8552cd2010-02-19 01:24:23 +00003800Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3801 QualType ResultType = Importer.Import(E->getType());
3802
3803 if (E->isArgumentType()) {
3804 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3805 if (!TInfo)
3806 return 0;
3807
3808 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3809 TInfo, ResultType,
3810 Importer.Import(E->getOperatorLoc()),
3811 Importer.Import(E->getRParenLoc()));
3812 }
3813
3814 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3815 if (!SubExpr)
3816 return 0;
3817
3818 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3819 SubExpr, ResultType,
3820 Importer.Import(E->getOperatorLoc()),
3821 Importer.Import(E->getRParenLoc()));
3822}
3823
Douglas Gregorc74247e2010-02-19 01:07:06 +00003824Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3825 QualType T = Importer.Import(E->getType());
3826 if (T.isNull())
3827 return 0;
3828
3829 Expr *LHS = Importer.Import(E->getLHS());
3830 if (!LHS)
3831 return 0;
3832
3833 Expr *RHS = Importer.Import(E->getRHS());
3834 if (!RHS)
3835 return 0;
3836
3837 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003838 T, E->getValueKind(),
3839 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003840 Importer.Import(E->getOperatorLoc()));
3841}
3842
3843Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3844 QualType T = Importer.Import(E->getType());
3845 if (T.isNull())
3846 return 0;
3847
3848 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3849 if (CompLHSType.isNull())
3850 return 0;
3851
3852 QualType CompResultType = Importer.Import(E->getComputationResultType());
3853 if (CompResultType.isNull())
3854 return 0;
3855
3856 Expr *LHS = Importer.Import(E->getLHS());
3857 if (!LHS)
3858 return 0;
3859
3860 Expr *RHS = Importer.Import(E->getRHS());
3861 if (!RHS)
3862 return 0;
3863
3864 return new (Importer.getToContext())
3865 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003866 T, E->getValueKind(),
3867 E->getObjectKind(),
3868 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003869 Importer.Import(E->getOperatorLoc()));
3870}
3871
John McCallcf142162010-08-07 06:22:56 +00003872bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3873 if (E->path_empty()) return false;
3874
3875 // TODO: import cast paths
3876 return true;
3877}
3878
Douglas Gregor98c10182010-02-12 22:17:39 +00003879Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3880 QualType T = Importer.Import(E->getType());
3881 if (T.isNull())
3882 return 0;
3883
3884 Expr *SubExpr = Importer.Import(E->getSubExpr());
3885 if (!SubExpr)
3886 return 0;
John McCallcf142162010-08-07 06:22:56 +00003887
3888 CXXCastPath BasePath;
3889 if (ImportCastPath(E, BasePath))
3890 return 0;
3891
3892 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003893 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003894}
3895
Douglas Gregor5481d322010-02-19 01:32:14 +00003896Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3897 QualType T = Importer.Import(E->getType());
3898 if (T.isNull())
3899 return 0;
3900
3901 Expr *SubExpr = Importer.Import(E->getSubExpr());
3902 if (!SubExpr)
3903 return 0;
3904
3905 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3906 if (!TInfo && E->getTypeInfoAsWritten())
3907 return 0;
3908
John McCallcf142162010-08-07 06:22:56 +00003909 CXXCastPath BasePath;
3910 if (ImportCastPath(E, BasePath))
3911 return 0;
3912
John McCall7decc9e2010-11-18 06:31:45 +00003913 return CStyleCastExpr::Create(Importer.getToContext(), T,
3914 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003915 SubExpr, &BasePath, TInfo,
3916 Importer.Import(E->getLParenLoc()),
3917 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003918}
3919
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003920ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003921 ASTContext &FromContext, FileManager &FromFileManager,
3922 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003923 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003924 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3925 Minimal(MinimalImport)
3926{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003927 ImportedDecls[FromContext.getTranslationUnitDecl()]
3928 = ToContext.getTranslationUnitDecl();
3929}
3930
3931ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003932
3933QualType ASTImporter::Import(QualType FromT) {
3934 if (FromT.isNull())
3935 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003936
3937 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003938
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003939 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003940 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3941 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003942 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003943 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003944
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003945 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003946 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003947 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003948 if (ToT.isNull())
3949 return ToT;
3950
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003951 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003952 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003953
John McCall424cec92011-01-19 06:33:43 +00003954 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003955}
3956
Douglas Gregor62d311f2010-02-09 19:21:46 +00003957TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003958 if (!FromTSI)
3959 return FromTSI;
3960
3961 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003962 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003963 QualType T = Import(FromTSI->getType());
3964 if (T.isNull())
3965 return 0;
3966
3967 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003968 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003969}
3970
3971Decl *ASTImporter::Import(Decl *FromD) {
3972 if (!FromD)
3973 return 0;
3974
3975 // Check whether we've already imported this declaration.
3976 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3977 if (Pos != ImportedDecls.end())
3978 return Pos->second;
3979
3980 // Import the type
3981 ASTNodeImporter Importer(*this);
3982 Decl *ToD = Importer.Visit(FromD);
3983 if (!ToD)
3984 return 0;
3985
3986 // Record the imported declaration.
3987 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003988
3989 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3990 // Keep track of anonymous tags that have an associated typedef.
3991 if (FromTag->getTypedefForAnonDecl())
3992 AnonTagsWithPendingTypedefs.push_back(FromTag);
3993 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3994 // When we've finished transforming a typedef, see whether it was the
3995 // typedef for an anonymous tag.
3996 for (llvm::SmallVector<TagDecl *, 4>::iterator
3997 FromTag = AnonTagsWithPendingTypedefs.begin(),
3998 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3999 FromTag != FromTagEnd; ++FromTag) {
4000 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
4001 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4002 // We found the typedef for an anonymous tag; link them.
4003 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
4004 AnonTagsWithPendingTypedefs.erase(FromTag);
4005 break;
4006 }
4007 }
4008 }
4009 }
4010
Douglas Gregor62d311f2010-02-09 19:21:46 +00004011 return ToD;
4012}
4013
4014DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4015 if (!FromDC)
4016 return FromDC;
4017
4018 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4019}
4020
4021Expr *ASTImporter::Import(Expr *FromE) {
4022 if (!FromE)
4023 return 0;
4024
4025 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4026}
4027
4028Stmt *ASTImporter::Import(Stmt *FromS) {
4029 if (!FromS)
4030 return 0;
4031
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004032 // Check whether we've already imported this declaration.
4033 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4034 if (Pos != ImportedStmts.end())
4035 return Pos->second;
4036
4037 // Import the type
4038 ASTNodeImporter Importer(*this);
4039 Stmt *ToS = Importer.Visit(FromS);
4040 if (!ToS)
4041 return 0;
4042
4043 // Record the imported declaration.
4044 ImportedStmts[FromS] = ToS;
4045 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004046}
4047
4048NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4049 if (!FromNNS)
4050 return 0;
4051
4052 // FIXME: Implement!
4053 return 0;
4054}
4055
Douglas Gregor14454802011-02-25 02:25:35 +00004056NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4057 // FIXME: Implement!
4058 return NestedNameSpecifierLoc();
4059}
4060
Douglas Gregore2e50d332010-12-01 01:36:18 +00004061TemplateName ASTImporter::Import(TemplateName From) {
4062 switch (From.getKind()) {
4063 case TemplateName::Template:
4064 if (TemplateDecl *ToTemplate
4065 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4066 return TemplateName(ToTemplate);
4067
4068 return TemplateName();
4069
4070 case TemplateName::OverloadedTemplate: {
4071 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4072 UnresolvedSet<2> ToTemplates;
4073 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4074 E = FromStorage->end();
4075 I != E; ++I) {
4076 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4077 ToTemplates.addDecl(To);
4078 else
4079 return TemplateName();
4080 }
4081 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4082 ToTemplates.end());
4083 }
4084
4085 case TemplateName::QualifiedTemplate: {
4086 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4087 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4088 if (!Qualifier)
4089 return TemplateName();
4090
4091 if (TemplateDecl *ToTemplate
4092 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4093 return ToContext.getQualifiedTemplateName(Qualifier,
4094 QTN->hasTemplateKeyword(),
4095 ToTemplate);
4096
4097 return TemplateName();
4098 }
4099
4100 case TemplateName::DependentTemplate: {
4101 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4102 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4103 if (!Qualifier)
4104 return TemplateName();
4105
4106 if (DTN->isIdentifier()) {
4107 return ToContext.getDependentTemplateName(Qualifier,
4108 Import(DTN->getIdentifier()));
4109 }
4110
4111 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4112 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004113
4114 case TemplateName::SubstTemplateTemplateParmPack: {
4115 SubstTemplateTemplateParmPackStorage *SubstPack
4116 = From.getAsSubstTemplateTemplateParmPack();
4117 TemplateTemplateParmDecl *Param
4118 = cast_or_null<TemplateTemplateParmDecl>(
4119 Import(SubstPack->getParameterPack()));
4120 if (!Param)
4121 return TemplateName();
4122
4123 ASTNodeImporter Importer(*this);
4124 TemplateArgument ArgPack
4125 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4126 if (ArgPack.isNull())
4127 return TemplateName();
4128
4129 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4130 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004131 }
4132
4133 llvm_unreachable("Invalid template name kind");
4134 return TemplateName();
4135}
4136
Douglas Gregor62d311f2010-02-09 19:21:46 +00004137SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4138 if (FromLoc.isInvalid())
4139 return SourceLocation();
4140
Douglas Gregor811663e2010-02-10 00:15:17 +00004141 SourceManager &FromSM = FromContext.getSourceManager();
4142
4143 // For now, map everything down to its spelling location, so that we
4144 // don't have to import macro instantiations.
4145 // FIXME: Import macro instantiations!
4146 FromLoc = FromSM.getSpellingLoc(FromLoc);
4147 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4148 SourceManager &ToSM = ToContext.getSourceManager();
4149 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4150 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004151}
4152
4153SourceRange ASTImporter::Import(SourceRange FromRange) {
4154 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4155}
4156
Douglas Gregor811663e2010-02-10 00:15:17 +00004157FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004158 llvm::DenseMap<FileID, FileID>::iterator Pos
4159 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004160 if (Pos != ImportedFileIDs.end())
4161 return Pos->second;
4162
4163 SourceManager &FromSM = FromContext.getSourceManager();
4164 SourceManager &ToSM = ToContext.getSourceManager();
4165 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4166 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4167
4168 // Include location of this file.
4169 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4170
4171 // Map the FileID for to the "to" source manager.
4172 FileID ToID;
4173 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004174 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004175 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4176 // disk again
4177 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4178 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004179 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004180 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4181 FromSLoc.getFile().getFileCharacteristic());
4182 } else {
4183 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004184 const llvm::MemoryBuffer *
4185 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004186 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004187 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004188 FromBuf->getBufferIdentifier());
4189 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4190 }
4191
4192
Sebastian Redl99219f12010-09-30 01:03:06 +00004193 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004194 return ToID;
4195}
4196
Douglas Gregor0a791672011-01-18 03:11:38 +00004197void ASTImporter::ImportDefinition(Decl *From) {
4198 Decl *To = Import(From);
4199 if (!To)
4200 return;
4201
4202 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4203 ASTNodeImporter Importer(*this);
4204 Importer.ImportDeclContext(FromDC, true);
4205 }
4206}
4207
Douglas Gregor96e578d2010-02-05 17:54:41 +00004208DeclarationName ASTImporter::Import(DeclarationName FromName) {
4209 if (!FromName)
4210 return DeclarationName();
4211
4212 switch (FromName.getNameKind()) {
4213 case DeclarationName::Identifier:
4214 return Import(FromName.getAsIdentifierInfo());
4215
4216 case DeclarationName::ObjCZeroArgSelector:
4217 case DeclarationName::ObjCOneArgSelector:
4218 case DeclarationName::ObjCMultiArgSelector:
4219 return Import(FromName.getObjCSelector());
4220
4221 case DeclarationName::CXXConstructorName: {
4222 QualType T = Import(FromName.getCXXNameType());
4223 if (T.isNull())
4224 return DeclarationName();
4225
4226 return ToContext.DeclarationNames.getCXXConstructorName(
4227 ToContext.getCanonicalType(T));
4228 }
4229
4230 case DeclarationName::CXXDestructorName: {
4231 QualType T = Import(FromName.getCXXNameType());
4232 if (T.isNull())
4233 return DeclarationName();
4234
4235 return ToContext.DeclarationNames.getCXXDestructorName(
4236 ToContext.getCanonicalType(T));
4237 }
4238
4239 case DeclarationName::CXXConversionFunctionName: {
4240 QualType T = Import(FromName.getCXXNameType());
4241 if (T.isNull())
4242 return DeclarationName();
4243
4244 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4245 ToContext.getCanonicalType(T));
4246 }
4247
4248 case DeclarationName::CXXOperatorName:
4249 return ToContext.DeclarationNames.getCXXOperatorName(
4250 FromName.getCXXOverloadedOperator());
4251
4252 case DeclarationName::CXXLiteralOperatorName:
4253 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4254 Import(FromName.getCXXLiteralIdentifier()));
4255
4256 case DeclarationName::CXXUsingDirective:
4257 // FIXME: STATICS!
4258 return DeclarationName::getUsingDirectiveName();
4259 }
4260
4261 // Silence bogus GCC warning
4262 return DeclarationName();
4263}
4264
Douglas Gregore2e50d332010-12-01 01:36:18 +00004265IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004266 if (!FromId)
4267 return 0;
4268
4269 return &ToContext.Idents.get(FromId->getName());
4270}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004271
Douglas Gregor43f54792010-02-17 02:12:47 +00004272Selector ASTImporter::Import(Selector FromSel) {
4273 if (FromSel.isNull())
4274 return Selector();
4275
4276 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4277 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4278 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4279 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4280 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4281}
4282
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004283DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4284 DeclContext *DC,
4285 unsigned IDNS,
4286 NamedDecl **Decls,
4287 unsigned NumDecls) {
4288 return Name;
4289}
4290
4291DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004292 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004293}
4294
4295DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004296 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004297}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004298
4299Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4300 ImportedDecls[From] = To;
4301 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004302}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004303
4304bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004305 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004306 = ImportedTypes.find(From.getTypePtr());
4307 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4308 return true;
4309
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004310 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004311 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004312}