blob: 8e95cfecf4cfdbd8838f3d7d57ecf66185c8c04e [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
15
16#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000034 ASTImporter &Importer;
35
36 public:
37 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
38
39 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000042
43 // Importing types
John McCall424cec92011-01-19 06:33:43 +000044 QualType VisitType(const Type *T);
45 QualType VisitBuiltinType(const BuiltinType *T);
46 QualType VisitComplexType(const ComplexType *T);
47 QualType VisitPointerType(const PointerType *T);
48 QualType VisitBlockPointerType(const BlockPointerType *T);
49 QualType VisitLValueReferenceType(const LValueReferenceType *T);
50 QualType VisitRValueReferenceType(const RValueReferenceType *T);
51 QualType VisitMemberPointerType(const MemberPointerType *T);
52 QualType VisitConstantArrayType(const ConstantArrayType *T);
53 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
54 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000057 QualType VisitVectorType(const VectorType *T);
58 QualType VisitExtVectorType(const ExtVectorType *T);
59 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
60 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
John McCall424cec92011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000067 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
81 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000084 SourceLocation &Loc);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000085 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
86 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000087 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregore2e50d332010-12-01 01:36:18 +000088 bool ImportDefinition(RecordDecl *From, RecordDecl *To);
Douglas Gregora082a492010-11-30 19:14:50 +000089 TemplateParameterList *ImportTemplateParameterList(
90 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +000091 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
92 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
93 unsigned NumFromArgs,
94 llvm::SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor5c73e912010-02-11 00:48:18 +000095 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor3996e242010-02-15 22:01:00 +000096 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregora082a492010-11-30 19:14:50 +000097 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +000098 Decl *VisitDecl(Decl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +000099 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000100 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000101 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000102 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000103 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000104 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000105 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000106 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000107 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
108 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
109 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
110 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000111 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000112 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000113 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000114 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000115 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000116 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000117 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000118 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000119 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000120 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000121 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000122 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000123 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000124 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor8661a722010-02-18 02:12:22 +0000125 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000126 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000127 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
128 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
129 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
130 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000131 Decl *VisitClassTemplateSpecializationDecl(
132 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000133
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000134 // Importing statements
135 Stmt *VisitStmt(Stmt *S);
136
137 // Importing expressions
138 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000139 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000140 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000141 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000142 Expr *VisitParenExpr(ParenExpr *E);
143 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000144 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000145 Expr *VisitBinaryOperator(BinaryOperator *E);
146 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000147 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000148 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000149 };
150}
151
152//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000153// Structural Equivalence
154//----------------------------------------------------------------------------
155
156namespace {
157 struct StructuralEquivalenceContext {
158 /// \brief AST contexts for which we are checking structural equivalence.
159 ASTContext &C1, &C2;
160
Douglas Gregor3996e242010-02-15 22:01:00 +0000161 /// \brief The set of "tentative" equivalences between two canonical
162 /// declarations, mapping from a declaration in the first context to the
163 /// declaration in the second context that we believe to be equivalent.
164 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
165
166 /// \brief Queue of declarations in the first context whose equivalence
167 /// with a declaration in the second context still needs to be verified.
168 std::deque<Decl *> DeclsToCheck;
169
Douglas Gregorb4964f72010-02-15 23:54:17 +0000170 /// \brief Declaration (from, to) pairs that are known not to be equivalent
171 /// (which we have already complained about).
172 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
173
Douglas Gregor3996e242010-02-15 22:01:00 +0000174 /// \brief Whether we're being strict about the spelling of types when
175 /// unifying two types.
176 bool StrictTypeSpelling;
177
178 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000179 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor3996e242010-02-15 22:01:00 +0000180 bool StrictTypeSpelling = false)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000181 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorb4964f72010-02-15 23:54:17 +0000182 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor3996e242010-02-15 22:01:00 +0000183
184 /// \brief Determine whether the two declarations are structurally
185 /// equivalent.
186 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
187
188 /// \brief Determine whether the two types are structurally equivalent.
189 bool IsStructurallyEquivalent(QualType T1, QualType T2);
190
191 private:
192 /// \brief Finish checking all of the structural equivalences.
193 ///
194 /// \returns true if an error occurred, false otherwise.
195 bool Finish();
196
197 public:
198 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000199 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000200 }
201
202 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000203 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000204 }
205 };
206}
207
208static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
209 QualType T1, QualType T2);
210static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
211 Decl *D1, Decl *D2);
212
213/// \brief Determine if two APInts have the same value, after zero-extending
214/// one of them (if needed!) to ensure that the bit-widths match.
215static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
216 if (I1.getBitWidth() == I2.getBitWidth())
217 return I1 == I2;
218
219 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000220 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor3996e242010-02-15 22:01:00 +0000221
Jay Foad6d4db0c2010-12-07 08:25:34 +0000222 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor3996e242010-02-15 22:01:00 +0000223}
224
225/// \brief Determine if two APSInts have the same value, zero- or sign-extending
226/// as needed.
227static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
228 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
229 return I1 == I2;
230
231 // Check for a bit-width mismatch.
232 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000233 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor3996e242010-02-15 22:01:00 +0000234 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad6d4db0c2010-12-07 08:25:34 +0000235 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000236
237 // We have a signedness mismatch. Turn the signed value into an unsigned
238 // value.
239 if (I1.isSigned()) {
240 if (I1.isNegative())
241 return false;
242
243 return llvm::APSInt(I1, true) == I2;
244 }
245
246 if (I2.isNegative())
247 return false;
248
249 return I1 == llvm::APSInt(I2, true);
250}
251
252/// \brief Determine structural equivalence of two expressions.
253static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
254 Expr *E1, Expr *E2) {
255 if (!E1 || !E2)
256 return E1 == E2;
257
258 // FIXME: Actually perform a structural comparison!
259 return true;
260}
261
262/// \brief Determine whether two identifiers are equivalent.
263static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
264 const IdentifierInfo *Name2) {
265 if (!Name1 || !Name2)
266 return Name1 == Name2;
267
268 return Name1->getName() == Name2->getName();
269}
270
271/// \brief Determine whether two nested-name-specifiers are equivalent.
272static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
273 NestedNameSpecifier *NNS1,
274 NestedNameSpecifier *NNS2) {
275 // FIXME: Implement!
276 return true;
277}
278
279/// \brief Determine whether two template arguments are equivalent.
280static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
281 const TemplateArgument &Arg1,
282 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000283 if (Arg1.getKind() != Arg2.getKind())
284 return false;
285
286 switch (Arg1.getKind()) {
287 case TemplateArgument::Null:
288 return true;
289
290 case TemplateArgument::Type:
291 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
292
293 case TemplateArgument::Integral:
294 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
295 Arg2.getIntegralType()))
296 return false;
297
298 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
299
300 case TemplateArgument::Declaration:
301 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
302
303 case TemplateArgument::Template:
304 return IsStructurallyEquivalent(Context,
305 Arg1.getAsTemplate(),
306 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000307
308 case TemplateArgument::TemplateExpansion:
309 return IsStructurallyEquivalent(Context,
310 Arg1.getAsTemplateOrTemplatePattern(),
311 Arg2.getAsTemplateOrTemplatePattern());
312
Douglas Gregore2e50d332010-12-01 01:36:18 +0000313 case TemplateArgument::Expression:
314 return IsStructurallyEquivalent(Context,
315 Arg1.getAsExpr(), Arg2.getAsExpr());
316
317 case TemplateArgument::Pack:
318 if (Arg1.pack_size() != Arg2.pack_size())
319 return false;
320
321 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
322 if (!IsStructurallyEquivalent(Context,
323 Arg1.pack_begin()[I],
324 Arg2.pack_begin()[I]))
325 return false;
326
327 return true;
328 }
329
330 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000331 return true;
332}
333
334/// \brief Determine structural equivalence for the common part of array
335/// types.
336static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
337 const ArrayType *Array1,
338 const ArrayType *Array2) {
339 if (!IsStructurallyEquivalent(Context,
340 Array1->getElementType(),
341 Array2->getElementType()))
342 return false;
343 if (Array1->getSizeModifier() != Array2->getSizeModifier())
344 return false;
345 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
346 return false;
347
348 return true;
349}
350
351/// \brief Determine structural equivalence of two types.
352static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
353 QualType T1, QualType T2) {
354 if (T1.isNull() || T2.isNull())
355 return T1.isNull() && T2.isNull();
356
357 if (!Context.StrictTypeSpelling) {
358 // We aren't being strict about token-to-token equivalence of types,
359 // so map down to the canonical type.
360 T1 = Context.C1.getCanonicalType(T1);
361 T2 = Context.C2.getCanonicalType(T2);
362 }
363
364 if (T1.getQualifiers() != T2.getQualifiers())
365 return false;
366
Douglas Gregorb4964f72010-02-15 23:54:17 +0000367 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000368
Douglas Gregorb4964f72010-02-15 23:54:17 +0000369 if (T1->getTypeClass() != T2->getTypeClass()) {
370 // Compare function types with prototypes vs. without prototypes as if
371 // both did not have prototypes.
372 if (T1->getTypeClass() == Type::FunctionProto &&
373 T2->getTypeClass() == Type::FunctionNoProto)
374 TC = Type::FunctionNoProto;
375 else if (T1->getTypeClass() == Type::FunctionNoProto &&
376 T2->getTypeClass() == Type::FunctionProto)
377 TC = Type::FunctionNoProto;
378 else
379 return false;
380 }
381
382 switch (TC) {
383 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000384 // FIXME: Deal with Char_S/Char_U.
385 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
386 return false;
387 break;
388
389 case Type::Complex:
390 if (!IsStructurallyEquivalent(Context,
391 cast<ComplexType>(T1)->getElementType(),
392 cast<ComplexType>(T2)->getElementType()))
393 return false;
394 break;
395
396 case Type::Pointer:
397 if (!IsStructurallyEquivalent(Context,
398 cast<PointerType>(T1)->getPointeeType(),
399 cast<PointerType>(T2)->getPointeeType()))
400 return false;
401 break;
402
403 case Type::BlockPointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<BlockPointerType>(T1)->getPointeeType(),
406 cast<BlockPointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::LValueReference:
411 case Type::RValueReference: {
412 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
413 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
414 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
415 return false;
416 if (Ref1->isInnerRef() != Ref2->isInnerRef())
417 return false;
418 if (!IsStructurallyEquivalent(Context,
419 Ref1->getPointeeTypeAsWritten(),
420 Ref2->getPointeeTypeAsWritten()))
421 return false;
422 break;
423 }
424
425 case Type::MemberPointer: {
426 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
427 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
428 if (!IsStructurallyEquivalent(Context,
429 MemPtr1->getPointeeType(),
430 MemPtr2->getPointeeType()))
431 return false;
432 if (!IsStructurallyEquivalent(Context,
433 QualType(MemPtr1->getClass(), 0),
434 QualType(MemPtr2->getClass(), 0)))
435 return false;
436 break;
437 }
438
439 case Type::ConstantArray: {
440 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
441 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
442 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
443 return false;
444
445 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
446 return false;
447 break;
448 }
449
450 case Type::IncompleteArray:
451 if (!IsArrayStructurallyEquivalent(Context,
452 cast<ArrayType>(T1),
453 cast<ArrayType>(T2)))
454 return false;
455 break;
456
457 case Type::VariableArray: {
458 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
459 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
460 if (!IsStructurallyEquivalent(Context,
461 Array1->getSizeExpr(), Array2->getSizeExpr()))
462 return false;
463
464 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
465 return false;
466
467 break;
468 }
469
470 case Type::DependentSizedArray: {
471 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
472 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
473 if (!IsStructurallyEquivalent(Context,
474 Array1->getSizeExpr(), Array2->getSizeExpr()))
475 return false;
476
477 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
478 return false;
479
480 break;
481 }
482
483 case Type::DependentSizedExtVector: {
484 const DependentSizedExtVectorType *Vec1
485 = cast<DependentSizedExtVectorType>(T1);
486 const DependentSizedExtVectorType *Vec2
487 = cast<DependentSizedExtVectorType>(T2);
488 if (!IsStructurallyEquivalent(Context,
489 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
490 return false;
491 if (!IsStructurallyEquivalent(Context,
492 Vec1->getElementType(),
493 Vec2->getElementType()))
494 return false;
495 break;
496 }
497
498 case Type::Vector:
499 case Type::ExtVector: {
500 const VectorType *Vec1 = cast<VectorType>(T1);
501 const VectorType *Vec2 = cast<VectorType>(T2);
502 if (!IsStructurallyEquivalent(Context,
503 Vec1->getElementType(),
504 Vec2->getElementType()))
505 return false;
506 if (Vec1->getNumElements() != Vec2->getNumElements())
507 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000508 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000509 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000510 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000511 }
512
513 case Type::FunctionProto: {
514 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
515 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
516 if (Proto1->getNumArgs() != Proto2->getNumArgs())
517 return false;
518 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
519 if (!IsStructurallyEquivalent(Context,
520 Proto1->getArgType(I),
521 Proto2->getArgType(I)))
522 return false;
523 }
524 if (Proto1->isVariadic() != Proto2->isVariadic())
525 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000526 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000527 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000528 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
529 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
530 return false;
531 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
532 if (!IsStructurallyEquivalent(Context,
533 Proto1->getExceptionType(I),
534 Proto2->getExceptionType(I)))
535 return false;
536 }
537 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000538 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000539 Proto1->getNoexceptExpr(),
540 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000541 return false;
542 }
543 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
544 return false;
545
546 // Fall through to check the bits common with FunctionNoProtoType.
547 }
548
549 case Type::FunctionNoProto: {
550 const FunctionType *Function1 = cast<FunctionType>(T1);
551 const FunctionType *Function2 = cast<FunctionType>(T2);
552 if (!IsStructurallyEquivalent(Context,
553 Function1->getResultType(),
554 Function2->getResultType()))
555 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000556 if (Function1->getExtInfo() != Function2->getExtInfo())
557 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000558 break;
559 }
560
561 case Type::UnresolvedUsing:
562 if (!IsStructurallyEquivalent(Context,
563 cast<UnresolvedUsingType>(T1)->getDecl(),
564 cast<UnresolvedUsingType>(T2)->getDecl()))
565 return false;
566
567 break;
John McCall81904512011-01-06 01:58:22 +0000568
569 case Type::Attributed:
570 if (!IsStructurallyEquivalent(Context,
571 cast<AttributedType>(T1)->getModifiedType(),
572 cast<AttributedType>(T2)->getModifiedType()))
573 return false;
574 if (!IsStructurallyEquivalent(Context,
575 cast<AttributedType>(T1)->getEquivalentType(),
576 cast<AttributedType>(T2)->getEquivalentType()))
577 return false;
578 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000579
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000580 case Type::Paren:
581 if (!IsStructurallyEquivalent(Context,
582 cast<ParenType>(T1)->getInnerType(),
583 cast<ParenType>(T2)->getInnerType()))
584 return false;
585 break;
586
Douglas Gregor3996e242010-02-15 22:01:00 +0000587 case Type::Typedef:
588 if (!IsStructurallyEquivalent(Context,
589 cast<TypedefType>(T1)->getDecl(),
590 cast<TypedefType>(T2)->getDecl()))
591 return false;
592 break;
593
594 case Type::TypeOfExpr:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
597 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
598 return false;
599 break;
600
601 case Type::TypeOf:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfType>(T1)->getUnderlyingType(),
604 cast<TypeOfType>(T2)->getUnderlyingType()))
605 return false;
606 break;
607
608 case Type::Decltype:
609 if (!IsStructurallyEquivalent(Context,
610 cast<DecltypeType>(T1)->getUnderlyingExpr(),
611 cast<DecltypeType>(T2)->getUnderlyingExpr()))
612 return false;
613 break;
614
Richard Smith30482bc2011-02-20 03:19:35 +0000615 case Type::Auto:
616 if (!IsStructurallyEquivalent(Context,
617 cast<AutoType>(T1)->getDeducedType(),
618 cast<AutoType>(T2)->getDeducedType()))
619 return false;
620 break;
621
Douglas Gregor3996e242010-02-15 22:01:00 +0000622 case Type::Record:
623 case Type::Enum:
624 if (!IsStructurallyEquivalent(Context,
625 cast<TagType>(T1)->getDecl(),
626 cast<TagType>(T2)->getDecl()))
627 return false;
628 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000629
Douglas Gregor3996e242010-02-15 22:01:00 +0000630 case Type::TemplateTypeParm: {
631 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
632 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
633 if (Parm1->getDepth() != Parm2->getDepth())
634 return false;
635 if (Parm1->getIndex() != Parm2->getIndex())
636 return false;
637 if (Parm1->isParameterPack() != Parm2->isParameterPack())
638 return false;
639
640 // Names of template type parameters are never significant.
641 break;
642 }
643
644 case Type::SubstTemplateTypeParm: {
645 const SubstTemplateTypeParmType *Subst1
646 = cast<SubstTemplateTypeParmType>(T1);
647 const SubstTemplateTypeParmType *Subst2
648 = cast<SubstTemplateTypeParmType>(T2);
649 if (!IsStructurallyEquivalent(Context,
650 QualType(Subst1->getReplacedParameter(), 0),
651 QualType(Subst2->getReplacedParameter(), 0)))
652 return false;
653 if (!IsStructurallyEquivalent(Context,
654 Subst1->getReplacementType(),
655 Subst2->getReplacementType()))
656 return false;
657 break;
658 }
659
Douglas Gregorfb322d82011-01-14 05:11:40 +0000660 case Type::SubstTemplateTypeParmPack: {
661 const SubstTemplateTypeParmPackType *Subst1
662 = cast<SubstTemplateTypeParmPackType>(T1);
663 const SubstTemplateTypeParmPackType *Subst2
664 = cast<SubstTemplateTypeParmPackType>(T2);
665 if (!IsStructurallyEquivalent(Context,
666 QualType(Subst1->getReplacedParameter(), 0),
667 QualType(Subst2->getReplacedParameter(), 0)))
668 return false;
669 if (!IsStructurallyEquivalent(Context,
670 Subst1->getArgumentPack(),
671 Subst2->getArgumentPack()))
672 return false;
673 break;
674 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000675 case Type::TemplateSpecialization: {
676 const TemplateSpecializationType *Spec1
677 = cast<TemplateSpecializationType>(T1);
678 const TemplateSpecializationType *Spec2
679 = cast<TemplateSpecializationType>(T2);
680 if (!IsStructurallyEquivalent(Context,
681 Spec1->getTemplateName(),
682 Spec2->getTemplateName()))
683 return false;
684 if (Spec1->getNumArgs() != Spec2->getNumArgs())
685 return false;
686 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
687 if (!IsStructurallyEquivalent(Context,
688 Spec1->getArg(I), Spec2->getArg(I)))
689 return false;
690 }
691 break;
692 }
693
Abramo Bagnara6150c882010-05-11 21:36:43 +0000694 case Type::Elaborated: {
695 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
696 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
697 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
698 if (Elab1->getKeyword() != Elab2->getKeyword())
699 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000700 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000701 Elab1->getQualifier(),
702 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000703 return false;
704 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000705 Elab1->getNamedType(),
706 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000707 return false;
708 break;
709 }
710
John McCalle78aac42010-03-10 03:28:59 +0000711 case Type::InjectedClassName: {
712 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
713 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
714 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000715 Inj1->getInjectedSpecializationType(),
716 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000717 return false;
718 break;
719 }
720
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000721 case Type::DependentName: {
722 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
723 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000724 if (!IsStructurallyEquivalent(Context,
725 Typename1->getQualifier(),
726 Typename2->getQualifier()))
727 return false;
728 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
729 Typename2->getIdentifier()))
730 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000731
732 break;
733 }
734
John McCallc392f372010-06-11 00:33:02 +0000735 case Type::DependentTemplateSpecialization: {
736 const DependentTemplateSpecializationType *Spec1 =
737 cast<DependentTemplateSpecializationType>(T1);
738 const DependentTemplateSpecializationType *Spec2 =
739 cast<DependentTemplateSpecializationType>(T2);
740 if (!IsStructurallyEquivalent(Context,
741 Spec1->getQualifier(),
742 Spec2->getQualifier()))
743 return false;
744 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
745 Spec2->getIdentifier()))
746 return false;
747 if (Spec1->getNumArgs() != Spec2->getNumArgs())
748 return false;
749 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
750 if (!IsStructurallyEquivalent(Context,
751 Spec1->getArg(I), Spec2->getArg(I)))
752 return false;
753 }
754 break;
755 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000756
757 case Type::PackExpansion:
758 if (!IsStructurallyEquivalent(Context,
759 cast<PackExpansionType>(T1)->getPattern(),
760 cast<PackExpansionType>(T2)->getPattern()))
761 return false;
762 break;
763
Douglas Gregor3996e242010-02-15 22:01:00 +0000764 case Type::ObjCInterface: {
765 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
766 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
767 if (!IsStructurallyEquivalent(Context,
768 Iface1->getDecl(), Iface2->getDecl()))
769 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000770 break;
771 }
772
773 case Type::ObjCObject: {
774 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
775 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
776 if (!IsStructurallyEquivalent(Context,
777 Obj1->getBaseType(),
778 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000779 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000780 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
781 return false;
782 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000783 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000784 Obj1->getProtocol(I),
785 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000786 return false;
787 }
788 break;
789 }
790
791 case Type::ObjCObjectPointer: {
792 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
793 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
794 if (!IsStructurallyEquivalent(Context,
795 Ptr1->getPointeeType(),
796 Ptr2->getPointeeType()))
797 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000798 break;
799 }
800
801 } // end switch
802
803 return true;
804}
805
806/// \brief Determine structural equivalence of two records.
807static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
808 RecordDecl *D1, RecordDecl *D2) {
809 if (D1->isUnion() != D2->isUnion()) {
810 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
811 << Context.C2.getTypeDeclType(D2);
812 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
813 << D1->getDeclName() << (unsigned)D1->getTagKind();
814 return false;
815 }
816
Douglas Gregore2e50d332010-12-01 01:36:18 +0000817 // If both declarations are class template specializations, we know
818 // the ODR applies, so check the template and template arguments.
819 ClassTemplateSpecializationDecl *Spec1
820 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
821 ClassTemplateSpecializationDecl *Spec2
822 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
823 if (Spec1 && Spec2) {
824 // Check that the specialized templates are the same.
825 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
826 Spec2->getSpecializedTemplate()))
827 return false;
828
829 // Check that the template arguments are the same.
830 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
831 return false;
832
833 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
834 if (!IsStructurallyEquivalent(Context,
835 Spec1->getTemplateArgs().get(I),
836 Spec2->getTemplateArgs().get(I)))
837 return false;
838 }
839 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000840 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000841 else if (Spec1 || Spec2)
842 return false;
843
Douglas Gregorb4964f72010-02-15 23:54:17 +0000844 // Compare the definitions of these two records. If either or both are
845 // incomplete, we assume that they are equivalent.
846 D1 = D1->getDefinition();
847 D2 = D2->getDefinition();
848 if (!D1 || !D2)
849 return true;
850
Douglas Gregor3996e242010-02-15 22:01:00 +0000851 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
852 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
853 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
854 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregora082a492010-11-30 19:14:50 +0000855 << Context.C2.getTypeDeclType(D2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000856 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000857 << D2CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000858 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregora082a492010-11-30 19:14:50 +0000859 << D1CXX->getNumBases();
Douglas Gregor3996e242010-02-15 22:01:00 +0000860 return false;
861 }
862
863 // Check the base classes.
864 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
865 BaseEnd1 = D1CXX->bases_end(),
866 Base2 = D2CXX->bases_begin();
867 Base1 != BaseEnd1;
868 ++Base1, ++Base2) {
869 if (!IsStructurallyEquivalent(Context,
870 Base1->getType(), Base2->getType())) {
871 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
872 << Context.C2.getTypeDeclType(D2);
873 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
874 << Base2->getType()
875 << Base2->getSourceRange();
876 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
877 << Base1->getType()
878 << Base1->getSourceRange();
879 return false;
880 }
881
882 // Check virtual vs. non-virtual inheritance mismatch.
883 if (Base1->isVirtual() != Base2->isVirtual()) {
884 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
885 << Context.C2.getTypeDeclType(D2);
886 Context.Diag2(Base2->getSourceRange().getBegin(),
887 diag::note_odr_virtual_base)
888 << Base2->isVirtual() << Base2->getSourceRange();
889 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
890 << Base1->isVirtual()
891 << Base1->getSourceRange();
892 return false;
893 }
894 }
895 } else if (D1CXX->getNumBases() > 0) {
896 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
897 << Context.C2.getTypeDeclType(D2);
898 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
899 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
900 << Base1->getType()
901 << Base1->getSourceRange();
902 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
903 return false;
904 }
905 }
906
907 // Check the fields for consistency.
908 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
909 Field2End = D2->field_end();
910 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
911 Field1End = D1->field_end();
912 Field1 != Field1End;
913 ++Field1, ++Field2) {
914 if (Field2 == Field2End) {
915 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
916 << Context.C2.getTypeDeclType(D2);
917 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
918 << Field1->getDeclName() << Field1->getType();
919 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
920 return false;
921 }
922
923 if (!IsStructurallyEquivalent(Context,
924 Field1->getType(), Field2->getType())) {
925 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
926 << Context.C2.getTypeDeclType(D2);
927 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
928 << Field2->getDeclName() << Field2->getType();
929 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
930 << Field1->getDeclName() << Field1->getType();
931 return false;
932 }
933
934 if (Field1->isBitField() != Field2->isBitField()) {
935 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
936 << Context.C2.getTypeDeclType(D2);
937 if (Field1->isBitField()) {
938 llvm::APSInt Bits;
939 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
940 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
941 << Field1->getDeclName() << Field1->getType()
942 << Bits.toString(10, false);
943 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
944 << Field2->getDeclName();
945 } else {
946 llvm::APSInt Bits;
947 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
948 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
949 << Field2->getDeclName() << Field2->getType()
950 << Bits.toString(10, false);
951 Context.Diag1(Field1->getLocation(),
952 diag::note_odr_not_bit_field)
953 << Field1->getDeclName();
954 }
955 return false;
956 }
957
958 if (Field1->isBitField()) {
959 // Make sure that the bit-fields are the same length.
960 llvm::APSInt Bits1, Bits2;
961 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
962 return false;
963 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
964 return false;
965
966 if (!IsSameValue(Bits1, Bits2)) {
967 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
968 << Context.C2.getTypeDeclType(D2);
969 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
970 << Field2->getDeclName() << Field2->getType()
971 << Bits2.toString(10, false);
972 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
973 << Field1->getDeclName() << Field1->getType()
974 << Bits1.toString(10, false);
975 return false;
976 }
977 }
978 }
979
980 if (Field2 != Field2End) {
981 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
982 << Context.C2.getTypeDeclType(D2);
983 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
984 << Field2->getDeclName() << Field2->getType();
985 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
986 return false;
987 }
988
989 return true;
990}
991
992/// \brief Determine structural equivalence of two enums.
993static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
994 EnumDecl *D1, EnumDecl *D2) {
995 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
996 EC2End = D2->enumerator_end();
997 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
998 EC1End = D1->enumerator_end();
999 EC1 != EC1End; ++EC1, ++EC2) {
1000 if (EC2 == EC2End) {
1001 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1002 << Context.C2.getTypeDeclType(D2);
1003 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1004 << EC1->getDeclName()
1005 << EC1->getInitVal().toString(10);
1006 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1007 return false;
1008 }
1009
1010 llvm::APSInt Val1 = EC1->getInitVal();
1011 llvm::APSInt Val2 = EC2->getInitVal();
1012 if (!IsSameValue(Val1, Val2) ||
1013 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1014 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1015 << Context.C2.getTypeDeclType(D2);
1016 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1017 << EC2->getDeclName()
1018 << EC2->getInitVal().toString(10);
1019 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1020 << EC1->getDeclName()
1021 << EC1->getInitVal().toString(10);
1022 return false;
1023 }
1024 }
1025
1026 if (EC2 != EC2End) {
1027 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1028 << Context.C2.getTypeDeclType(D2);
1029 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1030 << EC2->getDeclName()
1031 << EC2->getInitVal().toString(10);
1032 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1033 return false;
1034 }
1035
1036 return true;
1037}
Douglas Gregora082a492010-11-30 19:14:50 +00001038
1039static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1040 TemplateParameterList *Params1,
1041 TemplateParameterList *Params2) {
1042 if (Params1->size() != Params2->size()) {
1043 Context.Diag2(Params2->getTemplateLoc(),
1044 diag::err_odr_different_num_template_parameters)
1045 << Params1->size() << Params2->size();
1046 Context.Diag1(Params1->getTemplateLoc(),
1047 diag::note_odr_template_parameter_list);
1048 return false;
1049 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001050
Douglas Gregora082a492010-11-30 19:14:50 +00001051 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1052 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1053 Context.Diag2(Params2->getParam(I)->getLocation(),
1054 diag::err_odr_different_template_parameter_kind);
1055 Context.Diag1(Params1->getParam(I)->getLocation(),
1056 diag::note_odr_template_parameter_here);
1057 return false;
1058 }
1059
1060 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1061 Params2->getParam(I))) {
1062
1063 return false;
1064 }
1065 }
1066
1067 return true;
1068}
1069
1070static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1071 TemplateTypeParmDecl *D1,
1072 TemplateTypeParmDecl *D2) {
1073 if (D1->isParameterPack() != D2->isParameterPack()) {
1074 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1075 << D2->isParameterPack();
1076 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1077 << D1->isParameterPack();
1078 return false;
1079 }
1080
1081 return true;
1082}
1083
1084static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1085 NonTypeTemplateParmDecl *D1,
1086 NonTypeTemplateParmDecl *D2) {
1087 // FIXME: Enable once we have variadic templates.
1088#if 0
1089 if (D1->isParameterPack() != D2->isParameterPack()) {
1090 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1091 << D2->isParameterPack();
1092 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1093 << D1->isParameterPack();
1094 return false;
1095 }
1096#endif
1097
1098 // Check types.
1099 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1100 Context.Diag2(D2->getLocation(),
1101 diag::err_odr_non_type_parameter_type_inconsistent)
1102 << D2->getType() << D1->getType();
1103 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1104 << D1->getType();
1105 return false;
1106 }
1107
1108 return true;
1109}
1110
1111static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1112 TemplateTemplateParmDecl *D1,
1113 TemplateTemplateParmDecl *D2) {
1114 // FIXME: Enable once we have variadic templates.
1115#if 0
1116 if (D1->isParameterPack() != D2->isParameterPack()) {
1117 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1118 << D2->isParameterPack();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1120 << D1->isParameterPack();
1121 return false;
1122 }
1123#endif
1124
1125 // Check template parameter lists.
1126 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1127 D2->getTemplateParameters());
1128}
1129
1130static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1131 ClassTemplateDecl *D1,
1132 ClassTemplateDecl *D2) {
1133 // Check template parameters.
1134 if (!IsStructurallyEquivalent(Context,
1135 D1->getTemplateParameters(),
1136 D2->getTemplateParameters()))
1137 return false;
1138
1139 // Check the templated declaration.
1140 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1141 D2->getTemplatedDecl());
1142}
1143
Douglas Gregor3996e242010-02-15 22:01:00 +00001144/// \brief Determine structural equivalence of two declarations.
1145static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1146 Decl *D1, Decl *D2) {
1147 // FIXME: Check for known structural equivalences via a callback of some sort.
1148
Douglas Gregorb4964f72010-02-15 23:54:17 +00001149 // Check whether we already know that these two declarations are not
1150 // structurally equivalent.
1151 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1152 D2->getCanonicalDecl())))
1153 return false;
1154
Douglas Gregor3996e242010-02-15 22:01:00 +00001155 // Determine whether we've already produced a tentative equivalence for D1.
1156 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1157 if (EquivToD1)
1158 return EquivToD1 == D2->getCanonicalDecl();
1159
1160 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1161 EquivToD1 = D2->getCanonicalDecl();
1162 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1163 return true;
1164}
1165
1166bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1167 Decl *D2) {
1168 if (!::IsStructurallyEquivalent(*this, D1, D2))
1169 return false;
1170
1171 return !Finish();
1172}
1173
1174bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1175 QualType T2) {
1176 if (!::IsStructurallyEquivalent(*this, T1, T2))
1177 return false;
1178
1179 return !Finish();
1180}
1181
1182bool StructuralEquivalenceContext::Finish() {
1183 while (!DeclsToCheck.empty()) {
1184 // Check the next declaration.
1185 Decl *D1 = DeclsToCheck.front();
1186 DeclsToCheck.pop_front();
1187
1188 Decl *D2 = TentativeEquivalences[D1];
1189 assert(D2 && "Unrecorded tentative equivalence?");
1190
Douglas Gregorb4964f72010-02-15 23:54:17 +00001191 bool Equivalent = true;
1192
Douglas Gregor3996e242010-02-15 22:01:00 +00001193 // FIXME: Switch on all declaration kinds. For now, we're just going to
1194 // check the obvious ones.
1195 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1196 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1197 // Check for equivalent structure names.
1198 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001199 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1200 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001201 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001202 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1203 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001204 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1205 !::IsStructurallyEquivalent(*this, Record1, Record2))
1206 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001207 } else {
1208 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001209 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001210 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001211 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001212 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1213 // Check for equivalent enum names.
1214 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001215 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1216 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001217 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001218 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1219 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001220 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1221 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1222 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001223 } else {
1224 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001225 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001226 }
Richard Smithdda56e42011-04-15 14:24:37 +00001227 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1228 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001229 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001230 Typedef2->getIdentifier()) ||
1231 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001232 Typedef1->getUnderlyingType(),
1233 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001234 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001235 } else {
1236 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001237 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001238 }
Douglas Gregora082a492010-11-30 19:14:50 +00001239 } else if (ClassTemplateDecl *ClassTemplate1
1240 = dyn_cast<ClassTemplateDecl>(D1)) {
1241 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1242 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1243 ClassTemplate2->getIdentifier()) ||
1244 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1245 Equivalent = false;
1246 } else {
1247 // Class template/non-class-template mismatch.
1248 Equivalent = false;
1249 }
1250 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1251 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1252 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1253 Equivalent = false;
1254 } else {
1255 // Kind mismatch.
1256 Equivalent = false;
1257 }
1258 } else if (NonTypeTemplateParmDecl *NTTP1
1259 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1260 if (NonTypeTemplateParmDecl *NTTP2
1261 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1262 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1263 Equivalent = false;
1264 } else {
1265 // Kind mismatch.
1266 Equivalent = false;
1267 }
1268 } else if (TemplateTemplateParmDecl *TTP1
1269 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1270 if (TemplateTemplateParmDecl *TTP2
1271 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1272 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1273 Equivalent = false;
1274 } else {
1275 // Kind mismatch.
1276 Equivalent = false;
1277 }
1278 }
1279
Douglas Gregorb4964f72010-02-15 23:54:17 +00001280 if (!Equivalent) {
1281 // Note that these two declarations are not equivalent (and we already
1282 // know about it).
1283 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1284 D2->getCanonicalDecl()));
1285 return true;
1286 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001287 // FIXME: Check other declaration kinds!
1288 }
1289
1290 return false;
1291}
1292
1293//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001294// Import Types
1295//----------------------------------------------------------------------------
1296
John McCall424cec92011-01-19 06:33:43 +00001297QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001298 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1299 << T->getTypeClassName();
1300 return QualType();
1301}
1302
John McCall424cec92011-01-19 06:33:43 +00001303QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001304 switch (T->getKind()) {
1305 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1306 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1307
1308 case BuiltinType::Char_U:
1309 // The context we're importing from has an unsigned 'char'. If we're
1310 // importing into a context with a signed 'char', translate to
1311 // 'unsigned char' instead.
1312 if (Importer.getToContext().getLangOptions().CharIsSigned)
1313 return Importer.getToContext().UnsignedCharTy;
1314
1315 return Importer.getToContext().CharTy;
1316
1317 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1318
1319 case BuiltinType::Char16:
1320 // FIXME: Make sure that the "to" context supports C++!
1321 return Importer.getToContext().Char16Ty;
1322
1323 case BuiltinType::Char32:
1324 // FIXME: Make sure that the "to" context supports C++!
1325 return Importer.getToContext().Char32Ty;
1326
1327 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1328 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1329 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1330 case BuiltinType::ULongLong:
1331 return Importer.getToContext().UnsignedLongLongTy;
1332 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1333
1334 case BuiltinType::Char_S:
1335 // The context we're importing from has an unsigned 'char'. If we're
1336 // importing into a context with a signed 'char', translate to
1337 // 'unsigned char' instead.
1338 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1339 return Importer.getToContext().SignedCharTy;
1340
1341 return Importer.getToContext().CharTy;
1342
1343 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattnerad3467e2010-12-25 23:25:43 +00001344 case BuiltinType::WChar_S:
1345 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001346 // FIXME: If not in C++, shall we translate to the C equivalent of
1347 // wchar_t?
1348 return Importer.getToContext().WCharTy;
1349
1350 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1351 case BuiltinType::Int : return Importer.getToContext().IntTy;
1352 case BuiltinType::Long : return Importer.getToContext().LongTy;
1353 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1354 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1355 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1356 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1357 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1358
1359 case BuiltinType::NullPtr:
1360 // FIXME: Make sure that the "to" context supports C++0x!
1361 return Importer.getToContext().NullPtrTy;
1362
1363 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1364 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall31996342011-04-07 08:22:57 +00001365 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall0009fcc2011-04-26 20:42:42 +00001366 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001367
1368 case BuiltinType::ObjCId:
1369 // FIXME: Make sure that the "to" context supports Objective-C!
1370 return Importer.getToContext().ObjCBuiltinIdTy;
1371
1372 case BuiltinType::ObjCClass:
1373 return Importer.getToContext().ObjCBuiltinClassTy;
1374
1375 case BuiltinType::ObjCSel:
1376 return Importer.getToContext().ObjCBuiltinSelTy;
1377 }
1378
1379 return QualType();
1380}
1381
John McCall424cec92011-01-19 06:33:43 +00001382QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001383 QualType ToElementType = Importer.Import(T->getElementType());
1384 if (ToElementType.isNull())
1385 return QualType();
1386
1387 return Importer.getToContext().getComplexType(ToElementType);
1388}
1389
John McCall424cec92011-01-19 06:33:43 +00001390QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001391 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1392 if (ToPointeeType.isNull())
1393 return QualType();
1394
1395 return Importer.getToContext().getPointerType(ToPointeeType);
1396}
1397
John McCall424cec92011-01-19 06:33:43 +00001398QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001399 // FIXME: Check for blocks support in "to" context.
1400 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1401 if (ToPointeeType.isNull())
1402 return QualType();
1403
1404 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1405}
1406
John McCall424cec92011-01-19 06:33:43 +00001407QualType
1408ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001409 // FIXME: Check for C++ support in "to" context.
1410 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1411 if (ToPointeeType.isNull())
1412 return QualType();
1413
1414 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1415}
1416
John McCall424cec92011-01-19 06:33:43 +00001417QualType
1418ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001419 // FIXME: Check for C++0x support in "to" context.
1420 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1421 if (ToPointeeType.isNull())
1422 return QualType();
1423
1424 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1425}
1426
John McCall424cec92011-01-19 06:33:43 +00001427QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001428 // FIXME: Check for C++ support in "to" context.
1429 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1430 if (ToPointeeType.isNull())
1431 return QualType();
1432
1433 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1434 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1435 ClassType.getTypePtr());
1436}
1437
John McCall424cec92011-01-19 06:33:43 +00001438QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439 QualType ToElementType = Importer.Import(T->getElementType());
1440 if (ToElementType.isNull())
1441 return QualType();
1442
1443 return Importer.getToContext().getConstantArrayType(ToElementType,
1444 T->getSize(),
1445 T->getSizeModifier(),
1446 T->getIndexTypeCVRQualifiers());
1447}
1448
John McCall424cec92011-01-19 06:33:43 +00001449QualType
1450ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001451 QualType ToElementType = Importer.Import(T->getElementType());
1452 if (ToElementType.isNull())
1453 return QualType();
1454
1455 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1456 T->getSizeModifier(),
1457 T->getIndexTypeCVRQualifiers());
1458}
1459
John McCall424cec92011-01-19 06:33:43 +00001460QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001461 QualType ToElementType = Importer.Import(T->getElementType());
1462 if (ToElementType.isNull())
1463 return QualType();
1464
1465 Expr *Size = Importer.Import(T->getSizeExpr());
1466 if (!Size)
1467 return QualType();
1468
1469 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1470 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1471 T->getSizeModifier(),
1472 T->getIndexTypeCVRQualifiers(),
1473 Brackets);
1474}
1475
John McCall424cec92011-01-19 06:33:43 +00001476QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001477 QualType ToElementType = Importer.Import(T->getElementType());
1478 if (ToElementType.isNull())
1479 return QualType();
1480
1481 return Importer.getToContext().getVectorType(ToElementType,
1482 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001483 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001484}
1485
John McCall424cec92011-01-19 06:33:43 +00001486QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001487 QualType ToElementType = Importer.Import(T->getElementType());
1488 if (ToElementType.isNull())
1489 return QualType();
1490
1491 return Importer.getToContext().getExtVectorType(ToElementType,
1492 T->getNumElements());
1493}
1494
John McCall424cec92011-01-19 06:33:43 +00001495QualType
1496ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001497 // FIXME: What happens if we're importing a function without a prototype
1498 // into C++? Should we make it variadic?
1499 QualType ToResultType = Importer.Import(T->getResultType());
1500 if (ToResultType.isNull())
1501 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001502
Douglas Gregor96e578d2010-02-05 17:54:41 +00001503 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001504 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001505}
1506
John McCall424cec92011-01-19 06:33:43 +00001507QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001508 QualType ToResultType = Importer.Import(T->getResultType());
1509 if (ToResultType.isNull())
1510 return QualType();
1511
1512 // Import argument types
1513 llvm::SmallVector<QualType, 4> ArgTypes;
1514 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1515 AEnd = T->arg_type_end();
1516 A != AEnd; ++A) {
1517 QualType ArgType = Importer.Import(*A);
1518 if (ArgType.isNull())
1519 return QualType();
1520 ArgTypes.push_back(ArgType);
1521 }
1522
1523 // Import exception types
1524 llvm::SmallVector<QualType, 4> ExceptionTypes;
1525 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1526 EEnd = T->exception_end();
1527 E != EEnd; ++E) {
1528 QualType ExceptionType = Importer.Import(*E);
1529 if (ExceptionType.isNull())
1530 return QualType();
1531 ExceptionTypes.push_back(ExceptionType);
1532 }
John McCalldb40c7f2010-12-14 08:05:40 +00001533
1534 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1535 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001536
1537 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalldb40c7f2010-12-14 08:05:40 +00001538 ArgTypes.size(), EPI);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001539}
1540
John McCall424cec92011-01-19 06:33:43 +00001541QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001542 TypedefNameDecl *ToDecl
1543 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001544 if (!ToDecl)
1545 return QualType();
1546
1547 return Importer.getToContext().getTypeDeclType(ToDecl);
1548}
1549
John McCall424cec92011-01-19 06:33:43 +00001550QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001551 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1552 if (!ToExpr)
1553 return QualType();
1554
1555 return Importer.getToContext().getTypeOfExprType(ToExpr);
1556}
1557
John McCall424cec92011-01-19 06:33:43 +00001558QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001559 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1560 if (ToUnderlyingType.isNull())
1561 return QualType();
1562
1563 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1564}
1565
John McCall424cec92011-01-19 06:33:43 +00001566QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001567 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001568 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1569 if (!ToExpr)
1570 return QualType();
1571
1572 return Importer.getToContext().getDecltypeType(ToExpr);
1573}
1574
Richard Smith30482bc2011-02-20 03:19:35 +00001575QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1576 // FIXME: Make sure that the "to" context supports C++0x!
1577 QualType FromDeduced = T->getDeducedType();
1578 QualType ToDeduced;
1579 if (!FromDeduced.isNull()) {
1580 ToDeduced = Importer.Import(FromDeduced);
1581 if (ToDeduced.isNull())
1582 return QualType();
1583 }
1584
1585 return Importer.getToContext().getAutoType(ToDeduced);
1586}
1587
John McCall424cec92011-01-19 06:33:43 +00001588QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001589 RecordDecl *ToDecl
1590 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1591 if (!ToDecl)
1592 return QualType();
1593
1594 return Importer.getToContext().getTagDeclType(ToDecl);
1595}
1596
John McCall424cec92011-01-19 06:33:43 +00001597QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598 EnumDecl *ToDecl
1599 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1600 if (!ToDecl)
1601 return QualType();
1602
1603 return Importer.getToContext().getTagDeclType(ToDecl);
1604}
1605
Douglas Gregore2e50d332010-12-01 01:36:18 +00001606QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001607 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001608 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1609 if (ToTemplate.isNull())
1610 return QualType();
1611
1612 llvm::SmallVector<TemplateArgument, 2> ToTemplateArgs;
1613 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1614 return QualType();
1615
1616 QualType ToCanonType;
1617 if (!QualType(T, 0).isCanonical()) {
1618 QualType FromCanonType
1619 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1620 ToCanonType =Importer.Import(FromCanonType);
1621 if (ToCanonType.isNull())
1622 return QualType();
1623 }
1624 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1625 ToTemplateArgs.data(),
1626 ToTemplateArgs.size(),
1627 ToCanonType);
1628}
1629
John McCall424cec92011-01-19 06:33:43 +00001630QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001631 NestedNameSpecifier *ToQualifier = 0;
1632 // Note: the qualifier in an ElaboratedType is optional.
1633 if (T->getQualifier()) {
1634 ToQualifier = Importer.Import(T->getQualifier());
1635 if (!ToQualifier)
1636 return QualType();
1637 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638
1639 QualType ToNamedType = Importer.Import(T->getNamedType());
1640 if (ToNamedType.isNull())
1641 return QualType();
1642
Abramo Bagnara6150c882010-05-11 21:36:43 +00001643 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1644 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001645}
1646
John McCall424cec92011-01-19 06:33:43 +00001647QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001648 ObjCInterfaceDecl *Class
1649 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1650 if (!Class)
1651 return QualType();
1652
John McCall8b07ec22010-05-15 11:32:37 +00001653 return Importer.getToContext().getObjCInterfaceType(Class);
1654}
1655
John McCall424cec92011-01-19 06:33:43 +00001656QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001657 QualType ToBaseType = Importer.Import(T->getBaseType());
1658 if (ToBaseType.isNull())
1659 return QualType();
1660
Douglas Gregor96e578d2010-02-05 17:54:41 +00001661 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001662 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001663 PEnd = T->qual_end();
1664 P != PEnd; ++P) {
1665 ObjCProtocolDecl *Protocol
1666 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1667 if (!Protocol)
1668 return QualType();
1669 Protocols.push_back(Protocol);
1670 }
1671
John McCall8b07ec22010-05-15 11:32:37 +00001672 return Importer.getToContext().getObjCObjectType(ToBaseType,
1673 Protocols.data(),
1674 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001675}
1676
John McCall424cec92011-01-19 06:33:43 +00001677QualType
1678ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001679 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1680 if (ToPointeeType.isNull())
1681 return QualType();
1682
John McCall8b07ec22010-05-15 11:32:37 +00001683 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001684}
1685
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001686//----------------------------------------------------------------------------
1687// Import Declarations
1688//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001689bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1690 DeclContext *&LexicalDC,
1691 DeclarationName &Name,
1692 SourceLocation &Loc) {
1693 // Import the context of this declaration.
1694 DC = Importer.ImportContext(D->getDeclContext());
1695 if (!DC)
1696 return true;
1697
1698 LexicalDC = DC;
1699 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1700 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1701 if (!LexicalDC)
1702 return true;
1703 }
1704
1705 // Import the name of this declaration.
1706 Name = Importer.Import(D->getDeclName());
1707 if (D->getDeclName() && !Name)
1708 return true;
1709
1710 // Import the location of this declaration.
1711 Loc = Importer.Import(D->getLocation());
1712 return false;
1713}
1714
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001715void
1716ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1717 DeclarationNameInfo& To) {
1718 // NOTE: To.Name and To.Loc are already imported.
1719 // We only have to import To.LocInfo.
1720 switch (To.getName().getNameKind()) {
1721 case DeclarationName::Identifier:
1722 case DeclarationName::ObjCZeroArgSelector:
1723 case DeclarationName::ObjCOneArgSelector:
1724 case DeclarationName::ObjCMultiArgSelector:
1725 case DeclarationName::CXXUsingDirective:
1726 return;
1727
1728 case DeclarationName::CXXOperatorName: {
1729 SourceRange Range = From.getCXXOperatorNameRange();
1730 To.setCXXOperatorNameRange(Importer.Import(Range));
1731 return;
1732 }
1733 case DeclarationName::CXXLiteralOperatorName: {
1734 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1735 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1736 return;
1737 }
1738 case DeclarationName::CXXConstructorName:
1739 case DeclarationName::CXXDestructorName:
1740 case DeclarationName::CXXConversionFunctionName: {
1741 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1742 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1743 return;
1744 }
1745 assert(0 && "Unknown name kind.");
1746 }
1747}
1748
Douglas Gregor0a791672011-01-18 03:11:38 +00001749void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1750 if (Importer.isMinimalImport() && !ForceImport) {
1751 if (DeclContext *ToDC = Importer.ImportContext(FromDC)) {
1752 ToDC->setHasExternalLexicalStorage();
1753 ToDC->setHasExternalVisibleStorage();
1754 }
1755 return;
1756 }
1757
Douglas Gregor968d6332010-02-21 18:24:45 +00001758 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1759 FromEnd = FromDC->decls_end();
1760 From != FromEnd;
1761 ++From)
1762 Importer.Import(*From);
1763}
1764
Douglas Gregore2e50d332010-12-01 01:36:18 +00001765bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To) {
1766 if (To->getDefinition())
1767 return false;
1768
1769 To->startDefinition();
1770
1771 // Add base classes.
1772 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1773 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1774
1775 llvm::SmallVector<CXXBaseSpecifier *, 4> Bases;
1776 for (CXXRecordDecl::base_class_iterator
1777 Base1 = FromCXX->bases_begin(),
1778 FromBaseEnd = FromCXX->bases_end();
1779 Base1 != FromBaseEnd;
1780 ++Base1) {
1781 QualType T = Importer.Import(Base1->getType());
1782 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001783 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001784
1785 SourceLocation EllipsisLoc;
1786 if (Base1->isPackExpansion())
1787 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregore2e50d332010-12-01 01:36:18 +00001788
1789 Bases.push_back(
1790 new (Importer.getToContext())
1791 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1792 Base1->isVirtual(),
1793 Base1->isBaseOfClass(),
1794 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001795 Importer.Import(Base1->getTypeSourceInfo()),
1796 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00001797 }
1798 if (!Bases.empty())
1799 ToCXX->setBases(Bases.data(), Bases.size());
1800 }
1801
1802 ImportDeclContext(From);
1803 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00001804 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001805}
1806
Douglas Gregora082a492010-11-30 19:14:50 +00001807TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1808 TemplateParameterList *Params) {
1809 llvm::SmallVector<NamedDecl *, 4> ToParams;
1810 ToParams.reserve(Params->size());
1811 for (TemplateParameterList::iterator P = Params->begin(),
1812 PEnd = Params->end();
1813 P != PEnd; ++P) {
1814 Decl *To = Importer.Import(*P);
1815 if (!To)
1816 return 0;
1817
1818 ToParams.push_back(cast<NamedDecl>(To));
1819 }
1820
1821 return TemplateParameterList::Create(Importer.getToContext(),
1822 Importer.Import(Params->getTemplateLoc()),
1823 Importer.Import(Params->getLAngleLoc()),
1824 ToParams.data(), ToParams.size(),
1825 Importer.Import(Params->getRAngleLoc()));
1826}
1827
Douglas Gregore2e50d332010-12-01 01:36:18 +00001828TemplateArgument
1829ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1830 switch (From.getKind()) {
1831 case TemplateArgument::Null:
1832 return TemplateArgument();
1833
1834 case TemplateArgument::Type: {
1835 QualType ToType = Importer.Import(From.getAsType());
1836 if (ToType.isNull())
1837 return TemplateArgument();
1838 return TemplateArgument(ToType);
1839 }
1840
1841 case TemplateArgument::Integral: {
1842 QualType ToType = Importer.Import(From.getIntegralType());
1843 if (ToType.isNull())
1844 return TemplateArgument();
1845 return TemplateArgument(*From.getAsIntegral(), ToType);
1846 }
1847
1848 case TemplateArgument::Declaration:
1849 if (Decl *To = Importer.Import(From.getAsDecl()))
1850 return TemplateArgument(To);
1851 return TemplateArgument();
1852
1853 case TemplateArgument::Template: {
1854 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1855 if (ToTemplate.isNull())
1856 return TemplateArgument();
1857
1858 return TemplateArgument(ToTemplate);
1859 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001860
1861 case TemplateArgument::TemplateExpansion: {
1862 TemplateName ToTemplate
1863 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1864 if (ToTemplate.isNull())
1865 return TemplateArgument();
1866
Douglas Gregore1d60df2011-01-14 23:41:42 +00001867 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00001868 }
1869
Douglas Gregore2e50d332010-12-01 01:36:18 +00001870 case TemplateArgument::Expression:
1871 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1872 return TemplateArgument(ToExpr);
1873 return TemplateArgument();
1874
1875 case TemplateArgument::Pack: {
1876 llvm::SmallVector<TemplateArgument, 2> ToPack;
1877 ToPack.reserve(From.pack_size());
1878 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1879 return TemplateArgument();
1880
1881 TemplateArgument *ToArgs
1882 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1883 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1884 return TemplateArgument(ToArgs, ToPack.size());
1885 }
1886 }
1887
1888 llvm_unreachable("Invalid template argument kind");
1889 return TemplateArgument();
1890}
1891
1892bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1893 unsigned NumFromArgs,
1894 llvm::SmallVectorImpl<TemplateArgument> &ToArgs) {
1895 for (unsigned I = 0; I != NumFromArgs; ++I) {
1896 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1897 if (To.isNull() && !FromArgs[I].isNull())
1898 return true;
1899
1900 ToArgs.push_back(To);
1901 }
1902
1903 return false;
1904}
1905
Douglas Gregor5c73e912010-02-11 00:48:18 +00001906bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor3996e242010-02-15 22:01:00 +00001907 RecordDecl *ToRecord) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001908 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001909 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001910 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001911 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00001912}
1913
Douglas Gregor98c10182010-02-12 22:17:39 +00001914bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001915 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001916 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001917 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00001918 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00001919}
1920
Douglas Gregora082a492010-11-30 19:14:50 +00001921bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
1922 ClassTemplateDecl *To) {
1923 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
1924 Importer.getToContext(),
1925 Importer.getNonEquivalentDecls());
1926 return Ctx.IsStructurallyEquivalent(From, To);
1927}
1928
Douglas Gregore4c83e42010-02-09 22:48:33 +00001929Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00001930 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00001931 << D->getDeclKindName();
1932 return 0;
1933}
1934
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001935Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
1936 // Import the major distinguishing characteristics of this namespace.
1937 DeclContext *DC, *LexicalDC;
1938 DeclarationName Name;
1939 SourceLocation Loc;
1940 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
1941 return 0;
1942
1943 NamespaceDecl *MergeWithNamespace = 0;
1944 if (!Name) {
1945 // This is an anonymous namespace. Adopt an existing anonymous
1946 // namespace if we can.
1947 // FIXME: Not testable.
1948 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1949 MergeWithNamespace = TU->getAnonymousNamespace();
1950 else
1951 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
1952 } else {
1953 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
1954 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
1955 Lookup.first != Lookup.second;
1956 ++Lookup.first) {
John McCalle87beb22010-04-23 18:46:30 +00001957 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001958 continue;
1959
1960 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
1961 MergeWithNamespace = FoundNS;
1962 ConflictingDecls.clear();
1963 break;
1964 }
1965
1966 ConflictingDecls.push_back(*Lookup.first);
1967 }
1968
1969 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00001970 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001971 ConflictingDecls.data(),
1972 ConflictingDecls.size());
1973 }
1974 }
1975
1976 // Create the "to" namespace, if needed.
1977 NamespaceDecl *ToNamespace = MergeWithNamespace;
1978 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00001979 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
1980 Importer.Import(D->getLocStart()),
1981 Loc, Name.getAsIdentifierInfo());
Douglas Gregorf18a2c72010-02-21 18:26:36 +00001982 ToNamespace->setLexicalDeclContext(LexicalDC);
1983 LexicalDC->addDecl(ToNamespace);
1984
1985 // If this is an anonymous namespace, register it as the anonymous
1986 // namespace within its context.
1987 if (!Name) {
1988 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
1989 TU->setAnonymousNamespace(ToNamespace);
1990 else
1991 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
1992 }
1993 }
1994 Importer.Imported(D, ToNamespace);
1995
1996 ImportDeclContext(D);
1997
1998 return ToNamespace;
1999}
2000
Richard Smithdda56e42011-04-15 14:24:37 +00002001Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002002 // Import the major distinguishing characteristics of this typedef.
2003 DeclContext *DC, *LexicalDC;
2004 DeclarationName Name;
2005 SourceLocation Loc;
2006 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2007 return 0;
2008
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002009 // If this typedef is not in block scope, determine whether we've
2010 // seen a typedef with the same name (that we can merge with) or any
2011 // other entity by that name (which name lookup could conflict with).
2012 if (!DC->isFunctionOrMethod()) {
2013 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2014 unsigned IDNS = Decl::IDNS_Ordinary;
2015 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2016 Lookup.first != Lookup.second;
2017 ++Lookup.first) {
2018 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2019 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002020 if (TypedefNameDecl *FoundTypedef =
2021 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002022 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2023 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002024 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002025 }
2026
2027 ConflictingDecls.push_back(*Lookup.first);
2028 }
2029
2030 if (!ConflictingDecls.empty()) {
2031 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2032 ConflictingDecls.data(),
2033 ConflictingDecls.size());
2034 if (!Name)
2035 return 0;
2036 }
2037 }
2038
Douglas Gregorb4964f72010-02-15 23:54:17 +00002039 // Import the underlying type of this typedef;
2040 QualType T = Importer.Import(D->getUnderlyingType());
2041 if (T.isNull())
2042 return 0;
2043
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002044 // Create the new typedef node.
2045 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002046 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002047 TypedefNameDecl *ToTypedef;
2048 if (IsAlias)
2049 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2050 StartL, Loc,
2051 Name.getAsIdentifierInfo(),
2052 TInfo);
2053 else
2054 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2055 StartL, Loc,
2056 Name.getAsIdentifierInfo(),
2057 TInfo);
Douglas Gregordd483172010-02-22 17:42:47 +00002058 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002059 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002060 Importer.Imported(D, ToTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002061 LexicalDC->addDecl(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002062
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002063 return ToTypedef;
2064}
2065
Richard Smithdda56e42011-04-15 14:24:37 +00002066Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2067 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2068}
2069
2070Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2071 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2072}
2073
Douglas Gregor98c10182010-02-12 22:17:39 +00002074Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2075 // Import the major distinguishing characteristics of this enum.
2076 DeclContext *DC, *LexicalDC;
2077 DeclarationName Name;
2078 SourceLocation Loc;
2079 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2080 return 0;
2081
2082 // Figure out what enum name we're looking for.
2083 unsigned IDNS = Decl::IDNS_Tag;
2084 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002085 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2086 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002087 IDNS = Decl::IDNS_Ordinary;
2088 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2089 IDNS |= Decl::IDNS_Ordinary;
2090
2091 // We may already have an enum of the same name; try to find and match it.
2092 if (!DC->isFunctionOrMethod() && SearchName) {
2093 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2094 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2095 Lookup.first != Lookup.second;
2096 ++Lookup.first) {
2097 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2098 continue;
2099
2100 Decl *Found = *Lookup.first;
Richard Smithdda56e42011-04-15 14:24:37 +00002101 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002102 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2103 Found = Tag->getDecl();
2104 }
2105
2106 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002107 if (IsStructuralMatch(D, FoundEnum))
2108 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002109 }
2110
2111 ConflictingDecls.push_back(*Lookup.first);
2112 }
2113
2114 if (!ConflictingDecls.empty()) {
2115 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2116 ConflictingDecls.data(),
2117 ConflictingDecls.size());
2118 }
2119 }
2120
2121 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002122 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2123 Importer.Import(D->getLocStart()),
2124 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002125 D->isScoped(), D->isScopedUsingClassTag(),
2126 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002127 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002128 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002129 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002130 D2->setLexicalDeclContext(LexicalDC);
2131 Importer.Imported(D, D2);
2132 LexicalDC->addDecl(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002133
2134 // Import the integer type.
2135 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2136 if (ToIntegerType.isNull())
2137 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002138 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002139
2140 // Import the definition
2141 if (D->isDefinition()) {
2142 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(D));
2143 if (T.isNull())
2144 return 0;
2145
2146 QualType ToPromotionType = Importer.Import(D->getPromotionType());
2147 if (ToPromotionType.isNull())
2148 return 0;
2149
Douglas Gregor3996e242010-02-15 22:01:00 +00002150 D2->startDefinition();
Douglas Gregor968d6332010-02-21 18:24:45 +00002151 ImportDeclContext(D);
John McCall9aa35be2010-05-06 08:49:23 +00002152
2153 // FIXME: we might need to merge the number of positive or negative bits
2154 // if the enumerator lists don't match.
2155 D2->completeDefinition(T, ToPromotionType,
2156 D->getNumPositiveBits(),
2157 D->getNumNegativeBits());
Douglas Gregor98c10182010-02-12 22:17:39 +00002158 }
2159
Douglas Gregor3996e242010-02-15 22:01:00 +00002160 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002161}
2162
Douglas Gregor5c73e912010-02-11 00:48:18 +00002163Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2164 // If this record has a definition in the translation unit we're coming from,
2165 // but this particular declaration is not that definition, import the
2166 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002167 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002168 if (Definition && Definition != D) {
2169 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002170 if (!ImportedDef)
2171 return 0;
2172
2173 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002174 }
2175
2176 // Import the major distinguishing characteristics of this record.
2177 DeclContext *DC, *LexicalDC;
2178 DeclarationName Name;
2179 SourceLocation Loc;
2180 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2181 return 0;
2182
2183 // Figure out what structure name we're looking for.
2184 unsigned IDNS = Decl::IDNS_Tag;
2185 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002186 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2187 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002188 IDNS = Decl::IDNS_Ordinary;
2189 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2190 IDNS |= Decl::IDNS_Ordinary;
2191
2192 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002193 RecordDecl *AdoptDecl = 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002194 if (!DC->isFunctionOrMethod() && SearchName) {
2195 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2196 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2197 Lookup.first != Lookup.second;
2198 ++Lookup.first) {
2199 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2200 continue;
2201
2202 Decl *Found = *Lookup.first;
Richard Smithdda56e42011-04-15 14:24:37 +00002203 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002204 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2205 Found = Tag->getDecl();
2206 }
2207
2208 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregor25791052010-02-12 00:09:27 +00002209 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2210 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2211 // The record types structurally match, or the "from" translation
2212 // unit only had a forward declaration anyway; call it the same
2213 // function.
2214 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002215 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002216 }
2217 } else {
2218 // We have a forward declaration of this type, so adopt that forward
2219 // declaration rather than building a new one.
2220 AdoptDecl = FoundRecord;
2221 continue;
2222 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002223 }
2224
2225 ConflictingDecls.push_back(*Lookup.first);
2226 }
2227
2228 if (!ConflictingDecls.empty()) {
2229 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2230 ConflictingDecls.data(),
2231 ConflictingDecls.size());
2232 }
2233 }
2234
2235 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002236 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002237 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002238 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002239 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002240 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002241 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002242 DC, StartLoc, Loc,
2243 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002244 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002245 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002246 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002247 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002248 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002249 }
Douglas Gregor14454802011-02-25 02:25:35 +00002250
2251 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002252 D2->setLexicalDeclContext(LexicalDC);
2253 LexicalDC->addDecl(D2);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002254 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002255
Douglas Gregor3996e242010-02-15 22:01:00 +00002256 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002257
Douglas Gregore2e50d332010-12-01 01:36:18 +00002258 if (D->isDefinition() && ImportDefinition(D, D2))
2259 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002260
Douglas Gregor3996e242010-02-15 22:01:00 +00002261 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002262}
2263
Douglas Gregor98c10182010-02-12 22:17:39 +00002264Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2265 // Import the major distinguishing characteristics of this enumerator.
2266 DeclContext *DC, *LexicalDC;
2267 DeclarationName Name;
2268 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002269 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002270 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002271
2272 QualType T = Importer.Import(D->getType());
2273 if (T.isNull())
2274 return 0;
2275
Douglas Gregor98c10182010-02-12 22:17:39 +00002276 // Determine whether there are any other declarations with the same name and
2277 // in the same context.
2278 if (!LexicalDC->isFunctionOrMethod()) {
2279 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2280 unsigned IDNS = Decl::IDNS_Ordinary;
2281 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2282 Lookup.first != Lookup.second;
2283 ++Lookup.first) {
2284 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2285 continue;
2286
2287 ConflictingDecls.push_back(*Lookup.first);
2288 }
2289
2290 if (!ConflictingDecls.empty()) {
2291 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2292 ConflictingDecls.data(),
2293 ConflictingDecls.size());
2294 if (!Name)
2295 return 0;
2296 }
2297 }
2298
2299 Expr *Init = Importer.Import(D->getInitExpr());
2300 if (D->getInitExpr() && !Init)
2301 return 0;
2302
2303 EnumConstantDecl *ToEnumerator
2304 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2305 Name.getAsIdentifierInfo(), T,
2306 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002307 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002308 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002309 Importer.Imported(D, ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002310 LexicalDC->addDecl(ToEnumerator);
2311 return ToEnumerator;
2312}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002313
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002314Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2315 // Import the major distinguishing characteristics of this function.
2316 DeclContext *DC, *LexicalDC;
2317 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002318 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002319 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002320 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002321
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002322 // Try to find a function in our own ("to") context with the same name, same
2323 // type, and in the same context as the function we're importing.
2324 if (!LexicalDC->isFunctionOrMethod()) {
2325 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2326 unsigned IDNS = Decl::IDNS_Ordinary;
2327 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2328 Lookup.first != Lookup.second;
2329 ++Lookup.first) {
2330 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2331 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002332
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002333 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2334 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2335 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002336 if (Importer.IsStructurallyEquivalent(D->getType(),
2337 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002338 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002339 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002340 }
2341
2342 // FIXME: Check for overloading more carefully, e.g., by boosting
2343 // Sema::IsOverload out to the AST library.
2344
2345 // Function overloading is okay in C++.
2346 if (Importer.getToContext().getLangOptions().CPlusPlus)
2347 continue;
2348
2349 // Complain about inconsistent function types.
2350 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002351 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002352 Importer.ToDiag(FoundFunction->getLocation(),
2353 diag::note_odr_value_here)
2354 << FoundFunction->getType();
2355 }
2356 }
2357
2358 ConflictingDecls.push_back(*Lookup.first);
2359 }
2360
2361 if (!ConflictingDecls.empty()) {
2362 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2363 ConflictingDecls.data(),
2364 ConflictingDecls.size());
2365 if (!Name)
2366 return 0;
2367 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002368 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002369
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002370 DeclarationNameInfo NameInfo(Name, Loc);
2371 // Import additional name location/type info.
2372 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2373
Douglas Gregorb4964f72010-02-15 23:54:17 +00002374 // Import the type.
2375 QualType T = Importer.Import(D->getType());
2376 if (T.isNull())
2377 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002378
2379 // Import the function parameters.
2380 llvm::SmallVector<ParmVarDecl *, 8> Parameters;
2381 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2382 P != PEnd; ++P) {
2383 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2384 if (!ToP)
2385 return 0;
2386
2387 Parameters.push_back(ToP);
2388 }
2389
2390 // Create the imported function.
2391 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002392 FunctionDecl *ToFunction = 0;
2393 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2394 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2395 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002396 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002397 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002398 FromConstructor->isExplicit(),
2399 D->isInlineSpecified(),
Alexis Hunt1adeff92011-05-05 03:36:28 +00002400 D->isImplicit(),
2401 FromConstructor->isExplicitlyDefaulted());
Douglas Gregor00eace12010-02-21 18:29:16 +00002402 } else if (isa<CXXDestructorDecl>(D)) {
2403 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2404 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002405 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002406 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002407 D->isInlineSpecified(),
2408 D->isImplicit());
2409 } else if (CXXConversionDecl *FromConversion
2410 = dyn_cast<CXXConversionDecl>(D)) {
2411 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2412 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002413 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002414 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002415 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002416 FromConversion->isExplicit(),
2417 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002418 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2419 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2420 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002421 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002422 NameInfo, T, TInfo,
2423 Method->isStatic(),
2424 Method->getStorageClassAsWritten(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002425 Method->isInlineSpecified(),
2426 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002427 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002428 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002429 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002430 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002431 D->getStorageClassAsWritten(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002432 D->isInlineSpecified(),
2433 D->hasWrittenPrototype());
2434 }
John McCall3e11ebe2010-03-15 10:12:16 +00002435
2436 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002437 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002438 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002439 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002440 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2441 ToFunction->setTrivial(D->isTrivial());
2442 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002443 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002444
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002445 // Set the parameters.
2446 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002447 Parameters[I]->setOwningFunction(ToFunction);
2448 ToFunction->addDecl(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002449 }
Douglas Gregor43f54792010-02-17 02:12:47 +00002450 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002451
2452 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002453
2454 // Add this function to the lexical context.
2455 LexicalDC->addDecl(ToFunction);
2456
Douglas Gregor43f54792010-02-17 02:12:47 +00002457 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002458}
2459
Douglas Gregor00eace12010-02-21 18:29:16 +00002460Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2461 return VisitFunctionDecl(D);
2462}
2463
2464Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2465 return VisitCXXMethodDecl(D);
2466}
2467
2468Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2469 return VisitCXXMethodDecl(D);
2470}
2471
2472Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2473 return VisitCXXMethodDecl(D);
2474}
2475
Douglas Gregor5c73e912010-02-11 00:48:18 +00002476Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2477 // Import the major distinguishing characteristics of a variable.
2478 DeclContext *DC, *LexicalDC;
2479 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002480 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002481 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2482 return 0;
2483
2484 // Import the type.
2485 QualType T = Importer.Import(D->getType());
2486 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002487 return 0;
2488
2489 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2490 Expr *BitWidth = Importer.Import(D->getBitWidth());
2491 if (!BitWidth && D->getBitWidth())
2492 return 0;
2493
Abramo Bagnaradff19302011-03-08 08:55:46 +00002494 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2495 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002496 Loc, Name.getAsIdentifierInfo(),
2497 T, TInfo, BitWidth, D->isMutable());
Douglas Gregordd483172010-02-22 17:42:47 +00002498 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002499 ToField->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002500 Importer.Imported(D, ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002501 LexicalDC->addDecl(ToField);
2502 return ToField;
2503}
2504
Francois Pichet783dd6e2010-11-21 06:08:52 +00002505Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2506 // Import the major distinguishing characteristics of a variable.
2507 DeclContext *DC, *LexicalDC;
2508 DeclarationName Name;
2509 SourceLocation Loc;
2510 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2511 return 0;
2512
2513 // Import the type.
2514 QualType T = Importer.Import(D->getType());
2515 if (T.isNull())
2516 return 0;
2517
2518 NamedDecl **NamedChain =
2519 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2520
2521 unsigned i = 0;
2522 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2523 PE = D->chain_end(); PI != PE; ++PI) {
2524 Decl* D = Importer.Import(*PI);
2525 if (!D)
2526 return 0;
2527 NamedChain[i++] = cast<NamedDecl>(D);
2528 }
2529
2530 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2531 Importer.getToContext(), DC,
2532 Loc, Name.getAsIdentifierInfo(), T,
2533 NamedChain, D->getChainingSize());
2534 ToIndirectField->setAccess(D->getAccess());
2535 ToIndirectField->setLexicalDeclContext(LexicalDC);
2536 Importer.Imported(D, ToIndirectField);
2537 LexicalDC->addDecl(ToIndirectField);
2538 return ToIndirectField;
2539}
2540
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002541Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2542 // Import the major distinguishing characteristics of an ivar.
2543 DeclContext *DC, *LexicalDC;
2544 DeclarationName Name;
2545 SourceLocation Loc;
2546 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2547 return 0;
2548
2549 // Determine whether we've already imported this ivar
2550 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2551 Lookup.first != Lookup.second;
2552 ++Lookup.first) {
2553 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2554 if (Importer.IsStructurallyEquivalent(D->getType(),
2555 FoundIvar->getType())) {
2556 Importer.Imported(D, FoundIvar);
2557 return FoundIvar;
2558 }
2559
2560 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2561 << Name << D->getType() << FoundIvar->getType();
2562 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2563 << FoundIvar->getType();
2564 return 0;
2565 }
2566 }
2567
2568 // Import the type.
2569 QualType T = Importer.Import(D->getType());
2570 if (T.isNull())
2571 return 0;
2572
2573 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2574 Expr *BitWidth = Importer.Import(D->getBitWidth());
2575 if (!BitWidth && D->getBitWidth())
2576 return 0;
2577
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002578 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2579 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002580 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002581 Loc, Name.getAsIdentifierInfo(),
2582 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002583 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002584 ToIvar->setLexicalDeclContext(LexicalDC);
2585 Importer.Imported(D, ToIvar);
2586 LexicalDC->addDecl(ToIvar);
2587 return ToIvar;
2588
2589}
2590
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002591Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2592 // Import the major distinguishing characteristics of a variable.
2593 DeclContext *DC, *LexicalDC;
2594 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002595 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002596 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002597 return 0;
2598
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002599 // Try to find a variable in our own ("to") context with the same name and
2600 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002601 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002602 VarDecl *MergeWithVar = 0;
2603 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
2604 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor62d311f2010-02-09 19:21:46 +00002605 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002606 Lookup.first != Lookup.second;
2607 ++Lookup.first) {
2608 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2609 continue;
2610
2611 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2612 // We have found a variable that we may need to merge with. Check it.
2613 if (isExternalLinkage(FoundVar->getLinkage()) &&
2614 isExternalLinkage(D->getLinkage())) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002615 if (Importer.IsStructurallyEquivalent(D->getType(),
2616 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002617 MergeWithVar = FoundVar;
2618 break;
2619 }
2620
Douglas Gregor56521c52010-02-12 17:23:39 +00002621 const ArrayType *FoundArray
2622 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2623 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00002624 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00002625 if (FoundArray && TArray) {
2626 if (isa<IncompleteArrayType>(FoundArray) &&
2627 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002628 // Import the type.
2629 QualType T = Importer.Import(D->getType());
2630 if (T.isNull())
2631 return 0;
2632
Douglas Gregor56521c52010-02-12 17:23:39 +00002633 FoundVar->setType(T);
2634 MergeWithVar = FoundVar;
2635 break;
2636 } else if (isa<IncompleteArrayType>(TArray) &&
2637 isa<ConstantArrayType>(FoundArray)) {
2638 MergeWithVar = FoundVar;
2639 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00002640 }
2641 }
2642
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002643 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002644 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002645 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2646 << FoundVar->getType();
2647 }
2648 }
2649
2650 ConflictingDecls.push_back(*Lookup.first);
2651 }
2652
2653 if (MergeWithVar) {
2654 // An equivalent variable with external linkage has been found. Link
2655 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002656 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002657
2658 if (VarDecl *DDef = D->getDefinition()) {
2659 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2660 Importer.ToDiag(ExistingDef->getLocation(),
2661 diag::err_odr_variable_multiple_def)
2662 << Name;
2663 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2664 } else {
2665 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00002666 MergeWithVar->setInit(Init);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002667 }
2668 }
2669
2670 return MergeWithVar;
2671 }
2672
2673 if (!ConflictingDecls.empty()) {
2674 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2675 ConflictingDecls.data(),
2676 ConflictingDecls.size());
2677 if (!Name)
2678 return 0;
2679 }
2680 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002681
Douglas Gregorb4964f72010-02-15 23:54:17 +00002682 // Import the type.
2683 QualType T = Importer.Import(D->getType());
2684 if (T.isNull())
2685 return 0;
2686
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002687 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002688 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00002689 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2690 Importer.Import(D->getInnerLocStart()),
2691 Loc, Name.getAsIdentifierInfo(),
2692 T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002693 D->getStorageClass(),
2694 D->getStorageClassAsWritten());
Douglas Gregor14454802011-02-25 02:25:35 +00002695 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002696 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00002697 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002698 Importer.Imported(D, ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002699 LexicalDC->addDecl(ToVar);
2700
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002701 // Merge the initializer.
2702 // FIXME: Can we really import any initializer? Alternatively, we could force
2703 // ourselves to import every declaration of a variable and then only use
2704 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00002705 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002706
2707 // FIXME: Other bits to merge?
2708
2709 return ToVar;
2710}
2711
Douglas Gregor8b228d72010-02-17 21:22:52 +00002712Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2713 // Parameters are created in the translation unit's context, then moved
2714 // into the function declaration's context afterward.
2715 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2716
2717 // Import the name of this declaration.
2718 DeclarationName Name = Importer.Import(D->getDeclName());
2719 if (D->getDeclName() && !Name)
2720 return 0;
2721
2722 // Import the location of this declaration.
2723 SourceLocation Loc = Importer.Import(D->getLocation());
2724
2725 // Import the parameter's type.
2726 QualType T = Importer.Import(D->getType());
2727 if (T.isNull())
2728 return 0;
2729
2730 // Create the imported parameter.
2731 ImplicitParamDecl *ToParm
2732 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2733 Loc, Name.getAsIdentifierInfo(),
2734 T);
2735 return Importer.Imported(D, ToParm);
2736}
2737
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002738Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2739 // Parameters are created in the translation unit's context, then moved
2740 // into the function declaration's context afterward.
2741 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2742
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00002743 // Import the name of this declaration.
2744 DeclarationName Name = Importer.Import(D->getDeclName());
2745 if (D->getDeclName() && !Name)
2746 return 0;
2747
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002748 // Import the location of this declaration.
2749 SourceLocation Loc = Importer.Import(D->getLocation());
2750
2751 // Import the parameter's type.
2752 QualType T = Importer.Import(D->getType());
2753 if (T.isNull())
2754 return 0;
2755
2756 // Create the imported parameter.
2757 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2758 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002759 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002760 Loc, Name.getAsIdentifierInfo(),
2761 T, TInfo, D->getStorageClass(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002762 D->getStorageClassAsWritten(),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002763 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00002764 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002765 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002766}
2767
Douglas Gregor43f54792010-02-17 02:12:47 +00002768Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2769 // Import the major distinguishing characteristics of a method.
2770 DeclContext *DC, *LexicalDC;
2771 DeclarationName Name;
2772 SourceLocation Loc;
2773 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2774 return 0;
2775
2776 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2777 Lookup.first != Lookup.second;
2778 ++Lookup.first) {
2779 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2780 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2781 continue;
2782
2783 // Check return types.
2784 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2785 FoundMethod->getResultType())) {
2786 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2787 << D->isInstanceMethod() << Name
2788 << D->getResultType() << FoundMethod->getResultType();
2789 Importer.ToDiag(FoundMethod->getLocation(),
2790 diag::note_odr_objc_method_here)
2791 << D->isInstanceMethod() << Name;
2792 return 0;
2793 }
2794
2795 // Check the number of parameters.
2796 if (D->param_size() != FoundMethod->param_size()) {
2797 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2798 << D->isInstanceMethod() << Name
2799 << D->param_size() << FoundMethod->param_size();
2800 Importer.ToDiag(FoundMethod->getLocation(),
2801 diag::note_odr_objc_method_here)
2802 << D->isInstanceMethod() << Name;
2803 return 0;
2804 }
2805
2806 // Check parameter types.
2807 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2808 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2809 P != PEnd; ++P, ++FoundP) {
2810 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2811 (*FoundP)->getType())) {
2812 Importer.FromDiag((*P)->getLocation(),
2813 diag::err_odr_objc_method_param_type_inconsistent)
2814 << D->isInstanceMethod() << Name
2815 << (*P)->getType() << (*FoundP)->getType();
2816 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2817 << (*FoundP)->getType();
2818 return 0;
2819 }
2820 }
2821
2822 // Check variadic/non-variadic.
2823 // Check the number of parameters.
2824 if (D->isVariadic() != FoundMethod->isVariadic()) {
2825 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2826 << D->isInstanceMethod() << Name;
2827 Importer.ToDiag(FoundMethod->getLocation(),
2828 diag::note_odr_objc_method_here)
2829 << D->isInstanceMethod() << Name;
2830 return 0;
2831 }
2832
2833 // FIXME: Any other bits we need to merge?
2834 return Importer.Imported(D, FoundMethod);
2835 }
2836 }
2837
2838 // Import the result type.
2839 QualType ResultTy = Importer.Import(D->getResultType());
2840 if (ResultTy.isNull())
2841 return 0;
2842
Douglas Gregor12852d92010-03-08 14:59:44 +00002843 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2844
Douglas Gregor43f54792010-02-17 02:12:47 +00002845 ObjCMethodDecl *ToMethod
2846 = ObjCMethodDecl::Create(Importer.getToContext(),
2847 Loc,
2848 Importer.Import(D->getLocEnd()),
2849 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00002850 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00002851 D->isInstanceMethod(),
2852 D->isVariadic(),
2853 D->isSynthesized(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002854 D->isDefined(),
Douglas Gregor43f54792010-02-17 02:12:47 +00002855 D->getImplementationControl());
2856
2857 // FIXME: When we decide to merge method definitions, we'll need to
2858 // deal with implicit parameters.
2859
2860 // Import the parameters
2861 llvm::SmallVector<ParmVarDecl *, 5> ToParams;
2862 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2863 FromPEnd = D->param_end();
2864 FromP != FromPEnd;
2865 ++FromP) {
2866 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2867 if (!ToP)
2868 return 0;
2869
2870 ToParams.push_back(ToP);
2871 }
2872
2873 // Set the parameters.
2874 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2875 ToParams[I]->setOwningFunction(ToMethod);
2876 ToMethod->addDecl(ToParams[I]);
2877 }
2878 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahaniancdabb312010-04-09 15:40:42 +00002879 ToParams.data(), ToParams.size(),
2880 ToParams.size());
Douglas Gregor43f54792010-02-17 02:12:47 +00002881
2882 ToMethod->setLexicalDeclContext(LexicalDC);
2883 Importer.Imported(D, ToMethod);
2884 LexicalDC->addDecl(ToMethod);
2885 return ToMethod;
2886}
2887
Douglas Gregor84c51c32010-02-18 01:47:50 +00002888Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2889 // Import the major distinguishing characteristics of a category.
2890 DeclContext *DC, *LexicalDC;
2891 DeclarationName Name;
2892 SourceLocation Loc;
2893 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2894 return 0;
2895
2896 ObjCInterfaceDecl *ToInterface
2897 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2898 if (!ToInterface)
2899 return 0;
2900
2901 // Determine if we've already encountered this category.
2902 ObjCCategoryDecl *MergeWithCategory
2903 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2904 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2905 if (!ToCategory) {
2906 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2907 Importer.Import(D->getAtLoc()),
2908 Loc,
2909 Importer.Import(D->getCategoryNameLoc()),
2910 Name.getAsIdentifierInfo());
2911 ToCategory->setLexicalDeclContext(LexicalDC);
2912 LexicalDC->addDecl(ToCategory);
2913 Importer.Imported(D, ToCategory);
2914
2915 // Link this category into its class's category list.
2916 ToCategory->setClassInterface(ToInterface);
2917 ToCategory->insertNextClassCategory();
2918
2919 // Import protocols
2920 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2921 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2922 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2923 = D->protocol_loc_begin();
2924 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2925 FromProtoEnd = D->protocol_end();
2926 FromProto != FromProtoEnd;
2927 ++FromProto, ++FromProtoLoc) {
2928 ObjCProtocolDecl *ToProto
2929 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2930 if (!ToProto)
2931 return 0;
2932 Protocols.push_back(ToProto);
2933 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2934 }
2935
2936 // FIXME: If we're merging, make sure that the protocol list is the same.
2937 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
2938 ProtocolLocs.data(), Importer.getToContext());
2939
2940 } else {
2941 Importer.Imported(D, ToCategory);
2942 }
2943
2944 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00002945 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00002946
2947 // If we have an implementation, import it as well.
2948 if (D->getImplementation()) {
2949 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00002950 = cast_or_null<ObjCCategoryImplDecl>(
2951 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00002952 if (!Impl)
2953 return 0;
2954
2955 ToCategory->setImplementation(Impl);
2956 }
2957
2958 return ToCategory;
2959}
2960
Douglas Gregor98d156a2010-02-17 16:12:00 +00002961Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor84c51c32010-02-18 01:47:50 +00002962 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00002963 DeclContext *DC, *LexicalDC;
2964 DeclarationName Name;
2965 SourceLocation Loc;
2966 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2967 return 0;
2968
2969 ObjCProtocolDecl *MergeWithProtocol = 0;
2970 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2971 Lookup.first != Lookup.second;
2972 ++Lookup.first) {
2973 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
2974 continue;
2975
2976 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
2977 break;
2978 }
2979
2980 ObjCProtocolDecl *ToProto = MergeWithProtocol;
2981 if (!ToProto || ToProto->isForwardDecl()) {
2982 if (!ToProto) {
2983 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
2984 Name.getAsIdentifierInfo());
2985 ToProto->setForwardDecl(D->isForwardDecl());
2986 ToProto->setLexicalDeclContext(LexicalDC);
2987 LexicalDC->addDecl(ToProto);
2988 }
2989 Importer.Imported(D, ToProto);
2990
2991 // Import protocols
2992 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
2993 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
2994 ObjCProtocolDecl::protocol_loc_iterator
2995 FromProtoLoc = D->protocol_loc_begin();
2996 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
2997 FromProtoEnd = D->protocol_end();
2998 FromProto != FromProtoEnd;
2999 ++FromProto, ++FromProtoLoc) {
3000 ObjCProtocolDecl *ToProto
3001 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3002 if (!ToProto)
3003 return 0;
3004 Protocols.push_back(ToProto);
3005 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3006 }
3007
3008 // FIXME: If we're merging, make sure that the protocol list is the same.
3009 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3010 ProtocolLocs.data(), Importer.getToContext());
3011 } else {
3012 Importer.Imported(D, ToProto);
3013 }
3014
Douglas Gregor84c51c32010-02-18 01:47:50 +00003015 // Import all of the members of this protocol.
Douglas Gregor968d6332010-02-21 18:24:45 +00003016 ImportDeclContext(D);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003017
3018 return ToProto;
3019}
3020
Douglas Gregor45635322010-02-16 01:20:57 +00003021Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3022 // Import the major distinguishing characteristics of an @interface.
3023 DeclContext *DC, *LexicalDC;
3024 DeclarationName Name;
3025 SourceLocation Loc;
3026 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3027 return 0;
3028
3029 ObjCInterfaceDecl *MergeWithIface = 0;
3030 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3031 Lookup.first != Lookup.second;
3032 ++Lookup.first) {
3033 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3034 continue;
3035
3036 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3037 break;
3038 }
3039
3040 ObjCInterfaceDecl *ToIface = MergeWithIface;
3041 if (!ToIface || ToIface->isForwardDecl()) {
3042 if (!ToIface) {
3043 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3044 DC, Loc,
3045 Name.getAsIdentifierInfo(),
Douglas Gregor1c283312010-08-11 12:19:30 +00003046 Importer.Import(D->getClassLoc()),
Douglas Gregor45635322010-02-16 01:20:57 +00003047 D->isForwardDecl(),
3048 D->isImplicitInterfaceDecl());
Douglas Gregor98d156a2010-02-17 16:12:00 +00003049 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregor45635322010-02-16 01:20:57 +00003050 ToIface->setLexicalDeclContext(LexicalDC);
3051 LexicalDC->addDecl(ToIface);
3052 }
3053 Importer.Imported(D, ToIface);
3054
Douglas Gregor45635322010-02-16 01:20:57 +00003055 if (D->getSuperClass()) {
3056 ObjCInterfaceDecl *Super
3057 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3058 if (!Super)
3059 return 0;
3060
3061 ToIface->setSuperClass(Super);
3062 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3063 }
3064
3065 // Import protocols
3066 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3067 llvm::SmallVector<SourceLocation, 4> ProtocolLocs;
3068 ObjCInterfaceDecl::protocol_loc_iterator
3069 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek0ef508d2010-09-01 01:21:15 +00003070
3071 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregor45635322010-02-16 01:20:57 +00003072 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3073 FromProtoEnd = D->protocol_end();
3074 FromProto != FromProtoEnd;
3075 ++FromProto, ++FromProtoLoc) {
3076 ObjCProtocolDecl *ToProto
3077 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3078 if (!ToProto)
3079 return 0;
3080 Protocols.push_back(ToProto);
3081 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3082 }
3083
3084 // FIXME: If we're merging, make sure that the protocol list is the same.
3085 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3086 ProtocolLocs.data(), Importer.getToContext());
3087
Douglas Gregor45635322010-02-16 01:20:57 +00003088 // Import @end range
3089 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3090 } else {
3091 Importer.Imported(D, ToIface);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003092
3093 // Check for consistency of superclasses.
3094 DeclarationName FromSuperName, ToSuperName;
3095 if (D->getSuperClass())
3096 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3097 if (ToIface->getSuperClass())
3098 ToSuperName = ToIface->getSuperClass()->getDeclName();
3099 if (FromSuperName != ToSuperName) {
3100 Importer.ToDiag(ToIface->getLocation(),
3101 diag::err_odr_objc_superclass_inconsistent)
3102 << ToIface->getDeclName();
3103 if (ToIface->getSuperClass())
3104 Importer.ToDiag(ToIface->getSuperClassLoc(),
3105 diag::note_odr_objc_superclass)
3106 << ToIface->getSuperClass()->getDeclName();
3107 else
3108 Importer.ToDiag(ToIface->getLocation(),
3109 diag::note_odr_objc_missing_superclass);
3110 if (D->getSuperClass())
3111 Importer.FromDiag(D->getSuperClassLoc(),
3112 diag::note_odr_objc_superclass)
3113 << D->getSuperClass()->getDeclName();
3114 else
3115 Importer.FromDiag(D->getLocation(),
3116 diag::note_odr_objc_missing_superclass);
3117 return 0;
3118 }
Douglas Gregor45635322010-02-16 01:20:57 +00003119 }
3120
Douglas Gregor84c51c32010-02-18 01:47:50 +00003121 // Import categories. When the categories themselves are imported, they'll
3122 // hook themselves into this interface.
3123 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3124 FromCat = FromCat->getNextClassCategory())
3125 Importer.Import(FromCat);
3126
Douglas Gregor45635322010-02-16 01:20:57 +00003127 // Import all of the members of this class.
Douglas Gregor968d6332010-02-21 18:24:45 +00003128 ImportDeclContext(D);
Douglas Gregor45635322010-02-16 01:20:57 +00003129
3130 // If we have an @implementation, import it as well.
3131 if (D->getImplementation()) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003132 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3133 Importer.Import(D->getImplementation()));
Douglas Gregor45635322010-02-16 01:20:57 +00003134 if (!Impl)
3135 return 0;
3136
3137 ToIface->setImplementation(Impl);
3138 }
3139
Douglas Gregor98d156a2010-02-17 16:12:00 +00003140 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003141}
3142
Douglas Gregor4da9d682010-12-07 15:32:12 +00003143Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3144 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3145 Importer.Import(D->getCategoryDecl()));
3146 if (!Category)
3147 return 0;
3148
3149 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3150 if (!ToImpl) {
3151 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3152 if (!DC)
3153 return 0;
3154
3155 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3156 Importer.Import(D->getLocation()),
3157 Importer.Import(D->getIdentifier()),
3158 Category->getClassInterface());
3159
3160 DeclContext *LexicalDC = DC;
3161 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3162 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3163 if (!LexicalDC)
3164 return 0;
3165
3166 ToImpl->setLexicalDeclContext(LexicalDC);
3167 }
3168
3169 LexicalDC->addDecl(ToImpl);
3170 Category->setImplementation(ToImpl);
3171 }
3172
3173 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003174 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003175 return ToImpl;
3176}
3177
Douglas Gregorda8025c2010-12-07 01:26:03 +00003178Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3179 // Find the corresponding interface.
3180 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3181 Importer.Import(D->getClassInterface()));
3182 if (!Iface)
3183 return 0;
3184
3185 // Import the superclass, if any.
3186 ObjCInterfaceDecl *Super = 0;
3187 if (D->getSuperClass()) {
3188 Super = cast_or_null<ObjCInterfaceDecl>(
3189 Importer.Import(D->getSuperClass()));
3190 if (!Super)
3191 return 0;
3192 }
3193
3194 ObjCImplementationDecl *Impl = Iface->getImplementation();
3195 if (!Impl) {
3196 // We haven't imported an implementation yet. Create a new @implementation
3197 // now.
3198 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3199 Importer.ImportContext(D->getDeclContext()),
3200 Importer.Import(D->getLocation()),
3201 Iface, Super);
3202
3203 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3204 DeclContext *LexicalDC
3205 = Importer.ImportContext(D->getLexicalDeclContext());
3206 if (!LexicalDC)
3207 return 0;
3208 Impl->setLexicalDeclContext(LexicalDC);
3209 }
3210
3211 // Associate the implementation with the class it implements.
3212 Iface->setImplementation(Impl);
3213 Importer.Imported(D, Iface->getImplementation());
3214 } else {
3215 Importer.Imported(D, Iface->getImplementation());
3216
3217 // Verify that the existing @implementation has the same superclass.
3218 if ((Super && !Impl->getSuperClass()) ||
3219 (!Super && Impl->getSuperClass()) ||
3220 (Super && Impl->getSuperClass() &&
3221 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3222 Importer.ToDiag(Impl->getLocation(),
3223 diag::err_odr_objc_superclass_inconsistent)
3224 << Iface->getDeclName();
3225 // FIXME: It would be nice to have the location of the superclass
3226 // below.
3227 if (Impl->getSuperClass())
3228 Importer.ToDiag(Impl->getLocation(),
3229 diag::note_odr_objc_superclass)
3230 << Impl->getSuperClass()->getDeclName();
3231 else
3232 Importer.ToDiag(Impl->getLocation(),
3233 diag::note_odr_objc_missing_superclass);
3234 if (D->getSuperClass())
3235 Importer.FromDiag(D->getLocation(),
3236 diag::note_odr_objc_superclass)
3237 << D->getSuperClass()->getDeclName();
3238 else
3239 Importer.FromDiag(D->getLocation(),
3240 diag::note_odr_objc_missing_superclass);
3241 return 0;
3242 }
3243 }
3244
3245 // Import all of the members of this @implementation.
3246 ImportDeclContext(D);
3247
3248 return Impl;
3249}
3250
Douglas Gregora11c4582010-02-17 18:02:10 +00003251Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3252 // Import the major distinguishing characteristics of an @property.
3253 DeclContext *DC, *LexicalDC;
3254 DeclarationName Name;
3255 SourceLocation Loc;
3256 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3257 return 0;
3258
3259 // Check whether we have already imported this property.
3260 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3261 Lookup.first != Lookup.second;
3262 ++Lookup.first) {
3263 if (ObjCPropertyDecl *FoundProp
3264 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3265 // Check property types.
3266 if (!Importer.IsStructurallyEquivalent(D->getType(),
3267 FoundProp->getType())) {
3268 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3269 << Name << D->getType() << FoundProp->getType();
3270 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3271 << FoundProp->getType();
3272 return 0;
3273 }
3274
3275 // FIXME: Check property attributes, getters, setters, etc.?
3276
3277 // Consider these properties to be equivalent.
3278 Importer.Imported(D, FoundProp);
3279 return FoundProp;
3280 }
3281 }
3282
3283 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003284 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3285 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003286 return 0;
3287
3288 // Create the new property.
3289 ObjCPropertyDecl *ToProperty
3290 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3291 Name.getAsIdentifierInfo(),
3292 Importer.Import(D->getAtLoc()),
3293 T,
3294 D->getPropertyImplementation());
3295 Importer.Imported(D, ToProperty);
3296 ToProperty->setLexicalDeclContext(LexicalDC);
3297 LexicalDC->addDecl(ToProperty);
3298
3299 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003300 ToProperty->setPropertyAttributesAsWritten(
3301 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003302 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3303 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3304 ToProperty->setGetterMethodDecl(
3305 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3306 ToProperty->setSetterMethodDecl(
3307 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3308 ToProperty->setPropertyIvarDecl(
3309 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3310 return ToProperty;
3311}
3312
Douglas Gregor14a49e22010-12-07 18:32:03 +00003313Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3314 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3315 Importer.Import(D->getPropertyDecl()));
3316 if (!Property)
3317 return 0;
3318
3319 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3320 if (!DC)
3321 return 0;
3322
3323 // Import the lexical declaration context.
3324 DeclContext *LexicalDC = DC;
3325 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3326 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3327 if (!LexicalDC)
3328 return 0;
3329 }
3330
3331 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3332 if (!InImpl)
3333 return 0;
3334
3335 // Import the ivar (for an @synthesize).
3336 ObjCIvarDecl *Ivar = 0;
3337 if (D->getPropertyIvarDecl()) {
3338 Ivar = cast_or_null<ObjCIvarDecl>(
3339 Importer.Import(D->getPropertyIvarDecl()));
3340 if (!Ivar)
3341 return 0;
3342 }
3343
3344 ObjCPropertyImplDecl *ToImpl
3345 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3346 if (!ToImpl) {
3347 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3348 Importer.Import(D->getLocStart()),
3349 Importer.Import(D->getLocation()),
3350 Property,
3351 D->getPropertyImplementation(),
3352 Ivar,
3353 Importer.Import(D->getPropertyIvarDeclLoc()));
3354 ToImpl->setLexicalDeclContext(LexicalDC);
3355 Importer.Imported(D, ToImpl);
3356 LexicalDC->addDecl(ToImpl);
3357 } else {
3358 // Check that we have the same kind of property implementation (@synthesize
3359 // vs. @dynamic).
3360 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3361 Importer.ToDiag(ToImpl->getLocation(),
3362 diag::err_odr_objc_property_impl_kind_inconsistent)
3363 << Property->getDeclName()
3364 << (ToImpl->getPropertyImplementation()
3365 == ObjCPropertyImplDecl::Dynamic);
3366 Importer.FromDiag(D->getLocation(),
3367 diag::note_odr_objc_property_impl_kind)
3368 << D->getPropertyDecl()->getDeclName()
3369 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3370 return 0;
3371 }
3372
3373 // For @synthesize, check that we have the same
3374 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3375 Ivar != ToImpl->getPropertyIvarDecl()) {
3376 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3377 diag::err_odr_objc_synthesize_ivar_inconsistent)
3378 << Property->getDeclName()
3379 << ToImpl->getPropertyIvarDecl()->getDeclName()
3380 << Ivar->getDeclName();
3381 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3382 diag::note_odr_objc_synthesize_ivar_here)
3383 << D->getPropertyIvarDecl()->getDeclName();
3384 return 0;
3385 }
3386
3387 // Merge the existing implementation with the new implementation.
3388 Importer.Imported(D, ToImpl);
3389 }
3390
3391 return ToImpl;
3392}
3393
Douglas Gregor8661a722010-02-18 02:12:22 +00003394Decl *
3395ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3396 // Import the context of this declaration.
3397 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3398 if (!DC)
3399 return 0;
3400
3401 DeclContext *LexicalDC = DC;
3402 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3403 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3404 if (!LexicalDC)
3405 return 0;
3406 }
3407
3408 // Import the location of this declaration.
3409 SourceLocation Loc = Importer.Import(D->getLocation());
3410
3411 llvm::SmallVector<ObjCProtocolDecl *, 4> Protocols;
3412 llvm::SmallVector<SourceLocation, 4> Locations;
3413 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3414 = D->protocol_loc_begin();
3415 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3416 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3417 FromProto != FromProtoEnd;
3418 ++FromProto, ++FromProtoLoc) {
3419 ObjCProtocolDecl *ToProto
3420 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3421 if (!ToProto)
3422 continue;
3423
3424 Protocols.push_back(ToProto);
3425 Locations.push_back(Importer.Import(*FromProtoLoc));
3426 }
3427
3428 ObjCForwardProtocolDecl *ToForward
3429 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3430 Protocols.data(), Protocols.size(),
3431 Locations.data());
3432 ToForward->setLexicalDeclContext(LexicalDC);
3433 LexicalDC->addDecl(ToForward);
3434 Importer.Imported(D, ToForward);
3435 return ToForward;
3436}
3437
Douglas Gregor06537af2010-02-18 02:04:09 +00003438Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3439 // Import the context of this declaration.
3440 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3441 if (!DC)
3442 return 0;
3443
3444 DeclContext *LexicalDC = DC;
3445 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3446 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3447 if (!LexicalDC)
3448 return 0;
3449 }
3450
3451 // Import the location of this declaration.
3452 SourceLocation Loc = Importer.Import(D->getLocation());
3453
3454 llvm::SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3455 llvm::SmallVector<SourceLocation, 4> Locations;
3456 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3457 From != FromEnd; ++From) {
3458 ObjCInterfaceDecl *ToIface
3459 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3460 if (!ToIface)
3461 continue;
3462
3463 Interfaces.push_back(ToIface);
3464 Locations.push_back(Importer.Import(From->getLocation()));
3465 }
3466
3467 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3468 Loc,
3469 Interfaces.data(),
3470 Locations.data(),
3471 Interfaces.size());
3472 ToClass->setLexicalDeclContext(LexicalDC);
3473 LexicalDC->addDecl(ToClass);
3474 Importer.Imported(D, ToClass);
3475 return ToClass;
3476}
3477
Douglas Gregora082a492010-11-30 19:14:50 +00003478Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3479 // For template arguments, we adopt the translation unit as our declaration
3480 // context. This context will be fixed when the actual template declaration
3481 // is created.
3482
3483 // FIXME: Import default argument.
3484 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3485 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003486 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003487 Importer.Import(D->getLocation()),
3488 D->getDepth(),
3489 D->getIndex(),
3490 Importer.Import(D->getIdentifier()),
3491 D->wasDeclaredWithTypename(),
3492 D->isParameterPack());
3493}
3494
3495Decl *
3496ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3497 // Import the name of this declaration.
3498 DeclarationName Name = Importer.Import(D->getDeclName());
3499 if (D->getDeclName() && !Name)
3500 return 0;
3501
3502 // Import the location of this declaration.
3503 SourceLocation Loc = Importer.Import(D->getLocation());
3504
3505 // Import the type of this declaration.
3506 QualType T = Importer.Import(D->getType());
3507 if (T.isNull())
3508 return 0;
3509
3510 // Import type-source information.
3511 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3512 if (D->getTypeSourceInfo() && !TInfo)
3513 return 0;
3514
3515 // FIXME: Import default argument.
3516
3517 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3518 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003519 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003520 Loc, D->getDepth(), D->getPosition(),
3521 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003522 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003523}
3524
3525Decl *
3526ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3527 // Import the name of this declaration.
3528 DeclarationName Name = Importer.Import(D->getDeclName());
3529 if (D->getDeclName() && !Name)
3530 return 0;
3531
3532 // Import the location of this declaration.
3533 SourceLocation Loc = Importer.Import(D->getLocation());
3534
3535 // Import template parameters.
3536 TemplateParameterList *TemplateParams
3537 = ImportTemplateParameterList(D->getTemplateParameters());
3538 if (!TemplateParams)
3539 return 0;
3540
3541 // FIXME: Import default argument.
3542
3543 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3544 Importer.getToContext().getTranslationUnitDecl(),
3545 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003546 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003547 Name.getAsIdentifierInfo(),
3548 TemplateParams);
3549}
3550
3551Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3552 // If this record has a definition in the translation unit we're coming from,
3553 // but this particular declaration is not that definition, import the
3554 // definition and map to that.
3555 CXXRecordDecl *Definition
3556 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3557 if (Definition && Definition != D->getTemplatedDecl()) {
3558 Decl *ImportedDef
3559 = Importer.Import(Definition->getDescribedClassTemplate());
3560 if (!ImportedDef)
3561 return 0;
3562
3563 return Importer.Imported(D, ImportedDef);
3564 }
3565
3566 // Import the major distinguishing characteristics of this class template.
3567 DeclContext *DC, *LexicalDC;
3568 DeclarationName Name;
3569 SourceLocation Loc;
3570 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3571 return 0;
3572
3573 // We may already have a template of the same name; try to find and match it.
3574 if (!DC->isFunctionOrMethod()) {
3575 llvm::SmallVector<NamedDecl *, 4> ConflictingDecls;
3576 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3577 Lookup.first != Lookup.second;
3578 ++Lookup.first) {
3579 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3580 continue;
3581
3582 Decl *Found = *Lookup.first;
3583 if (ClassTemplateDecl *FoundTemplate
3584 = dyn_cast<ClassTemplateDecl>(Found)) {
3585 if (IsStructuralMatch(D, FoundTemplate)) {
3586 // The class templates structurally match; call it the same template.
3587 // FIXME: We may be filling in a forward declaration here. Handle
3588 // this case!
3589 Importer.Imported(D->getTemplatedDecl(),
3590 FoundTemplate->getTemplatedDecl());
3591 return Importer.Imported(D, FoundTemplate);
3592 }
3593 }
3594
3595 ConflictingDecls.push_back(*Lookup.first);
3596 }
3597
3598 if (!ConflictingDecls.empty()) {
3599 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3600 ConflictingDecls.data(),
3601 ConflictingDecls.size());
3602 }
3603
3604 if (!Name)
3605 return 0;
3606 }
3607
3608 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3609
3610 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003611 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3612 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003613 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3614 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003615 DC, StartLoc, IdLoc,
3616 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003617 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003618 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003619 D2Templated->setLexicalDeclContext(LexicalDC);
3620
3621 // Create the class template declaration itself.
3622 TemplateParameterList *TemplateParams
3623 = ImportTemplateParameterList(D->getTemplateParameters());
3624 if (!TemplateParams)
3625 return 0;
3626
3627 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3628 Loc, Name, TemplateParams,
3629 D2Templated,
3630 /*PrevDecl=*/0);
3631 D2Templated->setDescribedClassTemplate(D2);
3632
3633 D2->setAccess(D->getAccess());
3634 D2->setLexicalDeclContext(LexicalDC);
3635 LexicalDC->addDecl(D2);
3636
3637 // Note the relationship between the class templates.
3638 Importer.Imported(D, D2);
3639 Importer.Imported(DTemplated, D2Templated);
3640
3641 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3642 // FIXME: Import definition!
3643 }
3644
3645 return D2;
3646}
3647
Douglas Gregore2e50d332010-12-01 01:36:18 +00003648Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3649 ClassTemplateSpecializationDecl *D) {
3650 // If this record has a definition in the translation unit we're coming from,
3651 // but this particular declaration is not that definition, import the
3652 // definition and map to that.
3653 TagDecl *Definition = D->getDefinition();
3654 if (Definition && Definition != D) {
3655 Decl *ImportedDef = Importer.Import(Definition);
3656 if (!ImportedDef)
3657 return 0;
3658
3659 return Importer.Imported(D, ImportedDef);
3660 }
3661
3662 ClassTemplateDecl *ClassTemplate
3663 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3664 D->getSpecializedTemplate()));
3665 if (!ClassTemplate)
3666 return 0;
3667
3668 // Import the context of this declaration.
3669 DeclContext *DC = ClassTemplate->getDeclContext();
3670 if (!DC)
3671 return 0;
3672
3673 DeclContext *LexicalDC = DC;
3674 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3675 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3676 if (!LexicalDC)
3677 return 0;
3678 }
3679
3680 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003681 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3682 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00003683
3684 // Import template arguments.
3685 llvm::SmallVector<TemplateArgument, 2> TemplateArgs;
3686 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3687 D->getTemplateArgs().size(),
3688 TemplateArgs))
3689 return 0;
3690
3691 // Try to find an existing specialization with these template arguments.
3692 void *InsertPos = 0;
3693 ClassTemplateSpecializationDecl *D2
3694 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3695 TemplateArgs.size(), InsertPos);
3696 if (D2) {
3697 // We already have a class template specialization with these template
3698 // arguments.
3699
3700 // FIXME: Check for specialization vs. instantiation errors.
3701
3702 if (RecordDecl *FoundDef = D2->getDefinition()) {
3703 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3704 // The record types structurally match, or the "from" translation
3705 // unit only had a forward declaration anyway; call it the same
3706 // function.
3707 return Importer.Imported(D, FoundDef);
3708 }
3709 }
3710 } else {
3711 // Create a new specialization.
3712 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3713 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003714 StartLoc, IdLoc,
3715 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00003716 TemplateArgs.data(),
3717 TemplateArgs.size(),
3718 /*PrevDecl=*/0);
3719 D2->setSpecializationKind(D->getSpecializationKind());
3720
3721 // Add this specialization to the class template.
3722 ClassTemplate->AddSpecialization(D2, InsertPos);
3723
3724 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003725 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00003726
3727 // Add the specialization to this context.
3728 D2->setLexicalDeclContext(LexicalDC);
3729 LexicalDC->addDecl(D2);
3730 }
3731 Importer.Imported(D, D2);
3732
3733 if (D->isDefinition() && ImportDefinition(D, D2))
3734 return 0;
3735
3736 return D2;
3737}
3738
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003739//----------------------------------------------------------------------------
3740// Import Statements
3741//----------------------------------------------------------------------------
3742
3743Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3744 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3745 << S->getStmtClassName();
3746 return 0;
3747}
3748
3749//----------------------------------------------------------------------------
3750// Import Expressions
3751//----------------------------------------------------------------------------
3752Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3753 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3754 << E->getStmtClassName();
3755 return 0;
3756}
3757
Douglas Gregor52f820e2010-02-19 01:17:02 +00003758Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00003759 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3760 if (!ToD)
3761 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003762
3763 NamedDecl *FoundD = 0;
3764 if (E->getDecl() != E->getFoundDecl()) {
3765 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3766 if (!FoundD)
3767 return 0;
3768 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00003769
3770 QualType T = Importer.Import(E->getType());
3771 if (T.isNull())
3772 return 0;
3773
Douglas Gregorea972d32011-02-28 21:54:11 +00003774 return DeclRefExpr::Create(Importer.getToContext(),
3775 Importer.Import(E->getQualifierLoc()),
Douglas Gregor52f820e2010-02-19 01:17:02 +00003776 ToD,
3777 Importer.Import(E->getLocation()),
John McCall7decc9e2010-11-18 06:31:45 +00003778 T, E->getValueKind(),
Chandler Carruth8d26bb02011-05-01 23:48:14 +00003779 FoundD,
Douglas Gregor52f820e2010-02-19 01:17:02 +00003780 /*FIXME:TemplateArgs=*/0);
3781}
3782
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003783Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3784 QualType T = Importer.Import(E->getType());
3785 if (T.isNull())
3786 return 0;
3787
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00003788 return IntegerLiteral::Create(Importer.getToContext(),
3789 E->getValue(), T,
3790 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00003791}
3792
Douglas Gregor623421d2010-02-18 02:21:22 +00003793Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3794 QualType T = Importer.Import(E->getType());
3795 if (T.isNull())
3796 return 0;
3797
3798 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3799 E->isWide(), T,
3800 Importer.Import(E->getLocation()));
3801}
3802
Douglas Gregorc74247e2010-02-19 01:07:06 +00003803Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3804 Expr *SubExpr = Importer.Import(E->getSubExpr());
3805 if (!SubExpr)
3806 return 0;
3807
3808 return new (Importer.getToContext())
3809 ParenExpr(Importer.Import(E->getLParen()),
3810 Importer.Import(E->getRParen()),
3811 SubExpr);
3812}
3813
3814Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3815 QualType T = Importer.Import(E->getType());
3816 if (T.isNull())
3817 return 0;
3818
3819 Expr *SubExpr = Importer.Import(E->getSubExpr());
3820 if (!SubExpr)
3821 return 0;
3822
3823 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003824 T, E->getValueKind(),
3825 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003826 Importer.Import(E->getOperatorLoc()));
3827}
3828
Peter Collingbournee190dee2011-03-11 19:24:49 +00003829Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3830 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00003831 QualType ResultType = Importer.Import(E->getType());
3832
3833 if (E->isArgumentType()) {
3834 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3835 if (!TInfo)
3836 return 0;
3837
Peter Collingbournee190dee2011-03-11 19:24:49 +00003838 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3839 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003840 Importer.Import(E->getOperatorLoc()),
3841 Importer.Import(E->getRParenLoc()));
3842 }
3843
3844 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3845 if (!SubExpr)
3846 return 0;
3847
Peter Collingbournee190dee2011-03-11 19:24:49 +00003848 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3849 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00003850 Importer.Import(E->getOperatorLoc()),
3851 Importer.Import(E->getRParenLoc()));
3852}
3853
Douglas Gregorc74247e2010-02-19 01:07:06 +00003854Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3855 QualType T = Importer.Import(E->getType());
3856 if (T.isNull())
3857 return 0;
3858
3859 Expr *LHS = Importer.Import(E->getLHS());
3860 if (!LHS)
3861 return 0;
3862
3863 Expr *RHS = Importer.Import(E->getRHS());
3864 if (!RHS)
3865 return 0;
3866
3867 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003868 T, E->getValueKind(),
3869 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00003870 Importer.Import(E->getOperatorLoc()));
3871}
3872
3873Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3874 QualType T = Importer.Import(E->getType());
3875 if (T.isNull())
3876 return 0;
3877
3878 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3879 if (CompLHSType.isNull())
3880 return 0;
3881
3882 QualType CompResultType = Importer.Import(E->getComputationResultType());
3883 if (CompResultType.isNull())
3884 return 0;
3885
3886 Expr *LHS = Importer.Import(E->getLHS());
3887 if (!LHS)
3888 return 0;
3889
3890 Expr *RHS = Importer.Import(E->getRHS());
3891 if (!RHS)
3892 return 0;
3893
3894 return new (Importer.getToContext())
3895 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00003896 T, E->getValueKind(),
3897 E->getObjectKind(),
3898 CompLHSType, CompResultType,
Douglas Gregorc74247e2010-02-19 01:07:06 +00003899 Importer.Import(E->getOperatorLoc()));
3900}
3901
Benjamin Kramer8aef5962011-03-26 12:38:21 +00003902static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00003903 if (E->path_empty()) return false;
3904
3905 // TODO: import cast paths
3906 return true;
3907}
3908
Douglas Gregor98c10182010-02-12 22:17:39 +00003909Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3910 QualType T = Importer.Import(E->getType());
3911 if (T.isNull())
3912 return 0;
3913
3914 Expr *SubExpr = Importer.Import(E->getSubExpr());
3915 if (!SubExpr)
3916 return 0;
John McCallcf142162010-08-07 06:22:56 +00003917
3918 CXXCastPath BasePath;
3919 if (ImportCastPath(E, BasePath))
3920 return 0;
3921
3922 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00003923 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00003924}
3925
Douglas Gregor5481d322010-02-19 01:32:14 +00003926Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3927 QualType T = Importer.Import(E->getType());
3928 if (T.isNull())
3929 return 0;
3930
3931 Expr *SubExpr = Importer.Import(E->getSubExpr());
3932 if (!SubExpr)
3933 return 0;
3934
3935 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
3936 if (!TInfo && E->getTypeInfoAsWritten())
3937 return 0;
3938
John McCallcf142162010-08-07 06:22:56 +00003939 CXXCastPath BasePath;
3940 if (ImportCastPath(E, BasePath))
3941 return 0;
3942
John McCall7decc9e2010-11-18 06:31:45 +00003943 return CStyleCastExpr::Create(Importer.getToContext(), T,
3944 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00003945 SubExpr, &BasePath, TInfo,
3946 Importer.Import(E->getLParenLoc()),
3947 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00003948}
3949
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00003950ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00003951 ASTContext &FromContext, FileManager &FromFileManager,
3952 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00003953 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00003954 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
3955 Minimal(MinimalImport)
3956{
Douglas Gregor62d311f2010-02-09 19:21:46 +00003957 ImportedDecls[FromContext.getTranslationUnitDecl()]
3958 = ToContext.getTranslationUnitDecl();
3959}
3960
3961ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00003962
3963QualType ASTImporter::Import(QualType FromT) {
3964 if (FromT.isNull())
3965 return QualType();
John McCall424cec92011-01-19 06:33:43 +00003966
3967 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00003968
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003969 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00003970 llvm::DenseMap<const Type *, const Type *>::iterator Pos
3971 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003972 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00003973 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003974
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003975 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00003976 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00003977 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00003978 if (ToT.isNull())
3979 return ToT;
3980
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003981 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00003982 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00003983
John McCall424cec92011-01-19 06:33:43 +00003984 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00003985}
3986
Douglas Gregor62d311f2010-02-09 19:21:46 +00003987TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003988 if (!FromTSI)
3989 return FromTSI;
3990
3991 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00003992 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003993 QualType T = Import(FromTSI->getType());
3994 if (T.isNull())
3995 return 0;
3996
3997 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnara1108e7b2010-05-20 10:00:11 +00003998 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003999}
4000
4001Decl *ASTImporter::Import(Decl *FromD) {
4002 if (!FromD)
4003 return 0;
4004
4005 // Check whether we've already imported this declaration.
4006 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
4007 if (Pos != ImportedDecls.end())
4008 return Pos->second;
4009
4010 // Import the type
4011 ASTNodeImporter Importer(*this);
4012 Decl *ToD = Importer.Visit(FromD);
4013 if (!ToD)
4014 return 0;
4015
4016 // Record the imported declaration.
4017 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004018
4019 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4020 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004021 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004022 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004023 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004024 // When we've finished transforming a typedef, see whether it was the
4025 // typedef for an anonymous tag.
4026 for (llvm::SmallVector<TagDecl *, 4>::iterator
4027 FromTag = AnonTagsWithPendingTypedefs.begin(),
4028 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4029 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004030 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004031 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4032 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004033 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004034 AnonTagsWithPendingTypedefs.erase(FromTag);
4035 break;
4036 }
4037 }
4038 }
4039 }
4040
Douglas Gregor62d311f2010-02-09 19:21:46 +00004041 return ToD;
4042}
4043
4044DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4045 if (!FromDC)
4046 return FromDC;
4047
4048 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4049}
4050
4051Expr *ASTImporter::Import(Expr *FromE) {
4052 if (!FromE)
4053 return 0;
4054
4055 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4056}
4057
4058Stmt *ASTImporter::Import(Stmt *FromS) {
4059 if (!FromS)
4060 return 0;
4061
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004062 // Check whether we've already imported this declaration.
4063 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4064 if (Pos != ImportedStmts.end())
4065 return Pos->second;
4066
4067 // Import the type
4068 ASTNodeImporter Importer(*this);
4069 Stmt *ToS = Importer.Visit(FromS);
4070 if (!ToS)
4071 return 0;
4072
4073 // Record the imported declaration.
4074 ImportedStmts[FromS] = ToS;
4075 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004076}
4077
4078NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4079 if (!FromNNS)
4080 return 0;
4081
Douglas Gregor90ebf252011-04-27 16:48:40 +00004082 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4083
4084 switch (FromNNS->getKind()) {
4085 case NestedNameSpecifier::Identifier:
4086 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4087 return NestedNameSpecifier::Create(ToContext, prefix, II);
4088 }
4089 return 0;
4090
4091 case NestedNameSpecifier::Namespace:
4092 if (NamespaceDecl *NS =
4093 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4094 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4095 }
4096 return 0;
4097
4098 case NestedNameSpecifier::NamespaceAlias:
4099 if (NamespaceAliasDecl *NSAD =
4100 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4101 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4102 }
4103 return 0;
4104
4105 case NestedNameSpecifier::Global:
4106 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4107
4108 case NestedNameSpecifier::TypeSpec:
4109 case NestedNameSpecifier::TypeSpecWithTemplate: {
4110 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4111 if (!T.isNull()) {
4112 bool bTemplate = FromNNS->getKind() ==
4113 NestedNameSpecifier::TypeSpecWithTemplate;
4114 return NestedNameSpecifier::Create(ToContext, prefix,
4115 bTemplate, T.getTypePtr());
4116 }
4117 }
4118 return 0;
4119 }
4120
4121 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004122 return 0;
4123}
4124
Douglas Gregor14454802011-02-25 02:25:35 +00004125NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4126 // FIXME: Implement!
4127 return NestedNameSpecifierLoc();
4128}
4129
Douglas Gregore2e50d332010-12-01 01:36:18 +00004130TemplateName ASTImporter::Import(TemplateName From) {
4131 switch (From.getKind()) {
4132 case TemplateName::Template:
4133 if (TemplateDecl *ToTemplate
4134 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4135 return TemplateName(ToTemplate);
4136
4137 return TemplateName();
4138
4139 case TemplateName::OverloadedTemplate: {
4140 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4141 UnresolvedSet<2> ToTemplates;
4142 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4143 E = FromStorage->end();
4144 I != E; ++I) {
4145 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4146 ToTemplates.addDecl(To);
4147 else
4148 return TemplateName();
4149 }
4150 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4151 ToTemplates.end());
4152 }
4153
4154 case TemplateName::QualifiedTemplate: {
4155 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4156 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4157 if (!Qualifier)
4158 return TemplateName();
4159
4160 if (TemplateDecl *ToTemplate
4161 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4162 return ToContext.getQualifiedTemplateName(Qualifier,
4163 QTN->hasTemplateKeyword(),
4164 ToTemplate);
4165
4166 return TemplateName();
4167 }
4168
4169 case TemplateName::DependentTemplate: {
4170 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4171 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4172 if (!Qualifier)
4173 return TemplateName();
4174
4175 if (DTN->isIdentifier()) {
4176 return ToContext.getDependentTemplateName(Qualifier,
4177 Import(DTN->getIdentifier()));
4178 }
4179
4180 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4181 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004182
4183 case TemplateName::SubstTemplateTemplateParmPack: {
4184 SubstTemplateTemplateParmPackStorage *SubstPack
4185 = From.getAsSubstTemplateTemplateParmPack();
4186 TemplateTemplateParmDecl *Param
4187 = cast_or_null<TemplateTemplateParmDecl>(
4188 Import(SubstPack->getParameterPack()));
4189 if (!Param)
4190 return TemplateName();
4191
4192 ASTNodeImporter Importer(*this);
4193 TemplateArgument ArgPack
4194 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4195 if (ArgPack.isNull())
4196 return TemplateName();
4197
4198 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4199 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004200 }
4201
4202 llvm_unreachable("Invalid template name kind");
4203 return TemplateName();
4204}
4205
Douglas Gregor62d311f2010-02-09 19:21:46 +00004206SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4207 if (FromLoc.isInvalid())
4208 return SourceLocation();
4209
Douglas Gregor811663e2010-02-10 00:15:17 +00004210 SourceManager &FromSM = FromContext.getSourceManager();
4211
4212 // For now, map everything down to its spelling location, so that we
4213 // don't have to import macro instantiations.
4214 // FIXME: Import macro instantiations!
4215 FromLoc = FromSM.getSpellingLoc(FromLoc);
4216 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4217 SourceManager &ToSM = ToContext.getSourceManager();
4218 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4219 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004220}
4221
4222SourceRange ASTImporter::Import(SourceRange FromRange) {
4223 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4224}
4225
Douglas Gregor811663e2010-02-10 00:15:17 +00004226FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004227 llvm::DenseMap<FileID, FileID>::iterator Pos
4228 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004229 if (Pos != ImportedFileIDs.end())
4230 return Pos->second;
4231
4232 SourceManager &FromSM = FromContext.getSourceManager();
4233 SourceManager &ToSM = ToContext.getSourceManager();
4234 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
4235 assert(FromSLoc.isFile() && "Cannot handle macro instantiations yet");
4236
4237 // Include location of this file.
4238 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4239
4240 // Map the FileID for to the "to" source manager.
4241 FileID ToID;
4242 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004243 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004244 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4245 // disk again
4246 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4247 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004248 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004249 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4250 FromSLoc.getFile().getFileCharacteristic());
4251 } else {
4252 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004253 const llvm::MemoryBuffer *
4254 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004255 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004256 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004257 FromBuf->getBufferIdentifier());
4258 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4259 }
4260
4261
Sebastian Redl99219f12010-09-30 01:03:06 +00004262 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004263 return ToID;
4264}
4265
Douglas Gregor0a791672011-01-18 03:11:38 +00004266void ASTImporter::ImportDefinition(Decl *From) {
4267 Decl *To = Import(From);
4268 if (!To)
4269 return;
4270
4271 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4272 ASTNodeImporter Importer(*this);
4273 Importer.ImportDeclContext(FromDC, true);
4274 }
4275}
4276
Douglas Gregor96e578d2010-02-05 17:54:41 +00004277DeclarationName ASTImporter::Import(DeclarationName FromName) {
4278 if (!FromName)
4279 return DeclarationName();
4280
4281 switch (FromName.getNameKind()) {
4282 case DeclarationName::Identifier:
4283 return Import(FromName.getAsIdentifierInfo());
4284
4285 case DeclarationName::ObjCZeroArgSelector:
4286 case DeclarationName::ObjCOneArgSelector:
4287 case DeclarationName::ObjCMultiArgSelector:
4288 return Import(FromName.getObjCSelector());
4289
4290 case DeclarationName::CXXConstructorName: {
4291 QualType T = Import(FromName.getCXXNameType());
4292 if (T.isNull())
4293 return DeclarationName();
4294
4295 return ToContext.DeclarationNames.getCXXConstructorName(
4296 ToContext.getCanonicalType(T));
4297 }
4298
4299 case DeclarationName::CXXDestructorName: {
4300 QualType T = Import(FromName.getCXXNameType());
4301 if (T.isNull())
4302 return DeclarationName();
4303
4304 return ToContext.DeclarationNames.getCXXDestructorName(
4305 ToContext.getCanonicalType(T));
4306 }
4307
4308 case DeclarationName::CXXConversionFunctionName: {
4309 QualType T = Import(FromName.getCXXNameType());
4310 if (T.isNull())
4311 return DeclarationName();
4312
4313 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4314 ToContext.getCanonicalType(T));
4315 }
4316
4317 case DeclarationName::CXXOperatorName:
4318 return ToContext.DeclarationNames.getCXXOperatorName(
4319 FromName.getCXXOverloadedOperator());
4320
4321 case DeclarationName::CXXLiteralOperatorName:
4322 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4323 Import(FromName.getCXXLiteralIdentifier()));
4324
4325 case DeclarationName::CXXUsingDirective:
4326 // FIXME: STATICS!
4327 return DeclarationName::getUsingDirectiveName();
4328 }
4329
4330 // Silence bogus GCC warning
4331 return DeclarationName();
4332}
4333
Douglas Gregore2e50d332010-12-01 01:36:18 +00004334IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004335 if (!FromId)
4336 return 0;
4337
4338 return &ToContext.Idents.get(FromId->getName());
4339}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004340
Douglas Gregor43f54792010-02-17 02:12:47 +00004341Selector ASTImporter::Import(Selector FromSel) {
4342 if (FromSel.isNull())
4343 return Selector();
4344
4345 llvm::SmallVector<IdentifierInfo *, 4> Idents;
4346 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4347 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4348 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4349 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4350}
4351
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004352DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4353 DeclContext *DC,
4354 unsigned IDNS,
4355 NamedDecl **Decls,
4356 unsigned NumDecls) {
4357 return Name;
4358}
4359
4360DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004361 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004362}
4363
4364DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004365 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004366}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004367
4368Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4369 ImportedDecls[From] = To;
4370 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004371}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004372
4373bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCall424cec92011-01-19 06:33:43 +00004374 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004375 = ImportedTypes.find(From.getTypePtr());
4376 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4377 return true;
4378
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004379 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004380 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004381}