blob: c5314059c088ced87e63cacff52a7cddd65155fc [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
Douglas Gregore4c83e42010-02-09 22:48:33 +000044 QualType VisitType(Type *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000045 QualType VisitBuiltinType(BuiltinType *T);
46 QualType VisitComplexType(ComplexType *T);
47 QualType VisitPointerType(PointerType *T);
48 QualType VisitBlockPointerType(BlockPointerType *T);
49 QualType VisitLValueReferenceType(LValueReferenceType *T);
50 QualType VisitRValueReferenceType(RValueReferenceType *T);
51 QualType VisitMemberPointerType(MemberPointerType *T);
52 QualType VisitConstantArrayType(ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(IncompleteArrayType *T);
54 QualType VisitVariableArrayType(VariableArrayType *T);
55 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
57 QualType VisitVectorType(VectorType *T);
58 QualType VisitExtVectorType(ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(FunctionProtoType *T);
61 // FIXME: UnresolvedUsingType
62 QualType VisitTypedefType(TypedefType *T);
63 QualType VisitTypeOfExprType(TypeOfExprType *T);
64 // FIXME: DependentTypeOfExprType
65 QualType VisitTypeOfType(TypeOfType *T);
66 QualType VisitDecltypeType(DecltypeType *T);
67 // FIXME: DependentDecltypeType
68 QualType VisitRecordType(RecordType *T);
69 QualType VisitEnumType(EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
Douglas Gregore2e50d332010-12-01 01:36:18 +000072 QualType VisitTemplateSpecializationType(TemplateSpecializationType *T);
Abramo Bagnara6150c882010-05-11 21:36:43 +000073 QualType VisitElaboratedType(ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
Douglas Gregor96e578d2010-02-05 17:54:41 +000076 QualType VisitObjCInterfaceType(ObjCInterfaceType *T);
John McCall8b07ec22010-05-15 11:32:37 +000077 QualType VisitObjCObjectType(ObjCObjectType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000078 QualType VisitObjCObjectPointerType(ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
80 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000084 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
85 DeclarationNameInfo& To);
Douglas Gregor968d6332010-02-21 18:24:45 +000086 void ImportDeclContext(DeclContext *FromDC);
Douglas Gregore2e50d332010-12-01 01:36:18 +000087 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +000088 TemplateParameterList *ImportTemplateParameterList(
89 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +000090 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
91 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
92 unsigned NumFromArgs,
93 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor5c73e912010-02-11 00:48:18 +000094 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000095 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000096 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000097 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000098 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Douglas Gregor5fa74c32010-02-10 21:10:29 +000099 Decl *VisitTypedefDecl(TypedefDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000100 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000101 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000102 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000103 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000104 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
105 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
106 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
107 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000108 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000109 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000110 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000111 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000112 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000113 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000114 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000115 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000116 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000117 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000118 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000119 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000120 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000121 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000122 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000123 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
124 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
125 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
126 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000127 Decl *VisitClassTemplateSpecializationDecl(
128 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000129
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000130 // Importing statements
131 Stmt *VisitStmt(Stmt *S);
132
133 // Importing expressions
134 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000135 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000136 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000137 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000138 Expr *VisitParenExpr(ParenExpr *E);
139 Expr *VisitUnaryOperator(UnaryOperator *E);
Douglas Gregord8552cd2010-02-19 01:24:23 +0000140 Expr *VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000141 Expr *VisitBinaryOperator(BinaryOperator *E);
142 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000143 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000144 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000145 };
146}
147
148//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000149// Structural Equivalence
150//----------------------------------------------------------------------------
151
152namespace {
153 struct StructuralEquivalenceContext {
154 /// \brief AST contexts for which we are checking structural equivalence.
155 ASTContext &C1, &C2;
156
Douglas Gregor3996e242010-02-15 22:01:00 +0000157 /// \brief The set of "tentative" equivalences between two canonical
158 /// declarations, mapping from a declaration in the first context to the
159 /// declaration in the second context that we believe to be equivalent.
160 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
161
162 /// \brief Queue of declarations in the first context whose equivalence
163 /// with a declaration in the second context still needs to be verified.
164 std::deque<Decl *> DeclsToCheck;
165
Douglas Gregorb4964f72010-02-15 23:54:17 +0000166 /// \brief Declaration (from, to) pairs that are known not to be equivalent
167 /// (which we have already complained about).
168 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
169
Douglas Gregor3996e242010-02-15 22:01:00 +0000170 /// \brief Whether we're being strict about the spelling of types when
171 /// unifying two types.
172 bool StrictTypeSpelling;
173
174 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000175 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000176 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000177 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000178 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000179
180 /// \brief Determine whether the two declarations are structurally
181 /// equivalent.
182 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
183
184 /// \brief Determine whether the two types are structurally equivalent.
185 bool IsStructurallyEquivalent(QualType T1, QualType T2);
186
187 private:
188 /// \brief Finish checking all of the structural equivalences.
189 ///
190 /// \returns true if an error occurred, false otherwise.
191 bool Finish();
192
193 public:
194 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000195 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000196 }
197
198 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000199 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000200 }
201 };
202}
203
204static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
205 QualType T1, QualType T2);
206static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
207 Decl *D1, Decl *D2);
208
209/// \brief Determine if two APInts have the same value, after zero-extending
210/// one of them (if needed!) to ensure that the bit-widths match.
211static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
212 if (I1.getBitWidth() == I2.getBitWidth())
213 return I1 == I2;
214
215 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000216 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor3996e242010-02-15 22:01:00 +0000217
Jay Foad6d4db0c2010-12-07 08:25:34 +0000218 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor3996e242010-02-15 22:01:00 +0000219}
220
221/// \brief Determine if two APSInts have the same value, zero- or sign-extending
222/// as needed.
223static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
224 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
225 return I1 == I2;
226
227 // Check for a bit-width mismatch.
228 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000229 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor3996e242010-02-15 22:01:00 +0000230 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000231 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000232
233 // We have a signedness mismatch. Turn the signed value into an unsigned
234 // value.
235 if (I1.isSigned()) {
236 if (I1.isNegative())
237 return false;
238
239 return llvm::APSInt(I1, true) == I2;
240 }
241
242 if (I2.isNegative())
243 return false;
244
245 return I1 == llvm::APSInt(I2, true);
246}
247
248/// \brief Determine structural equivalence of two expressions.
249static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
250 Expr *E1, Expr *E2) {
251 if (!E1 || !E2)
252 return E1 == E2;
253
254 // FIXME: Actually perform a structural comparison!
255 return true;
256}
257
258/// \brief Determine whether two identifiers are equivalent.
259static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
260 const IdentifierInfo *Name2) {
261 if (!Name1 || !Name2)
262 return Name1 == Name2;
263
264 return Name1->getName() == Name2->getName();
265}
266
267/// \brief Determine whether two nested-name-specifiers are equivalent.
268static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
269 NestedNameSpecifier *NNS1,
270 NestedNameSpecifier *NNS2) {
271 // FIXME: Implement!
272 return true;
273}
274
275/// \brief Determine whether two template arguments are equivalent.
276static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
277 const TemplateArgument &Arg1,
278 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000279 if (Arg1.getKind() != Arg2.getKind())
280 return false;
281
282 switch (Arg1.getKind()) {
283 case TemplateArgument::Null:
284 return true;
285
286 case TemplateArgument::Type:
287 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
288
289 case TemplateArgument::Integral:
290 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
291 Arg2.getIntegralType()))
292 return false;
293
294 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
295
296 case TemplateArgument::Declaration:
297 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
298
299 case TemplateArgument::Template:
300 return IsStructurallyEquivalent(Context,
301 Arg1.getAsTemplate(),
302 Arg2.getAsTemplate());
303
304 case TemplateArgument::Expression:
305 return IsStructurallyEquivalent(Context,
306 Arg1.getAsExpr(), Arg2.getAsExpr());
307
308 case TemplateArgument::Pack:
309 if (Arg1.pack_size() != Arg2.pack_size())
310 return false;
311
312 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
313 if (!IsStructurallyEquivalent(Context,
314 Arg1.pack_begin()[I],
315 Arg2.pack_begin()[I]))
316 return false;
317
318 return true;
319 }
320
321 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000322 return true;
323}
324
325/// \brief Determine structural equivalence for the common part of array
326/// types.
327static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
328 const ArrayType *Array1,
329 const ArrayType *Array2) {
330 if (!IsStructurallyEquivalent(Context,
331 Array1->getElementType(),
332 Array2->getElementType()))
333 return false;
334 if (Array1->getSizeModifier() != Array2->getSizeModifier())
335 return false;
336 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
337 return false;
338
339 return true;
340}
341
342/// \brief Determine structural equivalence of two types.
343static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
344 QualType T1, QualType T2) {
345 if (T1.isNull() || T2.isNull())
346 return T1.isNull() && T2.isNull();
347
348 if (!Context.StrictTypeSpelling) {
349 // We aren't being strict about token-to-token equivalence of types,
350 // so map down to the canonical type.
351 T1 = Context.C1.getCanonicalType(T1);
352 T2 = Context.C2.getCanonicalType(T2);
353 }
354
355 if (T1.getQualifiers() != T2.getQualifiers())
356 return false;
357
Douglas Gregorb4964f72010-02-15 23:54:17 +0000358 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000359
Douglas Gregorb4964f72010-02-15 23:54:17 +0000360 if (T1->getTypeClass() != T2->getTypeClass()) {
361 // Compare function types with prototypes vs. without prototypes as if
362 // both did not have prototypes.
363 if (T1->getTypeClass() == Type::FunctionProto &&
364 T2->getTypeClass() == Type::FunctionNoProto)
365 TC = Type::FunctionNoProto;
366 else if (T1->getTypeClass() == Type::FunctionNoProto &&
367 T2->getTypeClass() == Type::FunctionProto)
368 TC = Type::FunctionNoProto;
369 else
370 return false;
371 }
372
373 switch (TC) {
374 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000375 // FIXME: Deal with Char_S/Char_U.
376 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
377 return false;
378 break;
379
380 case Type::Complex:
381 if (!IsStructurallyEquivalent(Context,
382 cast<ComplexType>(T1)->getElementType(),
383 cast<ComplexType>(T2)->getElementType()))
384 return false;
385 break;
386
387 case Type::Pointer:
388 if (!IsStructurallyEquivalent(Context,
389 cast<PointerType>(T1)->getPointeeType(),
390 cast<PointerType>(T2)->getPointeeType()))
391 return false;
392 break;
393
394 case Type::BlockPointer:
395 if (!IsStructurallyEquivalent(Context,
396 cast<BlockPointerType>(T1)->getPointeeType(),
397 cast<BlockPointerType>(T2)->getPointeeType()))
398 return false;
399 break;
400
401 case Type::LValueReference:
402 case Type::RValueReference: {
403 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
404 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
405 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
406 return false;
407 if (Ref1->isInnerRef() != Ref2->isInnerRef())
408 return false;
409 if (!IsStructurallyEquivalent(Context,
410 Ref1->getPointeeTypeAsWritten(),
411 Ref2->getPointeeTypeAsWritten()))
412 return false;
413 break;
414 }
415
416 case Type::MemberPointer: {
417 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
418 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
419 if (!IsStructurallyEquivalent(Context,
420 MemPtr1->getPointeeType(),
421 MemPtr2->getPointeeType()))
422 return false;
423 if (!IsStructurallyEquivalent(Context,
424 QualType(MemPtr1->getClass(), 0),
425 QualType(MemPtr2->getClass(), 0)))
426 return false;
427 break;
428 }
429
430 case Type::ConstantArray: {
431 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
432 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
433 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
434 return false;
435
436 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
437 return false;
438 break;
439 }
440
441 case Type::IncompleteArray:
442 if (!IsArrayStructurallyEquivalent(Context,
443 cast<ArrayType>(T1),
444 cast<ArrayType>(T2)))
445 return false;
446 break;
447
448 case Type::VariableArray: {
449 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
450 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
451 if (!IsStructurallyEquivalent(Context,
452 Array1->getSizeExpr(), Array2->getSizeExpr()))
453 return false;
454
455 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
456 return false;
457
458 break;
459 }
460
461 case Type::DependentSizedArray: {
462 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
463 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
464 if (!IsStructurallyEquivalent(Context,
465 Array1->getSizeExpr(), Array2->getSizeExpr()))
466 return false;
467
468 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
469 return false;
470
471 break;
472 }
473
474 case Type::DependentSizedExtVector: {
475 const DependentSizedExtVectorType *Vec1
476 = cast<DependentSizedExtVectorType>(T1);
477 const DependentSizedExtVectorType *Vec2
478 = cast<DependentSizedExtVectorType>(T2);
479 if (!IsStructurallyEquivalent(Context,
480 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
481 return false;
482 if (!IsStructurallyEquivalent(Context,
483 Vec1->getElementType(),
484 Vec2->getElementType()))
485 return false;
486 break;
487 }
488
489 case Type::Vector:
490 case Type::ExtVector: {
491 const VectorType *Vec1 = cast<VectorType>(T1);
492 const VectorType *Vec2 = cast<VectorType>(T2);
493 if (!IsStructurallyEquivalent(Context,
494 Vec1->getElementType(),
495 Vec2->getElementType()))
496 return false;
497 if (Vec1->getNumElements() != Vec2->getNumElements())
498 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000499 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000500 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000501 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000502 }
503
504 case Type::FunctionProto: {
505 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
506 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
507 if (Proto1->getNumArgs() != Proto2->getNumArgs())
508 return false;
509 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
510 if (!IsStructurallyEquivalent(Context,
511 Proto1->getArgType(I),
512 Proto2->getArgType(I)))
513 return false;
514 }
515 if (Proto1->isVariadic() != Proto2->isVariadic())
516 return false;
517 if (Proto1->hasExceptionSpec() != Proto2->hasExceptionSpec())
518 return false;
519 if (Proto1->hasAnyExceptionSpec() != Proto2->hasAnyExceptionSpec())
520 return false;
521 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
522 return false;
523 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
524 if (!IsStructurallyEquivalent(Context,
525 Proto1->getExceptionType(I),
526 Proto2->getExceptionType(I)))
527 return false;
528 }
529 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
530 return false;
531
532 // Fall through to check the bits common with FunctionNoProtoType.
533 }
534
535 case Type::FunctionNoProto: {
536 const FunctionType *Function1 = cast<FunctionType>(T1);
537 const FunctionType *Function2 = cast<FunctionType>(T2);
538 if (!IsStructurallyEquivalent(Context,
539 Function1->getResultType(),
540 Function2->getResultType()))
541 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000542 if (Function1->getExtInfo() != Function2->getExtInfo())
543 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000544 break;
545 }
546
547 case Type::UnresolvedUsing:
548 if (!IsStructurallyEquivalent(Context,
549 cast<UnresolvedUsingType>(T1)->getDecl(),
550 cast<UnresolvedUsingType>(T2)->getDecl()))
551 return false;
552
553 break;
554
555 case Type::Typedef:
556 if (!IsStructurallyEquivalent(Context,
557 cast<TypedefType>(T1)->getDecl(),
558 cast<TypedefType>(T2)->getDecl()))
559 return false;
560 break;
561
562 case Type::TypeOfExpr:
563 if (!IsStructurallyEquivalent(Context,
564 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
565 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
566 return false;
567 break;
568
569 case Type::TypeOf:
570 if (!IsStructurallyEquivalent(Context,
571 cast<TypeOfType>(T1)->getUnderlyingType(),
572 cast<TypeOfType>(T2)->getUnderlyingType()))
573 return false;
574 break;
575
576 case Type::Decltype:
577 if (!IsStructurallyEquivalent(Context,
578 cast<DecltypeType>(T1)->getUnderlyingExpr(),
579 cast<DecltypeType>(T2)->getUnderlyingExpr()))
580 return false;
581 break;
582
583 case Type::Record:
584 case Type::Enum:
585 if (!IsStructurallyEquivalent(Context,
586 cast<TagType>(T1)->getDecl(),
587 cast<TagType>(T2)->getDecl()))
588 return false;
589 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000590
Douglas Gregor3996e242010-02-15 22:01:00 +0000591 case Type::TemplateTypeParm: {
592 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
593 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
594 if (Parm1->getDepth() != Parm2->getDepth())
595 return false;
596 if (Parm1->getIndex() != Parm2->getIndex())
597 return false;
598 if (Parm1->isParameterPack() != Parm2->isParameterPack())
599 return false;
600
601 // Names of template type parameters are never significant.
602 break;
603 }
604
605 case Type::SubstTemplateTypeParm: {
606 const SubstTemplateTypeParmType *Subst1
607 = cast<SubstTemplateTypeParmType>(T1);
608 const SubstTemplateTypeParmType *Subst2
609 = cast<SubstTemplateTypeParmType>(T2);
610 if (!IsStructurallyEquivalent(Context,
611 QualType(Subst1->getReplacedParameter(), 0),
612 QualType(Subst2->getReplacedParameter(), 0)))
613 return false;
614 if (!IsStructurallyEquivalent(Context,
615 Subst1->getReplacementType(),
616 Subst2->getReplacementType()))
617 return false;
618 break;
619 }
620
621 case Type::TemplateSpecialization: {
622 const TemplateSpecializationType *Spec1
623 = cast<TemplateSpecializationType>(T1);
624 const TemplateSpecializationType *Spec2
625 = cast<TemplateSpecializationType>(T2);
626 if (!IsStructurallyEquivalent(Context,
627 Spec1->getTemplateName(),
628 Spec2->getTemplateName()))
629 return false;
630 if (Spec1->getNumArgs() != Spec2->getNumArgs())
631 return false;
632 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
633 if (!IsStructurallyEquivalent(Context,
634 Spec1->getArg(I), Spec2->getArg(I)))
635 return false;
636 }
637 break;
638 }
639
Abramo Bagnara6150c882010-05-11 21:36:43 +0000640 case Type::Elaborated: {
641 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
642 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
643 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
644 if (Elab1->getKeyword() != Elab2->getKeyword())
645 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000646 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000647 Elab1->getQualifier(),
648 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000649 return false;
650 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000651 Elab1->getNamedType(),
652 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000653 return false;
654 break;
655 }
656
John McCalle78aac42010-03-10 03:28:59 +0000657 case Type::InjectedClassName: {
658 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
659 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
660 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000661 Inj1->getInjectedSpecializationType(),
662 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000663 return false;
664 break;
665 }
666
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000667 case Type::DependentName: {
668 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
669 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000670 if (!IsStructurallyEquivalent(Context,
671 Typename1->getQualifier(),
672 Typename2->getQualifier()))
673 return false;
674 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
675 Typename2->getIdentifier()))
676 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000677
678 break;
679 }
680
John McCallc392f372010-06-11 00:33:02 +0000681 case Type::DependentTemplateSpecialization: {
682 const DependentTemplateSpecializationType *Spec1 =
683 cast<DependentTemplateSpecializationType>(T1);
684 const DependentTemplateSpecializationType *Spec2 =
685 cast<DependentTemplateSpecializationType>(T2);
686 if (!IsStructurallyEquivalent(Context,
687 Spec1->getQualifier(),
688 Spec2->getQualifier()))
689 return false;
690 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
691 Spec2->getIdentifier()))
692 return false;
693 if (Spec1->getNumArgs() != Spec2->getNumArgs())
694 return false;
695 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
696 if (!IsStructurallyEquivalent(Context,
697 Spec1->getArg(I), Spec2->getArg(I)))
698 return false;
699 }
700 break;
701 }
702
Douglas Gregor3996e242010-02-15 22:01:00 +0000703 case Type::ObjCInterface: {
704 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
705 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
706 if (!IsStructurallyEquivalent(Context,
707 Iface1->getDecl(), Iface2->getDecl()))
708 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000709 break;
710 }
711
712 case Type::ObjCObject: {
713 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
714 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
715 if (!IsStructurallyEquivalent(Context,
716 Obj1->getBaseType(),
717 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000718 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000719 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
720 return false;
721 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000722 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000723 Obj1->getProtocol(I),
724 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000725 return false;
726 }
727 break;
728 }
729
730 case Type::ObjCObjectPointer: {
731 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
732 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
733 if (!IsStructurallyEquivalent(Context,
734 Ptr1->getPointeeType(),
735 Ptr2->getPointeeType()))
736 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000737 break;
738 }
739
740 } // end switch
741
742 return true;
743}
744
745/// \brief Determine structural equivalence of two records.
746static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
747 RecordDecl *D1, RecordDecl *D2) {
748 if (D1->isUnion() != D2->isUnion()) {
749 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
750 << Context.C2.getTypeDeclType(D2);
751 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
752 << D1->getDeclName() << (unsigned)D1->getTagKind();
753 return false;
754 }
755
Douglas Gregore2e50d332010-12-01 01:36:18 +0000756 // If both declarations are class template specializations, we know
757 // the ODR applies, so check the template and template arguments.
758 ClassTemplateSpecializationDecl *Spec1
759 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
760 ClassTemplateSpecializationDecl *Spec2
761 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
762 if (Spec1 && Spec2) {
763 // Check that the specialized templates are the same.
764 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
765 Spec2->getSpecializedTemplate()))
766 return false;
767
768 // Check that the template arguments are the same.
769 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
770 return false;
771
772 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
773 if (!IsStructurallyEquivalent(Context,
774 Spec1->getTemplateArgs().get(I),
775 Spec2->getTemplateArgs().get(I)))
776 return false;
777 }
778 // If one is a class template specialization and the other is not, these
779 // structures are diferent.
780 else if (Spec1 || Spec2)
781 return false;
782
Douglas Gregorb4964f72010-02-15 23:54:17 +0000783 // Compare the definitions of these two records. If either or both are
784 // incomplete, we assume that they are equivalent.
785 D1 = D1->getDefinition();
786 D2 = D2->getDefinition();
787 if (!D1 || !D2)
788 return true;
789
Douglas Gregor3996e242010-02-15 22:01:00 +0000790 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
791 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
792 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
793 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000794 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000795 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000796 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000797 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000798 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000799 return false;
800 }
801
802 // Check the base classes.
803 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
804 BaseEnd1 = D1CXX->bases_end(),
805 Base2 = D2CXX->bases_begin();
806 Base1 != BaseEnd1;
807 ++Base1, ++Base2) {
808 if (!IsStructurallyEquivalent(Context,
809 Base1->getType(), Base2->getType())) {
810 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
811 << Context.C2.getTypeDeclType(D2);
812 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
813 << Base2->getType()
814 << Base2->getSourceRange();
815 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
816 << Base1->getType()
817 << Base1->getSourceRange();
818 return false;
819 }
820
821 // Check virtual vs. non-virtual inheritance mismatch.
822 if (Base1->isVirtual() != Base2->isVirtual()) {
823 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
824 << Context.C2.getTypeDeclType(D2);
825 Context.Diag2(Base2->getSourceRange().getBegin(),
826 diag::note_odr_virtual_base)
827 << Base2->isVirtual() << Base2->getSourceRange();
828 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
829 << Base1->isVirtual()
830 << Base1->getSourceRange();
831 return false;
832 }
833 }
834 } else if (D1CXX->getNumBases() > 0) {
835 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
836 << Context.C2.getTypeDeclType(D2);
837 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
838 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
839 << Base1->getType()
840 << Base1->getSourceRange();
841 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
842 return false;
843 }
844 }
845
846 // Check the fields for consistency.
847 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
848 Field2End = D2->field_end();
849 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
850 Field1End = D1->field_end();
851 Field1 != Field1End;
852 ++Field1, ++Field2) {
853 if (Field2 == Field2End) {
854 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
855 << Context.C2.getTypeDeclType(D2);
856 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
857 << Field1->getDeclName() << Field1->getType();
858 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
859 return false;
860 }
861
862 if (!IsStructurallyEquivalent(Context,
863 Field1->getType(), Field2->getType())) {
864 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
865 << Context.C2.getTypeDeclType(D2);
866 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
867 << Field2->getDeclName() << Field2->getType();
868 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
869 << Field1->getDeclName() << Field1->getType();
870 return false;
871 }
872
873 if (Field1->isBitField() != Field2->isBitField()) {
874 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
875 << Context.C2.getTypeDeclType(D2);
876 if (Field1->isBitField()) {
877 llvm::APSInt Bits;
878 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
879 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
880 << Field1->getDeclName() << Field1->getType()
881 << Bits.toString(10, false);
882 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
883 << Field2->getDeclName();
884 } else {
885 llvm::APSInt Bits;
886 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
887 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
888 << Field2->getDeclName() << Field2->getType()
889 << Bits.toString(10, false);
890 Context.Diag1(Field1->getLocation(),
891 diag::note_odr_not_bit_field)
892 << Field1->getDeclName();
893 }
894 return false;
895 }
896
897 if (Field1->isBitField()) {
898 // Make sure that the bit-fields are the same length.
899 llvm::APSInt Bits1, Bits2;
900 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
901 return false;
902 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
903 return false;
904
905 if (!IsSameValue(Bits1, Bits2)) {
906 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
907 << Context.C2.getTypeDeclType(D2);
908 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
909 << Field2->getDeclName() << Field2->getType()
910 << Bits2.toString(10, false);
911 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
912 << Field1->getDeclName() << Field1->getType()
913 << Bits1.toString(10, false);
914 return false;
915 }
916 }
917 }
918
919 if (Field2 != Field2End) {
920 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
921 << Context.C2.getTypeDeclType(D2);
922 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
923 << Field2->getDeclName() << Field2->getType();
924 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
925 return false;
926 }
927
928 return true;
929}
930
931/// \brief Determine structural equivalence of two enums.
932static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
933 EnumDecl *D1, EnumDecl *D2) {
934 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
935 EC2End = D2->enumerator_end();
936 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
937 EC1End = D1->enumerator_end();
938 EC1 != EC1End; ++EC1, ++EC2) {
939 if (EC2 == EC2End) {
940 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
941 << Context.C2.getTypeDeclType(D2);
942 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
943 << EC1->getDeclName()
944 << EC1->getInitVal().toString(10);
945 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
946 return false;
947 }
948
949 llvm::APSInt Val1 = EC1->getInitVal();
950 llvm::APSInt Val2 = EC2->getInitVal();
951 if (!IsSameValue(Val1, Val2) ||
952 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
953 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
954 << Context.C2.getTypeDeclType(D2);
955 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
956 << EC2->getDeclName()
957 << EC2->getInitVal().toString(10);
958 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
959 << EC1->getDeclName()
960 << EC1->getInitVal().toString(10);
961 return false;
962 }
963 }
964
965 if (EC2 != EC2End) {
966 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
967 << Context.C2.getTypeDeclType(D2);
968 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
969 << EC2->getDeclName()
970 << EC2->getInitVal().toString(10);
971 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
972 return false;
973 }
974
975 return true;
976}
Douglas Gregora082a492010-11-30 19:14:50 +0000977
978static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
979 TemplateParameterList *Params1,
980 TemplateParameterList *Params2) {
981 if (Params1->size() != Params2->size()) {
982 Context.Diag2(Params2->getTemplateLoc(),
983 diag::err_odr_different_num_template_parameters)
984 << Params1->size() << Params2->size();
985 Context.Diag1(Params1->getTemplateLoc(),
986 diag::note_odr_template_parameter_list);
987 return false;
988 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000989
Douglas Gregora082a492010-11-30 19:14:50 +0000990 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
991 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
992 Context.Diag2(Params2->getParam(I)->getLocation(),
993 diag::err_odr_different_template_parameter_kind);
994 Context.Diag1(Params1->getParam(I)->getLocation(),
995 diag::note_odr_template_parameter_here);
996 return false;
997 }
998
999 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1000 Params2->getParam(I))) {
1001
1002 return false;
1003 }
1004 }
1005
1006 return true;
1007}
1008
1009static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1010 TemplateTypeParmDecl *D1,
1011 TemplateTypeParmDecl *D2) {
1012 if (D1->isParameterPack() != D2->isParameterPack()) {
1013 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1014 << D2->isParameterPack();
1015 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1016 << D1->isParameterPack();
1017 return false;
1018 }
1019
1020 return true;
1021}
1022
1023static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1024 NonTypeTemplateParmDecl *D1,
1025 NonTypeTemplateParmDecl *D2) {
1026 // FIXME: Enable once we have variadic templates.
1027#if 0
1028 if (D1->isParameterPack() != D2->isParameterPack()) {
1029 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1030 << D2->isParameterPack();
1031 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1032 << D1->isParameterPack();
1033 return false;
1034 }
1035#endif
1036
1037 // Check types.
1038 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1039 Context.Diag2(D2->getLocation(),
1040 diag::err_odr_non_type_parameter_type_inconsistent)
1041 << D2->getType() << D1->getType();
1042 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1043 << D1->getType();
1044 return false;
1045 }
1046
1047 return true;
1048}
1049
1050static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1051 TemplateTemplateParmDecl *D1,
1052 TemplateTemplateParmDecl *D2) {
1053 // FIXME: Enable once we have variadic templates.
1054#if 0
1055 if (D1->isParameterPack() != D2->isParameterPack()) {
1056 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1057 << D2->isParameterPack();
1058 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1059 << D1->isParameterPack();
1060 return false;
1061 }
1062#endif
1063
1064 // Check template parameter lists.
1065 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1066 D2->getTemplateParameters());
1067}
1068
1069static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1070 ClassTemplateDecl *D1,
1071 ClassTemplateDecl *D2) {
1072 // Check template parameters.
1073 if (!IsStructurallyEquivalent(Context,
1074 D1->getTemplateParameters(),
1075 D2->getTemplateParameters()))
1076 return false;
1077
1078 // Check the templated declaration.
1079 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1080 D2->getTemplatedDecl());
1081}
1082
Douglas Gregor3996e242010-02-15 22:01:00 +00001083/// \brief Determine structural equivalence of two declarations.
1084static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1085 Decl *D1, Decl *D2) {
1086 // FIXME: Check for known structural equivalences via a callback of some sort.
1087
Douglas Gregorb4964f72010-02-15 23:54:17 +00001088 // Check whether we already know that these two declarations are not
1089 // structurally equivalent.
1090 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1091 D2->getCanonicalDecl())))
1092 return false;
1093
Douglas Gregor3996e242010-02-15 22:01:00 +00001094 // Determine whether we've already produced a tentative equivalence for D1.
1095 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1096 if (EquivToD1)
1097 return EquivToD1 == D2->getCanonicalDecl();
1098
1099 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1100 EquivToD1 = D2->getCanonicalDecl();
1101 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1102 return true;
1103}
1104
1105bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1106 Decl *D2) {
1107 if (!::IsStructurallyEquivalent(*this, D1, D2))
1108 return false;
1109
1110 return !Finish();
1111}
1112
1113bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1114 QualType T2) {
1115 if (!::IsStructurallyEquivalent(*this, T1, T2))
1116 return false;
1117
1118 return !Finish();
1119}
1120
1121bool StructuralEquivalenceContext::Finish() {
1122 while (!DeclsToCheck.empty()) {
1123 // Check the next declaration.
1124 Decl *D1 = DeclsToCheck.front();
1125 DeclsToCheck.pop_front();
1126
1127 Decl *D2 = TentativeEquivalences[D1];
1128 assert(D2 && "Unrecorded tentative equivalence?");
1129
Douglas Gregorb4964f72010-02-15 23:54:17 +00001130 bool Equivalent = true;
1131
Douglas Gregor3996e242010-02-15 22:01:00 +00001132 // FIXME: Switch on all declaration kinds. For now, we're just going to
1133 // check the obvious ones.
1134 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1135 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1136 // Check for equivalent structure names.
1137 IdentifierInfo *Name1 = Record1->getIdentifier();
1138 if (!Name1 && Record1->getTypedefForAnonDecl())
1139 Name1 = Record1->getTypedefForAnonDecl()->getIdentifier();
1140 IdentifierInfo *Name2 = Record2->getIdentifier();
1141 if (!Name2 && Record2->getTypedefForAnonDecl())
1142 Name2 = Record2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001143 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1144 !::IsStructurallyEquivalent(*this, Record1, Record2))
1145 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001146 } else {
1147 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001148 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001149 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001150 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001151 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1152 // Check for equivalent enum names.
1153 IdentifierInfo *Name1 = Enum1->getIdentifier();
1154 if (!Name1 && Enum1->getTypedefForAnonDecl())
1155 Name1 = Enum1->getTypedefForAnonDecl()->getIdentifier();
1156 IdentifierInfo *Name2 = Enum2->getIdentifier();
1157 if (!Name2 && Enum2->getTypedefForAnonDecl())
1158 Name2 = Enum2->getTypedefForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001159 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1160 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1161 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001162 } else {
1163 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001164 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001165 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001166 } else if (TypedefDecl *Typedef1 = dyn_cast<TypedefDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001167 if (TypedefDecl *Typedef2 = dyn_cast<TypedefDecl>(D2)) {
1168 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001169 Typedef2->getIdentifier()) ||
1170 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 Typedef1->getUnderlyingType(),
1172 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001173 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001174 } else {
1175 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001176 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001177 }
Douglas Gregora082a492010-11-30 19:14:50 +00001178 } else if (ClassTemplateDecl *ClassTemplate1
1179 = dyn_cast<ClassTemplateDecl>(D1)) {
1180 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1181 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1182 ClassTemplate2->getIdentifier()) ||
1183 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1184 Equivalent = false;
1185 } else {
1186 // Class template/non-class-template mismatch.
1187 Equivalent = false;
1188 }
1189 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1190 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1191 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1192 Equivalent = false;
1193 } else {
1194 // Kind mismatch.
1195 Equivalent = false;
1196 }
1197 } else if (NonTypeTemplateParmDecl *NTTP1
1198 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1199 if (NonTypeTemplateParmDecl *NTTP2
1200 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1201 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1202 Equivalent = false;
1203 } else {
1204 // Kind mismatch.
1205 Equivalent = false;
1206 }
1207 } else if (TemplateTemplateParmDecl *TTP1
1208 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1209 if (TemplateTemplateParmDecl *TTP2
1210 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1211 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1212 Equivalent = false;
1213 } else {
1214 // Kind mismatch.
1215 Equivalent = false;
1216 }
1217 }
1218
Douglas Gregorb4964f72010-02-15 23:54:17 +00001219 if (!Equivalent) {
1220 // Note that these two declarations are not equivalent (and we already
1221 // know about it).
1222 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1223 D2->getCanonicalDecl()));
1224 return true;
1225 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001226 // FIXME: Check other declaration kinds!
1227 }
1228
1229 return false;
1230}
1231
1232//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001233// Import Types
1234//----------------------------------------------------------------------------
1235
Douglas Gregore4c83e42010-02-09 22:48:33 +00001236QualType ASTNodeImporter::VisitType(Type *T) {
1237 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1238 << T->getTypeClassName();
1239 return QualType();
1240}
1241
Douglas Gregor96e578d2010-02-05 17:54:41 +00001242QualType ASTNodeImporter::VisitBuiltinType(BuiltinType *T) {
1243 switch (T->getKind()) {
1244 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1245 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1246
1247 case BuiltinType::Char_U:
1248 // The context we're importing from has an unsigned 'char'. If we're
1249 // importing into a context with a signed 'char', translate to
1250 // 'unsigned char' instead.
1251 if (Importer.getToContext().getLangOptions().CharIsSigned)
1252 return Importer.getToContext().UnsignedCharTy;
1253
1254 return Importer.getToContext().CharTy;
1255
1256 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1257
1258 case BuiltinType::Char16:
1259 // FIXME: Make sure that the "to" context supports C++!
1260 return Importer.getToContext().Char16Ty;
1261
1262 case BuiltinType::Char32:
1263 // FIXME: Make sure that the "to" context supports C++!
1264 return Importer.getToContext().Char32Ty;
1265
1266 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1267 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1268 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1269 case BuiltinType::ULongLong:
1270 return Importer.getToContext().UnsignedLongLongTy;
1271 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1272
1273 case BuiltinType::Char_S:
1274 // The context we're importing from has an unsigned 'char'. If we're
1275 // importing into a context with a signed 'char', translate to
1276 // 'unsigned char' instead.
1277 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1278 return Importer.getToContext().SignedCharTy;
1279
1280 return Importer.getToContext().CharTy;
1281
1282 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
1283 case BuiltinType::WChar:
1284 // FIXME: If not in C++, shall we translate to the C equivalent of
1285 // wchar_t?
1286 return Importer.getToContext().WCharTy;
1287
1288 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1289 case BuiltinType::Int : return Importer.getToContext().IntTy;
1290 case BuiltinType::Long : return Importer.getToContext().LongTy;
1291 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1292 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1293 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1294 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1295 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1296
1297 case BuiltinType::NullPtr:
1298 // FIXME: Make sure that the "to" context supports C++0x!
1299 return Importer.getToContext().NullPtrTy;
1300
1301 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1302 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
1303 case BuiltinType::UndeducedAuto:
1304 // FIXME: Make sure that the "to" context supports C++0x!
1305 return Importer.getToContext().UndeducedAutoTy;
1306
1307 case BuiltinType::ObjCId:
1308 // FIXME: Make sure that the "to" context supports Objective-C!
1309 return Importer.getToContext().ObjCBuiltinIdTy;
1310
1311 case BuiltinType::ObjCClass:
1312 return Importer.getToContext().ObjCBuiltinClassTy;
1313
1314 case BuiltinType::ObjCSel:
1315 return Importer.getToContext().ObjCBuiltinSelTy;
1316 }
1317
1318 return QualType();
1319}
1320
1321QualType ASTNodeImporter::VisitComplexType(ComplexType *T) {
1322 QualType ToElementType = Importer.Import(T->getElementType());
1323 if (ToElementType.isNull())
1324 return QualType();
1325
1326 return Importer.getToContext().getComplexType(ToElementType);
1327}
1328
1329QualType ASTNodeImporter::VisitPointerType(PointerType *T) {
1330 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1331 if (ToPointeeType.isNull())
1332 return QualType();
1333
1334 return Importer.getToContext().getPointerType(ToPointeeType);
1335}
1336
1337QualType ASTNodeImporter::VisitBlockPointerType(BlockPointerType *T) {
1338 // FIXME: Check for blocks support in "to" context.
1339 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1340 if (ToPointeeType.isNull())
1341 return QualType();
1342
1343 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1344}
1345
1346QualType ASTNodeImporter::VisitLValueReferenceType(LValueReferenceType *T) {
1347 // FIXME: Check for C++ support in "to" context.
1348 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1349 if (ToPointeeType.isNull())
1350 return QualType();
1351
1352 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1353}
1354
1355QualType ASTNodeImporter::VisitRValueReferenceType(RValueReferenceType *T) {
1356 // FIXME: Check for C++0x support in "to" context.
1357 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1358 if (ToPointeeType.isNull())
1359 return QualType();
1360
1361 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1362}
1363
1364QualType ASTNodeImporter::VisitMemberPointerType(MemberPointerType *T) {
1365 // FIXME: Check for C++ support in "to" context.
1366 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1367 if (ToPointeeType.isNull())
1368 return QualType();
1369
1370 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1371 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1372 ClassType.getTypePtr());
1373}
1374
1375QualType ASTNodeImporter::VisitConstantArrayType(ConstantArrayType *T) {
1376 QualType ToElementType = Importer.Import(T->getElementType());
1377 if (ToElementType.isNull())
1378 return QualType();
1379
1380 return Importer.getToContext().getConstantArrayType(ToElementType,
1381 T->getSize(),
1382 T->getSizeModifier(),
1383 T->getIndexTypeCVRQualifiers());
1384}
1385
1386QualType ASTNodeImporter::VisitIncompleteArrayType(IncompleteArrayType *T) {
1387 QualType ToElementType = Importer.Import(T->getElementType());
1388 if (ToElementType.isNull())
1389 return QualType();
1390
1391 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1392 T->getSizeModifier(),
1393 T->getIndexTypeCVRQualifiers());
1394}
1395
1396QualType ASTNodeImporter::VisitVariableArrayType(VariableArrayType *T) {
1397 QualType ToElementType = Importer.Import(T->getElementType());
1398 if (ToElementType.isNull())
1399 return QualType();
1400
1401 Expr *Size = Importer.Import(T->getSizeExpr());
1402 if (!Size)
1403 return QualType();
1404
1405 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1406 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1407 T->getSizeModifier(),
1408 T->getIndexTypeCVRQualifiers(),
1409 Brackets);
1410}
1411
1412QualType ASTNodeImporter::VisitVectorType(VectorType *T) {
1413 QualType ToElementType = Importer.Import(T->getElementType());
1414 if (ToElementType.isNull())
1415 return QualType();
1416
1417 return Importer.getToContext().getVectorType(ToElementType,
1418 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001419 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001420}
1421
1422QualType ASTNodeImporter::VisitExtVectorType(ExtVectorType *T) {
1423 QualType ToElementType = Importer.Import(T->getElementType());
1424 if (ToElementType.isNull())
1425 return QualType();
1426
1427 return Importer.getToContext().getExtVectorType(ToElementType,
1428 T->getNumElements());
1429}
1430
1431QualType ASTNodeImporter::VisitFunctionNoProtoType(FunctionNoProtoType *T) {
1432 // FIXME: What happens if we're importing a function without a prototype
1433 // into C++? Should we make it variadic?
1434 QualType ToResultType = Importer.Import(T->getResultType());
1435 if (ToResultType.isNull())
1436 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001437
Douglas Gregor96e578d2010-02-05 17:54:41 +00001438 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001439 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001440}
1441
1442QualType ASTNodeImporter::VisitFunctionProtoType(FunctionProtoType *T) {
1443 QualType ToResultType = Importer.Import(T->getResultType());
1444 if (ToResultType.isNull())
1445 return QualType();
1446
1447 // Import argument types
1448 llvm::SmallVector<QualType, 4> ArgTypes;
1449 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1450 AEnd = T->arg_type_end();
1451 A != AEnd; ++A) {
1452 QualType ArgType = Importer.Import(*A);
1453 if (ArgType.isNull())
1454 return QualType();
1455 ArgTypes.push_back(ArgType);
1456 }
1457
1458 // Import exception types
1459 llvm::SmallVector<QualType, 4> ExceptionTypes;
1460 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1461 EEnd = T->exception_end();
1462 E != EEnd; ++E) {
1463 QualType ExceptionType = Importer.Import(*E);
1464 if (ExceptionType.isNull())
1465 return QualType();
1466 ExceptionTypes.push_back(ExceptionType);
1467 }
1468
1469 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
1470 ArgTypes.size(),
1471 T->isVariadic(),
1472 T->getTypeQuals(),
1473 T->hasExceptionSpec(),
1474 T->hasAnyExceptionSpec(),
1475 ExceptionTypes.size(),
1476 ExceptionTypes.data(),
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001477 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001478}
1479
1480QualType ASTNodeImporter::VisitTypedefType(TypedefType *T) {
1481 TypedefDecl *ToDecl
1482 = dyn_cast_or_null<TypedefDecl>(Importer.Import(T->getDecl()));
1483 if (!ToDecl)
1484 return QualType();
1485
1486 return Importer.getToContext().getTypeDeclType(ToDecl);
1487}
1488
1489QualType ASTNodeImporter::VisitTypeOfExprType(TypeOfExprType *T) {
1490 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1491 if (!ToExpr)
1492 return QualType();
1493
1494 return Importer.getToContext().getTypeOfExprType(ToExpr);
1495}
1496
1497QualType ASTNodeImporter::VisitTypeOfType(TypeOfType *T) {
1498 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1499 if (ToUnderlyingType.isNull())
1500 return QualType();
1501
1502 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1503}
1504
1505QualType ASTNodeImporter::VisitDecltypeType(DecltypeType *T) {
1506 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1507 if (!ToExpr)
1508 return QualType();
1509
1510 return Importer.getToContext().getDecltypeType(ToExpr);
1511}
1512
1513QualType ASTNodeImporter::VisitRecordType(RecordType *T) {
1514 RecordDecl *ToDecl
1515 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1516 if (!ToDecl)
1517 return QualType();
1518
1519 return Importer.getToContext().getTagDeclType(ToDecl);
1520}
1521
1522QualType ASTNodeImporter::VisitEnumType(EnumType *T) {
1523 EnumDecl *ToDecl
1524 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1525 if (!ToDecl)
1526 return QualType();
1527
1528 return Importer.getToContext().getTagDeclType(ToDecl);
1529}
1530
Douglas Gregore2e50d332010-12-01 01:36:18 +00001531QualType ASTNodeImporter::VisitTemplateSpecializationType(
1532 TemplateSpecializationType *T) {
1533 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1534 if (ToTemplate.isNull())
1535 return QualType();
1536
1537 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1538 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1539 return QualType();
1540
1541 QualType ToCanonType;
1542 if (!QualType(T, 0).isCanonical()) {
1543 QualType FromCanonType
1544 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1545 ToCanonType =Importer.Import(FromCanonType);
1546 if (ToCanonType.isNull())
1547 return QualType();
1548 }
1549 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1550 ToTemplateArgs.data(),
1551 ToTemplateArgs.size(),
1552 ToCanonType);
1553}
1554
Douglas Gregor96e578d2010-02-05 17:54:41 +00001555QualType ASTNodeImporter::VisitElaboratedType(ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001556 NestedNameSpecifier *ToQualifier = 0;
1557 // Note: the qualifier in an ElaboratedType is optional.
1558 if (T->getQualifier()) {
1559 ToQualifier = Importer.Import(T->getQualifier());
1560 if (!ToQualifier)
1561 return QualType();
1562 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001563
1564 QualType ToNamedType = Importer.Import(T->getNamedType());
1565 if (ToNamedType.isNull())
1566 return QualType();
1567
Abramo Bagnara6150c882010-05-11 21:36:43 +00001568 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1569 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001570}
1571
1572QualType ASTNodeImporter::VisitObjCInterfaceType(ObjCInterfaceType *T) {
1573 ObjCInterfaceDecl *Class
1574 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1575 if (!Class)
1576 return QualType();
1577
John McCall8b07ec22010-05-15 11:32:37 +00001578 return Importer.getToContext().getObjCInterfaceType(Class);
1579}
1580
1581QualType ASTNodeImporter::VisitObjCObjectType(ObjCObjectType *T) {
1582 QualType ToBaseType = Importer.Import(T->getBaseType());
1583 if (ToBaseType.isNull())
1584 return QualType();
1585
Douglas Gregor96e578d2010-02-05 17:54:41 +00001586 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001587 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 PEnd = T->qual_end();
1589 P != PEnd; ++P) {
1590 ObjCProtocolDecl *Protocol
1591 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1592 if (!Protocol)
1593 return QualType();
1594 Protocols.push_back(Protocol);
1595 }
1596
John McCall8b07ec22010-05-15 11:32:37 +00001597 return Importer.getToContext().getObjCObjectType(ToBaseType,
1598 Protocols.data(),
1599 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001600}
1601
1602QualType ASTNodeImporter::VisitObjCObjectPointerType(ObjCObjectPointerType *T) {
1603 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1604 if (ToPointeeType.isNull())
1605 return QualType();
1606
John McCall8b07ec22010-05-15 11:32:37 +00001607 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001608}
1609
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001610//----------------------------------------------------------------------------
1611// Import Declarations
1612//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001613bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1614 DeclContext *&LexicalDC,
1615 DeclarationName &Name,
1616 SourceLocation &Loc) {
1617 // Import the context of this declaration.
1618 DC = Importer.ImportContext(D->getDeclContext());
1619 if (!DC)
1620 return true;
1621
1622 LexicalDC = DC;
1623 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1624 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1625 if (!LexicalDC)
1626 return true;
1627 }
1628
1629 // Import the name of this declaration.
1630 Name = Importer.Import(D->getDeclName());
1631 if (D->getDeclName() && !Name)
1632 return true;
1633
1634 // Import the location of this declaration.
1635 Loc = Importer.Import(D->getLocation());
1636 return false;
1637}
1638
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001639void
1640ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1641 DeclarationNameInfo& To) {
1642 // NOTE: To.Name and To.Loc are already imported.
1643 // We only have to import To.LocInfo.
1644 switch (To.getName().getNameKind()) {
1645 case DeclarationName::Identifier:
1646 case DeclarationName::ObjCZeroArgSelector:
1647 case DeclarationName::ObjCOneArgSelector:
1648 case DeclarationName::ObjCMultiArgSelector:
1649 case DeclarationName::CXXUsingDirective:
1650 return;
1651
1652 case DeclarationName::CXXOperatorName: {
1653 SourceRange Range = From.getCXXOperatorNameRange();
1654 To.setCXXOperatorNameRange(Importer.Import(Range));
1655 return;
1656 }
1657 case DeclarationName::CXXLiteralOperatorName: {
1658 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1659 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1660 return;
1661 }
1662 case DeclarationName::CXXConstructorName:
1663 case DeclarationName::CXXDestructorName:
1664 case DeclarationName::CXXConversionFunctionName: {
1665 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1666 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1667 return;
1668 }
1669 assert(0 && "Unknown name kind.");
1670 }
1671}
1672
Douglas Gregor968d6332010-02-21 18:24:45 +00001673void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC) {
1674 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1675 FromEnd = FromDC->decls_end();
1676 From != FromEnd;
1677 ++From)
1678 Importer.Import(*From);
1679}
1680
Douglas Gregore2e50d332010-12-01 01:36:18 +00001681bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1682 if (To->getDefinition())
1683 return false;
1684
1685 To->startDefinition();
1686
1687 // Add base classes.
1688 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1689 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1690
1691 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1692 for (CXXRecordDecl::base_class_iterator
1693 Base1 = FromCXX->bases_begin(),
1694 FromBaseEnd = FromCXX->bases_end();
1695 Base1 != FromBaseEnd;
1696 ++Base1) {
1697 QualType T = Importer.Import(Base1->getType());
1698 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001699 return true;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001700
1701 Bases.push_back(
1702 new (Importer.getToContext())
1703 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1704 Base1->isVirtual(),
1705 Base1->isBaseOfClass(),
1706 Base1->getAccessSpecifierAsWritten(),
1707 Importer.Import(Base1->getTypeSourceInfo())));
1708 }
1709 if (!Bases.empty())
1710 ToCXX->setBases(Bases.data(), Bases.size());
1711 }
1712
1713 ImportDeclContext(From);
1714 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001715 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001716}
1717
Douglas Gregora082a492010-11-30 19:14:50 +00001718TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1719 TemplateParameterList *Params) {
1720 llvm::SmallVector<NamedDecl *, 4> ToParams;
1721 ToParams.reserve(Params->size());
1722 for (TemplateParameterList::iterator P = Params->begin(),
1723 PEnd = Params->end();
1724 P != PEnd; ++P) {
1725 Decl *To = Importer.Import(*P);
1726 if (!To)
1727 return 0;
1728
1729 ToParams.push_back(cast<NamedDecl>(To));
1730 }
1731
1732 return TemplateParameterList::Create(Importer.getToContext(),
1733 Importer.Import(Params->getTemplateLoc()),
1734 Importer.Import(Params->getLAngleLoc()),
1735 ToParams.data(), ToParams.size(),
1736 Importer.Import(Params->getRAngleLoc()));
1737}
1738
Douglas Gregore2e50d332010-12-01 01:36:18 +00001739TemplateArgument
1740ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1741 switch (From.getKind()) {
1742 case TemplateArgument::Null:
1743 return TemplateArgument();
1744
1745 case TemplateArgument::Type: {
1746 QualType ToType = Importer.Import(From.getAsType());
1747 if (ToType.isNull())
1748 return TemplateArgument();
1749 return TemplateArgument(ToType);
1750 }
1751
1752 case TemplateArgument::Integral: {
1753 QualType ToType = Importer.Import(From.getIntegralType());
1754 if (ToType.isNull())
1755 return TemplateArgument();
1756 return TemplateArgument(*From.getAsIntegral(), ToType);
1757 }
1758
1759 case TemplateArgument::Declaration:
1760 if (Decl *To = Importer.Import(From.getAsDecl()))
1761 return TemplateArgument(To);
1762 return TemplateArgument();
1763
1764 case TemplateArgument::Template: {
1765 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1766 if (ToTemplate.isNull())
1767 return TemplateArgument();
1768
1769 return TemplateArgument(ToTemplate);
1770 }
1771
1772 case TemplateArgument::Expression:
1773 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1774 return TemplateArgument(ToExpr);
1775 return TemplateArgument();
1776
1777 case TemplateArgument::Pack: {
1778 llvm::SmallVector<TemplateArgument, 2> ToPack;
1779 ToPack.reserve(From.pack_size());
1780 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1781 return TemplateArgument();
1782
1783 TemplateArgument *ToArgs
1784 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1785 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1786 return TemplateArgument(ToArgs, ToPack.size());
1787 }
1788 }
1789
1790 llvm_unreachable("Invalid template argument kind");
1791 return TemplateArgument();
1792}
1793
1794bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1795 unsigned NumFromArgs,
1796 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1797 for (unsigned I = 0; I != NumFromArgs; ++I) {
1798 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1799 if (To.isNull() && !FromArgs[I].isNull())
1800 return true;
1801
1802 ToArgs.push_back(To);
1803 }
1804
1805 return false;
1806}
1807
Douglas Gregor5c73e912010-02-11 00:48:18 +00001808bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001809 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001810 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001811 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001812 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001813 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001814}
1815
Douglas Gregor98c10182010-02-12 22:17:39 +00001816bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001817 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001818 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001819 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001820 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001821}
1822
Douglas Gregora082a492010-11-30 19:14:50 +00001823bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1824 ClassTemplateDecl *To) {
1825 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1826 Importer.getToContext(),
1827 Importer.getNonEquivalentDecls());
1828 return Ctx.IsStructurallyEquivalent(From, To);
1829}
1830
Douglas Gregore4c83e42010-02-09 22:48:33 +00001831Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001832 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001833 << D->getDeclKindName();
1834 return 0;
1835}
1836
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001837Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1838 // Import the major distinguishing characteristics of this namespace.
1839 DeclContext *DC, *LexicalDC;
1840 DeclarationName Name;
1841 SourceLocation Loc;
1842 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1843 return 0;
1844
1845 NamespaceDecl *MergeWithNamespace = 0;
1846 if (!Name) {
1847 // This is an anonymous namespace. Adopt an existing anonymous
1848 // namespace if we can.
1849 // FIXME: Not testable.
1850 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1851 MergeWithNamespace = TU->getAnonymousNamespace();
1852 else
1853 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1854 } else {
1855 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1856 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1857 Lookup.first != Lookup.second;
1858 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001859 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001860 continue;
1861
1862 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1863 MergeWithNamespace = FoundNS;
1864 ConflictingDecls.clear();
1865 break;
1866 }
1867
1868 ConflictingDecls.push_back(*Lookup.first);
1869 }
1870
1871 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001872 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001873 ConflictingDecls.data(),
1874 ConflictingDecls.size());
1875 }
1876 }
1877
1878 // Create the "to" namespace, if needed.
1879 NamespaceDecl *ToNamespace = MergeWithNamespace;
1880 if (!ToNamespace) {
1881 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC, Loc,
1882 Name.getAsIdentifierInfo());
1883 ToNamespace->setLexicalDeclContext(LexicalDC);
1884 LexicalDC->addDecl(ToNamespace);
1885
1886 // If this is an anonymous namespace, register it as the anonymous
1887 // namespace within its context.
1888 if (!Name) {
1889 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1890 TU->setAnonymousNamespace(ToNamespace);
1891 else
1892 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1893 }
1894 }
1895 Importer.Imported(D, ToNamespace);
1896
1897 ImportDeclContext(D);
1898
1899 return ToNamespace;
1900}
1901
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001902Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
1903 // Import the major distinguishing characteristics of this typedef.
1904 DeclContext *DC, *LexicalDC;
1905 DeclarationName Name;
1906 SourceLocation Loc;
1907 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1908 return 0;
1909
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001910 // If this typedef is not in block scope, determine whether we've
1911 // seen a typedef with the same name (that we can merge with) or any
1912 // other entity by that name (which name lookup could conflict with).
1913 if (!DC->isFunctionOrMethod()) {
1914 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1915 unsigned IDNS = Decl::IDNS_Ordinary;
1916 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1917 Lookup.first != Lookup.second;
1918 ++Lookup.first) {
1919 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1920 continue;
1921 if (TypedefDecl *FoundTypedef = dyn_cast<TypedefDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00001922 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
1923 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001924 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001925 }
1926
1927 ConflictingDecls.push_back(*Lookup.first);
1928 }
1929
1930 if (!ConflictingDecls.empty()) {
1931 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1932 ConflictingDecls.data(),
1933 ConflictingDecls.size());
1934 if (!Name)
1935 return 0;
1936 }
1937 }
1938
Douglas Gregorb4964f72010-02-15 23:54:17 +00001939 // Import the underlying type of this typedef;
1940 QualType T = Importer.Import(D->getUnderlyingType());
1941 if (T.isNull())
1942 return 0;
1943
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001944 // Create the new typedef node.
1945 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
1946 TypedefDecl *ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
1947 Loc, Name.getAsIdentifierInfo(),
1948 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00001949 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001950 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001951 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001952 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00001953
Douglas Gregor5fa74c32010-02-10 21:10:29 +00001954 return ToTypedef;
1955}
1956
Douglas Gregor98c10182010-02-12 22:17:39 +00001957Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
1958 // Import the major distinguishing characteristics of this enum.
1959 DeclContext *DC, *LexicalDC;
1960 DeclarationName Name;
1961 SourceLocation Loc;
1962 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1963 return 0;
1964
1965 // Figure out what enum name we're looking for.
1966 unsigned IDNS = Decl::IDNS_Tag;
1967 DeclarationName SearchName = Name;
1968 if (!SearchName && D->getTypedefForAnonDecl()) {
1969 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
1970 IDNS = Decl::IDNS_Ordinary;
1971 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
1972 IDNS |= Decl::IDNS_Ordinary;
1973
1974 // We may already have an enum of the same name; try to find and match it.
1975 if (!DC->isFunctionOrMethod() && SearchName) {
1976 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1977 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1978 Lookup.first != Lookup.second;
1979 ++Lookup.first) {
1980 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
1981 continue;
1982
1983 Decl *Found = *Lookup.first;
1984 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
1985 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
1986 Found = Tag->getDecl();
1987 }
1988
1989 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00001990 if (IsStructuralMatch(D, FoundEnum))
1991 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001992 }
1993
1994 ConflictingDecls.push_back(*Lookup.first);
1995 }
1996
1997 if (!ConflictingDecls.empty()) {
1998 Name = Importer.HandleNameConflict(Name, DC, IDNS,
1999 ConflictingDecls.data(),
2000 ConflictingDecls.size());
2001 }
2002 }
2003
2004 // Create the enum declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002005 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC, Loc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002006 Name.getAsIdentifierInfo(),
2007 Importer.Import(D->getTagKeywordLoc()), 0,
2008 D->isScoped(), D->isScopedUsingClassTag(),
2009 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002010 // Import the qualifier, if any.
2011 if (D->getQualifier()) {
2012 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2013 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2014 D2->setQualifierInfo(NNS, NNSRange);
2015 }
Douglas Gregordd483172010-02-22 17:42:47 +00002016 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002017 D2->setLexicalDeclContext(LexicalDC);
2018 Importer.Imported(D, D2);
2019 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002020
2021 // Import the integer type.
2022 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2023 if (ToIntegerType.isNull())
2024 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002025 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002026
2027 // Import the definition
2028 if (D->isDefinition()) {
2029 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2030 if (T.isNull())
2031 return 0;
2032
2033 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2034 if (ToPromotionType.isNull())
2035 return 0;
2036
Douglas Gregor3996e242010-02-15 22:01:00 +00002037 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002038 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002039
2040 // FIXME: we might need to merge the number of positive or negative bits
2041 // if the enumerator lists don't match.
2042 D2->completeDefinition(T, ToPromotionType,
2043 D->getNumPositiveBits(),
2044 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002045 }
2046
Douglas Gregor3996e242010-02-15 22:01:00 +00002047 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002048}
2049
Douglas Gregor5c73e912010-02-11 00:48:18 +00002050Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2051 // If this record has a definition in the translation unit we're coming from,
2052 // but this particular declaration is not that definition, import the
2053 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002054 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002055 if (Definition && Definition != D) {
2056 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002057 if (!ImportedDef)
2058 return 0;
2059
2060 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002061 }
2062
2063 // Import the major distinguishing characteristics of this record.
2064 DeclContext *DC, *LexicalDC;
2065 DeclarationName Name;
2066 SourceLocation Loc;
2067 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2068 return 0;
2069
2070 // Figure out what structure name we're looking for.
2071 unsigned IDNS = Decl::IDNS_Tag;
2072 DeclarationName SearchName = Name;
2073 if (!SearchName && D->getTypedefForAnonDecl()) {
2074 SearchName = Importer.Import(D->getTypedefForAnonDecl()->getDeclName());
2075 IDNS = Decl::IDNS_Ordinary;
2076 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2077 IDNS |= Decl::IDNS_Ordinary;
2078
2079 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002080 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002081 if (!DC->isFunctionOrMethod() && SearchName) {
2082 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2083 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2084 Lookup.first != Lookup.second;
2085 ++Lookup.first) {
2086 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2087 continue;
2088
2089 Decl *Found = *Lookup.first;
2090 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Found)) {
2091 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2092 Found = Tag->getDecl();
2093 }
2094
2095 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002096 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2097 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2098 // The record types structurally match, or the "from" translation
2099 // unit only had a forward declaration anyway; call it the same
2100 // function.
2101 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002102 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002103 }
2104 } else {
2105 // We have a forward declaration of this type, so adopt that forward
2106 // declaration rather than building a new one.
2107 AdoptDecl = FoundRecord;
2108 continue;
2109 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002110 }
2111
2112 ConflictingDecls.push_back(*Lookup.first);
2113 }
2114
2115 if (!ConflictingDecls.empty()) {
2116 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2117 ConflictingDecls.data(),
2118 ConflictingDecls.size());
2119 }
2120 }
2121
2122 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002123 RecordDecl *D2 = AdoptDecl;
2124 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002125 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002126 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002127 D->getTagKind(),
2128 DC, Loc,
2129 Name.getAsIdentifierInfo(),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002130 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002131 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002132 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002133 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002134 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Douglas Gregor25791052010-02-12 00:09:27 +00002135 DC, Loc,
2136 Name.getAsIdentifierInfo(),
2137 Importer.Import(D->getTagKeywordLoc()));
Douglas Gregor5c73e912010-02-11 00:48:18 +00002138 }
John McCall3e11ebe2010-03-15 10:12:16 +00002139 // Import the qualifier, if any.
2140 if (D->getQualifier()) {
2141 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2142 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2143 D2->setQualifierInfo(NNS, NNSRange);
2144 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002145 D2->setLexicalDeclContext(LexicalDC);
2146 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002147 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002148
Douglas Gregor3996e242010-02-15 22:01:00 +00002149 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002150
Douglas Gregore2e50d332010-12-01 01:36:18 +00002151 if (D->isDefinition() && ImportDefinition(D, D2))
2152 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002153
Douglas Gregor3996e242010-02-15 22:01:00 +00002154 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002155}
2156
Douglas Gregor98c10182010-02-12 22:17:39 +00002157Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2158 // Import the major distinguishing characteristics of this enumerator.
2159 DeclContext *DC, *LexicalDC;
2160 DeclarationName Name;
2161 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002162 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002163 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002164
2165 QualType T = Importer.Import(D->getType());
2166 if (T.isNull())
2167 return 0;
2168
Douglas Gregor98c10182010-02-12 22:17:39 +00002169 // Determine whether there are any other declarations with the same name and
2170 // in the same context.
2171 if (!LexicalDC->isFunctionOrMethod()) {
2172 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2173 unsigned IDNS = Decl::IDNS_Ordinary;
2174 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2175 Lookup.first != Lookup.second;
2176 ++Lookup.first) {
2177 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2178 continue;
2179
2180 ConflictingDecls.push_back(*Lookup.first);
2181 }
2182
2183 if (!ConflictingDecls.empty()) {
2184 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2185 ConflictingDecls.data(),
2186 ConflictingDecls.size());
2187 if (!Name)
2188 return 0;
2189 }
2190 }
2191
2192 Expr *Init = Importer.Import(D->getInitExpr());
2193 if (D->getInitExpr() && !Init)
2194 return 0;
2195
2196 EnumConstantDecl *ToEnumerator
2197 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2198 Name.getAsIdentifierInfo(), T,
2199 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002200 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002201 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002202 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002203 LexicalDC->addDecl(ToEnumerator);
2204 return ToEnumerator;
2205}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002206
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002207Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2208 // Import the major distinguishing characteristics of this function.
2209 DeclContext *DC, *LexicalDC;
2210 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002211 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002212 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002213 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002214
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002215 // Try to find a function in our own ("to") context with the same name, same
2216 // type, and in the same context as the function we're importing.
2217 if (!LexicalDC->isFunctionOrMethod()) {
2218 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2219 unsigned IDNS = Decl::IDNS_Ordinary;
2220 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2221 Lookup.first != Lookup.second;
2222 ++Lookup.first) {
2223 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2224 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002225
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002226 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2227 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2228 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002229 if (Importer.IsStructurallyEquivalent(D->getType(),
2230 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002231 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002232 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002233 }
2234
2235 // FIXME: Check for overloading more carefully, e.g., by boosting
2236 // Sema::IsOverload out to the AST library.
2237
2238 // Function overloading is okay in C++.
2239 if (Importer.getToContext().getLangOptions().CPlusPlus)
2240 continue;
2241
2242 // Complain about inconsistent function types.
2243 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002244 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002245 Importer.ToDiag(FoundFunction->getLocation(),
2246 diag::note_odr_value_here)
2247 << FoundFunction->getType();
2248 }
2249 }
2250
2251 ConflictingDecls.push_back(*Lookup.first);
2252 }
2253
2254 if (!ConflictingDecls.empty()) {
2255 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2256 ConflictingDecls.data(),
2257 ConflictingDecls.size());
2258 if (!Name)
2259 return 0;
2260 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002261 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002262
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002263 DeclarationNameInfo NameInfo(Name, Loc);
2264 // Import additional name location/type info.
2265 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2266
Douglas Gregorb4964f72010-02-15 23:54:17 +00002267 // Import the type.
2268 QualType T = Importer.Import(D->getType());
2269 if (T.isNull())
2270 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002271
2272 // Import the function parameters.
2273 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2274 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2275 P != PEnd; ++P) {
2276 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2277 if (!ToP)
2278 return 0;
2279
2280 Parameters.push_back(ToP);
2281 }
2282
2283 // Create the imported function.
2284 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002285 FunctionDecl *ToFunction = 0;
2286 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2287 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2288 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002289 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002290 FromConstructor->isExplicit(),
2291 D->isInlineSpecified(),
2292 D->isImplicit());
2293 } else if (isa<CXXDestructorDecl>(D)) {
2294 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2295 cast<CXXRecordDecl>(DC),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002296 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002297 D->isInlineSpecified(),
2298 D->isImplicit());
2299 } else if (CXXConversionDecl *FromConversion
2300 = dyn_cast<CXXConversionDecl>(D)) {
2301 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2302 cast<CXXRecordDecl>(DC),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002303 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002304 D->isInlineSpecified(),
2305 FromConversion->isExplicit());
Douglas Gregora50ad132010-11-29 16:04:58 +00002306 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2307 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2308 cast<CXXRecordDecl>(DC),
2309 NameInfo, T, TInfo,
2310 Method->isStatic(),
2311 Method->getStorageClassAsWritten(),
2312 Method->isInlineSpecified());
Douglas Gregor00eace12010-02-21 18:29:16 +00002313 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002314 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
2315 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002316 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002317 D->isInlineSpecified(),
2318 D->hasWrittenPrototype());
2319 }
John McCall3e11ebe2010-03-15 10:12:16 +00002320
2321 // Import the qualifier, if any.
2322 if (D->getQualifier()) {
2323 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2324 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2325 ToFunction->setQualifierInfo(NNS, NNSRange);
2326 }
Douglas Gregordd483172010-02-22 17:42:47 +00002327 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002328 ToFunction->setLexicalDeclContext(LexicalDC);
2329 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002330
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002331 // Set the parameters.
2332 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002333 Parameters[I]->setOwningFunction(ToFunction);
2334 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002335 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002336 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002337
2338 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002339
2340 // Add this function to the lexical context.
2341 LexicalDC->addDecl(ToFunction);
2342
Douglas Gregor43f54792010-02-17 02:12:47 +00002343 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002344}
2345
Douglas Gregor00eace12010-02-21 18:29:16 +00002346Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2347 return VisitFunctionDecl(D);
2348}
2349
2350Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2351 return VisitCXXMethodDecl(D);
2352}
2353
2354Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2355 return VisitCXXMethodDecl(D);
2356}
2357
2358Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2359 return VisitCXXMethodDecl(D);
2360}
2361
Douglas Gregor5c73e912010-02-11 00:48:18 +00002362Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2363 // Import the major distinguishing characteristics of a variable.
2364 DeclContext *DC, *LexicalDC;
2365 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002366 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002367 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2368 return 0;
2369
2370 // Import the type.
2371 QualType T = Importer.Import(D->getType());
2372 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002373 return 0;
2374
2375 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2376 Expr *BitWidth = Importer.Import(D->getBitWidth());
2377 if (!BitWidth && D->getBitWidth())
2378 return 0;
2379
2380 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2381 Loc, Name.getAsIdentifierInfo(),
2382 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002383 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002384 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002385 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002386 LexicalDC->addDecl(ToField);
2387 return ToField;
2388}
2389
Francois Pichet783dd6e2010-11-21 06:08:52 +00002390Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2391 // Import the major distinguishing characteristics of a variable.
2392 DeclContext *DC, *LexicalDC;
2393 DeclarationName Name;
2394 SourceLocation Loc;
2395 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2396 return 0;
2397
2398 // Import the type.
2399 QualType T = Importer.Import(D->getType());
2400 if (T.isNull())
2401 return 0;
2402
2403 NamedDecl **NamedChain =
2404 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2405
2406 unsigned i = 0;
2407 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2408 PE = D->chain_end(); PI != PE; ++PI) {
2409 Decl* D = Importer.Import(*PI);
2410 if (!D)
2411 return 0;
2412 NamedChain[i++] = cast<NamedDecl>(D);
2413 }
2414
2415 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2416 Importer.getToContext(), DC,
2417 Loc, Name.getAsIdentifierInfo(), T,
2418 NamedChain, D->getChainingSize());
2419 ToIndirectField->setAccess(D->getAccess());
2420 ToIndirectField->setLexicalDeclContext(LexicalDC);
2421 Importer.Imported(D, ToIndirectField);
2422 LexicalDC->addDecl(ToIndirectField);
2423 return ToIndirectField;
2424}
2425
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002426Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2427 // Import the major distinguishing characteristics of an ivar.
2428 DeclContext *DC, *LexicalDC;
2429 DeclarationName Name;
2430 SourceLocation Loc;
2431 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2432 return 0;
2433
2434 // Determine whether we've already imported this ivar
2435 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2436 Lookup.first != Lookup.second;
2437 ++Lookup.first) {
2438 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2439 if (Importer.IsStructurallyEquivalent(D->getType(),
2440 FoundIvar->getType())) {
2441 Importer.Imported(D, FoundIvar);
2442 return FoundIvar;
2443 }
2444
2445 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2446 << Name << D->getType() << FoundIvar->getType();
2447 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2448 << FoundIvar->getType();
2449 return 0;
2450 }
2451 }
2452
2453 // Import the type.
2454 QualType T = Importer.Import(D->getType());
2455 if (T.isNull())
2456 return 0;
2457
2458 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2459 Expr *BitWidth = Importer.Import(D->getBitWidth());
2460 if (!BitWidth && D->getBitWidth())
2461 return 0;
2462
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002463 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2464 cast<ObjCContainerDecl>(DC),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002465 Loc, Name.getAsIdentifierInfo(),
2466 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002467 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002468 ToIvar->setLexicalDeclContext(LexicalDC);
2469 Importer.Imported(D, ToIvar);
2470 LexicalDC->addDecl(ToIvar);
2471 return ToIvar;
2472
2473}
2474
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002475Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2476 // Import the major distinguishing characteristics of a variable.
2477 DeclContext *DC, *LexicalDC;
2478 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002479 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002480 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002481 return 0;
2482
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002483 // Try to find a variable in our own ("to") context with the same name and
2484 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002485 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002486 VarDecl *MergeWithVar = 0;
2487 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2488 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002489 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002490 Lookup.first != Lookup.second;
2491 ++Lookup.first) {
2492 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2493 continue;
2494
2495 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2496 // We have found a variable that we may need to merge with. Check it.
2497 if (isExternalLinkage(FoundVar->getLinkage()) &&
2498 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002499 if (Importer.IsStructurallyEquivalent(D->getType(),
2500 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002501 MergeWithVar = FoundVar;
2502 break;
2503 }
2504
Douglas Gregor56521c52010-02-12 17:23:39 +00002505 const ArrayType *FoundArray
2506 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2507 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002508 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002509 if (FoundArray && TArray) {
2510 if (isa<IncompleteArrayType>(FoundArray) &&
2511 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002512 // Import the type.
2513 QualType T = Importer.Import(D->getType());
2514 if (T.isNull())
2515 return 0;
2516
Douglas Gregor56521c52010-02-12 17:23:39 +00002517 FoundVar->setType(T);
2518 MergeWithVar = FoundVar;
2519 break;
2520 } else if (isa<IncompleteArrayType>(TArray) &&
2521 isa<ConstantArrayType>(FoundArray)) {
2522 MergeWithVar = FoundVar;
2523 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002524 }
2525 }
2526
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002527 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002528 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002529 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2530 << FoundVar->getType();
2531 }
2532 }
2533
2534 ConflictingDecls.push_back(*Lookup.first);
2535 }
2536
2537 if (MergeWithVar) {
2538 // An equivalent variable with external linkage has been found. Link
2539 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002540 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002541
2542 if (VarDecl *DDef = D->getDefinition()) {
2543 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2544 Importer.ToDiag(ExistingDef->getLocation(),
2545 diag::err_odr_variable_multiple_def)
2546 << Name;
2547 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2548 } else {
2549 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002550 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002551 }
2552 }
2553
2554 return MergeWithVar;
2555 }
2556
2557 if (!ConflictingDecls.empty()) {
2558 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2559 ConflictingDecls.data(),
2560 ConflictingDecls.size());
2561 if (!Name)
2562 return 0;
2563 }
2564 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002565
Douglas Gregorb4964f72010-02-15 23:54:17 +00002566 // Import the type.
2567 QualType T = Importer.Import(D->getType());
2568 if (T.isNull())
2569 return 0;
2570
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002571 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002572 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002573 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC, Loc,
2574 Name.getAsIdentifierInfo(), T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002575 D->getStorageClass(),
2576 D->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002577 // Import the qualifier, if any.
2578 if (D->getQualifier()) {
2579 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
2580 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
2581 ToVar->setQualifierInfo(NNS, NNSRange);
2582 }
Douglas Gregordd483172010-02-22 17:42:47 +00002583 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002584 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002585 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002586 LexicalDC->addDecl(ToVar);
2587
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002588 // Merge the initializer.
2589 // FIXME: Can we really import any initializer? Alternatively, we could force
2590 // ourselves to import every declaration of a variable and then only use
2591 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002592 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002593
2594 // FIXME: Other bits to merge?
2595
2596 return ToVar;
2597}
2598
Douglas Gregor8b228d72010-02-17 21:22:52 +00002599Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2600 // Parameters are created in the translation unit's context, then moved
2601 // into the function declaration's context afterward.
2602 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2603
2604 // Import the name of this declaration.
2605 DeclarationName Name = Importer.Import(D->getDeclName());
2606 if (D->getDeclName() && !Name)
2607 return 0;
2608
2609 // Import the location of this declaration.
2610 SourceLocation Loc = Importer.Import(D->getLocation());
2611
2612 // Import the parameter's type.
2613 QualType T = Importer.Import(D->getType());
2614 if (T.isNull())
2615 return 0;
2616
2617 // Create the imported parameter.
2618 ImplicitParamDecl *ToParm
2619 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2620 Loc, Name.getAsIdentifierInfo(),
2621 T);
2622 return Importer.Imported(D, ToParm);
2623}
2624
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002625Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2626 // Parameters are created in the translation unit's context, then moved
2627 // into the function declaration's context afterward.
2628 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2629
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002630 // Import the name of this declaration.
2631 DeclarationName Name = Importer.Import(D->getDeclName());
2632 if (D->getDeclName() && !Name)
2633 return 0;
2634
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002635 // Import the location of this declaration.
2636 SourceLocation Loc = Importer.Import(D->getLocation());
2637
2638 // Import the parameter's type.
2639 QualType T = Importer.Import(D->getType());
2640 if (T.isNull())
2641 return 0;
2642
2643 // Create the imported parameter.
2644 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2645 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
2646 Loc, Name.getAsIdentifierInfo(),
2647 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002648 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002649 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002650 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002651 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002652}
2653
Douglas Gregor43f54792010-02-17 02:12:47 +00002654Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2655 // Import the major distinguishing characteristics of a method.
2656 DeclContext *DC, *LexicalDC;
2657 DeclarationName Name;
2658 SourceLocation Loc;
2659 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2660 return 0;
2661
2662 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2663 Lookup.first != Lookup.second;
2664 ++Lookup.first) {
2665 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2666 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2667 continue;
2668
2669 // Check return types.
2670 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2671 FoundMethod->getResultType())) {
2672 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2673 << D->isInstanceMethod() << Name
2674 << D->getResultType() << FoundMethod->getResultType();
2675 Importer.ToDiag(FoundMethod->getLocation(),
2676 diag::note_odr_objc_method_here)
2677 << D->isInstanceMethod() << Name;
2678 return 0;
2679 }
2680
2681 // Check the number of parameters.
2682 if (D->param_size() != FoundMethod->param_size()) {
2683 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2684 << D->isInstanceMethod() << Name
2685 << D->param_size() << FoundMethod->param_size();
2686 Importer.ToDiag(FoundMethod->getLocation(),
2687 diag::note_odr_objc_method_here)
2688 << D->isInstanceMethod() << Name;
2689 return 0;
2690 }
2691
2692 // Check parameter types.
2693 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2694 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2695 P != PEnd; ++P, ++FoundP) {
2696 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2697 (*FoundP)->getType())) {
2698 Importer.FromDiag((*P)->getLocation(),
2699 diag::err_odr_objc_method_param_type_inconsistent)
2700 << D->isInstanceMethod() << Name
2701 << (*P)->getType() << (*FoundP)->getType();
2702 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2703 << (*FoundP)->getType();
2704 return 0;
2705 }
2706 }
2707
2708 // Check variadic/non-variadic.
2709 // Check the number of parameters.
2710 if (D->isVariadic() != FoundMethod->isVariadic()) {
2711 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2712 << D->isInstanceMethod() << Name;
2713 Importer.ToDiag(FoundMethod->getLocation(),
2714 diag::note_odr_objc_method_here)
2715 << D->isInstanceMethod() << Name;
2716 return 0;
2717 }
2718
2719 // FIXME: Any other bits we need to merge?
2720 return Importer.Imported(D, FoundMethod);
2721 }
2722 }
2723
2724 // Import the result type.
2725 QualType ResultTy = Importer.Import(D->getResultType());
2726 if (ResultTy.isNull())
2727 return 0;
2728
Douglas Gregor12852d92010-03-08 14:59:44 +00002729 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2730
Douglas Gregor43f54792010-02-17 02:12:47 +00002731 ObjCMethodDecl *ToMethod
2732 = ObjCMethodDecl::Create(Importer.getToContext(),
2733 Loc,
2734 Importer.Import(D->getLocEnd()),
2735 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002736 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002737 D->isInstanceMethod(),
2738 D->isVariadic(),
2739 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002740 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002741 D->getImplementationControl());
2742
2743 // FIXME: When we decide to merge method definitions, we'll need to
2744 // deal with implicit parameters.
2745
2746 // Import the parameters
2747 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2748 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2749 FromPEnd = D->param_end();
2750 FromP != FromPEnd;
2751 ++FromP) {
2752 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2753 if (!ToP)
2754 return 0;
2755
2756 ToParams.push_back(ToP);
2757 }
2758
2759 // Set the parameters.
2760 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2761 ToParams[I]->setOwningFunction(ToMethod);
2762 ToMethod->addDecl(ToParams[I]);
2763 }
2764 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002765 ToParams.data(), ToParams.size(),
2766 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002767
2768 ToMethod->setLexicalDeclContext(LexicalDC);
2769 Importer.Imported(D, ToMethod);
2770 LexicalDC->addDecl(ToMethod);
2771 return ToMethod;
2772}
2773
Douglas Gregor84c51c32010-02-18 01:47:50 +00002774Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2775 // Import the major distinguishing characteristics of a category.
2776 DeclContext *DC, *LexicalDC;
2777 DeclarationName Name;
2778 SourceLocation Loc;
2779 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2780 return 0;
2781
2782 ObjCInterfaceDecl *ToInterface
2783 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2784 if (!ToInterface)
2785 return 0;
2786
2787 // Determine if we've already encountered this category.
2788 ObjCCategoryDecl *MergeWithCategory
2789 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2790 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2791 if (!ToCategory) {
2792 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2793 Importer.Import(D->getAtLoc()),
2794 Loc,
2795 Importer.Import(D->getCategoryNameLoc()),
2796 Name.getAsIdentifierInfo());
2797 ToCategory->setLexicalDeclContext(LexicalDC);
2798 LexicalDC->addDecl(ToCategory);
2799 Importer.Imported(D, ToCategory);
2800
2801 // Link this category into its class's category list.
2802 ToCategory->setClassInterface(ToInterface);
2803 ToCategory->insertNextClassCategory();
2804
2805 // Import protocols
2806 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2807 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2808 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2809 = D->protocol_loc_begin();
2810 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2811 FromProtoEnd = D->protocol_end();
2812 FromProto != FromProtoEnd;
2813 ++FromProto, ++FromProtoLoc) {
2814 ObjCProtocolDecl *ToProto
2815 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2816 if (!ToProto)
2817 return 0;
2818 Protocols.push_back(ToProto);
2819 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2820 }
2821
2822 // FIXME: If we're merging, make sure that the protocol list is the same.
2823 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2824 ProtocolLocs.data(), Importer.getToContext());
2825
2826 } else {
2827 Importer.Imported(D, ToCategory);
2828 }
2829
2830 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002831 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002832
2833 // If we have an implementation, import it as well.
2834 if (D->getImplementation()) {
2835 ObjCCategoryImplDecl *Impl
2836 = cast<ObjCCategoryImplDecl>(Importer.Import(D->getImplementation()));
2837 if (!Impl)
2838 return 0;
2839
2840 ToCategory->setImplementation(Impl);
2841 }
2842
2843 return ToCategory;
2844}
2845
Douglas Gregor98d156a2010-02-17 16:12:00 +00002846Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002847 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002848 DeclContext *DC, *LexicalDC;
2849 DeclarationName Name;
2850 SourceLocation Loc;
2851 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2852 return 0;
2853
2854 ObjCProtocolDecl *MergeWithProtocol = 0;
2855 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2856 Lookup.first != Lookup.second;
2857 ++Lookup.first) {
2858 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2859 continue;
2860
2861 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2862 break;
2863 }
2864
2865 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2866 if (!ToProto || ToProto->isForwardDecl()) {
2867 if (!ToProto) {
2868 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2869 Name.getAsIdentifierInfo());
2870 ToProto->setForwardDecl(D->isForwardDecl());
2871 ToProto->setLexicalDeclContext(LexicalDC);
2872 LexicalDC->addDecl(ToProto);
2873 }
2874 Importer.Imported(D, ToProto);
2875
2876 // Import protocols
2877 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2878 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2879 ObjCProtocolDecl::protocol_loc_iterator
2880 FromProtoLoc = D->protocol_loc_begin();
2881 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2882 FromProtoEnd = D->protocol_end();
2883 FromProto != FromProtoEnd;
2884 ++FromProto, ++FromProtoLoc) {
2885 ObjCProtocolDecl *ToProto
2886 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2887 if (!ToProto)
2888 return 0;
2889 Protocols.push_back(ToProto);
2890 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2891 }
2892
2893 // FIXME: If we're merging, make sure that the protocol list is the same.
2894 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
2895 ProtocolLocs.data(), Importer.getToContext());
2896 } else {
2897 Importer.Imported(D, ToProto);
2898 }
2899
Douglas Gregor84c51c32010-02-18 01:47:50 +00002900 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00002901 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00002902
2903 return ToProto;
2904}
2905
Douglas Gregor45635322010-02-16 01:20:57 +00002906Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
2907 // Import the major distinguishing characteristics of an @interface.
2908 DeclContext *DC, *LexicalDC;
2909 DeclarationName Name;
2910 SourceLocation Loc;
2911 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2912 return 0;
2913
2914 ObjCInterfaceDecl *MergeWithIface = 0;
2915 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2916 Lookup.first != Lookup.second;
2917 ++Lookup.first) {
2918 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
2919 continue;
2920
2921 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
2922 break;
2923 }
2924
2925 ObjCInterfaceDecl *ToIface = MergeWithIface;
2926 if (!ToIface || ToIface->isForwardDecl()) {
2927 if (!ToIface) {
2928 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
2929 DC, Loc,
2930 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00002931 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00002932 D->isForwardDecl(),
2933 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00002934 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00002935 ToIface->setLexicalDeclContext(LexicalDC);
2936 LexicalDC->addDecl(ToIface);
2937 }
2938 Importer.Imported(D, ToIface);
2939
Douglas Gregor45635322010-02-16 01:20:57 +00002940 if (D->getSuperClass()) {
2941 ObjCInterfaceDecl *Super
2942 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
2943 if (!Super)
2944 return 0;
2945
2946 ToIface->setSuperClass(Super);
2947 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
2948 }
2949
2950 // Import protocols
2951 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2952 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2953 ObjCInterfaceDecl::protocol_loc_iterator
2954 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002955
2956 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00002957 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
2958 FromProtoEnd = D->protocol_end();
2959 FromProto != FromProtoEnd;
2960 ++FromProto, ++FromProtoLoc) {
2961 ObjCProtocolDecl *ToProto
2962 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2963 if (!ToProto)
2964 return 0;
2965 Protocols.push_back(ToProto);
2966 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2967 }
2968
2969 // FIXME: If we're merging, make sure that the protocol list is the same.
2970 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
2971 ProtocolLocs.data(), Importer.getToContext());
2972
Douglas Gregor45635322010-02-16 01:20:57 +00002973 // Import @end range
2974 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
2975 } else {
2976 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002977
2978 // Check for consistency of superclasses.
2979 DeclarationName FromSuperName, ToSuperName;
2980 if (D->getSuperClass())
2981 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
2982 if (ToIface->getSuperClass())
2983 ToSuperName = ToIface->getSuperClass()->getDeclName();
2984 if (FromSuperName != ToSuperName) {
2985 Importer.ToDiag(ToIface->getLocation(),
2986 diag::err_odr_objc_superclass_inconsistent)
2987 << ToIface->getDeclName();
2988 if (ToIface->getSuperClass())
2989 Importer.ToDiag(ToIface->getSuperClassLoc(),
2990 diag::note_odr_objc_superclass)
2991 << ToIface->getSuperClass()->getDeclName();
2992 else
2993 Importer.ToDiag(ToIface->getLocation(),
2994 diag::note_odr_objc_missing_superclass);
2995 if (D->getSuperClass())
2996 Importer.FromDiag(D->getSuperClassLoc(),
2997 diag::note_odr_objc_superclass)
2998 << D->getSuperClass()->getDeclName();
2999 else
3000 Importer.FromDiag(D->getLocation(),
3001 diag::note_odr_objc_missing_superclass);
3002 return 0;
3003 }
Douglas Gregor45635322010-02-16 01:20:57 +00003004 }
3005
Douglas Gregor84c51c32010-02-18 01:47:50 +00003006 // Import categories. When the categories themselves are imported, they'll
3007 // hook themselves into this interface.
3008 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3009 FromCat = FromCat->getNextClassCategory())
3010 Importer.Import(FromCat);
3011
Douglas Gregor45635322010-02-16 01:20:57 +00003012 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003013 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003014
3015 // If we have an @implementation, import it as well.
3016 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003017 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3018 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003019 if (!Impl)
3020 return 0;
3021
3022 ToIface->setImplementation(Impl);
3023 }
3024
Douglas Gregor98d156a2010-02-17 16:12:00 +00003025 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003026}
3027
Douglas Gregor4da9d682010-12-07 15:32:12 +00003028Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3029 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3030 Importer.Import(D->getCategoryDecl()));
3031 if (!Category)
3032 return 0;
3033
3034 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3035 if (!ToImpl) {
3036 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3037 if (!DC)
3038 return 0;
3039
3040 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3041 Importer.Import(D->getLocation()),
3042 Importer.Import(D->getIdentifier()),
3043 Category->getClassInterface());
3044
3045 DeclContext *LexicalDC = DC;
3046 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3047 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3048 if (!LexicalDC)
3049 return 0;
3050
3051 ToImpl->setLexicalDeclContext(LexicalDC);
3052 }
3053
3054 LexicalDC->addDecl(ToImpl);
3055 Category->setImplementation(ToImpl);
3056 }
3057
3058 Importer.Imported(D, ToImpl);
3059 ImportDeclContext(ToImpl);
3060 return ToImpl;
3061}
3062
Douglas Gregorda8025c2010-12-07 01:26:03 +00003063Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3064 // Find the corresponding interface.
3065 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3066 Importer.Import(D->getClassInterface()));
3067 if (!Iface)
3068 return 0;
3069
3070 // Import the superclass, if any.
3071 ObjCInterfaceDecl *Super = 0;
3072 if (D->getSuperClass()) {
3073 Super = cast_or_null<ObjCInterfaceDecl>(
3074 Importer.Import(D->getSuperClass()));
3075 if (!Super)
3076 return 0;
3077 }
3078
3079 ObjCImplementationDecl *Impl = Iface->getImplementation();
3080 if (!Impl) {
3081 // We haven't imported an implementation yet. Create a new @implementation
3082 // now.
3083 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3084 Importer.ImportContext(D->getDeclContext()),
3085 Importer.Import(D->getLocation()),
3086 Iface, Super);
3087
3088 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3089 DeclContext *LexicalDC
3090 = Importer.ImportContext(D->getLexicalDeclContext());
3091 if (!LexicalDC)
3092 return 0;
3093 Impl->setLexicalDeclContext(LexicalDC);
3094 }
3095
3096 // Associate the implementation with the class it implements.
3097 Iface->setImplementation(Impl);
3098 Importer.Imported(D, Iface->getImplementation());
3099 } else {
3100 Importer.Imported(D, Iface->getImplementation());
3101
3102 // Verify that the existing @implementation has the same superclass.
3103 if ((Super && !Impl->getSuperClass()) ||
3104 (!Super && Impl->getSuperClass()) ||
3105 (Super && Impl->getSuperClass() &&
3106 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3107 Importer.ToDiag(Impl->getLocation(),
3108 diag::err_odr_objc_superclass_inconsistent)
3109 << Iface->getDeclName();
3110 // FIXME: It would be nice to have the location of the superclass
3111 // below.
3112 if (Impl->getSuperClass())
3113 Importer.ToDiag(Impl->getLocation(),
3114 diag::note_odr_objc_superclass)
3115 << Impl->getSuperClass()->getDeclName();
3116 else
3117 Importer.ToDiag(Impl->getLocation(),
3118 diag::note_odr_objc_missing_superclass);
3119 if (D->getSuperClass())
3120 Importer.FromDiag(D->getLocation(),
3121 diag::note_odr_objc_superclass)
3122 << D->getSuperClass()->getDeclName();
3123 else
3124 Importer.FromDiag(D->getLocation(),
3125 diag::note_odr_objc_missing_superclass);
3126 return 0;
3127 }
3128 }
3129
3130 // Import all of the members of this @implementation.
3131 ImportDeclContext(D);
3132
3133 return Impl;
3134}
3135
Douglas Gregora11c4582010-02-17 18:02:10 +00003136Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3137 // Import the major distinguishing characteristics of an @property.
3138 DeclContext *DC, *LexicalDC;
3139 DeclarationName Name;
3140 SourceLocation Loc;
3141 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3142 return 0;
3143
3144 // Check whether we have already imported this property.
3145 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3146 Lookup.first != Lookup.second;
3147 ++Lookup.first) {
3148 if (ObjCPropertyDecl *FoundProp
3149 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3150 // Check property types.
3151 if (!Importer.IsStructurallyEquivalent(D->getType(),
3152 FoundProp->getType())) {
3153 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3154 << Name << D->getType() << FoundProp->getType();
3155 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3156 << FoundProp->getType();
3157 return 0;
3158 }
3159
3160 // FIXME: Check property attributes, getters, setters, etc.?
3161
3162 // Consider these properties to be equivalent.
3163 Importer.Imported(D, FoundProp);
3164 return FoundProp;
3165 }
3166 }
3167
3168 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003169 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3170 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003171 return 0;
3172
3173 // Create the new property.
3174 ObjCPropertyDecl *ToProperty
3175 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3176 Name.getAsIdentifierInfo(),
3177 Importer.Import(D->getAtLoc()),
3178 T,
3179 D->getPropertyImplementation());
3180 Importer.Imported(D, ToProperty);
3181 ToProperty->setLexicalDeclContext(LexicalDC);
3182 LexicalDC->addDecl(ToProperty);
3183
3184 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003185 ToProperty->setPropertyAttributesAsWritten(
3186 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003187 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3188 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3189 ToProperty->setGetterMethodDecl(
3190 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3191 ToProperty->setSetterMethodDecl(
3192 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3193 ToProperty->setPropertyIvarDecl(
3194 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3195 return ToProperty;
3196}
3197
Douglas Gregor8661a722010-02-18 02:12:22 +00003198Decl *
3199ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3200 // Import the context of this declaration.
3201 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3202 if (!DC)
3203 return 0;
3204
3205 DeclContext *LexicalDC = DC;
3206 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3207 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3208 if (!LexicalDC)
3209 return 0;
3210 }
3211
3212 // Import the location of this declaration.
3213 SourceLocation Loc = Importer.Import(D->getLocation());
3214
3215 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3216 llvm::SmallVector<SourceLocation, 4> Locations;
3217 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3218 = D->protocol_loc_begin();
3219 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3220 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3221 FromProto != FromProtoEnd;
3222 ++FromProto, ++FromProtoLoc) {
3223 ObjCProtocolDecl *ToProto
3224 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3225 if (!ToProto)
3226 continue;
3227
3228 Protocols.push_back(ToProto);
3229 Locations.push_back(Importer.Import(*FromProtoLoc));
3230 }
3231
3232 ObjCForwardProtocolDecl *ToForward
3233 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3234 Protocols.data(), Protocols.size(),
3235 Locations.data());
3236 ToForward->setLexicalDeclContext(LexicalDC);
3237 LexicalDC->addDecl(ToForward);
3238 Importer.Imported(D, ToForward);
3239 return ToForward;
3240}
3241
Douglas Gregor06537af2010-02-18 02:04:09 +00003242Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3243 // Import the context of this declaration.
3244 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3245 if (!DC)
3246 return 0;
3247
3248 DeclContext *LexicalDC = DC;
3249 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3250 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3251 if (!LexicalDC)
3252 return 0;
3253 }
3254
3255 // Import the location of this declaration.
3256 SourceLocation Loc = Importer.Import(D->getLocation());
3257
3258 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3259 llvm::SmallVector<SourceLocation, 4> Locations;
3260 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3261 From != FromEnd; ++From) {
3262 ObjCInterfaceDecl *ToIface
3263 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3264 if (!ToIface)
3265 continue;
3266
3267 Interfaces.push_back(ToIface);
3268 Locations.push_back(Importer.Import(From->getLocation()));
3269 }
3270
3271 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3272 Loc,
3273 Interfaces.data(),
3274 Locations.data(),
3275 Interfaces.size());
3276 ToClass->setLexicalDeclContext(LexicalDC);
3277 LexicalDC->addDecl(ToClass);
3278 Importer.Imported(D, ToClass);
3279 return ToClass;
3280}
3281
Douglas Gregora082a492010-11-30 19:14:50 +00003282Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3283 // For template arguments, we adopt the translation unit as our declaration
3284 // context. This context will be fixed when the actual template declaration
3285 // is created.
3286
3287 // FIXME: Import default argument.
3288 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3289 Importer.getToContext().getTranslationUnitDecl(),
3290 Importer.Import(D->getLocation()),
3291 D->getDepth(),
3292 D->getIndex(),
3293 Importer.Import(D->getIdentifier()),
3294 D->wasDeclaredWithTypename(),
3295 D->isParameterPack());
3296}
3297
3298Decl *
3299ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3300 // Import the name of this declaration.
3301 DeclarationName Name = Importer.Import(D->getDeclName());
3302 if (D->getDeclName() && !Name)
3303 return 0;
3304
3305 // Import the location of this declaration.
3306 SourceLocation Loc = Importer.Import(D->getLocation());
3307
3308 // Import the type of this declaration.
3309 QualType T = Importer.Import(D->getType());
3310 if (T.isNull())
3311 return 0;
3312
3313 // Import type-source information.
3314 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3315 if (D->getTypeSourceInfo() && !TInfo)
3316 return 0;
3317
3318 // FIXME: Import default argument.
3319
3320 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3321 Importer.getToContext().getTranslationUnitDecl(),
3322 Loc, D->getDepth(), D->getPosition(),
3323 Name.getAsIdentifierInfo(),
3324 T, TInfo);
3325}
3326
3327Decl *
3328ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3329 // Import the name of this declaration.
3330 DeclarationName Name = Importer.Import(D->getDeclName());
3331 if (D->getDeclName() && !Name)
3332 return 0;
3333
3334 // Import the location of this declaration.
3335 SourceLocation Loc = Importer.Import(D->getLocation());
3336
3337 // Import template parameters.
3338 TemplateParameterList *TemplateParams
3339 = ImportTemplateParameterList(D->getTemplateParameters());
3340 if (!TemplateParams)
3341 return 0;
3342
3343 // FIXME: Import default argument.
3344
3345 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3346 Importer.getToContext().getTranslationUnitDecl(),
3347 Loc, D->getDepth(), D->getPosition(),
3348 Name.getAsIdentifierInfo(),
3349 TemplateParams);
3350}
3351
3352Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3353 // If this record has a definition in the translation unit we're coming from,
3354 // but this particular declaration is not that definition, import the
3355 // definition and map to that.
3356 CXXRecordDecl *Definition
3357 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3358 if (Definition && Definition != D->getTemplatedDecl()) {
3359 Decl *ImportedDef
3360 = Importer.Import(Definition->getDescribedClassTemplate());
3361 if (!ImportedDef)
3362 return 0;
3363
3364 return Importer.Imported(D, ImportedDef);
3365 }
3366
3367 // Import the major distinguishing characteristics of this class template.
3368 DeclContext *DC, *LexicalDC;
3369 DeclarationName Name;
3370 SourceLocation Loc;
3371 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3372 return 0;
3373
3374 // We may already have a template of the same name; try to find and match it.
3375 if (!DC->isFunctionOrMethod()) {
3376 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3377 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3378 Lookup.first != Lookup.second;
3379 ++Lookup.first) {
3380 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3381 continue;
3382
3383 Decl *Found = *Lookup.first;
3384 if (ClassTemplateDecl *FoundTemplate
3385 = dyn_cast<ClassTemplateDecl>(Found)) {
3386 if (IsStructuralMatch(D, FoundTemplate)) {
3387 // The class templates structurally match; call it the same template.
3388 // FIXME: We may be filling in a forward declaration here. Handle
3389 // this case!
3390 Importer.Imported(D->getTemplatedDecl(),
3391 FoundTemplate->getTemplatedDecl());
3392 return Importer.Imported(D, FoundTemplate);
3393 }
3394 }
3395
3396 ConflictingDecls.push_back(*Lookup.first);
3397 }
3398
3399 if (!ConflictingDecls.empty()) {
3400 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3401 ConflictingDecls.data(),
3402 ConflictingDecls.size());
3403 }
3404
3405 if (!Name)
3406 return 0;
3407 }
3408
3409 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3410
3411 // Create the declaration that is being templated.
3412 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3413 DTemplated->getTagKind(),
3414 DC,
3415 Importer.Import(DTemplated->getLocation()),
3416 Name.getAsIdentifierInfo(),
3417 Importer.Import(DTemplated->getTagKeywordLoc()));
3418 D2Templated->setAccess(DTemplated->getAccess());
3419
3420
3421 // Import the qualifier, if any.
3422 if (DTemplated->getQualifier()) {
3423 NestedNameSpecifier *NNS = Importer.Import(DTemplated->getQualifier());
3424 SourceRange NNSRange = Importer.Import(DTemplated->getQualifierRange());
3425 D2Templated->setQualifierInfo(NNS, NNSRange);
3426 }
3427 D2Templated->setLexicalDeclContext(LexicalDC);
3428
3429 // Create the class template declaration itself.
3430 TemplateParameterList *TemplateParams
3431 = ImportTemplateParameterList(D->getTemplateParameters());
3432 if (!TemplateParams)
3433 return 0;
3434
3435 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3436 Loc, Name, TemplateParams,
3437 D2Templated,
3438 /*PrevDecl=*/0);
3439 D2Templated->setDescribedClassTemplate(D2);
3440
3441 D2->setAccess(D->getAccess());
3442 D2->setLexicalDeclContext(LexicalDC);
3443 LexicalDC->addDecl(D2);
3444
3445 // Note the relationship between the class templates.
3446 Importer.Imported(D, D2);
3447 Importer.Imported(DTemplated, D2Templated);
3448
3449 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3450 // FIXME: Import definition!
3451 }
3452
3453 return D2;
3454}
3455
Douglas Gregore2e50d332010-12-01 01:36:18 +00003456Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3457 ClassTemplateSpecializationDecl *D) {
3458 // If this record has a definition in the translation unit we're coming from,
3459 // but this particular declaration is not that definition, import the
3460 // definition and map to that.
3461 TagDecl *Definition = D->getDefinition();
3462 if (Definition && Definition != D) {
3463 Decl *ImportedDef = Importer.Import(Definition);
3464 if (!ImportedDef)
3465 return 0;
3466
3467 return Importer.Imported(D, ImportedDef);
3468 }
3469
3470 ClassTemplateDecl *ClassTemplate
3471 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3472 D->getSpecializedTemplate()));
3473 if (!ClassTemplate)
3474 return 0;
3475
3476 // Import the context of this declaration.
3477 DeclContext *DC = ClassTemplate->getDeclContext();
3478 if (!DC)
3479 return 0;
3480
3481 DeclContext *LexicalDC = DC;
3482 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3483 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3484 if (!LexicalDC)
3485 return 0;
3486 }
3487
3488 // Import the location of this declaration.
3489 SourceLocation Loc = Importer.Import(D->getLocation());
3490
3491 // Import template arguments.
3492 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3493 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3494 D->getTemplateArgs().size(),
3495 TemplateArgs))
3496 return 0;
3497
3498 // Try to find an existing specialization with these template arguments.
3499 void *InsertPos = 0;
3500 ClassTemplateSpecializationDecl *D2
3501 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3502 TemplateArgs.size(), InsertPos);
3503 if (D2) {
3504 // We already have a class template specialization with these template
3505 // arguments.
3506
3507 // FIXME: Check for specialization vs. instantiation errors.
3508
3509 if (RecordDecl *FoundDef = D2->getDefinition()) {
3510 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3511 // The record types structurally match, or the "from" translation
3512 // unit only had a forward declaration anyway; call it the same
3513 // function.
3514 return Importer.Imported(D, FoundDef);
3515 }
3516 }
3517 } else {
3518 // Create a new specialization.
3519 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3520 D->getTagKind(), DC,
3521 Loc, ClassTemplate,
3522 TemplateArgs.data(),
3523 TemplateArgs.size(),
3524 /*PrevDecl=*/0);
3525 D2->setSpecializationKind(D->getSpecializationKind());
3526
3527 // Add this specialization to the class template.
3528 ClassTemplate->AddSpecialization(D2, InsertPos);
3529
3530 // Import the qualifier, if any.
3531 if (D->getQualifier()) {
3532 NestedNameSpecifier *NNS = Importer.Import(D->getQualifier());
3533 SourceRange NNSRange = Importer.Import(D->getQualifierRange());
3534 D2->setQualifierInfo(NNS, NNSRange);
3535 }
3536
3537
3538 // Add the specialization to this context.
3539 D2->setLexicalDeclContext(LexicalDC);
3540 LexicalDC->addDecl(D2);
3541 }
3542 Importer.Imported(D, D2);
3543
3544 if (D->isDefinition() && ImportDefinition(D, D2))
3545 return 0;
3546
3547 return D2;
3548}
3549
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003550//----------------------------------------------------------------------------
3551// Import Statements
3552//----------------------------------------------------------------------------
3553
3554Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3555 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3556 << S->getStmtClassName();
3557 return 0;
3558}
3559
3560//----------------------------------------------------------------------------
3561// Import Expressions
3562//----------------------------------------------------------------------------
3563Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3564 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3565 << E->getStmtClassName();
3566 return 0;
3567}
3568
Douglas Gregor52f820e2010-02-19 01:17:02 +00003569Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
3570 NestedNameSpecifier *Qualifier = 0;
3571 if (E->getQualifier()) {
3572 Qualifier = Importer.Import(E->getQualifier());
3573 if (!E->getQualifier())
3574 return 0;
3575 }
3576
3577 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3578 if (!ToD)
3579 return 0;
3580
3581 QualType T = Importer.Import(E->getType());
3582 if (T.isNull())
3583 return 0;
3584
3585 return DeclRefExpr::Create(Importer.getToContext(), Qualifier,
3586 Importer.Import(E->getQualifierRange()),
3587 ToD,
3588 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003589 T, E->getValueKind(),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003590 /*FIXME:TemplateArgs=*/0);
3591}
3592
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003593Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3594 QualType T = Importer.Import(E->getType());
3595 if (T.isNull())
3596 return 0;
3597
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003598 return IntegerLiteral::Create(Importer.getToContext(),
3599 E->getValue(), T,
3600 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003601}
3602
Douglas Gregor623421d2010-02-18 02:21:22 +00003603Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3604 QualType T = Importer.Import(E->getType());
3605 if (T.isNull())
3606 return 0;
3607
3608 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3609 E->isWide(), T,
3610 Importer.Import(E->getLocation()));
3611}
3612
Douglas Gregorc74247e2010-02-19 01:07:06 +00003613Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3614 Expr *SubExpr = Importer.Import(E->getSubExpr());
3615 if (!SubExpr)
3616 return 0;
3617
3618 return new (Importer.getToContext())
3619 ParenExpr(Importer.Import(E->getLParen()),
3620 Importer.Import(E->getRParen()),
3621 SubExpr);
3622}
3623
3624Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3625 QualType T = Importer.Import(E->getType());
3626 if (T.isNull())
3627 return 0;
3628
3629 Expr *SubExpr = Importer.Import(E->getSubExpr());
3630 if (!SubExpr)
3631 return 0;
3632
3633 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003634 T, E->getValueKind(),
3635 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003636 Importer.Import(E->getOperatorLoc()));
3637}
3638
Douglas Gregord8552cd2010-02-19 01:24:23 +00003639Expr *ASTNodeImporter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
3640 QualType ResultType = Importer.Import(E->getType());
3641
3642 if (E->isArgumentType()) {
3643 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3644 if (!TInfo)
3645 return 0;
3646
3647 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3648 TInfo, ResultType,
3649 Importer.Import(E->getOperatorLoc()),
3650 Importer.Import(E->getRParenLoc()));
3651 }
3652
3653 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3654 if (!SubExpr)
3655 return 0;
3656
3657 return new (Importer.getToContext()) SizeOfAlignOfExpr(E->isSizeOf(),
3658 SubExpr, ResultType,
3659 Importer.Import(E->getOperatorLoc()),
3660 Importer.Import(E->getRParenLoc()));
3661}
3662
Douglas Gregorc74247e2010-02-19 01:07:06 +00003663Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3664 QualType T = Importer.Import(E->getType());
3665 if (T.isNull())
3666 return 0;
3667
3668 Expr *LHS = Importer.Import(E->getLHS());
3669 if (!LHS)
3670 return 0;
3671
3672 Expr *RHS = Importer.Import(E->getRHS());
3673 if (!RHS)
3674 return 0;
3675
3676 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003677 T, E->getValueKind(),
3678 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003679 Importer.Import(E->getOperatorLoc()));
3680}
3681
3682Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3683 QualType T = Importer.Import(E->getType());
3684 if (T.isNull())
3685 return 0;
3686
3687 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3688 if (CompLHSType.isNull())
3689 return 0;
3690
3691 QualType CompResultType = Importer.Import(E->getComputationResultType());
3692 if (CompResultType.isNull())
3693 return 0;
3694
3695 Expr *LHS = Importer.Import(E->getLHS());
3696 if (!LHS)
3697 return 0;
3698
3699 Expr *RHS = Importer.Import(E->getRHS());
3700 if (!RHS)
3701 return 0;
3702
3703 return new (Importer.getToContext())
3704 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003705 T, E->getValueKind(),
3706 E->getObjectKind(),
3707 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003708 Importer.Import(E->getOperatorLoc()));
3709}
3710
John McCallcf142162010-08-07 06:22:56 +00003711bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
3712 if (E->path_empty()) return false;
3713
3714 // TODO: import cast paths
3715 return true;
3716}
3717
Douglas Gregor98c10182010-02-12 22:17:39 +00003718Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3719 QualType T = Importer.Import(E->getType());
3720 if (T.isNull())
3721 return 0;
3722
3723 Expr *SubExpr = Importer.Import(E->getSubExpr());
3724 if (!SubExpr)
3725 return 0;
John McCallcf142162010-08-07 06:22:56 +00003726
3727 CXXCastPath BasePath;
3728 if (ImportCastPath(E, BasePath))
3729 return 0;
3730
3731 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003732 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003733}
3734
Douglas Gregor5481d322010-02-19 01:32:14 +00003735Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3736 QualType T = Importer.Import(E->getType());
3737 if (T.isNull())
3738 return 0;
3739
3740 Expr *SubExpr = Importer.Import(E->getSubExpr());
3741 if (!SubExpr)
3742 return 0;
3743
3744 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3745 if (!TInfo && E->getTypeInfoAsWritten())
3746 return 0;
3747
John McCallcf142162010-08-07 06:22:56 +00003748 CXXCastPath BasePath;
3749 if (ImportCastPath(E, BasePath))
3750 return 0;
3751
John McCall7decc9e2010-11-18 06:31:45 +00003752 return CStyleCastExpr::Create(Importer.getToContext(), T,
3753 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003754 SubExpr, &BasePath, TInfo,
3755 Importer.Import(E->getLParenLoc()),
3756 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003757}
3758
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003759ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Chris Lattner5159f612010-11-23 08:35:12 +00003760 ASTContext &FromContext, FileManager &FromFileManager)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003761 : ToContext(ToContext), FromContext(FromContext),
Chris Lattner5159f612010-11-23 08:35:12 +00003762 ToFileManager(ToFileManager), FromFileManager(FromFileManager) {
Douglas Gregor62d311f2010-02-09 19:21:46 +00003763 ImportedDecls[FromContext.getTranslationUnitDecl()]
3764 = ToContext.getTranslationUnitDecl();
3765}
3766
3767ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003768
3769QualType ASTImporter::Import(QualType FromT) {
3770 if (FromT.isNull())
3771 return QualType();
3772
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003773 // Check whether we've already imported this type.
3774 llvm::DenseMap<Type *, Type *>::iterator Pos
3775 = ImportedTypes.find(FromT.getTypePtr());
3776 if (Pos != ImportedTypes.end())
3777 return ToContext.getQualifiedType(Pos->second, FromT.getQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003778
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003779 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003780 ASTNodeImporter Importer(*this);
3781 QualType ToT = Importer.Visit(FromT.getTypePtr());
3782 if (ToT.isNull())
3783 return ToT;
3784
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003785 // Record the imported type.
3786 ImportedTypes[FromT.getTypePtr()] = ToT.getTypePtr();
3787
Douglas Gregor96e578d2010-02-05 17:54:41 +00003788 return ToContext.getQualifiedType(ToT, FromT.getQualifiers());
3789}
3790
Douglas Gregor62d311f2010-02-09 19:21:46 +00003791TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003792 if (!FromTSI)
3793 return FromTSI;
3794
3795 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003796 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003797 QualType T = Import(FromTSI->getType());
3798 if (T.isNull())
3799 return 0;
3800
3801 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003802 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003803}
3804
3805Decl *ASTImporter::Import(Decl *FromD) {
3806 if (!FromD)
3807 return 0;
3808
3809 // Check whether we've already imported this declaration.
3810 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
3811 if (Pos != ImportedDecls.end())
3812 return Pos->second;
3813
3814 // Import the type
3815 ASTNodeImporter Importer(*this);
3816 Decl *ToD = Importer.Visit(FromD);
3817 if (!ToD)
3818 return 0;
3819
3820 // Record the imported declaration.
3821 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003822
3823 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
3824 // Keep track of anonymous tags that have an associated typedef.
3825 if (FromTag->getTypedefForAnonDecl())
3826 AnonTagsWithPendingTypedefs.push_back(FromTag);
3827 } else if (TypedefDecl *FromTypedef = dyn_cast<TypedefDecl>(FromD)) {
3828 // When we've finished transforming a typedef, see whether it was the
3829 // typedef for an anonymous tag.
3830 for (llvm::SmallVector<TagDecl *, 4>::iterator
3831 FromTag = AnonTagsWithPendingTypedefs.begin(),
3832 FromTagEnd = AnonTagsWithPendingTypedefs.end();
3833 FromTag != FromTagEnd; ++FromTag) {
3834 if ((*FromTag)->getTypedefForAnonDecl() == FromTypedef) {
3835 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
3836 // We found the typedef for an anonymous tag; link them.
3837 ToTag->setTypedefForAnonDecl(cast<TypedefDecl>(ToD));
3838 AnonTagsWithPendingTypedefs.erase(FromTag);
3839 break;
3840 }
3841 }
3842 }
3843 }
3844
Douglas Gregor62d311f2010-02-09 19:21:46 +00003845 return ToD;
3846}
3847
3848DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
3849 if (!FromDC)
3850 return FromDC;
3851
3852 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
3853}
3854
3855Expr *ASTImporter::Import(Expr *FromE) {
3856 if (!FromE)
3857 return 0;
3858
3859 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
3860}
3861
3862Stmt *ASTImporter::Import(Stmt *FromS) {
3863 if (!FromS)
3864 return 0;
3865
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003866 // Check whether we've already imported this declaration.
3867 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
3868 if (Pos != ImportedStmts.end())
3869 return Pos->second;
3870
3871 // Import the type
3872 ASTNodeImporter Importer(*this);
3873 Stmt *ToS = Importer.Visit(FromS);
3874 if (!ToS)
3875 return 0;
3876
3877 // Record the imported declaration.
3878 ImportedStmts[FromS] = ToS;
3879 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00003880}
3881
3882NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
3883 if (!FromNNS)
3884 return 0;
3885
3886 // FIXME: Implement!
3887 return 0;
3888}
3889
Douglas Gregore2e50d332010-12-01 01:36:18 +00003890TemplateName ASTImporter::Import(TemplateName From) {
3891 switch (From.getKind()) {
3892 case TemplateName::Template:
3893 if (TemplateDecl *ToTemplate
3894 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3895 return TemplateName(ToTemplate);
3896
3897 return TemplateName();
3898
3899 case TemplateName::OverloadedTemplate: {
3900 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
3901 UnresolvedSet<2> ToTemplates;
3902 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
3903 E = FromStorage->end();
3904 I != E; ++I) {
3905 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
3906 ToTemplates.addDecl(To);
3907 else
3908 return TemplateName();
3909 }
3910 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
3911 ToTemplates.end());
3912 }
3913
3914 case TemplateName::QualifiedTemplate: {
3915 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
3916 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
3917 if (!Qualifier)
3918 return TemplateName();
3919
3920 if (TemplateDecl *ToTemplate
3921 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
3922 return ToContext.getQualifiedTemplateName(Qualifier,
3923 QTN->hasTemplateKeyword(),
3924 ToTemplate);
3925
3926 return TemplateName();
3927 }
3928
3929 case TemplateName::DependentTemplate: {
3930 DependentTemplateName *DTN = From.getAsDependentTemplateName();
3931 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
3932 if (!Qualifier)
3933 return TemplateName();
3934
3935 if (DTN->isIdentifier()) {
3936 return ToContext.getDependentTemplateName(Qualifier,
3937 Import(DTN->getIdentifier()));
3938 }
3939
3940 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
3941 }
3942 }
3943
3944 llvm_unreachable("Invalid template name kind");
3945 return TemplateName();
3946}
3947
Douglas Gregor62d311f2010-02-09 19:21:46 +00003948SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
3949 if (FromLoc.isInvalid())
3950 return SourceLocation();
3951
Douglas Gregor811663e2010-02-10 00:15:17 +00003952 SourceManager &FromSM = FromContext.getSourceManager();
3953
3954 // For now, map everything down to its spelling location, so that we
3955 // don't have to import macro instantiations.
3956 // FIXME: Import macro instantiations!
3957 FromLoc = FromSM.getSpellingLoc(FromLoc);
3958 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
3959 SourceManager &ToSM = ToContext.getSourceManager();
3960 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
3961 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003962}
3963
3964SourceRange ASTImporter::Import(SourceRange FromRange) {
3965 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
3966}
3967
Douglas Gregor811663e2010-02-10 00:15:17 +00003968FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00003969 llvm::DenseMap<FileID, FileID>::iterator Pos
3970 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00003971 if (Pos != ImportedFileIDs.end())
3972 return Pos->second;
3973
3974 SourceManager &FromSM = FromContext.getSourceManager();
3975 SourceManager &ToSM = ToContext.getSourceManager();
3976 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
3977 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
3978
3979 // Include location of this file.
3980 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
3981
3982 // Map the FileID for to the "to" source manager.
3983 FileID ToID;
3984 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
3985 if (Cache->Entry) {
3986 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
3987 // disk again
3988 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
3989 // than mmap the files several times.
Chris Lattner5159f612010-11-23 08:35:12 +00003990 const FileEntry *Entry = ToFileManager.getFile(Cache->Entry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00003991 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
3992 FromSLoc.getFile().getFileCharacteristic());
3993 } else {
3994 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003995 const llvm::MemoryBuffer *
3996 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00003997 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00003998 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00003999 FromBuf->getBufferIdentifier());
4000 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4001 }
4002
4003
Sebastian Redl99219f12010-09-30 01:03:06 +00004004 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004005 return ToID;
4006}
4007
Douglas Gregor96e578d2010-02-05 17:54:41 +00004008DeclarationName ASTImporter::Import(DeclarationName FromName) {
4009 if (!FromName)
4010 return DeclarationName();
4011
4012 switch (FromName.getNameKind()) {
4013 case DeclarationName::Identifier:
4014 return Import(FromName.getAsIdentifierInfo());
4015
4016 case DeclarationName::ObjCZeroArgSelector:
4017 case DeclarationName::ObjCOneArgSelector:
4018 case DeclarationName::ObjCMultiArgSelector:
4019 return Import(FromName.getObjCSelector());
4020
4021 case DeclarationName::CXXConstructorName: {
4022 QualType T = Import(FromName.getCXXNameType());
4023 if (T.isNull())
4024 return DeclarationName();
4025
4026 return ToContext.DeclarationNames.getCXXConstructorName(
4027 ToContext.getCanonicalType(T));
4028 }
4029
4030 case DeclarationName::CXXDestructorName: {
4031 QualType T = Import(FromName.getCXXNameType());
4032 if (T.isNull())
4033 return DeclarationName();
4034
4035 return ToContext.DeclarationNames.getCXXDestructorName(
4036 ToContext.getCanonicalType(T));
4037 }
4038
4039 case DeclarationName::CXXConversionFunctionName: {
4040 QualType T = Import(FromName.getCXXNameType());
4041 if (T.isNull())
4042 return DeclarationName();
4043
4044 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4045 ToContext.getCanonicalType(T));
4046 }
4047
4048 case DeclarationName::CXXOperatorName:
4049 return ToContext.DeclarationNames.getCXXOperatorName(
4050 FromName.getCXXOverloadedOperator());
4051
4052 case DeclarationName::CXXLiteralOperatorName:
4053 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4054 Import(FromName.getCXXLiteralIdentifier()));
4055
4056 case DeclarationName::CXXUsingDirective:
4057 // FIXME: STATICS!
4058 return DeclarationName::getUsingDirectiveName();
4059 }
4060
4061 // Silence bogus GCC warning
4062 return DeclarationName();
4063}
4064
Douglas Gregore2e50d332010-12-01 01:36:18 +00004065IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004066 if (!FromId)
4067 return 0;
4068
4069 return &ToContext.Idents.get(FromId->getName());
4070}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004071
Douglas Gregor43f54792010-02-17 02:12:47 +00004072Selector ASTImporter::Import(Selector FromSel) {
4073 if (FromSel.isNull())
4074 return Selector();
4075
4076 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4077 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4078 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4079 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4080 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4081}
4082
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004083DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4084 DeclContext *DC,
4085 unsigned IDNS,
4086 NamedDecl **Decls,
4087 unsigned NumDecls) {
4088 return Name;
4089}
4090
4091DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004092 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004093}
4094
4095DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004096 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004097}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004098
4099Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4100 ImportedDecls[From] = To;
4101 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004102}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004103
4104bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
4105 llvm::DenseMap<Type *, Type *>::iterator Pos
4106 = ImportedTypes.find(From.getTypePtr());
4107 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4108 return true;
4109
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004110 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004111 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004112}