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