blob: 867db91ac5431e2334fa4fea998355d7d60f0a91 [file] [log] [blame]
Douglas Gregor1b2949d2010-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 Gregor88523732010-02-10 00:15:17 +000017#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor96a01b42010-02-11 00:48:18 +000018#include "clang/AST/DeclCXX.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000019#include "clang/AST/DeclObjC.h"
Douglas Gregor089459a2010-02-08 21:09:39 +000020#include "clang/AST/DeclVisitor.h"
Douglas Gregor4800d952010-02-11 19:21:55 +000021#include "clang/AST/StmtVisitor.h"
Douglas Gregor1b2949d2010-02-05 17:54:41 +000022#include "clang/AST/TypeVisitor.h"
Douglas Gregor88523732010-02-10 00:15:17 +000023#include "clang/Basic/FileManager.h"
24#include "clang/Basic/SourceManager.h"
25#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor73dc30b2010-02-15 22:01:00 +000026#include <deque>
Douglas Gregor1b2949d2010-02-05 17:54:41 +000027
28using namespace clang;
29
30namespace {
Douglas Gregor089459a2010-02-08 21:09:39 +000031 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor4800d952010-02-11 19:21:55 +000032 public DeclVisitor<ASTNodeImporter, Decl *>,
33 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor1b2949d2010-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 Gregor9bed8792010-02-09 19:21:46 +000040 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor4800d952010-02-11 19:21:55 +000041 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor1b2949d2010-02-05 17:54:41 +000042
43 // Importing types
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000055 // FIXME: DependentSizedArrayType
56 // FIXME: DependentSizedExtVectorType
John McCallf4c73712011-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 Gregor1b2949d2010-02-05 17:54:41 +000061 // FIXME: UnresolvedUsingType
John McCallf4c73712011-01-19 06:33:43 +000062 QualType VisitTypedefType(const TypedefType *T);
63 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000064 // FIXME: DependentTypeOfExprType
John McCallf4c73712011-01-19 06:33:43 +000065 QualType VisitTypeOfType(const TypeOfType *T);
66 QualType VisitDecltypeType(const DecltypeType *T);
Sean Huntca63c202011-05-24 22:41:36 +000067 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith34b41d92011-02-20 03:19:35 +000068 QualType VisitAutoType(const AutoType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000069 // FIXME: DependentDecltypeType
John McCallf4c73712011-01-19 06:33:43 +000070 QualType VisitRecordType(const RecordType *T);
71 QualType VisitEnumType(const EnumType *T);
Douglas Gregor1b2949d2010-02-05 17:54:41 +000072 // FIXME: TemplateTypeParmType
73 // FIXME: SubstTemplateTypeParmType
John McCallf4c73712011-01-19 06:33:43 +000074 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
75 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregor4714c122010-03-31 17:34:00 +000076 // FIXME: DependentNameType
John McCall33500952010-06-11 00:33:02 +000077 // FIXME: DependentTemplateSpecializationType
John McCallf4c73712011-01-19 06:33:43 +000078 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
79 QualType VisitObjCObjectType(const ObjCObjectType *T);
80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor089459a2010-02-08 21:09:39 +000081
82 // Importing declarations
Douglas Gregora404ea62010-02-10 19:54:31 +000083 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
84 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregor788c62d2010-02-21 18:26:36 +000085 SourceLocation &Loc);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000086 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnara25777432010-08-11 22:01:17 +000087 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
88 DeclarationNameInfo& To);
Douglas Gregord8868a62011-01-18 03:11:38 +000089 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor1cf038c2011-07-29 23:31:30 +000090 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
91 bool ForceImport = false);
92 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
93 bool ForceImport = false);
Douglas Gregor040afae2010-11-30 19:14:50 +000094 TemplateParameterList *ImportTemplateParameterList(
95 TemplateParameterList *Params);
Douglas Gregord5dc83a2010-12-01 01:36:18 +000096 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
97 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
98 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +000099 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000100 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000101 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor040afae2010-11-30 19:14:50 +0000102 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregor89cc9d62010-02-09 22:48:33 +0000103 Decl *VisitDecl(Decl *D);
Douglas Gregor788c62d2010-02-21 18:26:36 +0000104 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000105 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor9e5d9962010-02-10 21:10:29 +0000106 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smith162e1c12011-04-15 14:24:37 +0000107 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000108 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000109 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000110 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000111 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregorc144f352010-02-21 18:29:16 +0000112 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
113 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
114 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
115 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor96a01b42010-02-11 00:48:18 +0000116 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet87c2e122010-11-21 06:08:52 +0000117 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +0000118 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor089459a2010-02-08 21:09:39 +0000119 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor2cd00932010-02-17 21:22:52 +0000120 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregora404ea62010-02-10 19:54:31 +0000121 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +0000122 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregorb4677b62010-02-18 01:47:50 +0000123 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +0000124 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregora12d2942010-02-16 01:20:57 +0000125 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor3daef292010-12-07 15:32:12 +0000126 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregordd182ff2010-12-07 01:26:03 +0000127 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregore3261622010-02-17 18:02:10 +0000128 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor954e0c72010-12-07 18:32:03 +0000129 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregor2b785022010-02-18 02:12:22 +0000130 Decl *VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000131 Decl *VisitObjCClassDecl(ObjCClassDecl *D);
Douglas Gregor040afae2010-11-30 19:14:50 +0000132 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
133 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
134 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
135 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000136 Decl *VisitClassTemplateSpecializationDecl(
137 ClassTemplateSpecializationDecl *D);
Douglas Gregora2bc15b2010-02-18 02:04:09 +0000138
Douglas Gregor4800d952010-02-11 19:21:55 +0000139 // Importing statements
140 Stmt *VisitStmt(Stmt *S);
141
142 // Importing expressions
143 Expr *VisitExpr(Expr *E);
Douglas Gregor44080632010-02-19 01:17:02 +0000144 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor4800d952010-02-11 19:21:55 +0000145 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregorb2e400a2010-02-18 02:21:22 +0000146 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000147 Expr *VisitParenExpr(ParenExpr *E);
148 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000149 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorf638f952010-02-19 01:07:06 +0000150 Expr *VisitBinaryOperator(BinaryOperator *E);
151 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor36ead2e2010-02-12 22:17:39 +0000152 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor008847a2010-02-19 01:32:14 +0000153 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor1b2949d2010-02-05 17:54:41 +0000154 };
155}
156
157//----------------------------------------------------------------------------
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000158// Structural Equivalence
159//----------------------------------------------------------------------------
160
161namespace {
162 struct StructuralEquivalenceContext {
163 /// \brief AST contexts for which we are checking structural equivalence.
164 ASTContext &C1, &C2;
165
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000166 /// \brief The set of "tentative" equivalences between two canonical
167 /// declarations, mapping from a declaration in the first context to the
168 /// declaration in the second context that we believe to be equivalent.
169 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
170
171 /// \brief Queue of declarations in the first context whose equivalence
172 /// with a declaration in the second context still needs to be verified.
173 std::deque<Decl *> DeclsToCheck;
174
Douglas Gregorea35d112010-02-15 23:54:17 +0000175 /// \brief Declaration (from, to) pairs that are known not to be equivalent
176 /// (which we have already complained about).
177 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
178
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000179 /// \brief Whether we're being strict about the spelling of types when
180 /// unifying two types.
181 bool StrictTypeSpelling;
182
183 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorea35d112010-02-15 23:54:17 +0000184 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000185 bool StrictTypeSpelling = false)
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000186 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Douglas Gregorea35d112010-02-15 23:54:17 +0000187 StrictTypeSpelling(StrictTypeSpelling) { }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000188
189 /// \brief Determine whether the two declarations are structurally
190 /// equivalent.
191 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
192
193 /// \brief Determine whether the two types are structurally equivalent.
194 bool IsStructurallyEquivalent(QualType T1, QualType T2);
195
196 private:
197 /// \brief Finish checking all of the structural equivalences.
198 ///
199 /// \returns true if an error occurred, false otherwise.
200 bool Finish();
201
202 public:
203 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000204 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000205 }
206
207 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +0000208 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000209 }
210 };
211}
212
213static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
214 QualType T1, QualType T2);
215static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
216 Decl *D1, Decl *D2);
217
218/// \brief Determine if two APInts have the same value, after zero-extending
219/// one of them (if needed!) to ensure that the bit-widths match.
220static bool IsSameValue(const llvm::APInt &I1, const llvm::APInt &I2) {
221 if (I1.getBitWidth() == I2.getBitWidth())
222 return I1 == I2;
223
224 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000225 return I1 == I2.zext(I1.getBitWidth());
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000226
Jay Foad9f71a8f2010-12-07 08:25:34 +0000227 return I1.zext(I2.getBitWidth()) == I2;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000228}
229
230/// \brief Determine if two APSInts have the same value, zero- or sign-extending
231/// as needed.
232static bool IsSameValue(const llvm::APSInt &I1, const llvm::APSInt &I2) {
233 if (I1.getBitWidth() == I2.getBitWidth() && I1.isSigned() == I2.isSigned())
234 return I1 == I2;
235
236 // Check for a bit-width mismatch.
237 if (I1.getBitWidth() > I2.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000238 return IsSameValue(I1, I2.extend(I1.getBitWidth()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000239 else if (I2.getBitWidth() > I1.getBitWidth())
Jay Foad9f71a8f2010-12-07 08:25:34 +0000240 return IsSameValue(I1.extend(I2.getBitWidth()), I2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000241
242 // We have a signedness mismatch. Turn the signed value into an unsigned
243 // value.
244 if (I1.isSigned()) {
245 if (I1.isNegative())
246 return false;
247
248 return llvm::APSInt(I1, true) == I2;
249 }
250
251 if (I2.isNegative())
252 return false;
253
254 return I1 == llvm::APSInt(I2, true);
255}
256
257/// \brief Determine structural equivalence of two expressions.
258static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
259 Expr *E1, Expr *E2) {
260 if (!E1 || !E2)
261 return E1 == E2;
262
263 // FIXME: Actually perform a structural comparison!
264 return true;
265}
266
267/// \brief Determine whether two identifiers are equivalent.
268static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
269 const IdentifierInfo *Name2) {
270 if (!Name1 || !Name2)
271 return Name1 == Name2;
272
273 return Name1->getName() == Name2->getName();
274}
275
276/// \brief Determine whether two nested-name-specifiers are equivalent.
277static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
278 NestedNameSpecifier *NNS1,
279 NestedNameSpecifier *NNS2) {
280 // FIXME: Implement!
281 return true;
282}
283
284/// \brief Determine whether two template arguments are equivalent.
285static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
286 const TemplateArgument &Arg1,
287 const TemplateArgument &Arg2) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000288 if (Arg1.getKind() != Arg2.getKind())
289 return false;
290
291 switch (Arg1.getKind()) {
292 case TemplateArgument::Null:
293 return true;
294
295 case TemplateArgument::Type:
296 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
297
298 case TemplateArgument::Integral:
299 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
300 Arg2.getIntegralType()))
301 return false;
302
303 return IsSameValue(*Arg1.getAsIntegral(), *Arg2.getAsIntegral());
304
305 case TemplateArgument::Declaration:
306 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
307
308 case TemplateArgument::Template:
309 return IsStructurallyEquivalent(Context,
310 Arg1.getAsTemplate(),
311 Arg2.getAsTemplate());
Douglas Gregora7fc9012011-01-05 18:58:31 +0000312
313 case TemplateArgument::TemplateExpansion:
314 return IsStructurallyEquivalent(Context,
315 Arg1.getAsTemplateOrTemplatePattern(),
316 Arg2.getAsTemplateOrTemplatePattern());
317
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000318 case TemplateArgument::Expression:
319 return IsStructurallyEquivalent(Context,
320 Arg1.getAsExpr(), Arg2.getAsExpr());
321
322 case TemplateArgument::Pack:
323 if (Arg1.pack_size() != Arg2.pack_size())
324 return false;
325
326 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
327 if (!IsStructurallyEquivalent(Context,
328 Arg1.pack_begin()[I],
329 Arg2.pack_begin()[I]))
330 return false;
331
332 return true;
333 }
334
335 llvm_unreachable("Invalid template argument kind");
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000336 return true;
337}
338
339/// \brief Determine structural equivalence for the common part of array
340/// types.
341static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
342 const ArrayType *Array1,
343 const ArrayType *Array2) {
344 if (!IsStructurallyEquivalent(Context,
345 Array1->getElementType(),
346 Array2->getElementType()))
347 return false;
348 if (Array1->getSizeModifier() != Array2->getSizeModifier())
349 return false;
350 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
351 return false;
352
353 return true;
354}
355
356/// \brief Determine structural equivalence of two types.
357static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
358 QualType T1, QualType T2) {
359 if (T1.isNull() || T2.isNull())
360 return T1.isNull() && T2.isNull();
361
362 if (!Context.StrictTypeSpelling) {
363 // We aren't being strict about token-to-token equivalence of types,
364 // so map down to the canonical type.
365 T1 = Context.C1.getCanonicalType(T1);
366 T2 = Context.C2.getCanonicalType(T2);
367 }
368
369 if (T1.getQualifiers() != T2.getQualifiers())
370 return false;
371
Douglas Gregorea35d112010-02-15 23:54:17 +0000372 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000373
Douglas Gregorea35d112010-02-15 23:54:17 +0000374 if (T1->getTypeClass() != T2->getTypeClass()) {
375 // Compare function types with prototypes vs. without prototypes as if
376 // both did not have prototypes.
377 if (T1->getTypeClass() == Type::FunctionProto &&
378 T2->getTypeClass() == Type::FunctionNoProto)
379 TC = Type::FunctionNoProto;
380 else if (T1->getTypeClass() == Type::FunctionNoProto &&
381 T2->getTypeClass() == Type::FunctionProto)
382 TC = Type::FunctionNoProto;
383 else
384 return false;
385 }
386
387 switch (TC) {
388 case Type::Builtin:
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000389 // FIXME: Deal with Char_S/Char_U.
390 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
391 return false;
392 break;
393
394 case Type::Complex:
395 if (!IsStructurallyEquivalent(Context,
396 cast<ComplexType>(T1)->getElementType(),
397 cast<ComplexType>(T2)->getElementType()))
398 return false;
399 break;
400
401 case Type::Pointer:
402 if (!IsStructurallyEquivalent(Context,
403 cast<PointerType>(T1)->getPointeeType(),
404 cast<PointerType>(T2)->getPointeeType()))
405 return false;
406 break;
407
408 case Type::BlockPointer:
409 if (!IsStructurallyEquivalent(Context,
410 cast<BlockPointerType>(T1)->getPointeeType(),
411 cast<BlockPointerType>(T2)->getPointeeType()))
412 return false;
413 break;
414
415 case Type::LValueReference:
416 case Type::RValueReference: {
417 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
418 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
419 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
420 return false;
421 if (Ref1->isInnerRef() != Ref2->isInnerRef())
422 return false;
423 if (!IsStructurallyEquivalent(Context,
424 Ref1->getPointeeTypeAsWritten(),
425 Ref2->getPointeeTypeAsWritten()))
426 return false;
427 break;
428 }
429
430 case Type::MemberPointer: {
431 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
432 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
433 if (!IsStructurallyEquivalent(Context,
434 MemPtr1->getPointeeType(),
435 MemPtr2->getPointeeType()))
436 return false;
437 if (!IsStructurallyEquivalent(Context,
438 QualType(MemPtr1->getClass(), 0),
439 QualType(MemPtr2->getClass(), 0)))
440 return false;
441 break;
442 }
443
444 case Type::ConstantArray: {
445 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
446 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
447 if (!IsSameValue(Array1->getSize(), Array2->getSize()))
448 return false;
449
450 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
451 return false;
452 break;
453 }
454
455 case Type::IncompleteArray:
456 if (!IsArrayStructurallyEquivalent(Context,
457 cast<ArrayType>(T1),
458 cast<ArrayType>(T2)))
459 return false;
460 break;
461
462 case Type::VariableArray: {
463 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
464 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
465 if (!IsStructurallyEquivalent(Context,
466 Array1->getSizeExpr(), Array2->getSizeExpr()))
467 return false;
468
469 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
470 return false;
471
472 break;
473 }
474
475 case Type::DependentSizedArray: {
476 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
477 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
478 if (!IsStructurallyEquivalent(Context,
479 Array1->getSizeExpr(), Array2->getSizeExpr()))
480 return false;
481
482 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
483 return false;
484
485 break;
486 }
487
488 case Type::DependentSizedExtVector: {
489 const DependentSizedExtVectorType *Vec1
490 = cast<DependentSizedExtVectorType>(T1);
491 const DependentSizedExtVectorType *Vec2
492 = cast<DependentSizedExtVectorType>(T2);
493 if (!IsStructurallyEquivalent(Context,
494 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
495 return false;
496 if (!IsStructurallyEquivalent(Context,
497 Vec1->getElementType(),
498 Vec2->getElementType()))
499 return false;
500 break;
501 }
502
503 case Type::Vector:
504 case Type::ExtVector: {
505 const VectorType *Vec1 = cast<VectorType>(T1);
506 const VectorType *Vec2 = cast<VectorType>(T2);
507 if (!IsStructurallyEquivalent(Context,
508 Vec1->getElementType(),
509 Vec2->getElementType()))
510 return false;
511 if (Vec1->getNumElements() != Vec2->getNumElements())
512 return false;
Bob Wilsone86d78c2010-11-10 21:56:12 +0000513 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000514 return false;
Douglas Gregor0e12b442010-02-19 01:36:36 +0000515 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000516 }
517
518 case Type::FunctionProto: {
519 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
520 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
521 if (Proto1->getNumArgs() != Proto2->getNumArgs())
522 return false;
523 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
524 if (!IsStructurallyEquivalent(Context,
525 Proto1->getArgType(I),
526 Proto2->getArgType(I)))
527 return false;
528 }
529 if (Proto1->isVariadic() != Proto2->isVariadic())
530 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000531 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000532 return false;
Sebastian Redl60618fa2011-03-12 11:50:43 +0000533 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
534 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
535 return false;
536 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
537 if (!IsStructurallyEquivalent(Context,
538 Proto1->getExceptionType(I),
539 Proto2->getExceptionType(I)))
540 return false;
541 }
542 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000543 if (!IsStructurallyEquivalent(Context,
Sebastian Redl60618fa2011-03-12 11:50:43 +0000544 Proto1->getNoexceptExpr(),
545 Proto2->getNoexceptExpr()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000546 return false;
547 }
548 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
549 return false;
550
551 // Fall through to check the bits common with FunctionNoProtoType.
552 }
553
554 case Type::FunctionNoProto: {
555 const FunctionType *Function1 = cast<FunctionType>(T1);
556 const FunctionType *Function2 = cast<FunctionType>(T2);
557 if (!IsStructurallyEquivalent(Context,
558 Function1->getResultType(),
559 Function2->getResultType()))
560 return false;
Rafael Espindola264ba482010-03-30 20:24:48 +0000561 if (Function1->getExtInfo() != Function2->getExtInfo())
562 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000563 break;
564 }
565
566 case Type::UnresolvedUsing:
567 if (!IsStructurallyEquivalent(Context,
568 cast<UnresolvedUsingType>(T1)->getDecl(),
569 cast<UnresolvedUsingType>(T2)->getDecl()))
570 return false;
571
572 break;
John McCall9d156a72011-01-06 01:58:22 +0000573
574 case Type::Attributed:
575 if (!IsStructurallyEquivalent(Context,
576 cast<AttributedType>(T1)->getModifiedType(),
577 cast<AttributedType>(T2)->getModifiedType()))
578 return false;
579 if (!IsStructurallyEquivalent(Context,
580 cast<AttributedType>(T1)->getEquivalentType(),
581 cast<AttributedType>(T2)->getEquivalentType()))
582 return false;
583 break;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000584
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000585 case Type::Paren:
586 if (!IsStructurallyEquivalent(Context,
587 cast<ParenType>(T1)->getInnerType(),
588 cast<ParenType>(T2)->getInnerType()))
589 return false;
590 break;
591
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000592 case Type::Typedef:
593 if (!IsStructurallyEquivalent(Context,
594 cast<TypedefType>(T1)->getDecl(),
595 cast<TypedefType>(T2)->getDecl()))
596 return false;
597 break;
598
599 case Type::TypeOfExpr:
600 if (!IsStructurallyEquivalent(Context,
601 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
602 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
603 return false;
604 break;
605
606 case Type::TypeOf:
607 if (!IsStructurallyEquivalent(Context,
608 cast<TypeOfType>(T1)->getUnderlyingType(),
609 cast<TypeOfType>(T2)->getUnderlyingType()))
610 return false;
611 break;
Sean Huntca63c202011-05-24 22:41:36 +0000612
613 case Type::UnaryTransform:
614 if (!IsStructurallyEquivalent(Context,
615 cast<UnaryTransformType>(T1)->getUnderlyingType(),
616 cast<UnaryTransformType>(T1)->getUnderlyingType()))
617 return false;
618 break;
619
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000620 case Type::Decltype:
621 if (!IsStructurallyEquivalent(Context,
622 cast<DecltypeType>(T1)->getUnderlyingExpr(),
623 cast<DecltypeType>(T2)->getUnderlyingExpr()))
624 return false;
625 break;
626
Richard Smith34b41d92011-02-20 03:19:35 +0000627 case Type::Auto:
628 if (!IsStructurallyEquivalent(Context,
629 cast<AutoType>(T1)->getDeducedType(),
630 cast<AutoType>(T2)->getDeducedType()))
631 return false;
632 break;
633
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000634 case Type::Record:
635 case Type::Enum:
636 if (!IsStructurallyEquivalent(Context,
637 cast<TagType>(T1)->getDecl(),
638 cast<TagType>(T2)->getDecl()))
639 return false;
640 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000641
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000642 case Type::TemplateTypeParm: {
643 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
644 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
645 if (Parm1->getDepth() != Parm2->getDepth())
646 return false;
647 if (Parm1->getIndex() != Parm2->getIndex())
648 return false;
649 if (Parm1->isParameterPack() != Parm2->isParameterPack())
650 return false;
651
652 // Names of template type parameters are never significant.
653 break;
654 }
655
656 case Type::SubstTemplateTypeParm: {
657 const SubstTemplateTypeParmType *Subst1
658 = cast<SubstTemplateTypeParmType>(T1);
659 const SubstTemplateTypeParmType *Subst2
660 = cast<SubstTemplateTypeParmType>(T2);
661 if (!IsStructurallyEquivalent(Context,
662 QualType(Subst1->getReplacedParameter(), 0),
663 QualType(Subst2->getReplacedParameter(), 0)))
664 return false;
665 if (!IsStructurallyEquivalent(Context,
666 Subst1->getReplacementType(),
667 Subst2->getReplacementType()))
668 return false;
669 break;
670 }
671
Douglas Gregor0bc15d92011-01-14 05:11:40 +0000672 case Type::SubstTemplateTypeParmPack: {
673 const SubstTemplateTypeParmPackType *Subst1
674 = cast<SubstTemplateTypeParmPackType>(T1);
675 const SubstTemplateTypeParmPackType *Subst2
676 = cast<SubstTemplateTypeParmPackType>(T2);
677 if (!IsStructurallyEquivalent(Context,
678 QualType(Subst1->getReplacedParameter(), 0),
679 QualType(Subst2->getReplacedParameter(), 0)))
680 return false;
681 if (!IsStructurallyEquivalent(Context,
682 Subst1->getArgumentPack(),
683 Subst2->getArgumentPack()))
684 return false;
685 break;
686 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000687 case Type::TemplateSpecialization: {
688 const TemplateSpecializationType *Spec1
689 = cast<TemplateSpecializationType>(T1);
690 const TemplateSpecializationType *Spec2
691 = cast<TemplateSpecializationType>(T2);
692 if (!IsStructurallyEquivalent(Context,
693 Spec1->getTemplateName(),
694 Spec2->getTemplateName()))
695 return false;
696 if (Spec1->getNumArgs() != Spec2->getNumArgs())
697 return false;
698 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
699 if (!IsStructurallyEquivalent(Context,
700 Spec1->getArg(I), Spec2->getArg(I)))
701 return false;
702 }
703 break;
704 }
705
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000706 case Type::Elaborated: {
707 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
708 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
709 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
710 if (Elab1->getKeyword() != Elab2->getKeyword())
711 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000712 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000713 Elab1->getQualifier(),
714 Elab2->getQualifier()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000715 return false;
716 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000717 Elab1->getNamedType(),
718 Elab2->getNamedType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000719 return false;
720 break;
721 }
722
John McCall3cb0ebd2010-03-10 03:28:59 +0000723 case Type::InjectedClassName: {
724 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
725 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
726 if (!IsStructurallyEquivalent(Context,
John McCall31f17ec2010-04-27 00:57:59 +0000727 Inj1->getInjectedSpecializationType(),
728 Inj2->getInjectedSpecializationType()))
John McCall3cb0ebd2010-03-10 03:28:59 +0000729 return false;
730 break;
731 }
732
Douglas Gregor4714c122010-03-31 17:34:00 +0000733 case Type::DependentName: {
734 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
735 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000736 if (!IsStructurallyEquivalent(Context,
737 Typename1->getQualifier(),
738 Typename2->getQualifier()))
739 return false;
740 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
741 Typename2->getIdentifier()))
742 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000743
744 break;
745 }
746
John McCall33500952010-06-11 00:33:02 +0000747 case Type::DependentTemplateSpecialization: {
748 const DependentTemplateSpecializationType *Spec1 =
749 cast<DependentTemplateSpecializationType>(T1);
750 const DependentTemplateSpecializationType *Spec2 =
751 cast<DependentTemplateSpecializationType>(T2);
752 if (!IsStructurallyEquivalent(Context,
753 Spec1->getQualifier(),
754 Spec2->getQualifier()))
755 return false;
756 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
757 Spec2->getIdentifier()))
758 return false;
759 if (Spec1->getNumArgs() != Spec2->getNumArgs())
760 return false;
761 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
762 if (!IsStructurallyEquivalent(Context,
763 Spec1->getArg(I), Spec2->getArg(I)))
764 return false;
765 }
766 break;
767 }
Douglas Gregor7536dd52010-12-20 02:24:11 +0000768
769 case Type::PackExpansion:
770 if (!IsStructurallyEquivalent(Context,
771 cast<PackExpansionType>(T1)->getPattern(),
772 cast<PackExpansionType>(T2)->getPattern()))
773 return false;
774 break;
775
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000776 case Type::ObjCInterface: {
777 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
778 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
779 if (!IsStructurallyEquivalent(Context,
780 Iface1->getDecl(), Iface2->getDecl()))
781 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000782 break;
783 }
784
785 case Type::ObjCObject: {
786 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
787 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
788 if (!IsStructurallyEquivalent(Context,
789 Obj1->getBaseType(),
790 Obj2->getBaseType()))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000791 return false;
John McCallc12c5bb2010-05-15 11:32:37 +0000792 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
793 return false;
794 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000795 if (!IsStructurallyEquivalent(Context,
John McCallc12c5bb2010-05-15 11:32:37 +0000796 Obj1->getProtocol(I),
797 Obj2->getProtocol(I)))
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000798 return false;
799 }
800 break;
801 }
802
803 case Type::ObjCObjectPointer: {
804 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
805 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
806 if (!IsStructurallyEquivalent(Context,
807 Ptr1->getPointeeType(),
808 Ptr2->getPointeeType()))
809 return false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000810 break;
811 }
812
813 } // end switch
814
815 return true;
816}
817
818/// \brief Determine structural equivalence of two records.
819static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
820 RecordDecl *D1, RecordDecl *D2) {
821 if (D1->isUnion() != D2->isUnion()) {
822 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
823 << Context.C2.getTypeDeclType(D2);
824 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
825 << D1->getDeclName() << (unsigned)D1->getTagKind();
826 return false;
827 }
828
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000829 // If both declarations are class template specializations, we know
830 // the ODR applies, so check the template and template arguments.
831 ClassTemplateSpecializationDecl *Spec1
832 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
833 ClassTemplateSpecializationDecl *Spec2
834 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
835 if (Spec1 && Spec2) {
836 // Check that the specialized templates are the same.
837 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
838 Spec2->getSpecializedTemplate()))
839 return false;
840
841 // Check that the template arguments are the same.
842 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
843 return false;
844
845 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
846 if (!IsStructurallyEquivalent(Context,
847 Spec1->getTemplateArgs().get(I),
848 Spec2->getTemplateArgs().get(I)))
849 return false;
850 }
851 // If one is a class template specialization and the other is not, these
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000852 // structures are different.
Douglas Gregord5dc83a2010-12-01 01:36:18 +0000853 else if (Spec1 || Spec2)
854 return false;
855
Douglas Gregorea35d112010-02-15 23:54:17 +0000856 // Compare the definitions of these two records. If either or both are
857 // incomplete, we assume that they are equivalent.
858 D1 = D1->getDefinition();
859 D2 = D2->getDefinition();
860 if (!D1 || !D2)
861 return true;
862
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000863 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
864 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
865 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
866 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
Douglas Gregor040afae2010-11-30 19:14:50 +0000867 << Context.C2.getTypeDeclType(D2);
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000868 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000869 << D2CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000870 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
Douglas Gregor040afae2010-11-30 19:14:50 +0000871 << D1CXX->getNumBases();
Douglas Gregor73dc30b2010-02-15 22:01:00 +0000872 return false;
873 }
874
875 // Check the base classes.
876 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
877 BaseEnd1 = D1CXX->bases_end(),
878 Base2 = D2CXX->bases_begin();
879 Base1 != BaseEnd1;
880 ++Base1, ++Base2) {
881 if (!IsStructurallyEquivalent(Context,
882 Base1->getType(), Base2->getType())) {
883 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
884 << Context.C2.getTypeDeclType(D2);
885 Context.Diag2(Base2->getSourceRange().getBegin(), diag::note_odr_base)
886 << Base2->getType()
887 << Base2->getSourceRange();
888 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
889 << Base1->getType()
890 << Base1->getSourceRange();
891 return false;
892 }
893
894 // Check virtual vs. non-virtual inheritance mismatch.
895 if (Base1->isVirtual() != Base2->isVirtual()) {
896 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
897 << Context.C2.getTypeDeclType(D2);
898 Context.Diag2(Base2->getSourceRange().getBegin(),
899 diag::note_odr_virtual_base)
900 << Base2->isVirtual() << Base2->getSourceRange();
901 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
902 << Base1->isVirtual()
903 << Base1->getSourceRange();
904 return false;
905 }
906 }
907 } else if (D1CXX->getNumBases() > 0) {
908 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
909 << Context.C2.getTypeDeclType(D2);
910 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
911 Context.Diag1(Base1->getSourceRange().getBegin(), diag::note_odr_base)
912 << Base1->getType()
913 << Base1->getSourceRange();
914 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
915 return false;
916 }
917 }
918
919 // Check the fields for consistency.
920 CXXRecordDecl::field_iterator Field2 = D2->field_begin(),
921 Field2End = D2->field_end();
922 for (CXXRecordDecl::field_iterator Field1 = D1->field_begin(),
923 Field1End = D1->field_end();
924 Field1 != Field1End;
925 ++Field1, ++Field2) {
926 if (Field2 == Field2End) {
927 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
928 << Context.C2.getTypeDeclType(D2);
929 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
930 << Field1->getDeclName() << Field1->getType();
931 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
932 return false;
933 }
934
935 if (!IsStructurallyEquivalent(Context,
936 Field1->getType(), Field2->getType())) {
937 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
938 << Context.C2.getTypeDeclType(D2);
939 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
940 << Field2->getDeclName() << Field2->getType();
941 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
942 << Field1->getDeclName() << Field1->getType();
943 return false;
944 }
945
946 if (Field1->isBitField() != Field2->isBitField()) {
947 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
948 << Context.C2.getTypeDeclType(D2);
949 if (Field1->isBitField()) {
950 llvm::APSInt Bits;
951 Field1->getBitWidth()->isIntegerConstantExpr(Bits, Context.C1);
952 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
953 << Field1->getDeclName() << Field1->getType()
954 << Bits.toString(10, false);
955 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
956 << Field2->getDeclName();
957 } else {
958 llvm::APSInt Bits;
959 Field2->getBitWidth()->isIntegerConstantExpr(Bits, Context.C2);
960 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
961 << Field2->getDeclName() << Field2->getType()
962 << Bits.toString(10, false);
963 Context.Diag1(Field1->getLocation(),
964 diag::note_odr_not_bit_field)
965 << Field1->getDeclName();
966 }
967 return false;
968 }
969
970 if (Field1->isBitField()) {
971 // Make sure that the bit-fields are the same length.
972 llvm::APSInt Bits1, Bits2;
973 if (!Field1->getBitWidth()->isIntegerConstantExpr(Bits1, Context.C1))
974 return false;
975 if (!Field2->getBitWidth()->isIntegerConstantExpr(Bits2, Context.C2))
976 return false;
977
978 if (!IsSameValue(Bits1, Bits2)) {
979 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
980 << Context.C2.getTypeDeclType(D2);
981 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
982 << Field2->getDeclName() << Field2->getType()
983 << Bits2.toString(10, false);
984 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
985 << Field1->getDeclName() << Field1->getType()
986 << Bits1.toString(10, false);
987 return false;
988 }
989 }
990 }
991
992 if (Field2 != Field2End) {
993 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
994 << Context.C2.getTypeDeclType(D2);
995 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
996 << Field2->getDeclName() << Field2->getType();
997 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
998 return false;
999 }
1000
1001 return true;
1002}
1003
1004/// \brief Determine structural equivalence of two enums.
1005static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1006 EnumDecl *D1, EnumDecl *D2) {
1007 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1008 EC2End = D2->enumerator_end();
1009 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1010 EC1End = D1->enumerator_end();
1011 EC1 != EC1End; ++EC1, ++EC2) {
1012 if (EC2 == EC2End) {
1013 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1014 << Context.C2.getTypeDeclType(D2);
1015 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1016 << EC1->getDeclName()
1017 << EC1->getInitVal().toString(10);
1018 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1019 return false;
1020 }
1021
1022 llvm::APSInt Val1 = EC1->getInitVal();
1023 llvm::APSInt Val2 = EC2->getInitVal();
1024 if (!IsSameValue(Val1, Val2) ||
1025 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
1026 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1027 << Context.C2.getTypeDeclType(D2);
1028 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1029 << EC2->getDeclName()
1030 << EC2->getInitVal().toString(10);
1031 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1032 << EC1->getDeclName()
1033 << EC1->getInitVal().toString(10);
1034 return false;
1035 }
1036 }
1037
1038 if (EC2 != EC2End) {
1039 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1040 << Context.C2.getTypeDeclType(D2);
1041 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1042 << EC2->getDeclName()
1043 << EC2->getInitVal().toString(10);
1044 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1045 return false;
1046 }
1047
1048 return true;
1049}
Douglas Gregor040afae2010-11-30 19:14:50 +00001050
1051static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1052 TemplateParameterList *Params1,
1053 TemplateParameterList *Params2) {
1054 if (Params1->size() != Params2->size()) {
1055 Context.Diag2(Params2->getTemplateLoc(),
1056 diag::err_odr_different_num_template_parameters)
1057 << Params1->size() << Params2->size();
1058 Context.Diag1(Params1->getTemplateLoc(),
1059 diag::note_odr_template_parameter_list);
1060 return false;
1061 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001062
Douglas Gregor040afae2010-11-30 19:14:50 +00001063 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1064 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
1065 Context.Diag2(Params2->getParam(I)->getLocation(),
1066 diag::err_odr_different_template_parameter_kind);
1067 Context.Diag1(Params1->getParam(I)->getLocation(),
1068 diag::note_odr_template_parameter_here);
1069 return false;
1070 }
1071
1072 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1073 Params2->getParam(I))) {
1074
1075 return false;
1076 }
1077 }
1078
1079 return true;
1080}
1081
1082static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1083 TemplateTypeParmDecl *D1,
1084 TemplateTypeParmDecl *D2) {
1085 if (D1->isParameterPack() != D2->isParameterPack()) {
1086 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1087 << D2->isParameterPack();
1088 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1089 << D1->isParameterPack();
1090 return false;
1091 }
1092
1093 return true;
1094}
1095
1096static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1097 NonTypeTemplateParmDecl *D1,
1098 NonTypeTemplateParmDecl *D2) {
1099 // FIXME: Enable once we have variadic templates.
1100#if 0
1101 if (D1->isParameterPack() != D2->isParameterPack()) {
1102 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1103 << D2->isParameterPack();
1104 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1105 << D1->isParameterPack();
1106 return false;
1107 }
1108#endif
1109
1110 // Check types.
1111 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
1112 Context.Diag2(D2->getLocation(),
1113 diag::err_odr_non_type_parameter_type_inconsistent)
1114 << D2->getType() << D1->getType();
1115 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1116 << D1->getType();
1117 return false;
1118 }
1119
1120 return true;
1121}
1122
1123static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1124 TemplateTemplateParmDecl *D1,
1125 TemplateTemplateParmDecl *D2) {
1126 // FIXME: Enable once we have variadic templates.
1127#if 0
1128 if (D1->isParameterPack() != D2->isParameterPack()) {
1129 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1130 << D2->isParameterPack();
1131 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1132 << D1->isParameterPack();
1133 return false;
1134 }
1135#endif
1136
1137 // Check template parameter lists.
1138 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1139 D2->getTemplateParameters());
1140}
1141
1142static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1143 ClassTemplateDecl *D1,
1144 ClassTemplateDecl *D2) {
1145 // Check template parameters.
1146 if (!IsStructurallyEquivalent(Context,
1147 D1->getTemplateParameters(),
1148 D2->getTemplateParameters()))
1149 return false;
1150
1151 // Check the templated declaration.
1152 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1153 D2->getTemplatedDecl());
1154}
1155
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001156/// \brief Determine structural equivalence of two declarations.
1157static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1158 Decl *D1, Decl *D2) {
1159 // FIXME: Check for known structural equivalences via a callback of some sort.
1160
Douglas Gregorea35d112010-02-15 23:54:17 +00001161 // Check whether we already know that these two declarations are not
1162 // structurally equivalent.
1163 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1164 D2->getCanonicalDecl())))
1165 return false;
1166
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001167 // Determine whether we've already produced a tentative equivalence for D1.
1168 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1169 if (EquivToD1)
1170 return EquivToD1 == D2->getCanonicalDecl();
1171
1172 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1173 EquivToD1 = D2->getCanonicalDecl();
1174 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1175 return true;
1176}
1177
1178bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1179 Decl *D2) {
1180 if (!::IsStructurallyEquivalent(*this, D1, D2))
1181 return false;
1182
1183 return !Finish();
1184}
1185
1186bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1187 QualType T2) {
1188 if (!::IsStructurallyEquivalent(*this, T1, T2))
1189 return false;
1190
1191 return !Finish();
1192}
1193
1194bool StructuralEquivalenceContext::Finish() {
1195 while (!DeclsToCheck.empty()) {
1196 // Check the next declaration.
1197 Decl *D1 = DeclsToCheck.front();
1198 DeclsToCheck.pop_front();
1199
1200 Decl *D2 = TentativeEquivalences[D1];
1201 assert(D2 && "Unrecorded tentative equivalence?");
1202
Douglas Gregorea35d112010-02-15 23:54:17 +00001203 bool Equivalent = true;
1204
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001205 // FIXME: Switch on all declaration kinds. For now, we're just going to
1206 // check the obvious ones.
1207 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1208 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1209 // Check for equivalent structure names.
1210 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001211 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1212 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001213 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001214 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1215 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001216 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1217 !::IsStructurallyEquivalent(*this, Record1, Record2))
1218 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001219 } else {
1220 // Record/non-record mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001221 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001222 }
Douglas Gregorea35d112010-02-15 23:54:17 +00001223 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001224 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1225 // Check for equivalent enum names.
1226 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001227 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1228 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001229 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smith162e1c12011-04-15 14:24:37 +00001230 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1231 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorea35d112010-02-15 23:54:17 +00001232 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1233 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1234 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001235 } else {
1236 // Enum/non-enum mismatch
Douglas Gregorea35d112010-02-15 23:54:17 +00001237 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001238 }
Richard Smith162e1c12011-04-15 14:24:37 +00001239 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1240 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001241 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001242 Typedef2->getIdentifier()) ||
1243 !::IsStructurallyEquivalent(*this,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001244 Typedef1->getUnderlyingType(),
1245 Typedef2->getUnderlyingType()))
Douglas Gregorea35d112010-02-15 23:54:17 +00001246 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001247 } else {
1248 // Typedef/non-typedef mismatch.
Douglas Gregorea35d112010-02-15 23:54:17 +00001249 Equivalent = false;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001250 }
Douglas Gregor040afae2010-11-30 19:14:50 +00001251 } else if (ClassTemplateDecl *ClassTemplate1
1252 = dyn_cast<ClassTemplateDecl>(D1)) {
1253 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1254 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1255 ClassTemplate2->getIdentifier()) ||
1256 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1257 Equivalent = false;
1258 } else {
1259 // Class template/non-class-template mismatch.
1260 Equivalent = false;
1261 }
1262 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1263 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1264 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1265 Equivalent = false;
1266 } else {
1267 // Kind mismatch.
1268 Equivalent = false;
1269 }
1270 } else if (NonTypeTemplateParmDecl *NTTP1
1271 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1272 if (NonTypeTemplateParmDecl *NTTP2
1273 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1274 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1275 Equivalent = false;
1276 } else {
1277 // Kind mismatch.
1278 Equivalent = false;
1279 }
1280 } else if (TemplateTemplateParmDecl *TTP1
1281 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1282 if (TemplateTemplateParmDecl *TTP2
1283 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1284 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1285 Equivalent = false;
1286 } else {
1287 // Kind mismatch.
1288 Equivalent = false;
1289 }
1290 }
1291
Douglas Gregorea35d112010-02-15 23:54:17 +00001292 if (!Equivalent) {
1293 // Note that these two declarations are not equivalent (and we already
1294 // know about it).
1295 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1296 D2->getCanonicalDecl()));
1297 return true;
1298 }
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001299 // FIXME: Check other declaration kinds!
1300 }
1301
1302 return false;
1303}
1304
1305//----------------------------------------------------------------------------
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001306// Import Types
1307//----------------------------------------------------------------------------
1308
John McCallf4c73712011-01-19 06:33:43 +00001309QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregor89cc9d62010-02-09 22:48:33 +00001310 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1311 << T->getTypeClassName();
1312 return QualType();
1313}
1314
John McCallf4c73712011-01-19 06:33:43 +00001315QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001316 switch (T->getKind()) {
1317 case BuiltinType::Void: return Importer.getToContext().VoidTy;
1318 case BuiltinType::Bool: return Importer.getToContext().BoolTy;
1319
1320 case BuiltinType::Char_U:
1321 // The context we're importing from has an unsigned 'char'. If we're
1322 // importing into a context with a signed 'char', translate to
1323 // 'unsigned char' instead.
1324 if (Importer.getToContext().getLangOptions().CharIsSigned)
1325 return Importer.getToContext().UnsignedCharTy;
1326
1327 return Importer.getToContext().CharTy;
1328
1329 case BuiltinType::UChar: return Importer.getToContext().UnsignedCharTy;
1330
1331 case BuiltinType::Char16:
1332 // FIXME: Make sure that the "to" context supports C++!
1333 return Importer.getToContext().Char16Ty;
1334
1335 case BuiltinType::Char32:
1336 // FIXME: Make sure that the "to" context supports C++!
1337 return Importer.getToContext().Char32Ty;
1338
1339 case BuiltinType::UShort: return Importer.getToContext().UnsignedShortTy;
1340 case BuiltinType::UInt: return Importer.getToContext().UnsignedIntTy;
1341 case BuiltinType::ULong: return Importer.getToContext().UnsignedLongTy;
1342 case BuiltinType::ULongLong:
1343 return Importer.getToContext().UnsignedLongLongTy;
1344 case BuiltinType::UInt128: return Importer.getToContext().UnsignedInt128Ty;
1345
1346 case BuiltinType::Char_S:
1347 // The context we're importing from has an unsigned 'char'. If we're
1348 // importing into a context with a signed 'char', translate to
1349 // 'unsigned char' instead.
1350 if (!Importer.getToContext().getLangOptions().CharIsSigned)
1351 return Importer.getToContext().SignedCharTy;
1352
1353 return Importer.getToContext().CharTy;
1354
1355 case BuiltinType::SChar: return Importer.getToContext().SignedCharTy;
Chris Lattner3f59c972010-12-25 23:25:43 +00001356 case BuiltinType::WChar_S:
1357 case BuiltinType::WChar_U:
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001358 // FIXME: If not in C++, shall we translate to the C equivalent of
1359 // wchar_t?
1360 return Importer.getToContext().WCharTy;
1361
1362 case BuiltinType::Short : return Importer.getToContext().ShortTy;
1363 case BuiltinType::Int : return Importer.getToContext().IntTy;
1364 case BuiltinType::Long : return Importer.getToContext().LongTy;
1365 case BuiltinType::LongLong : return Importer.getToContext().LongLongTy;
1366 case BuiltinType::Int128 : return Importer.getToContext().Int128Ty;
1367 case BuiltinType::Float: return Importer.getToContext().FloatTy;
1368 case BuiltinType::Double: return Importer.getToContext().DoubleTy;
1369 case BuiltinType::LongDouble: return Importer.getToContext().LongDoubleTy;
1370
1371 case BuiltinType::NullPtr:
1372 // FIXME: Make sure that the "to" context supports C++0x!
1373 return Importer.getToContext().NullPtrTy;
1374
1375 case BuiltinType::Overload: return Importer.getToContext().OverloadTy;
1376 case BuiltinType::Dependent: return Importer.getToContext().DependentTy;
John McCall1de4d4e2011-04-07 08:22:57 +00001377 case BuiltinType::UnknownAny: return Importer.getToContext().UnknownAnyTy;
John McCall864c0412011-04-26 20:42:42 +00001378 case BuiltinType::BoundMember: return Importer.getToContext().BoundMemberTy;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001379
1380 case BuiltinType::ObjCId:
1381 // FIXME: Make sure that the "to" context supports Objective-C!
1382 return Importer.getToContext().ObjCBuiltinIdTy;
1383
1384 case BuiltinType::ObjCClass:
1385 return Importer.getToContext().ObjCBuiltinClassTy;
1386
1387 case BuiltinType::ObjCSel:
1388 return Importer.getToContext().ObjCBuiltinSelTy;
1389 }
1390
1391 return QualType();
1392}
1393
John McCallf4c73712011-01-19 06:33:43 +00001394QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001395 QualType ToElementType = Importer.Import(T->getElementType());
1396 if (ToElementType.isNull())
1397 return QualType();
1398
1399 return Importer.getToContext().getComplexType(ToElementType);
1400}
1401
John McCallf4c73712011-01-19 06:33:43 +00001402QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001403 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1404 if (ToPointeeType.isNull())
1405 return QualType();
1406
1407 return Importer.getToContext().getPointerType(ToPointeeType);
1408}
1409
John McCallf4c73712011-01-19 06:33:43 +00001410QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001411 // FIXME: Check for blocks support in "to" context.
1412 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1413 if (ToPointeeType.isNull())
1414 return QualType();
1415
1416 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1417}
1418
John McCallf4c73712011-01-19 06:33:43 +00001419QualType
1420ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001421 // FIXME: Check for C++ support in "to" context.
1422 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1423 if (ToPointeeType.isNull())
1424 return QualType();
1425
1426 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1427}
1428
John McCallf4c73712011-01-19 06:33:43 +00001429QualType
1430ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001431 // FIXME: Check for C++0x support in "to" context.
1432 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1433 if (ToPointeeType.isNull())
1434 return QualType();
1435
1436 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1437}
1438
John McCallf4c73712011-01-19 06:33:43 +00001439QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001440 // FIXME: Check for C++ support in "to" context.
1441 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1442 if (ToPointeeType.isNull())
1443 return QualType();
1444
1445 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1446 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1447 ClassType.getTypePtr());
1448}
1449
John McCallf4c73712011-01-19 06:33:43 +00001450QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001451 QualType ToElementType = Importer.Import(T->getElementType());
1452 if (ToElementType.isNull())
1453 return QualType();
1454
1455 return Importer.getToContext().getConstantArrayType(ToElementType,
1456 T->getSize(),
1457 T->getSizeModifier(),
1458 T->getIndexTypeCVRQualifiers());
1459}
1460
John McCallf4c73712011-01-19 06:33:43 +00001461QualType
1462ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001463 QualType ToElementType = Importer.Import(T->getElementType());
1464 if (ToElementType.isNull())
1465 return QualType();
1466
1467 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1468 T->getSizeModifier(),
1469 T->getIndexTypeCVRQualifiers());
1470}
1471
John McCallf4c73712011-01-19 06:33:43 +00001472QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001473 QualType ToElementType = Importer.Import(T->getElementType());
1474 if (ToElementType.isNull())
1475 return QualType();
1476
1477 Expr *Size = Importer.Import(T->getSizeExpr());
1478 if (!Size)
1479 return QualType();
1480
1481 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1482 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1483 T->getSizeModifier(),
1484 T->getIndexTypeCVRQualifiers(),
1485 Brackets);
1486}
1487
John McCallf4c73712011-01-19 06:33:43 +00001488QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001489 QualType ToElementType = Importer.Import(T->getElementType());
1490 if (ToElementType.isNull())
1491 return QualType();
1492
1493 return Importer.getToContext().getVectorType(ToElementType,
1494 T->getNumElements(),
Bob Wilsone86d78c2010-11-10 21:56:12 +00001495 T->getVectorKind());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001496}
1497
John McCallf4c73712011-01-19 06:33:43 +00001498QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001499 QualType ToElementType = Importer.Import(T->getElementType());
1500 if (ToElementType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getExtVectorType(ToElementType,
1504 T->getNumElements());
1505}
1506
John McCallf4c73712011-01-19 06:33:43 +00001507QualType
1508ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001509 // FIXME: What happens if we're importing a function without a prototype
1510 // into C++? Should we make it variadic?
1511 QualType ToResultType = Importer.Import(T->getResultType());
1512 if (ToResultType.isNull())
1513 return QualType();
Rafael Espindola264ba482010-03-30 20:24:48 +00001514
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001515 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindola264ba482010-03-30 20:24:48 +00001516 T->getExtInfo());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001517}
1518
John McCallf4c73712011-01-19 06:33:43 +00001519QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001520 QualType ToResultType = Importer.Import(T->getResultType());
1521 if (ToResultType.isNull())
1522 return QualType();
1523
1524 // Import argument types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001525 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001526 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1527 AEnd = T->arg_type_end();
1528 A != AEnd; ++A) {
1529 QualType ArgType = Importer.Import(*A);
1530 if (ArgType.isNull())
1531 return QualType();
1532 ArgTypes.push_back(ArgType);
1533 }
1534
1535 // Import exception types
Chris Lattner5f9e2722011-07-23 10:55:15 +00001536 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001537 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1538 EEnd = T->exception_end();
1539 E != EEnd; ++E) {
1540 QualType ExceptionType = Importer.Import(*E);
1541 if (ExceptionType.isNull())
1542 return QualType();
1543 ExceptionTypes.push_back(ExceptionType);
1544 }
John McCalle23cf432010-12-14 08:05:40 +00001545
1546 FunctionProtoType::ExtProtoInfo EPI = T->getExtProtoInfo();
1547 EPI.Exceptions = ExceptionTypes.data();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001548
1549 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes.data(),
John McCalle23cf432010-12-14 08:05:40 +00001550 ArgTypes.size(), EPI);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001551}
1552
John McCallf4c73712011-01-19 06:33:43 +00001553QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smith162e1c12011-04-15 14:24:37 +00001554 TypedefNameDecl *ToDecl
1555 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001556 if (!ToDecl)
1557 return QualType();
1558
1559 return Importer.getToContext().getTypeDeclType(ToDecl);
1560}
1561
John McCallf4c73712011-01-19 06:33:43 +00001562QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001563 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1564 if (!ToExpr)
1565 return QualType();
1566
1567 return Importer.getToContext().getTypeOfExprType(ToExpr);
1568}
1569
John McCallf4c73712011-01-19 06:33:43 +00001570QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001571 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1572 if (ToUnderlyingType.isNull())
1573 return QualType();
1574
1575 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1576}
1577
John McCallf4c73712011-01-19 06:33:43 +00001578QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith34b41d92011-02-20 03:19:35 +00001579 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001580 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1581 if (!ToExpr)
1582 return QualType();
1583
1584 return Importer.getToContext().getDecltypeType(ToExpr);
1585}
1586
Sean Huntca63c202011-05-24 22:41:36 +00001587QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1588 QualType ToBaseType = Importer.Import(T->getBaseType());
1589 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1590 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1591 return QualType();
1592
1593 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1594 ToUnderlyingType,
1595 T->getUTTKind());
1596}
1597
Richard Smith34b41d92011-02-20 03:19:35 +00001598QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
1599 // FIXME: Make sure that the "to" context supports C++0x!
1600 QualType FromDeduced = T->getDeducedType();
1601 QualType ToDeduced;
1602 if (!FromDeduced.isNull()) {
1603 ToDeduced = Importer.Import(FromDeduced);
1604 if (ToDeduced.isNull())
1605 return QualType();
1606 }
1607
1608 return Importer.getToContext().getAutoType(ToDeduced);
1609}
1610
John McCallf4c73712011-01-19 06:33:43 +00001611QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001612 RecordDecl *ToDecl
1613 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1614 if (!ToDecl)
1615 return QualType();
1616
1617 return Importer.getToContext().getTagDeclType(ToDecl);
1618}
1619
John McCallf4c73712011-01-19 06:33:43 +00001620QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001621 EnumDecl *ToDecl
1622 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1623 if (!ToDecl)
1624 return QualType();
1625
1626 return Importer.getToContext().getTagDeclType(ToDecl);
1627}
1628
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001629QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCallf4c73712011-01-19 06:33:43 +00001630 const TemplateSpecializationType *T) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001631 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1632 if (ToTemplate.isNull())
1633 return QualType();
1634
Chris Lattner5f9e2722011-07-23 10:55:15 +00001635 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001636 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1637 return QualType();
1638
1639 QualType ToCanonType;
1640 if (!QualType(T, 0).isCanonical()) {
1641 QualType FromCanonType
1642 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1643 ToCanonType =Importer.Import(FromCanonType);
1644 if (ToCanonType.isNull())
1645 return QualType();
1646 }
1647 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1648 ToTemplateArgs.data(),
1649 ToTemplateArgs.size(),
1650 ToCanonType);
1651}
1652
John McCallf4c73712011-01-19 06:33:43 +00001653QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001654 NestedNameSpecifier *ToQualifier = 0;
1655 // Note: the qualifier in an ElaboratedType is optional.
1656 if (T->getQualifier()) {
1657 ToQualifier = Importer.Import(T->getQualifier());
1658 if (!ToQualifier)
1659 return QualType();
1660 }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001661
1662 QualType ToNamedType = Importer.Import(T->getNamedType());
1663 if (ToNamedType.isNull())
1664 return QualType();
1665
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001666 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1667 ToQualifier, ToNamedType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001668}
1669
John McCallf4c73712011-01-19 06:33:43 +00001670QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001671 ObjCInterfaceDecl *Class
1672 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1673 if (!Class)
1674 return QualType();
1675
John McCallc12c5bb2010-05-15 11:32:37 +00001676 return Importer.getToContext().getObjCInterfaceType(Class);
1677}
1678
John McCallf4c73712011-01-19 06:33:43 +00001679QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +00001680 QualType ToBaseType = Importer.Import(T->getBaseType());
1681 if (ToBaseType.isNull())
1682 return QualType();
1683
Chris Lattner5f9e2722011-07-23 10:55:15 +00001684 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCallc12c5bb2010-05-15 11:32:37 +00001685 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001686 PEnd = T->qual_end();
1687 P != PEnd; ++P) {
1688 ObjCProtocolDecl *Protocol
1689 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1690 if (!Protocol)
1691 return QualType();
1692 Protocols.push_back(Protocol);
1693 }
1694
John McCallc12c5bb2010-05-15 11:32:37 +00001695 return Importer.getToContext().getObjCObjectType(ToBaseType,
1696 Protocols.data(),
1697 Protocols.size());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001698}
1699
John McCallf4c73712011-01-19 06:33:43 +00001700QualType
1701ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001702 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1703 if (ToPointeeType.isNull())
1704 return QualType();
1705
John McCallc12c5bb2010-05-15 11:32:37 +00001706 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00001707}
1708
Douglas Gregor089459a2010-02-08 21:09:39 +00001709//----------------------------------------------------------------------------
1710// Import Declarations
1711//----------------------------------------------------------------------------
Douglas Gregora404ea62010-02-10 19:54:31 +00001712bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1713 DeclContext *&LexicalDC,
1714 DeclarationName &Name,
1715 SourceLocation &Loc) {
1716 // Import the context of this declaration.
1717 DC = Importer.ImportContext(D->getDeclContext());
1718 if (!DC)
1719 return true;
1720
1721 LexicalDC = DC;
1722 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1723 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1724 if (!LexicalDC)
1725 return true;
1726 }
1727
1728 // Import the name of this declaration.
1729 Name = Importer.Import(D->getDeclName());
1730 if (D->getDeclName() && !Name)
1731 return true;
1732
1733 // Import the location of this declaration.
1734 Loc = Importer.Import(D->getLocation());
1735 return false;
1736}
1737
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001738void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1739 if (!FromD)
1740 return;
1741
1742 if (!ToD) {
1743 ToD = Importer.Import(FromD);
1744 if (!ToD)
1745 return;
1746 }
1747
1748 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1749 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
1750 if (FromRecord->getDefinition() && !ToRecord->getDefinition()) {
1751 ImportDefinition(FromRecord, ToRecord);
1752 }
1753 }
1754 return;
1755 }
1756
1757 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1758 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1759 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1760 ImportDefinition(FromEnum, ToEnum);
1761 }
1762 }
1763 return;
1764 }
1765}
1766
Abramo Bagnara25777432010-08-11 22:01:17 +00001767void
1768ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1769 DeclarationNameInfo& To) {
1770 // NOTE: To.Name and To.Loc are already imported.
1771 // We only have to import To.LocInfo.
1772 switch (To.getName().getNameKind()) {
1773 case DeclarationName::Identifier:
1774 case DeclarationName::ObjCZeroArgSelector:
1775 case DeclarationName::ObjCOneArgSelector:
1776 case DeclarationName::ObjCMultiArgSelector:
1777 case DeclarationName::CXXUsingDirective:
1778 return;
1779
1780 case DeclarationName::CXXOperatorName: {
1781 SourceRange Range = From.getCXXOperatorNameRange();
1782 To.setCXXOperatorNameRange(Importer.Import(Range));
1783 return;
1784 }
1785 case DeclarationName::CXXLiteralOperatorName: {
1786 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1787 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1788 return;
1789 }
1790 case DeclarationName::CXXConstructorName:
1791 case DeclarationName::CXXDestructorName:
1792 case DeclarationName::CXXConversionFunctionName: {
1793 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1794 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1795 return;
1796 }
1797 assert(0 && "Unknown name kind.");
1798 }
1799}
1800
Douglas Gregord8868a62011-01-18 03:11:38 +00001801void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
1802 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan8cc4fd72011-07-22 23:46:03 +00001803 Importer.ImportContext(FromDC);
Douglas Gregord8868a62011-01-18 03:11:38 +00001804 return;
1805 }
1806
Douglas Gregor083a8212010-02-21 18:24:45 +00001807 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1808 FromEnd = FromDC->decls_end();
1809 From != FromEnd;
1810 ++From)
1811 Importer.Import(*From);
1812}
1813
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001814bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
1815 bool ForceImport) {
1816 if (To->getDefinition() || To->isBeingDefined())
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001817 return false;
1818
1819 To->startDefinition();
1820
1821 // Add base classes.
1822 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1823 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
1824
Chris Lattner5f9e2722011-07-23 10:55:15 +00001825 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001826 for (CXXRecordDecl::base_class_iterator
1827 Base1 = FromCXX->bases_begin(),
1828 FromBaseEnd = FromCXX->bases_end();
1829 Base1 != FromBaseEnd;
1830 ++Base1) {
1831 QualType T = Importer.Import(Base1->getType());
1832 if (T.isNull())
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001833 return true;
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001834
1835 SourceLocation EllipsisLoc;
1836 if (Base1->isPackExpansion())
1837 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001838
1839 // Ensure that we have a definition for the base.
1840 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1841
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001842 Bases.push_back(
1843 new (Importer.getToContext())
1844 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1845 Base1->isVirtual(),
1846 Base1->isBaseOfClass(),
1847 Base1->getAccessSpecifierAsWritten(),
Douglas Gregorf90b27a2011-01-03 22:36:02 +00001848 Importer.Import(Base1->getTypeSourceInfo()),
1849 EllipsisLoc));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001850 }
1851 if (!Bases.empty())
1852 ToCXX->setBases(Bases.data(), Bases.size());
1853 }
1854
Sean Callanan673e7752011-07-19 22:38:25 +00001855 ImportDeclContext(From, ForceImport);
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001856 To->completeDefinition();
Douglas Gregorc04d9d12010-12-02 19:33:37 +00001857 return false;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001858}
1859
Douglas Gregor1cf038c2011-07-29 23:31:30 +00001860bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
1861 bool ForceImport) {
1862 if (To->getDefinition() || To->isBeingDefined())
1863 return false;
1864
1865 To->startDefinition();
1866
1867 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
1868 if (T.isNull())
1869 return true;
1870
1871 QualType ToPromotionType = Importer.Import(From->getPromotionType());
1872 if (ToPromotionType.isNull())
1873 return true;
1874
1875 ImportDeclContext(From, ForceImport);
1876
1877 // FIXME: we might need to merge the number of positive or negative bits
1878 // if the enumerator lists don't match.
1879 To->completeDefinition(T, ToPromotionType,
1880 From->getNumPositiveBits(),
1881 From->getNumNegativeBits());
1882 return false;
1883}
1884
Douglas Gregor040afae2010-11-30 19:14:50 +00001885TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
1886 TemplateParameterList *Params) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001887 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregor040afae2010-11-30 19:14:50 +00001888 ToParams.reserve(Params->size());
1889 for (TemplateParameterList::iterator P = Params->begin(),
1890 PEnd = Params->end();
1891 P != PEnd; ++P) {
1892 Decl *To = Importer.Import(*P);
1893 if (!To)
1894 return 0;
1895
1896 ToParams.push_back(cast<NamedDecl>(To));
1897 }
1898
1899 return TemplateParameterList::Create(Importer.getToContext(),
1900 Importer.Import(Params->getTemplateLoc()),
1901 Importer.Import(Params->getLAngleLoc()),
1902 ToParams.data(), ToParams.size(),
1903 Importer.Import(Params->getRAngleLoc()));
1904}
1905
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001906TemplateArgument
1907ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
1908 switch (From.getKind()) {
1909 case TemplateArgument::Null:
1910 return TemplateArgument();
1911
1912 case TemplateArgument::Type: {
1913 QualType ToType = Importer.Import(From.getAsType());
1914 if (ToType.isNull())
1915 return TemplateArgument();
1916 return TemplateArgument(ToType);
1917 }
1918
1919 case TemplateArgument::Integral: {
1920 QualType ToType = Importer.Import(From.getIntegralType());
1921 if (ToType.isNull())
1922 return TemplateArgument();
1923 return TemplateArgument(*From.getAsIntegral(), ToType);
1924 }
1925
1926 case TemplateArgument::Declaration:
1927 if (Decl *To = Importer.Import(From.getAsDecl()))
1928 return TemplateArgument(To);
1929 return TemplateArgument();
1930
1931 case TemplateArgument::Template: {
1932 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
1933 if (ToTemplate.isNull())
1934 return TemplateArgument();
1935
1936 return TemplateArgument(ToTemplate);
1937 }
Douglas Gregora7fc9012011-01-05 18:58:31 +00001938
1939 case TemplateArgument::TemplateExpansion: {
1940 TemplateName ToTemplate
1941 = Importer.Import(From.getAsTemplateOrTemplatePattern());
1942 if (ToTemplate.isNull())
1943 return TemplateArgument();
1944
Douglas Gregor2be29f42011-01-14 23:41:42 +00001945 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregora7fc9012011-01-05 18:58:31 +00001946 }
1947
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001948 case TemplateArgument::Expression:
1949 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
1950 return TemplateArgument(ToExpr);
1951 return TemplateArgument();
1952
1953 case TemplateArgument::Pack: {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001954 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001955 ToPack.reserve(From.pack_size());
1956 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
1957 return TemplateArgument();
1958
1959 TemplateArgument *ToArgs
1960 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
1961 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
1962 return TemplateArgument(ToArgs, ToPack.size());
1963 }
1964 }
1965
1966 llvm_unreachable("Invalid template argument kind");
1967 return TemplateArgument();
1968}
1969
1970bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
1971 unsigned NumFromArgs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001972 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregord5dc83a2010-12-01 01:36:18 +00001973 for (unsigned I = 0; I != NumFromArgs; ++I) {
1974 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
1975 if (To.isNull() && !FromArgs[I].isNull())
1976 return true;
1977
1978 ToArgs.push_back(To);
1979 }
1980
1981 return false;
1982}
1983
Douglas Gregor96a01b42010-02-11 00:48:18 +00001984bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001985 RecordDecl *ToRecord) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001986 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001987 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001988 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001989 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor96a01b42010-02-11 00:48:18 +00001990}
1991
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001992bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001993 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor73dc30b2010-02-15 22:01:00 +00001994 Importer.getToContext(),
Douglas Gregorea35d112010-02-15 23:54:17 +00001995 Importer.getNonEquivalentDecls());
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00001996 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00001997}
1998
Douglas Gregor040afae2010-11-30 19:14:50 +00001999bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
2000 ClassTemplateDecl *To) {
2001 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2002 Importer.getToContext(),
2003 Importer.getNonEquivalentDecls());
2004 return Ctx.IsStructurallyEquivalent(From, To);
2005}
2006
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002007Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor88523732010-02-10 00:15:17 +00002008 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregor89cc9d62010-02-09 22:48:33 +00002009 << D->getDeclKindName();
2010 return 0;
2011}
2012
Douglas Gregor788c62d2010-02-21 18:26:36 +00002013Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2014 // Import the major distinguishing characteristics of this namespace.
2015 DeclContext *DC, *LexicalDC;
2016 DeclarationName Name;
2017 SourceLocation Loc;
2018 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2019 return 0;
2020
2021 NamespaceDecl *MergeWithNamespace = 0;
2022 if (!Name) {
2023 // This is an anonymous namespace. Adopt an existing anonymous
2024 // namespace if we can.
2025 // FIXME: Not testable.
2026 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2027 MergeWithNamespace = TU->getAnonymousNamespace();
2028 else
2029 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2030 } else {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002031 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor788c62d2010-02-21 18:26:36 +00002032 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2033 Lookup.first != Lookup.second;
2034 ++Lookup.first) {
John McCall0d6b1642010-04-23 18:46:30 +00002035 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregor788c62d2010-02-21 18:26:36 +00002036 continue;
2037
2038 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(*Lookup.first)) {
2039 MergeWithNamespace = FoundNS;
2040 ConflictingDecls.clear();
2041 break;
2042 }
2043
2044 ConflictingDecls.push_back(*Lookup.first);
2045 }
2046
2047 if (!ConflictingDecls.empty()) {
John McCall0d6b1642010-04-23 18:46:30 +00002048 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregor788c62d2010-02-21 18:26:36 +00002049 ConflictingDecls.data(),
2050 ConflictingDecls.size());
2051 }
2052 }
2053
2054 // Create the "to" namespace, if needed.
2055 NamespaceDecl *ToNamespace = MergeWithNamespace;
2056 if (!ToNamespace) {
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002057 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
2058 Importer.Import(D->getLocStart()),
2059 Loc, Name.getAsIdentifierInfo());
Douglas Gregor788c62d2010-02-21 18:26:36 +00002060 ToNamespace->setLexicalDeclContext(LexicalDC);
2061 LexicalDC->addDecl(ToNamespace);
2062
2063 // If this is an anonymous namespace, register it as the anonymous
2064 // namespace within its context.
2065 if (!Name) {
2066 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2067 TU->setAnonymousNamespace(ToNamespace);
2068 else
2069 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2070 }
2071 }
2072 Importer.Imported(D, ToNamespace);
2073
2074 ImportDeclContext(D);
2075
2076 return ToNamespace;
2077}
2078
Richard Smith162e1c12011-04-15 14:24:37 +00002079Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002080 // Import the major distinguishing characteristics of this typedef.
2081 DeclContext *DC, *LexicalDC;
2082 DeclarationName Name;
2083 SourceLocation Loc;
2084 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2085 return 0;
2086
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002087 // If this typedef is not in block scope, determine whether we've
2088 // seen a typedef with the same name (that we can merge with) or any
2089 // other entity by that name (which name lookup could conflict with).
2090 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002091 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002092 unsigned IDNS = Decl::IDNS_Ordinary;
2093 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2094 Lookup.first != Lookup.second;
2095 ++Lookup.first) {
2096 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2097 continue;
Richard Smith162e1c12011-04-15 14:24:37 +00002098 if (TypedefNameDecl *FoundTypedef =
2099 dyn_cast<TypedefNameDecl>(*Lookup.first)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002100 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2101 FoundTypedef->getUnderlyingType()))
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002102 return Importer.Imported(D, FoundTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002103 }
2104
2105 ConflictingDecls.push_back(*Lookup.first);
2106 }
2107
2108 if (!ConflictingDecls.empty()) {
2109 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2110 ConflictingDecls.data(),
2111 ConflictingDecls.size());
2112 if (!Name)
2113 return 0;
2114 }
2115 }
2116
Douglas Gregorea35d112010-02-15 23:54:17 +00002117 // Import the underlying type of this typedef;
2118 QualType T = Importer.Import(D->getUnderlyingType());
2119 if (T.isNull())
2120 return 0;
2121
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002122 // Create the new typedef node.
2123 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnara344577e2011-03-06 15:48:19 +00002124 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smith162e1c12011-04-15 14:24:37 +00002125 TypedefNameDecl *ToTypedef;
2126 if (IsAlias)
2127 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2128 StartL, Loc,
2129 Name.getAsIdentifierInfo(),
2130 TInfo);
2131 else
2132 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2133 StartL, Loc,
2134 Name.getAsIdentifierInfo(),
2135 TInfo);
Douglas Gregor325bf172010-02-22 17:42:47 +00002136 ToTypedef->setAccess(D->getAccess());
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002137 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002138 Importer.Imported(D, ToTypedef);
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002139 LexicalDC->addDecl(ToTypedef);
Douglas Gregorea35d112010-02-15 23:54:17 +00002140
Douglas Gregor9e5d9962010-02-10 21:10:29 +00002141 return ToTypedef;
2142}
2143
Richard Smith162e1c12011-04-15 14:24:37 +00002144Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2145 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2146}
2147
2148Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2149 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2150}
2151
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002152Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2153 // Import the major distinguishing characteristics of this enum.
2154 DeclContext *DC, *LexicalDC;
2155 DeclarationName Name;
2156 SourceLocation Loc;
2157 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2158 return 0;
2159
2160 // Figure out what enum name we're looking for.
2161 unsigned IDNS = Decl::IDNS_Tag;
2162 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002163 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2164 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002165 IDNS = Decl::IDNS_Ordinary;
2166 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2167 IDNS |= Decl::IDNS_Ordinary;
2168
2169 // We may already have an enum of the same name; try to find and match it.
2170 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002171 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002172 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2173 Lookup.first != Lookup.second;
2174 ++Lookup.first) {
2175 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2176 continue;
2177
2178 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002179 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002180 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2181 Found = Tag->getDecl();
2182 }
2183
2184 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002185 if (IsStructuralMatch(D, FoundEnum))
2186 return Importer.Imported(D, FoundEnum);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002187 }
2188
2189 ConflictingDecls.push_back(*Lookup.first);
2190 }
2191
2192 if (!ConflictingDecls.empty()) {
2193 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2194 ConflictingDecls.data(),
2195 ConflictingDecls.size());
2196 }
2197 }
2198
2199 // Create the enum declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002200 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2201 Importer.Import(D->getLocStart()),
2202 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002203 D->isScoped(), D->isScopedUsingClassTag(),
2204 D->isFixed());
John McCallb6217662010-03-15 10:12:16 +00002205 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002206 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002207 D2->setAccess(D->getAccess());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002208 D2->setLexicalDeclContext(LexicalDC);
2209 Importer.Imported(D, D2);
2210 LexicalDC->addDecl(D2);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002211
2212 // Import the integer type.
2213 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2214 if (ToIntegerType.isNull())
2215 return 0;
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002216 D2->setIntegerType(ToIntegerType);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002217
2218 // Import the definition
Douglas Gregor1cf038c2011-07-29 23:31:30 +00002219 if (D->isDefinition() && ImportDefinition(D, D2))
2220 return 0;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002221
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002222 return D2;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002223}
2224
Douglas Gregor96a01b42010-02-11 00:48:18 +00002225Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2226 // If this record has a definition in the translation unit we're coming from,
2227 // but this particular declaration is not that definition, import the
2228 // definition and map to that.
Douglas Gregor952b0172010-02-11 01:04:33 +00002229 TagDecl *Definition = D->getDefinition();
Douglas Gregor96a01b42010-02-11 00:48:18 +00002230 if (Definition && Definition != D) {
2231 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002232 if (!ImportedDef)
2233 return 0;
2234
2235 return Importer.Imported(D, ImportedDef);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002236 }
2237
2238 // Import the major distinguishing characteristics of this record.
2239 DeclContext *DC, *LexicalDC;
2240 DeclarationName Name;
2241 SourceLocation Loc;
2242 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2243 return 0;
2244
2245 // Figure out what structure name we're looking for.
2246 unsigned IDNS = Decl::IDNS_Tag;
2247 DeclarationName SearchName = Name;
Richard Smith162e1c12011-04-15 14:24:37 +00002248 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2249 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002250 IDNS = Decl::IDNS_Ordinary;
2251 } else if (Importer.getToContext().getLangOptions().CPlusPlus)
2252 IDNS |= Decl::IDNS_Ordinary;
2253
2254 // We may already have a record of the same name; try to find and match it.
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002255 RecordDecl *AdoptDecl = 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002256 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002257 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002258 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2259 Lookup.first != Lookup.second;
2260 ++Lookup.first) {
2261 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2262 continue;
2263
2264 Decl *Found = *Lookup.first;
Richard Smith162e1c12011-04-15 14:24:37 +00002265 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor96a01b42010-02-11 00:48:18 +00002266 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2267 Found = Tag->getDecl();
2268 }
2269
2270 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002271 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
2272 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
2273 // The record types structurally match, or the "from" translation
2274 // unit only had a forward declaration anyway; call it the same
2275 // function.
2276 // FIXME: For C++, we should also merge methods here.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002277 return Importer.Imported(D, FoundDef);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002278 }
2279 } else {
2280 // We have a forward declaration of this type, so adopt that forward
2281 // declaration rather than building a new one.
2282 AdoptDecl = FoundRecord;
2283 continue;
2284 }
Douglas Gregor96a01b42010-02-11 00:48:18 +00002285 }
2286
2287 ConflictingDecls.push_back(*Lookup.first);
2288 }
2289
2290 if (!ConflictingDecls.empty()) {
2291 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2292 ConflictingDecls.data(),
2293 ConflictingDecls.size());
2294 }
2295 }
2296
2297 // Create the record declaration.
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002298 RecordDecl *D2 = AdoptDecl;
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002299 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002300 if (!D2) {
John McCall5250f272010-06-03 19:28:45 +00002301 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002302 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002303 D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002304 DC, StartLoc, Loc,
2305 Name.getAsIdentifierInfo());
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002306 D2 = D2CXX;
Douglas Gregor325bf172010-02-22 17:42:47 +00002307 D2->setAccess(D->getAccess());
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002308 } else {
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002309 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002310 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002311 }
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002312
2313 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002314 D2->setLexicalDeclContext(LexicalDC);
2315 LexicalDC->addDecl(D2);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002316 }
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002317
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002318 Importer.Imported(D, D2);
Douglas Gregore72b5dc2010-02-12 00:09:27 +00002319
Douglas Gregord5dc83a2010-12-01 01:36:18 +00002320 if (D->isDefinition() && ImportDefinition(D, D2))
2321 return 0;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002322
Douglas Gregor73dc30b2010-02-15 22:01:00 +00002323 return D2;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002324}
2325
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002326Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2327 // Import the major distinguishing characteristics of this enumerator.
2328 DeclContext *DC, *LexicalDC;
2329 DeclarationName Name;
2330 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002331 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002332 return 0;
Douglas Gregorea35d112010-02-15 23:54:17 +00002333
2334 QualType T = Importer.Import(D->getType());
2335 if (T.isNull())
2336 return 0;
2337
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002338 // Determine whether there are any other declarations with the same name and
2339 // in the same context.
2340 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002341 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002342 unsigned IDNS = Decl::IDNS_Ordinary;
2343 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2344 Lookup.first != Lookup.second;
2345 ++Lookup.first) {
2346 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2347 continue;
2348
2349 ConflictingDecls.push_back(*Lookup.first);
2350 }
2351
2352 if (!ConflictingDecls.empty()) {
2353 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2354 ConflictingDecls.data(),
2355 ConflictingDecls.size());
2356 if (!Name)
2357 return 0;
2358 }
2359 }
2360
2361 Expr *Init = Importer.Import(D->getInitExpr());
2362 if (D->getInitExpr() && !Init)
2363 return 0;
2364
2365 EnumConstantDecl *ToEnumerator
2366 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2367 Name.getAsIdentifierInfo(), T,
2368 Init, D->getInitVal());
Douglas Gregor325bf172010-02-22 17:42:47 +00002369 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002370 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002371 Importer.Imported(D, ToEnumerator);
Douglas Gregor36ead2e2010-02-12 22:17:39 +00002372 LexicalDC->addDecl(ToEnumerator);
2373 return ToEnumerator;
2374}
Douglas Gregor96a01b42010-02-11 00:48:18 +00002375
Douglas Gregora404ea62010-02-10 19:54:31 +00002376Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2377 // Import the major distinguishing characteristics of this function.
2378 DeclContext *DC, *LexicalDC;
2379 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002380 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002381 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002382 return 0;
Abramo Bagnara25777432010-08-11 22:01:17 +00002383
Douglas Gregora404ea62010-02-10 19:54:31 +00002384 // Try to find a function in our own ("to") context with the same name, same
2385 // type, and in the same context as the function we're importing.
2386 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002387 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregora404ea62010-02-10 19:54:31 +00002388 unsigned IDNS = Decl::IDNS_Ordinary;
2389 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2390 Lookup.first != Lookup.second;
2391 ++Lookup.first) {
2392 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2393 continue;
Douglas Gregor089459a2010-02-08 21:09:39 +00002394
Douglas Gregora404ea62010-02-10 19:54:31 +00002395 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(*Lookup.first)) {
2396 if (isExternalLinkage(FoundFunction->getLinkage()) &&
2397 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002398 if (Importer.IsStructurallyEquivalent(D->getType(),
2399 FoundFunction->getType())) {
Douglas Gregora404ea62010-02-10 19:54:31 +00002400 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002401 return Importer.Imported(D, FoundFunction);
Douglas Gregora404ea62010-02-10 19:54:31 +00002402 }
2403
2404 // FIXME: Check for overloading more carefully, e.g., by boosting
2405 // Sema::IsOverload out to the AST library.
2406
2407 // Function overloading is okay in C++.
2408 if (Importer.getToContext().getLangOptions().CPlusPlus)
2409 continue;
2410
2411 // Complain about inconsistent function types.
2412 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002413 << Name << D->getType() << FoundFunction->getType();
Douglas Gregora404ea62010-02-10 19:54:31 +00002414 Importer.ToDiag(FoundFunction->getLocation(),
2415 diag::note_odr_value_here)
2416 << FoundFunction->getType();
2417 }
2418 }
2419
2420 ConflictingDecls.push_back(*Lookup.first);
2421 }
2422
2423 if (!ConflictingDecls.empty()) {
2424 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2425 ConflictingDecls.data(),
2426 ConflictingDecls.size());
2427 if (!Name)
2428 return 0;
2429 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00002430 }
Douglas Gregorea35d112010-02-15 23:54:17 +00002431
Abramo Bagnara25777432010-08-11 22:01:17 +00002432 DeclarationNameInfo NameInfo(Name, Loc);
2433 // Import additional name location/type info.
2434 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2435
Douglas Gregorea35d112010-02-15 23:54:17 +00002436 // Import the type.
2437 QualType T = Importer.Import(D->getType());
2438 if (T.isNull())
2439 return 0;
Douglas Gregora404ea62010-02-10 19:54:31 +00002440
2441 // Import the function parameters.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002442 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregora404ea62010-02-10 19:54:31 +00002443 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2444 P != PEnd; ++P) {
2445 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2446 if (!ToP)
2447 return 0;
2448
2449 Parameters.push_back(ToP);
2450 }
2451
2452 // Create the imported function.
2453 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregorc144f352010-02-21 18:29:16 +00002454 FunctionDecl *ToFunction = 0;
2455 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2456 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2457 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002458 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002459 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002460 FromConstructor->isExplicit(),
2461 D->isInlineSpecified(),
Sean Hunt5f802e52011-05-06 00:11:07 +00002462 D->isImplicit());
Douglas Gregorc144f352010-02-21 18:29:16 +00002463 } else if (isa<CXXDestructorDecl>(D)) {
2464 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2465 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002466 D->getInnerLocStart(),
Craig Silversteinb41d8992010-10-21 00:44:50 +00002467 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002468 D->isInlineSpecified(),
2469 D->isImplicit());
2470 } else if (CXXConversionDecl *FromConversion
2471 = dyn_cast<CXXConversionDecl>(D)) {
2472 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2473 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002474 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002475 NameInfo, T, TInfo,
Douglas Gregorc144f352010-02-21 18:29:16 +00002476 D->isInlineSpecified(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002477 FromConversion->isExplicit(),
2478 Importer.Import(D->getLocEnd()));
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002479 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2480 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2481 cast<CXXRecordDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002482 D->getInnerLocStart(),
Douglas Gregor0629cbe2010-11-29 16:04:58 +00002483 NameInfo, T, TInfo,
2484 Method->isStatic(),
2485 Method->getStorageClassAsWritten(),
Douglas Gregorf5251602011-03-08 17:10:18 +00002486 Method->isInlineSpecified(),
2487 Importer.Import(D->getLocEnd()));
Douglas Gregorc144f352010-02-21 18:29:16 +00002488 } else {
Abramo Bagnara25777432010-08-11 22:01:17 +00002489 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002490 D->getInnerLocStart(),
Abramo Bagnara25777432010-08-11 22:01:17 +00002491 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002492 D->getStorageClassAsWritten(),
Douglas Gregorc144f352010-02-21 18:29:16 +00002493 D->isInlineSpecified(),
2494 D->hasWrittenPrototype());
2495 }
John McCallb6217662010-03-15 10:12:16 +00002496
2497 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002498 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002499 ToFunction->setAccess(D->getAccess());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002500 ToFunction->setLexicalDeclContext(LexicalDC);
John McCallf2eca2c2011-01-27 02:37:01 +00002501 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2502 ToFunction->setTrivial(D->isTrivial());
2503 ToFunction->setPure(D->isPure());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002504 Importer.Imported(D, ToFunction);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002505
Douglas Gregora404ea62010-02-10 19:54:31 +00002506 // Set the parameters.
2507 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002508 Parameters[I]->setOwningFunction(ToFunction);
2509 ToFunction->addDecl(Parameters[I]);
Douglas Gregora404ea62010-02-10 19:54:31 +00002510 }
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002511 ToFunction->setParams(Parameters.data(), Parameters.size());
Douglas Gregora404ea62010-02-10 19:54:31 +00002512
2513 // FIXME: Other bits to merge?
Douglas Gregor81134ad2010-10-01 23:55:07 +00002514
2515 // Add this function to the lexical context.
2516 LexicalDC->addDecl(ToFunction);
2517
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002518 return ToFunction;
Douglas Gregora404ea62010-02-10 19:54:31 +00002519}
2520
Douglas Gregorc144f352010-02-21 18:29:16 +00002521Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2522 return VisitFunctionDecl(D);
2523}
2524
2525Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2526 return VisitCXXMethodDecl(D);
2527}
2528
2529Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2530 return VisitCXXMethodDecl(D);
2531}
2532
2533Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2534 return VisitCXXMethodDecl(D);
2535}
2536
Douglas Gregor96a01b42010-02-11 00:48:18 +00002537Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2538 // Import the major distinguishing characteristics of a variable.
2539 DeclContext *DC, *LexicalDC;
2540 DeclarationName Name;
Douglas Gregor96a01b42010-02-11 00:48:18 +00002541 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002542 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2543 return 0;
2544
2545 // Import the type.
2546 QualType T = Importer.Import(D->getType());
2547 if (T.isNull())
Douglas Gregor96a01b42010-02-11 00:48:18 +00002548 return 0;
2549
2550 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2551 Expr *BitWidth = Importer.Import(D->getBitWidth());
2552 if (!BitWidth && D->getBitWidth())
2553 return 0;
2554
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002555 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2556 Importer.Import(D->getInnerLocStart()),
Douglas Gregor96a01b42010-02-11 00:48:18 +00002557 Loc, Name.getAsIdentifierInfo(),
Richard Smith7a614d82011-06-11 17:19:42 +00002558 T, TInfo, BitWidth, D->isMutable(),
2559 D->hasInClassInitializer());
Douglas Gregor325bf172010-02-22 17:42:47 +00002560 ToField->setAccess(D->getAccess());
Douglas Gregor96a01b42010-02-11 00:48:18 +00002561 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith7a614d82011-06-11 17:19:42 +00002562 if (ToField->hasInClassInitializer())
2563 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002564 Importer.Imported(D, ToField);
Douglas Gregor96a01b42010-02-11 00:48:18 +00002565 LexicalDC->addDecl(ToField);
2566 return ToField;
2567}
2568
Francois Pichet87c2e122010-11-21 06:08:52 +00002569Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2570 // Import the major distinguishing characteristics of a variable.
2571 DeclContext *DC, *LexicalDC;
2572 DeclarationName Name;
2573 SourceLocation Loc;
2574 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2575 return 0;
2576
2577 // Import the type.
2578 QualType T = Importer.Import(D->getType());
2579 if (T.isNull())
2580 return 0;
2581
2582 NamedDecl **NamedChain =
2583 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2584
2585 unsigned i = 0;
2586 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2587 PE = D->chain_end(); PI != PE; ++PI) {
2588 Decl* D = Importer.Import(*PI);
2589 if (!D)
2590 return 0;
2591 NamedChain[i++] = cast<NamedDecl>(D);
2592 }
2593
2594 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2595 Importer.getToContext(), DC,
2596 Loc, Name.getAsIdentifierInfo(), T,
2597 NamedChain, D->getChainingSize());
2598 ToIndirectField->setAccess(D->getAccess());
2599 ToIndirectField->setLexicalDeclContext(LexicalDC);
2600 Importer.Imported(D, ToIndirectField);
2601 LexicalDC->addDecl(ToIndirectField);
2602 return ToIndirectField;
2603}
2604
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002605Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2606 // Import the major distinguishing characteristics of an ivar.
2607 DeclContext *DC, *LexicalDC;
2608 DeclarationName Name;
2609 SourceLocation Loc;
2610 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2611 return 0;
2612
2613 // Determine whether we've already imported this ivar
2614 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2615 Lookup.first != Lookup.second;
2616 ++Lookup.first) {
2617 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(*Lookup.first)) {
2618 if (Importer.IsStructurallyEquivalent(D->getType(),
2619 FoundIvar->getType())) {
2620 Importer.Imported(D, FoundIvar);
2621 return FoundIvar;
2622 }
2623
2624 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2625 << Name << D->getType() << FoundIvar->getType();
2626 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2627 << FoundIvar->getType();
2628 return 0;
2629 }
2630 }
2631
2632 // Import the type.
2633 QualType T = Importer.Import(D->getType());
2634 if (T.isNull())
2635 return 0;
2636
2637 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2638 Expr *BitWidth = Importer.Import(D->getBitWidth());
2639 if (!BitWidth && D->getBitWidth())
2640 return 0;
2641
Daniel Dunbara0654922010-04-02 20:10:03 +00002642 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2643 cast<ObjCContainerDecl>(DC),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002644 Importer.Import(D->getInnerLocStart()),
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002645 Loc, Name.getAsIdentifierInfo(),
2646 T, TInfo, D->getAccessControl(),
Fariborz Jahanianac0021b2010-07-17 18:35:47 +00002647 BitWidth, D->getSynthesize());
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00002648 ToIvar->setLexicalDeclContext(LexicalDC);
2649 Importer.Imported(D, ToIvar);
2650 LexicalDC->addDecl(ToIvar);
2651 return ToIvar;
2652
2653}
2654
Douglas Gregora404ea62010-02-10 19:54:31 +00002655Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2656 // Import the major distinguishing characteristics of a variable.
2657 DeclContext *DC, *LexicalDC;
2658 DeclarationName Name;
Douglas Gregora404ea62010-02-10 19:54:31 +00002659 SourceLocation Loc;
Douglas Gregorea35d112010-02-15 23:54:17 +00002660 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor089459a2010-02-08 21:09:39 +00002661 return 0;
2662
Douglas Gregor089459a2010-02-08 21:09:39 +00002663 // Try to find a variable in our own ("to") context with the same name and
2664 // in the same context as the variable we're importing.
Douglas Gregor9bed8792010-02-09 19:21:46 +00002665 if (D->isFileVarDecl()) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002666 VarDecl *MergeWithVar = 0;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor089459a2010-02-08 21:09:39 +00002668 unsigned IDNS = Decl::IDNS_Ordinary;
Douglas Gregor9bed8792010-02-09 19:21:46 +00002669 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
Douglas Gregor089459a2010-02-08 21:09:39 +00002670 Lookup.first != Lookup.second;
2671 ++Lookup.first) {
2672 if (!(*Lookup.first)->isInIdentifierNamespace(IDNS))
2673 continue;
2674
2675 if (VarDecl *FoundVar = dyn_cast<VarDecl>(*Lookup.first)) {
2676 // We have found a variable that we may need to merge with. Check it.
2677 if (isExternalLinkage(FoundVar->getLinkage()) &&
2678 isExternalLinkage(D->getLinkage())) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002679 if (Importer.IsStructurallyEquivalent(D->getType(),
2680 FoundVar->getType())) {
Douglas Gregor089459a2010-02-08 21:09:39 +00002681 MergeWithVar = FoundVar;
2682 break;
2683 }
2684
Douglas Gregord0145422010-02-12 17:23:39 +00002685 const ArrayType *FoundArray
2686 = Importer.getToContext().getAsArrayType(FoundVar->getType());
2687 const ArrayType *TArray
Douglas Gregorea35d112010-02-15 23:54:17 +00002688 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregord0145422010-02-12 17:23:39 +00002689 if (FoundArray && TArray) {
2690 if (isa<IncompleteArrayType>(FoundArray) &&
2691 isa<ConstantArrayType>(TArray)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00002692 // Import the type.
2693 QualType T = Importer.Import(D->getType());
2694 if (T.isNull())
2695 return 0;
2696
Douglas Gregord0145422010-02-12 17:23:39 +00002697 FoundVar->setType(T);
2698 MergeWithVar = FoundVar;
2699 break;
2700 } else if (isa<IncompleteArrayType>(TArray) &&
2701 isa<ConstantArrayType>(FoundArray)) {
2702 MergeWithVar = FoundVar;
2703 break;
Douglas Gregor0f962a82010-02-10 17:16:49 +00002704 }
2705 }
2706
Douglas Gregor089459a2010-02-08 21:09:39 +00002707 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorea35d112010-02-15 23:54:17 +00002708 << Name << D->getType() << FoundVar->getType();
Douglas Gregor089459a2010-02-08 21:09:39 +00002709 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
2710 << FoundVar->getType();
2711 }
2712 }
2713
2714 ConflictingDecls.push_back(*Lookup.first);
2715 }
2716
2717 if (MergeWithVar) {
2718 // An equivalent variable with external linkage has been found. Link
2719 // the two declarations, then merge them.
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002720 Importer.Imported(D, MergeWithVar);
Douglas Gregor089459a2010-02-08 21:09:39 +00002721
2722 if (VarDecl *DDef = D->getDefinition()) {
2723 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
2724 Importer.ToDiag(ExistingDef->getLocation(),
2725 diag::err_odr_variable_multiple_def)
2726 << Name;
2727 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
2728 } else {
2729 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregor838db382010-02-11 01:19:42 +00002730 MergeWithVar->setInit(Init);
Douglas Gregor089459a2010-02-08 21:09:39 +00002731 }
2732 }
2733
2734 return MergeWithVar;
2735 }
2736
2737 if (!ConflictingDecls.empty()) {
2738 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2739 ConflictingDecls.data(),
2740 ConflictingDecls.size());
2741 if (!Name)
2742 return 0;
2743 }
2744 }
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002745
Douglas Gregorea35d112010-02-15 23:54:17 +00002746 // Import the type.
2747 QualType T = Importer.Import(D->getType());
2748 if (T.isNull())
2749 return 0;
2750
Douglas Gregor089459a2010-02-08 21:09:39 +00002751 // Create the imported variable.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002752 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002753 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
2754 Importer.Import(D->getInnerLocStart()),
2755 Loc, Name.getAsIdentifierInfo(),
2756 T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002757 D->getStorageClass(),
2758 D->getStorageClassAsWritten());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002759 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor325bf172010-02-22 17:42:47 +00002760 ToVar->setAccess(D->getAccess());
Douglas Gregor9bed8792010-02-09 19:21:46 +00002761 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002762 Importer.Imported(D, ToVar);
Douglas Gregor9bed8792010-02-09 19:21:46 +00002763 LexicalDC->addDecl(ToVar);
2764
Douglas Gregor089459a2010-02-08 21:09:39 +00002765 // Merge the initializer.
2766 // FIXME: Can we really import any initializer? Alternatively, we could force
2767 // ourselves to import every declaration of a variable and then only use
2768 // getInit() here.
Douglas Gregor838db382010-02-11 01:19:42 +00002769 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor089459a2010-02-08 21:09:39 +00002770
2771 // FIXME: Other bits to merge?
2772
2773 return ToVar;
2774}
2775
Douglas Gregor2cd00932010-02-17 21:22:52 +00002776Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
2777 // Parameters are created in the translation unit's context, then moved
2778 // into the function declaration's context afterward.
2779 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2780
2781 // Import the name of this declaration.
2782 DeclarationName Name = Importer.Import(D->getDeclName());
2783 if (D->getDeclName() && !Name)
2784 return 0;
2785
2786 // Import the location of this declaration.
2787 SourceLocation Loc = Importer.Import(D->getLocation());
2788
2789 // Import the parameter's type.
2790 QualType T = Importer.Import(D->getType());
2791 if (T.isNull())
2792 return 0;
2793
2794 // Create the imported parameter.
2795 ImplicitParamDecl *ToParm
2796 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
2797 Loc, Name.getAsIdentifierInfo(),
2798 T);
2799 return Importer.Imported(D, ToParm);
2800}
2801
Douglas Gregora404ea62010-02-10 19:54:31 +00002802Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
2803 // Parameters are created in the translation unit's context, then moved
2804 // into the function declaration's context afterward.
2805 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
2806
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00002807 // Import the name of this declaration.
2808 DeclarationName Name = Importer.Import(D->getDeclName());
2809 if (D->getDeclName() && !Name)
2810 return 0;
2811
Douglas Gregora404ea62010-02-10 19:54:31 +00002812 // Import the location of this declaration.
2813 SourceLocation Loc = Importer.Import(D->getLocation());
2814
2815 // Import the parameter's type.
2816 QualType T = Importer.Import(D->getType());
2817 if (T.isNull())
2818 return 0;
2819
2820 // Create the imported parameter.
2821 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2822 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002823 Importer.Import(D->getInnerLocStart()),
Douglas Gregora404ea62010-02-10 19:54:31 +00002824 Loc, Name.getAsIdentifierInfo(),
2825 T, TInfo, D->getStorageClass(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002826 D->getStorageClassAsWritten(),
Douglas Gregora404ea62010-02-10 19:54:31 +00002827 /*FIXME: Default argument*/ 0);
John McCallbf73b352010-03-12 18:31:32 +00002828 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00002829 return Importer.Imported(D, ToParm);
Douglas Gregora404ea62010-02-10 19:54:31 +00002830}
2831
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002832Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
2833 // Import the major distinguishing characteristics of a method.
2834 DeclContext *DC, *LexicalDC;
2835 DeclarationName Name;
2836 SourceLocation Loc;
2837 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2838 return 0;
2839
2840 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
2841 Lookup.first != Lookup.second;
2842 ++Lookup.first) {
2843 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(*Lookup.first)) {
2844 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
2845 continue;
2846
2847 // Check return types.
2848 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
2849 FoundMethod->getResultType())) {
2850 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
2851 << D->isInstanceMethod() << Name
2852 << D->getResultType() << FoundMethod->getResultType();
2853 Importer.ToDiag(FoundMethod->getLocation(),
2854 diag::note_odr_objc_method_here)
2855 << D->isInstanceMethod() << Name;
2856 return 0;
2857 }
2858
2859 // Check the number of parameters.
2860 if (D->param_size() != FoundMethod->param_size()) {
2861 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
2862 << D->isInstanceMethod() << Name
2863 << D->param_size() << FoundMethod->param_size();
2864 Importer.ToDiag(FoundMethod->getLocation(),
2865 diag::note_odr_objc_method_here)
2866 << D->isInstanceMethod() << Name;
2867 return 0;
2868 }
2869
2870 // Check parameter types.
2871 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
2872 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
2873 P != PEnd; ++P, ++FoundP) {
2874 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
2875 (*FoundP)->getType())) {
2876 Importer.FromDiag((*P)->getLocation(),
2877 diag::err_odr_objc_method_param_type_inconsistent)
2878 << D->isInstanceMethod() << Name
2879 << (*P)->getType() << (*FoundP)->getType();
2880 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
2881 << (*FoundP)->getType();
2882 return 0;
2883 }
2884 }
2885
2886 // Check variadic/non-variadic.
2887 // Check the number of parameters.
2888 if (D->isVariadic() != FoundMethod->isVariadic()) {
2889 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
2890 << D->isInstanceMethod() << Name;
2891 Importer.ToDiag(FoundMethod->getLocation(),
2892 diag::note_odr_objc_method_here)
2893 << D->isInstanceMethod() << Name;
2894 return 0;
2895 }
2896
2897 // FIXME: Any other bits we need to merge?
2898 return Importer.Imported(D, FoundMethod);
2899 }
2900 }
2901
2902 // Import the result type.
2903 QualType ResultTy = Importer.Import(D->getResultType());
2904 if (ResultTy.isNull())
2905 return 0;
2906
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002907 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
2908
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002909 ObjCMethodDecl *ToMethod
2910 = ObjCMethodDecl::Create(Importer.getToContext(),
2911 Loc,
2912 Importer.Import(D->getLocEnd()),
2913 Name.getObjCSelector(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002914 ResultTy, ResultTInfo, DC,
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002915 D->isInstanceMethod(),
2916 D->isVariadic(),
2917 D->isSynthesized(),
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002918 D->isDefined(),
Douglas Gregor926df6c2011-06-11 01:09:30 +00002919 D->getImplementationControl(),
2920 D->hasRelatedResultType());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002921
2922 // FIXME: When we decide to merge method definitions, we'll need to
2923 // deal with implicit parameters.
2924
2925 // Import the parameters
Chris Lattner5f9e2722011-07-23 10:55:15 +00002926 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002927 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
2928 FromPEnd = D->param_end();
2929 FromP != FromPEnd;
2930 ++FromP) {
2931 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
2932 if (!ToP)
2933 return 0;
2934
2935 ToParams.push_back(ToP);
2936 }
2937
2938 // Set the parameters.
2939 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
2940 ToParams[I]->setOwningFunction(ToMethod);
2941 ToMethod->addDecl(ToParams[I]);
2942 }
2943 ToMethod->setMethodParams(Importer.getToContext(),
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00002944 ToParams.data(), ToParams.size(),
2945 ToParams.size());
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00002946
2947 ToMethod->setLexicalDeclContext(LexicalDC);
2948 Importer.Imported(D, ToMethod);
2949 LexicalDC->addDecl(ToMethod);
2950 return ToMethod;
2951}
2952
Douglas Gregorb4677b62010-02-18 01:47:50 +00002953Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
2954 // Import the major distinguishing characteristics of a category.
2955 DeclContext *DC, *LexicalDC;
2956 DeclarationName Name;
2957 SourceLocation Loc;
2958 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2959 return 0;
2960
2961 ObjCInterfaceDecl *ToInterface
2962 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
2963 if (!ToInterface)
2964 return 0;
2965
2966 // Determine if we've already encountered this category.
2967 ObjCCategoryDecl *MergeWithCategory
2968 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
2969 ObjCCategoryDecl *ToCategory = MergeWithCategory;
2970 if (!ToCategory) {
2971 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
2972 Importer.Import(D->getAtLoc()),
2973 Loc,
2974 Importer.Import(D->getCategoryNameLoc()),
2975 Name.getAsIdentifierInfo());
2976 ToCategory->setLexicalDeclContext(LexicalDC);
2977 LexicalDC->addDecl(ToCategory);
2978 Importer.Imported(D, ToCategory);
2979
2980 // Link this category into its class's category list.
2981 ToCategory->setClassInterface(ToInterface);
2982 ToCategory->insertNextClassCategory();
2983
2984 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00002985 SmallVector<ObjCProtocolDecl *, 4> Protocols;
2986 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregorb4677b62010-02-18 01:47:50 +00002987 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
2988 = D->protocol_loc_begin();
2989 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
2990 FromProtoEnd = D->protocol_end();
2991 FromProto != FromProtoEnd;
2992 ++FromProto, ++FromProtoLoc) {
2993 ObjCProtocolDecl *ToProto
2994 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
2995 if (!ToProto)
2996 return 0;
2997 Protocols.push_back(ToProto);
2998 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
2999 }
3000
3001 // FIXME: If we're merging, make sure that the protocol list is the same.
3002 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3003 ProtocolLocs.data(), Importer.getToContext());
3004
3005 } else {
3006 Importer.Imported(D, ToCategory);
3007 }
3008
3009 // Import all of the members of this category.
Douglas Gregor083a8212010-02-21 18:24:45 +00003010 ImportDeclContext(D);
Douglas Gregorb4677b62010-02-18 01:47:50 +00003011
3012 // If we have an implementation, import it as well.
3013 if (D->getImplementation()) {
3014 ObjCCategoryImplDecl *Impl
Douglas Gregorcad2c592010-12-08 16:41:55 +00003015 = cast_or_null<ObjCCategoryImplDecl>(
3016 Importer.Import(D->getImplementation()));
Douglas Gregorb4677b62010-02-18 01:47:50 +00003017 if (!Impl)
3018 return 0;
3019
3020 ToCategory->setImplementation(Impl);
3021 }
3022
3023 return ToCategory;
3024}
3025
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003026Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregorb4677b62010-02-18 01:47:50 +00003027 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003028 DeclContext *DC, *LexicalDC;
3029 DeclarationName Name;
3030 SourceLocation Loc;
3031 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3032 return 0;
3033
3034 ObjCProtocolDecl *MergeWithProtocol = 0;
3035 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3036 Lookup.first != Lookup.second;
3037 ++Lookup.first) {
3038 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
3039 continue;
3040
3041 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(*Lookup.first)))
3042 break;
3043 }
3044
3045 ObjCProtocolDecl *ToProto = MergeWithProtocol;
3046 if (!ToProto || ToProto->isForwardDecl()) {
3047 if (!ToProto) {
3048 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3049 Name.getAsIdentifierInfo());
3050 ToProto->setForwardDecl(D->isForwardDecl());
3051 ToProto->setLexicalDeclContext(LexicalDC);
3052 LexicalDC->addDecl(ToProto);
3053 }
3054 Importer.Imported(D, ToProto);
3055
3056 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003057 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3058 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003059 ObjCProtocolDecl::protocol_loc_iterator
3060 FromProtoLoc = D->protocol_loc_begin();
3061 for (ObjCProtocolDecl::protocol_iterator FromProto = D->protocol_begin(),
3062 FromProtoEnd = D->protocol_end();
3063 FromProto != FromProtoEnd;
3064 ++FromProto, ++FromProtoLoc) {
3065 ObjCProtocolDecl *ToProto
3066 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3067 if (!ToProto)
3068 return 0;
3069 Protocols.push_back(ToProto);
3070 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3071 }
3072
3073 // FIXME: If we're merging, make sure that the protocol list is the same.
3074 ToProto->setProtocolList(Protocols.data(), Protocols.size(),
3075 ProtocolLocs.data(), Importer.getToContext());
3076 } else {
3077 Importer.Imported(D, ToProto);
3078 }
3079
Douglas Gregorb4677b62010-02-18 01:47:50 +00003080 // Import all of the members of this protocol.
Douglas Gregor083a8212010-02-21 18:24:45 +00003081 ImportDeclContext(D);
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003082
3083 return ToProto;
3084}
3085
Douglas Gregora12d2942010-02-16 01:20:57 +00003086Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
3087 // Import the major distinguishing characteristics of an @interface.
3088 DeclContext *DC, *LexicalDC;
3089 DeclarationName Name;
3090 SourceLocation Loc;
3091 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3092 return 0;
3093
3094 ObjCInterfaceDecl *MergeWithIface = 0;
3095 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3096 Lookup.first != Lookup.second;
3097 ++Lookup.first) {
3098 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3099 continue;
3100
3101 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(*Lookup.first)))
3102 break;
3103 }
3104
3105 ObjCInterfaceDecl *ToIface = MergeWithIface;
3106 if (!ToIface || ToIface->isForwardDecl()) {
3107 if (!ToIface) {
3108 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(),
3109 DC, Loc,
3110 Name.getAsIdentifierInfo(),
Douglas Gregordeacbdc2010-08-11 12:19:30 +00003111 Importer.Import(D->getClassLoc()),
Douglas Gregora12d2942010-02-16 01:20:57 +00003112 D->isForwardDecl(),
3113 D->isImplicitInterfaceDecl());
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003114 ToIface->setForwardDecl(D->isForwardDecl());
Douglas Gregora12d2942010-02-16 01:20:57 +00003115 ToIface->setLexicalDeclContext(LexicalDC);
3116 LexicalDC->addDecl(ToIface);
3117 }
3118 Importer.Imported(D, ToIface);
3119
Douglas Gregora12d2942010-02-16 01:20:57 +00003120 if (D->getSuperClass()) {
3121 ObjCInterfaceDecl *Super
3122 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getSuperClass()));
3123 if (!Super)
3124 return 0;
3125
3126 ToIface->setSuperClass(Super);
3127 ToIface->setSuperClassLoc(Importer.Import(D->getSuperClassLoc()));
3128 }
3129
3130 // Import protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00003131 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3132 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregora12d2942010-02-16 01:20:57 +00003133 ObjCInterfaceDecl::protocol_loc_iterator
3134 FromProtoLoc = D->protocol_loc_begin();
Ted Kremenek53b94412010-09-01 01:21:15 +00003135
3136 // FIXME: Should we be usng all_referenced_protocol_begin() here?
Douglas Gregora12d2942010-02-16 01:20:57 +00003137 for (ObjCInterfaceDecl::protocol_iterator FromProto = D->protocol_begin(),
3138 FromProtoEnd = D->protocol_end();
3139 FromProto != FromProtoEnd;
3140 ++FromProto, ++FromProtoLoc) {
3141 ObjCProtocolDecl *ToProto
3142 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3143 if (!ToProto)
3144 return 0;
3145 Protocols.push_back(ToProto);
3146 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3147 }
3148
3149 // FIXME: If we're merging, make sure that the protocol list is the same.
3150 ToIface->setProtocolList(Protocols.data(), Protocols.size(),
3151 ProtocolLocs.data(), Importer.getToContext());
3152
Douglas Gregora12d2942010-02-16 01:20:57 +00003153 // Import @end range
3154 ToIface->setAtEndRange(Importer.Import(D->getAtEndRange()));
3155 } else {
3156 Importer.Imported(D, ToIface);
Douglas Gregor2e55e3a2010-02-17 00:34:30 +00003157
3158 // Check for consistency of superclasses.
3159 DeclarationName FromSuperName, ToSuperName;
3160 if (D->getSuperClass())
3161 FromSuperName = Importer.Import(D->getSuperClass()->getDeclName());
3162 if (ToIface->getSuperClass())
3163 ToSuperName = ToIface->getSuperClass()->getDeclName();
3164 if (FromSuperName != ToSuperName) {
3165 Importer.ToDiag(ToIface->getLocation(),
3166 diag::err_odr_objc_superclass_inconsistent)
3167 << ToIface->getDeclName();
3168 if (ToIface->getSuperClass())
3169 Importer.ToDiag(ToIface->getSuperClassLoc(),
3170 diag::note_odr_objc_superclass)
3171 << ToIface->getSuperClass()->getDeclName();
3172 else
3173 Importer.ToDiag(ToIface->getLocation(),
3174 diag::note_odr_objc_missing_superclass);
3175 if (D->getSuperClass())
3176 Importer.FromDiag(D->getSuperClassLoc(),
3177 diag::note_odr_objc_superclass)
3178 << D->getSuperClass()->getDeclName();
3179 else
3180 Importer.FromDiag(D->getLocation(),
3181 diag::note_odr_objc_missing_superclass);
3182 return 0;
3183 }
Douglas Gregora12d2942010-02-16 01:20:57 +00003184 }
3185
Douglas Gregorb4677b62010-02-18 01:47:50 +00003186 // Import categories. When the categories themselves are imported, they'll
3187 // hook themselves into this interface.
3188 for (ObjCCategoryDecl *FromCat = D->getCategoryList(); FromCat;
3189 FromCat = FromCat->getNextClassCategory())
3190 Importer.Import(FromCat);
3191
Douglas Gregora12d2942010-02-16 01:20:57 +00003192 // Import all of the members of this class.
Douglas Gregor083a8212010-02-21 18:24:45 +00003193 ImportDeclContext(D);
Douglas Gregora12d2942010-02-16 01:20:57 +00003194
3195 // If we have an @implementation, import it as well.
3196 if (D->getImplementation()) {
Douglas Gregordd182ff2010-12-07 01:26:03 +00003197 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3198 Importer.Import(D->getImplementation()));
Douglas Gregora12d2942010-02-16 01:20:57 +00003199 if (!Impl)
3200 return 0;
3201
3202 ToIface->setImplementation(Impl);
3203 }
3204
Douglas Gregor2e2a4002010-02-17 16:12:00 +00003205 return ToIface;
Douglas Gregora12d2942010-02-16 01:20:57 +00003206}
3207
Douglas Gregor3daef292010-12-07 15:32:12 +00003208Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3209 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3210 Importer.Import(D->getCategoryDecl()));
3211 if (!Category)
3212 return 0;
3213
3214 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3215 if (!ToImpl) {
3216 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3217 if (!DC)
3218 return 0;
3219
3220 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
3221 Importer.Import(D->getLocation()),
3222 Importer.Import(D->getIdentifier()),
3223 Category->getClassInterface());
3224
3225 DeclContext *LexicalDC = DC;
3226 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3227 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3228 if (!LexicalDC)
3229 return 0;
3230
3231 ToImpl->setLexicalDeclContext(LexicalDC);
3232 }
3233
3234 LexicalDC->addDecl(ToImpl);
3235 Category->setImplementation(ToImpl);
3236 }
3237
3238 Importer.Imported(D, ToImpl);
Douglas Gregorcad2c592010-12-08 16:41:55 +00003239 ImportDeclContext(D);
Douglas Gregor3daef292010-12-07 15:32:12 +00003240 return ToImpl;
3241}
3242
Douglas Gregordd182ff2010-12-07 01:26:03 +00003243Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3244 // Find the corresponding interface.
3245 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3246 Importer.Import(D->getClassInterface()));
3247 if (!Iface)
3248 return 0;
3249
3250 // Import the superclass, if any.
3251 ObjCInterfaceDecl *Super = 0;
3252 if (D->getSuperClass()) {
3253 Super = cast_or_null<ObjCInterfaceDecl>(
3254 Importer.Import(D->getSuperClass()));
3255 if (!Super)
3256 return 0;
3257 }
3258
3259 ObjCImplementationDecl *Impl = Iface->getImplementation();
3260 if (!Impl) {
3261 // We haven't imported an implementation yet. Create a new @implementation
3262 // now.
3263 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3264 Importer.ImportContext(D->getDeclContext()),
3265 Importer.Import(D->getLocation()),
3266 Iface, Super);
3267
3268 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3269 DeclContext *LexicalDC
3270 = Importer.ImportContext(D->getLexicalDeclContext());
3271 if (!LexicalDC)
3272 return 0;
3273 Impl->setLexicalDeclContext(LexicalDC);
3274 }
3275
3276 // Associate the implementation with the class it implements.
3277 Iface->setImplementation(Impl);
3278 Importer.Imported(D, Iface->getImplementation());
3279 } else {
3280 Importer.Imported(D, Iface->getImplementation());
3281
3282 // Verify that the existing @implementation has the same superclass.
3283 if ((Super && !Impl->getSuperClass()) ||
3284 (!Super && Impl->getSuperClass()) ||
3285 (Super && Impl->getSuperClass() &&
3286 Super->getCanonicalDecl() != Impl->getSuperClass())) {
3287 Importer.ToDiag(Impl->getLocation(),
3288 diag::err_odr_objc_superclass_inconsistent)
3289 << Iface->getDeclName();
3290 // FIXME: It would be nice to have the location of the superclass
3291 // below.
3292 if (Impl->getSuperClass())
3293 Importer.ToDiag(Impl->getLocation(),
3294 diag::note_odr_objc_superclass)
3295 << Impl->getSuperClass()->getDeclName();
3296 else
3297 Importer.ToDiag(Impl->getLocation(),
3298 diag::note_odr_objc_missing_superclass);
3299 if (D->getSuperClass())
3300 Importer.FromDiag(D->getLocation(),
3301 diag::note_odr_objc_superclass)
3302 << D->getSuperClass()->getDeclName();
3303 else
3304 Importer.FromDiag(D->getLocation(),
3305 diag::note_odr_objc_missing_superclass);
3306 return 0;
3307 }
3308 }
3309
3310 // Import all of the members of this @implementation.
3311 ImportDeclContext(D);
3312
3313 return Impl;
3314}
3315
Douglas Gregore3261622010-02-17 18:02:10 +00003316Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3317 // Import the major distinguishing characteristics of an @property.
3318 DeclContext *DC, *LexicalDC;
3319 DeclarationName Name;
3320 SourceLocation Loc;
3321 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3322 return 0;
3323
3324 // Check whether we have already imported this property.
3325 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3326 Lookup.first != Lookup.second;
3327 ++Lookup.first) {
3328 if (ObjCPropertyDecl *FoundProp
3329 = dyn_cast<ObjCPropertyDecl>(*Lookup.first)) {
3330 // Check property types.
3331 if (!Importer.IsStructurallyEquivalent(D->getType(),
3332 FoundProp->getType())) {
3333 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3334 << Name << D->getType() << FoundProp->getType();
3335 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3336 << FoundProp->getType();
3337 return 0;
3338 }
3339
3340 // FIXME: Check property attributes, getters, setters, etc.?
3341
3342 // Consider these properties to be equivalent.
3343 Importer.Imported(D, FoundProp);
3344 return FoundProp;
3345 }
3346 }
3347
3348 // Import the type.
John McCall83a230c2010-06-04 20:50:08 +00003349 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3350 if (!T)
Douglas Gregore3261622010-02-17 18:02:10 +00003351 return 0;
3352
3353 // Create the new property.
3354 ObjCPropertyDecl *ToProperty
3355 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3356 Name.getAsIdentifierInfo(),
3357 Importer.Import(D->getAtLoc()),
3358 T,
3359 D->getPropertyImplementation());
3360 Importer.Imported(D, ToProperty);
3361 ToProperty->setLexicalDeclContext(LexicalDC);
3362 LexicalDC->addDecl(ToProperty);
3363
3364 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00003365 ToProperty->setPropertyAttributesAsWritten(
3366 D->getPropertyAttributesAsWritten());
Douglas Gregore3261622010-02-17 18:02:10 +00003367 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3368 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3369 ToProperty->setGetterMethodDecl(
3370 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3371 ToProperty->setSetterMethodDecl(
3372 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3373 ToProperty->setPropertyIvarDecl(
3374 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3375 return ToProperty;
3376}
3377
Douglas Gregor954e0c72010-12-07 18:32:03 +00003378Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3379 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3380 Importer.Import(D->getPropertyDecl()));
3381 if (!Property)
3382 return 0;
3383
3384 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3385 if (!DC)
3386 return 0;
3387
3388 // Import the lexical declaration context.
3389 DeclContext *LexicalDC = DC;
3390 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3391 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3392 if (!LexicalDC)
3393 return 0;
3394 }
3395
3396 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3397 if (!InImpl)
3398 return 0;
3399
3400 // Import the ivar (for an @synthesize).
3401 ObjCIvarDecl *Ivar = 0;
3402 if (D->getPropertyIvarDecl()) {
3403 Ivar = cast_or_null<ObjCIvarDecl>(
3404 Importer.Import(D->getPropertyIvarDecl()));
3405 if (!Ivar)
3406 return 0;
3407 }
3408
3409 ObjCPropertyImplDecl *ToImpl
3410 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3411 if (!ToImpl) {
3412 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3413 Importer.Import(D->getLocStart()),
3414 Importer.Import(D->getLocation()),
3415 Property,
3416 D->getPropertyImplementation(),
3417 Ivar,
3418 Importer.Import(D->getPropertyIvarDeclLoc()));
3419 ToImpl->setLexicalDeclContext(LexicalDC);
3420 Importer.Imported(D, ToImpl);
3421 LexicalDC->addDecl(ToImpl);
3422 } else {
3423 // Check that we have the same kind of property implementation (@synthesize
3424 // vs. @dynamic).
3425 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3426 Importer.ToDiag(ToImpl->getLocation(),
3427 diag::err_odr_objc_property_impl_kind_inconsistent)
3428 << Property->getDeclName()
3429 << (ToImpl->getPropertyImplementation()
3430 == ObjCPropertyImplDecl::Dynamic);
3431 Importer.FromDiag(D->getLocation(),
3432 diag::note_odr_objc_property_impl_kind)
3433 << D->getPropertyDecl()->getDeclName()
3434 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3435 return 0;
3436 }
3437
3438 // For @synthesize, check that we have the same
3439 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3440 Ivar != ToImpl->getPropertyIvarDecl()) {
3441 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3442 diag::err_odr_objc_synthesize_ivar_inconsistent)
3443 << Property->getDeclName()
3444 << ToImpl->getPropertyIvarDecl()->getDeclName()
3445 << Ivar->getDeclName();
3446 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3447 diag::note_odr_objc_synthesize_ivar_here)
3448 << D->getPropertyIvarDecl()->getDeclName();
3449 return 0;
3450 }
3451
3452 // Merge the existing implementation with the new implementation.
3453 Importer.Imported(D, ToImpl);
3454 }
3455
3456 return ToImpl;
3457}
3458
Douglas Gregor2b785022010-02-18 02:12:22 +00003459Decl *
3460ASTNodeImporter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
3461 // Import the context of this declaration.
3462 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3463 if (!DC)
3464 return 0;
3465
3466 DeclContext *LexicalDC = DC;
3467 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3468 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3469 if (!LexicalDC)
3470 return 0;
3471 }
3472
3473 // Import the location of this declaration.
3474 SourceLocation Loc = Importer.Import(D->getLocation());
3475
Chris Lattner5f9e2722011-07-23 10:55:15 +00003476 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3477 SmallVector<SourceLocation, 4> Locations;
Douglas Gregor2b785022010-02-18 02:12:22 +00003478 ObjCForwardProtocolDecl::protocol_loc_iterator FromProtoLoc
3479 = D->protocol_loc_begin();
3480 for (ObjCForwardProtocolDecl::protocol_iterator FromProto
3481 = D->protocol_begin(), FromProtoEnd = D->protocol_end();
3482 FromProto != FromProtoEnd;
3483 ++FromProto, ++FromProtoLoc) {
3484 ObjCProtocolDecl *ToProto
3485 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3486 if (!ToProto)
3487 continue;
3488
3489 Protocols.push_back(ToProto);
3490 Locations.push_back(Importer.Import(*FromProtoLoc));
3491 }
3492
3493 ObjCForwardProtocolDecl *ToForward
3494 = ObjCForwardProtocolDecl::Create(Importer.getToContext(), DC, Loc,
3495 Protocols.data(), Protocols.size(),
3496 Locations.data());
3497 ToForward->setLexicalDeclContext(LexicalDC);
3498 LexicalDC->addDecl(ToForward);
3499 Importer.Imported(D, ToForward);
3500 return ToForward;
3501}
3502
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003503Decl *ASTNodeImporter::VisitObjCClassDecl(ObjCClassDecl *D) {
3504 // Import the context of this declaration.
3505 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3506 if (!DC)
3507 return 0;
3508
3509 DeclContext *LexicalDC = DC;
3510 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3511 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3512 if (!LexicalDC)
3513 return 0;
3514 }
3515
3516 // Import the location of this declaration.
3517 SourceLocation Loc = Importer.Import(D->getLocation());
3518
Chris Lattner5f9e2722011-07-23 10:55:15 +00003519 SmallVector<ObjCInterfaceDecl *, 4> Interfaces;
3520 SmallVector<SourceLocation, 4> Locations;
Douglas Gregora2bc15b2010-02-18 02:04:09 +00003521 for (ObjCClassDecl::iterator From = D->begin(), FromEnd = D->end();
3522 From != FromEnd; ++From) {
3523 ObjCInterfaceDecl *ToIface
3524 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(From->getInterface()));
3525 if (!ToIface)
3526 continue;
3527
3528 Interfaces.push_back(ToIface);
3529 Locations.push_back(Importer.Import(From->getLocation()));
3530 }
3531
3532 ObjCClassDecl *ToClass = ObjCClassDecl::Create(Importer.getToContext(), DC,
3533 Loc,
3534 Interfaces.data(),
3535 Locations.data(),
3536 Interfaces.size());
3537 ToClass->setLexicalDeclContext(LexicalDC);
3538 LexicalDC->addDecl(ToClass);
3539 Importer.Imported(D, ToClass);
3540 return ToClass;
3541}
3542
Douglas Gregor040afae2010-11-30 19:14:50 +00003543Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3544 // For template arguments, we adopt the translation unit as our declaration
3545 // context. This context will be fixed when the actual template declaration
3546 // is created.
3547
3548 // FIXME: Import default argument.
3549 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3550 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnara344577e2011-03-06 15:48:19 +00003551 Importer.Import(D->getLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003552 Importer.Import(D->getLocation()),
3553 D->getDepth(),
3554 D->getIndex(),
3555 Importer.Import(D->getIdentifier()),
3556 D->wasDeclaredWithTypename(),
3557 D->isParameterPack());
3558}
3559
3560Decl *
3561ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3562 // Import the name of this declaration.
3563 DeclarationName Name = Importer.Import(D->getDeclName());
3564 if (D->getDeclName() && !Name)
3565 return 0;
3566
3567 // Import the location of this declaration.
3568 SourceLocation Loc = Importer.Import(D->getLocation());
3569
3570 // Import the type of this declaration.
3571 QualType T = Importer.Import(D->getType());
3572 if (T.isNull())
3573 return 0;
3574
3575 // Import type-source information.
3576 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3577 if (D->getTypeSourceInfo() && !TInfo)
3578 return 0;
3579
3580 // FIXME: Import default argument.
3581
3582 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3583 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003584 Importer.Import(D->getInnerLocStart()),
Douglas Gregor040afae2010-11-30 19:14:50 +00003585 Loc, D->getDepth(), D->getPosition(),
3586 Name.getAsIdentifierInfo(),
Douglas Gregor10738d32010-12-23 23:51:58 +00003587 T, D->isParameterPack(), TInfo);
Douglas Gregor040afae2010-11-30 19:14:50 +00003588}
3589
3590Decl *
3591ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3592 // Import the name of this declaration.
3593 DeclarationName Name = Importer.Import(D->getDeclName());
3594 if (D->getDeclName() && !Name)
3595 return 0;
3596
3597 // Import the location of this declaration.
3598 SourceLocation Loc = Importer.Import(D->getLocation());
3599
3600 // Import template parameters.
3601 TemplateParameterList *TemplateParams
3602 = ImportTemplateParameterList(D->getTemplateParameters());
3603 if (!TemplateParams)
3604 return 0;
3605
3606 // FIXME: Import default argument.
3607
3608 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3609 Importer.getToContext().getTranslationUnitDecl(),
3610 Loc, D->getDepth(), D->getPosition(),
Douglas Gregor61c4d282011-01-05 15:48:55 +00003611 D->isParameterPack(),
Douglas Gregor040afae2010-11-30 19:14:50 +00003612 Name.getAsIdentifierInfo(),
3613 TemplateParams);
3614}
3615
3616Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3617 // If this record has a definition in the translation unit we're coming from,
3618 // but this particular declaration is not that definition, import the
3619 // definition and map to that.
3620 CXXRecordDecl *Definition
3621 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3622 if (Definition && Definition != D->getTemplatedDecl()) {
3623 Decl *ImportedDef
3624 = Importer.Import(Definition->getDescribedClassTemplate());
3625 if (!ImportedDef)
3626 return 0;
3627
3628 return Importer.Imported(D, ImportedDef);
3629 }
3630
3631 // Import the major distinguishing characteristics of this class template.
3632 DeclContext *DC, *LexicalDC;
3633 DeclarationName Name;
3634 SourceLocation Loc;
3635 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3636 return 0;
3637
3638 // We may already have a template of the same name; try to find and match it.
3639 if (!DC->isFunctionOrMethod()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00003640 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor040afae2010-11-30 19:14:50 +00003641 for (DeclContext::lookup_result Lookup = DC->lookup(Name);
3642 Lookup.first != Lookup.second;
3643 ++Lookup.first) {
3644 if (!(*Lookup.first)->isInIdentifierNamespace(Decl::IDNS_Ordinary))
3645 continue;
3646
3647 Decl *Found = *Lookup.first;
3648 if (ClassTemplateDecl *FoundTemplate
3649 = dyn_cast<ClassTemplateDecl>(Found)) {
3650 if (IsStructuralMatch(D, FoundTemplate)) {
3651 // The class templates structurally match; call it the same template.
3652 // FIXME: We may be filling in a forward declaration here. Handle
3653 // this case!
3654 Importer.Imported(D->getTemplatedDecl(),
3655 FoundTemplate->getTemplatedDecl());
3656 return Importer.Imported(D, FoundTemplate);
3657 }
3658 }
3659
3660 ConflictingDecls.push_back(*Lookup.first);
3661 }
3662
3663 if (!ConflictingDecls.empty()) {
3664 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3665 ConflictingDecls.data(),
3666 ConflictingDecls.size());
3667 }
3668
3669 if (!Name)
3670 return 0;
3671 }
3672
3673 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3674
3675 // Create the declaration that is being templated.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003676 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3677 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregor040afae2010-11-30 19:14:50 +00003678 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3679 DTemplated->getTagKind(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003680 DC, StartLoc, IdLoc,
3681 Name.getAsIdentifierInfo());
Douglas Gregor040afae2010-11-30 19:14:50 +00003682 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003683 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregor040afae2010-11-30 19:14:50 +00003684 D2Templated->setLexicalDeclContext(LexicalDC);
3685
3686 // Create the class template declaration itself.
3687 TemplateParameterList *TemplateParams
3688 = ImportTemplateParameterList(D->getTemplateParameters());
3689 if (!TemplateParams)
3690 return 0;
3691
3692 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3693 Loc, Name, TemplateParams,
3694 D2Templated,
3695 /*PrevDecl=*/0);
3696 D2Templated->setDescribedClassTemplate(D2);
3697
3698 D2->setAccess(D->getAccess());
3699 D2->setLexicalDeclContext(LexicalDC);
3700 LexicalDC->addDecl(D2);
3701
3702 // Note the relationship between the class templates.
3703 Importer.Imported(D, D2);
3704 Importer.Imported(DTemplated, D2Templated);
3705
3706 if (DTemplated->isDefinition() && !D2Templated->isDefinition()) {
3707 // FIXME: Import definition!
3708 }
3709
3710 return D2;
3711}
3712
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003713Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
3714 ClassTemplateSpecializationDecl *D) {
3715 // If this record has a definition in the translation unit we're coming from,
3716 // but this particular declaration is not that definition, import the
3717 // definition and map to that.
3718 TagDecl *Definition = D->getDefinition();
3719 if (Definition && Definition != D) {
3720 Decl *ImportedDef = Importer.Import(Definition);
3721 if (!ImportedDef)
3722 return 0;
3723
3724 return Importer.Imported(D, ImportedDef);
3725 }
3726
3727 ClassTemplateDecl *ClassTemplate
3728 = cast_or_null<ClassTemplateDecl>(Importer.Import(
3729 D->getSpecializedTemplate()));
3730 if (!ClassTemplate)
3731 return 0;
3732
3733 // Import the context of this declaration.
3734 DeclContext *DC = ClassTemplate->getDeclContext();
3735 if (!DC)
3736 return 0;
3737
3738 DeclContext *LexicalDC = DC;
3739 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3740 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3741 if (!LexicalDC)
3742 return 0;
3743 }
3744
3745 // Import the location of this declaration.
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003746 SourceLocation StartLoc = Importer.Import(D->getLocStart());
3747 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003748
3749 // Import template arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003750 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003751 if (ImportTemplateArguments(D->getTemplateArgs().data(),
3752 D->getTemplateArgs().size(),
3753 TemplateArgs))
3754 return 0;
3755
3756 // Try to find an existing specialization with these template arguments.
3757 void *InsertPos = 0;
3758 ClassTemplateSpecializationDecl *D2
3759 = ClassTemplate->findSpecialization(TemplateArgs.data(),
3760 TemplateArgs.size(), InsertPos);
3761 if (D2) {
3762 // We already have a class template specialization with these template
3763 // arguments.
3764
3765 // FIXME: Check for specialization vs. instantiation errors.
3766
3767 if (RecordDecl *FoundDef = D2->getDefinition()) {
3768 if (!D->isDefinition() || IsStructuralMatch(D, FoundDef)) {
3769 // The record types structurally match, or the "from" translation
3770 // unit only had a forward declaration anyway; call it the same
3771 // function.
3772 return Importer.Imported(D, FoundDef);
3773 }
3774 }
3775 } else {
3776 // Create a new specialization.
3777 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
3778 D->getTagKind(), DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00003779 StartLoc, IdLoc,
3780 ClassTemplate,
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003781 TemplateArgs.data(),
3782 TemplateArgs.size(),
3783 /*PrevDecl=*/0);
3784 D2->setSpecializationKind(D->getSpecializationKind());
3785
3786 // Add this specialization to the class template.
3787 ClassTemplate->AddSpecialization(D2, InsertPos);
3788
3789 // Import the qualifier, if any.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003790 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregord5dc83a2010-12-01 01:36:18 +00003791
3792 // Add the specialization to this context.
3793 D2->setLexicalDeclContext(LexicalDC);
3794 LexicalDC->addDecl(D2);
3795 }
3796 Importer.Imported(D, D2);
3797
3798 if (D->isDefinition() && ImportDefinition(D, D2))
3799 return 0;
3800
3801 return D2;
3802}
3803
Douglas Gregor4800d952010-02-11 19:21:55 +00003804//----------------------------------------------------------------------------
3805// Import Statements
3806//----------------------------------------------------------------------------
3807
3808Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
3809 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
3810 << S->getStmtClassName();
3811 return 0;
3812}
3813
3814//----------------------------------------------------------------------------
3815// Import Expressions
3816//----------------------------------------------------------------------------
3817Expr *ASTNodeImporter::VisitExpr(Expr *E) {
3818 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
3819 << E->getStmtClassName();
3820 return 0;
3821}
3822
Douglas Gregor44080632010-02-19 01:17:02 +00003823Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor44080632010-02-19 01:17:02 +00003824 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
3825 if (!ToD)
3826 return 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +00003827
3828 NamedDecl *FoundD = 0;
3829 if (E->getDecl() != E->getFoundDecl()) {
3830 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
3831 if (!FoundD)
3832 return 0;
3833 }
Douglas Gregor44080632010-02-19 01:17:02 +00003834
3835 QualType T = Importer.Import(E->getType());
3836 if (T.isNull())
3837 return 0;
3838
Douglas Gregor40d96a62011-02-28 21:54:11 +00003839 return DeclRefExpr::Create(Importer.getToContext(),
3840 Importer.Import(E->getQualifierLoc()),
Douglas Gregor44080632010-02-19 01:17:02 +00003841 ToD,
3842 Importer.Import(E->getLocation()),
John McCallf89e55a2010-11-18 06:31:45 +00003843 T, E->getValueKind(),
Chandler Carruth3aa81402011-05-01 23:48:14 +00003844 FoundD,
Douglas Gregor44080632010-02-19 01:17:02 +00003845 /*FIXME:TemplateArgs=*/0);
3846}
3847
Douglas Gregor4800d952010-02-11 19:21:55 +00003848Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
3849 QualType T = Importer.Import(E->getType());
3850 if (T.isNull())
3851 return 0;
3852
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +00003853 return IntegerLiteral::Create(Importer.getToContext(),
3854 E->getValue(), T,
3855 Importer.Import(E->getLocation()));
Douglas Gregor4800d952010-02-11 19:21:55 +00003856}
3857
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003858Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
3859 QualType T = Importer.Import(E->getType());
3860 if (T.isNull())
3861 return 0;
3862
Douglas Gregor5cee1192011-07-27 05:40:30 +00003863 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
3864 E->getKind(), T,
Douglas Gregorb2e400a2010-02-18 02:21:22 +00003865 Importer.Import(E->getLocation()));
3866}
3867
Douglas Gregorf638f952010-02-19 01:07:06 +00003868Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
3869 Expr *SubExpr = Importer.Import(E->getSubExpr());
3870 if (!SubExpr)
3871 return 0;
3872
3873 return new (Importer.getToContext())
3874 ParenExpr(Importer.Import(E->getLParen()),
3875 Importer.Import(E->getRParen()),
3876 SubExpr);
3877}
3878
3879Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
3880 QualType T = Importer.Import(E->getType());
3881 if (T.isNull())
3882 return 0;
3883
3884 Expr *SubExpr = Importer.Import(E->getSubExpr());
3885 if (!SubExpr)
3886 return 0;
3887
3888 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003889 T, E->getValueKind(),
3890 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003891 Importer.Import(E->getOperatorLoc()));
3892}
3893
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003894Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
3895 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregorbd249a52010-02-19 01:24:23 +00003896 QualType ResultType = Importer.Import(E->getType());
3897
3898 if (E->isArgumentType()) {
3899 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
3900 if (!TInfo)
3901 return 0;
3902
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003903 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3904 TInfo, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003905 Importer.Import(E->getOperatorLoc()),
3906 Importer.Import(E->getRParenLoc()));
3907 }
3908
3909 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
3910 if (!SubExpr)
3911 return 0;
3912
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003913 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
3914 SubExpr, ResultType,
Douglas Gregorbd249a52010-02-19 01:24:23 +00003915 Importer.Import(E->getOperatorLoc()),
3916 Importer.Import(E->getRParenLoc()));
3917}
3918
Douglas Gregorf638f952010-02-19 01:07:06 +00003919Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
3920 QualType T = Importer.Import(E->getType());
3921 if (T.isNull())
3922 return 0;
3923
3924 Expr *LHS = Importer.Import(E->getLHS());
3925 if (!LHS)
3926 return 0;
3927
3928 Expr *RHS = Importer.Import(E->getRHS());
3929 if (!RHS)
3930 return 0;
3931
3932 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003933 T, E->getValueKind(),
3934 E->getObjectKind(),
Douglas Gregorf638f952010-02-19 01:07:06 +00003935 Importer.Import(E->getOperatorLoc()));
3936}
3937
3938Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
3939 QualType T = Importer.Import(E->getType());
3940 if (T.isNull())
3941 return 0;
3942
3943 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
3944 if (CompLHSType.isNull())
3945 return 0;
3946
3947 QualType CompResultType = Importer.Import(E->getComputationResultType());
3948 if (CompResultType.isNull())
3949 return 0;
3950
3951 Expr *LHS = Importer.Import(E->getLHS());
3952 if (!LHS)
3953 return 0;
3954
3955 Expr *RHS = Importer.Import(E->getRHS());
3956 if (!RHS)
3957 return 0;
3958
3959 return new (Importer.getToContext())
3960 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCallf89e55a2010-11-18 06:31:45 +00003961 T, E->getValueKind(),
3962 E->getObjectKind(),
3963 CompLHSType, CompResultType,
Douglas Gregorf638f952010-02-19 01:07:06 +00003964 Importer.Import(E->getOperatorLoc()));
3965}
3966
Benjamin Kramerda57f3e2011-03-26 12:38:21 +00003967static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallf871d0c2010-08-07 06:22:56 +00003968 if (E->path_empty()) return false;
3969
3970 // TODO: import cast paths
3971 return true;
3972}
3973
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003974Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
3975 QualType T = Importer.Import(E->getType());
3976 if (T.isNull())
3977 return 0;
3978
3979 Expr *SubExpr = Importer.Import(E->getSubExpr());
3980 if (!SubExpr)
3981 return 0;
John McCallf871d0c2010-08-07 06:22:56 +00003982
3983 CXXCastPath BasePath;
3984 if (ImportCastPath(E, BasePath))
3985 return 0;
3986
3987 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall5baba9d2010-08-25 10:28:54 +00003988 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor36ead2e2010-02-12 22:17:39 +00003989}
3990
Douglas Gregor008847a2010-02-19 01:32:14 +00003991Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
3992 QualType T = Importer.Import(E->getType());
3993 if (T.isNull())
3994 return 0;
3995
3996 Expr *SubExpr = Importer.Import(E->getSubExpr());
3997 if (!SubExpr)
3998 return 0;
3999
4000 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4001 if (!TInfo && E->getTypeInfoAsWritten())
4002 return 0;
4003
John McCallf871d0c2010-08-07 06:22:56 +00004004 CXXCastPath BasePath;
4005 if (ImportCastPath(E, BasePath))
4006 return 0;
4007
John McCallf89e55a2010-11-18 06:31:45 +00004008 return CStyleCastExpr::Create(Importer.getToContext(), T,
4009 E->getValueKind(), E->getCastKind(),
John McCallf871d0c2010-08-07 06:22:56 +00004010 SubExpr, &BasePath, TInfo,
4011 Importer.Import(E->getLParenLoc()),
4012 Importer.Import(E->getRParenLoc()));
Douglas Gregor008847a2010-02-19 01:32:14 +00004013}
4014
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004015ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregord8868a62011-01-18 03:11:38 +00004016 ASTContext &FromContext, FileManager &FromFileManager,
4017 bool MinimalImport)
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004018 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregord8868a62011-01-18 03:11:38 +00004019 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
4020 Minimal(MinimalImport)
4021{
Douglas Gregor9bed8792010-02-09 19:21:46 +00004022 ImportedDecls[FromContext.getTranslationUnitDecl()]
4023 = ToContext.getTranslationUnitDecl();
4024}
4025
4026ASTImporter::~ASTImporter() { }
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004027
4028QualType ASTImporter::Import(QualType FromT) {
4029 if (FromT.isNull())
4030 return QualType();
John McCallf4c73712011-01-19 06:33:43 +00004031
4032 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004033
Douglas Gregor169fba52010-02-08 15:18:58 +00004034 // Check whether we've already imported this type.
John McCallf4c73712011-01-19 06:33:43 +00004035 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4036 = ImportedTypes.find(fromTy);
Douglas Gregor169fba52010-02-08 15:18:58 +00004037 if (Pos != ImportedTypes.end())
John McCallf4c73712011-01-19 06:33:43 +00004038 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004039
Douglas Gregor169fba52010-02-08 15:18:58 +00004040 // Import the type
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004041 ASTNodeImporter Importer(*this);
John McCallf4c73712011-01-19 06:33:43 +00004042 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004043 if (ToT.isNull())
4044 return ToT;
4045
Douglas Gregor169fba52010-02-08 15:18:58 +00004046 // Record the imported type.
John McCallf4c73712011-01-19 06:33:43 +00004047 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregor169fba52010-02-08 15:18:58 +00004048
John McCallf4c73712011-01-19 06:33:43 +00004049 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004050}
4051
Douglas Gregor9bed8792010-02-09 19:21:46 +00004052TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004053 if (!FromTSI)
4054 return FromTSI;
4055
4056 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky56062202010-07-26 16:56:01 +00004057 // on the type and a single location. Implement a real version of this.
Douglas Gregor82fc4bf2010-02-10 17:47:19 +00004058 QualType T = Import(FromTSI->getType());
4059 if (T.isNull())
4060 return 0;
4061
4062 return ToContext.getTrivialTypeSourceInfo(T,
Abramo Bagnarabd054db2010-05-20 10:00:11 +00004063 FromTSI->getTypeLoc().getSourceRange().getBegin());
Douglas Gregor9bed8792010-02-09 19:21:46 +00004064}
4065
4066Decl *ASTImporter::Import(Decl *FromD) {
4067 if (!FromD)
4068 return 0;
4069
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004070 ASTNodeImporter Importer(*this);
4071
Douglas Gregor9bed8792010-02-09 19:21:46 +00004072 // Check whether we've already imported this declaration.
4073 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004074 if (Pos != ImportedDecls.end()) {
4075 Decl *ToD = Pos->second;
4076 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4077 return ToD;
4078 }
Douglas Gregor9bed8792010-02-09 19:21:46 +00004079
4080 // Import the type
Douglas Gregor9bed8792010-02-09 19:21:46 +00004081 Decl *ToD = Importer.Visit(FromD);
4082 if (!ToD)
4083 return 0;
4084
4085 // Record the imported declaration.
4086 ImportedDecls[FromD] = ToD;
Douglas Gregorea35d112010-02-15 23:54:17 +00004087
4088 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4089 // Keep track of anonymous tags that have an associated typedef.
Richard Smith162e1c12011-04-15 14:24:37 +00004090 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorea35d112010-02-15 23:54:17 +00004091 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smith162e1c12011-04-15 14:24:37 +00004092 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004093 // When we've finished transforming a typedef, see whether it was the
4094 // typedef for an anonymous tag.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004095 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorea35d112010-02-15 23:54:17 +00004096 FromTag = AnonTagsWithPendingTypedefs.begin(),
4097 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4098 FromTag != FromTagEnd; ++FromTag) {
Richard Smith162e1c12011-04-15 14:24:37 +00004099 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorea35d112010-02-15 23:54:17 +00004100 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4101 // We found the typedef for an anonymous tag; link them.
Richard Smith162e1c12011-04-15 14:24:37 +00004102 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorea35d112010-02-15 23:54:17 +00004103 AnonTagsWithPendingTypedefs.erase(FromTag);
4104 break;
4105 }
4106 }
4107 }
4108 }
4109
Douglas Gregor9bed8792010-02-09 19:21:46 +00004110 return ToD;
4111}
4112
4113DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4114 if (!FromDC)
4115 return FromDC;
4116
4117 return cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
4118}
4119
4120Expr *ASTImporter::Import(Expr *FromE) {
4121 if (!FromE)
4122 return 0;
4123
4124 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4125}
4126
4127Stmt *ASTImporter::Import(Stmt *FromS) {
4128 if (!FromS)
4129 return 0;
4130
Douglas Gregor4800d952010-02-11 19:21:55 +00004131 // Check whether we've already imported this declaration.
4132 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4133 if (Pos != ImportedStmts.end())
4134 return Pos->second;
4135
4136 // Import the type
4137 ASTNodeImporter Importer(*this);
4138 Stmt *ToS = Importer.Visit(FromS);
4139 if (!ToS)
4140 return 0;
4141
4142 // Record the imported declaration.
4143 ImportedStmts[FromS] = ToS;
4144 return ToS;
Douglas Gregor9bed8792010-02-09 19:21:46 +00004145}
4146
4147NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4148 if (!FromNNS)
4149 return 0;
4150
Douglas Gregor8703b1c2011-04-27 16:48:40 +00004151 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4152
4153 switch (FromNNS->getKind()) {
4154 case NestedNameSpecifier::Identifier:
4155 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4156 return NestedNameSpecifier::Create(ToContext, prefix, II);
4157 }
4158 return 0;
4159
4160 case NestedNameSpecifier::Namespace:
4161 if (NamespaceDecl *NS =
4162 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4163 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4164 }
4165 return 0;
4166
4167 case NestedNameSpecifier::NamespaceAlias:
4168 if (NamespaceAliasDecl *NSAD =
4169 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4170 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4171 }
4172 return 0;
4173
4174 case NestedNameSpecifier::Global:
4175 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4176
4177 case NestedNameSpecifier::TypeSpec:
4178 case NestedNameSpecifier::TypeSpecWithTemplate: {
4179 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4180 if (!T.isNull()) {
4181 bool bTemplate = FromNNS->getKind() ==
4182 NestedNameSpecifier::TypeSpecWithTemplate;
4183 return NestedNameSpecifier::Create(ToContext, prefix,
4184 bTemplate, T.getTypePtr());
4185 }
4186 }
4187 return 0;
4188 }
4189
4190 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor9bed8792010-02-09 19:21:46 +00004191 return 0;
4192}
4193
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00004194NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4195 // FIXME: Implement!
4196 return NestedNameSpecifierLoc();
4197}
4198
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004199TemplateName ASTImporter::Import(TemplateName From) {
4200 switch (From.getKind()) {
4201 case TemplateName::Template:
4202 if (TemplateDecl *ToTemplate
4203 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4204 return TemplateName(ToTemplate);
4205
4206 return TemplateName();
4207
4208 case TemplateName::OverloadedTemplate: {
4209 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4210 UnresolvedSet<2> ToTemplates;
4211 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4212 E = FromStorage->end();
4213 I != E; ++I) {
4214 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4215 ToTemplates.addDecl(To);
4216 else
4217 return TemplateName();
4218 }
4219 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4220 ToTemplates.end());
4221 }
4222
4223 case TemplateName::QualifiedTemplate: {
4224 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4225 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4226 if (!Qualifier)
4227 return TemplateName();
4228
4229 if (TemplateDecl *ToTemplate
4230 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4231 return ToContext.getQualifiedTemplateName(Qualifier,
4232 QTN->hasTemplateKeyword(),
4233 ToTemplate);
4234
4235 return TemplateName();
4236 }
4237
4238 case TemplateName::DependentTemplate: {
4239 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4240 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4241 if (!Qualifier)
4242 return TemplateName();
4243
4244 if (DTN->isIdentifier()) {
4245 return ToContext.getDependentTemplateName(Qualifier,
4246 Import(DTN->getIdentifier()));
4247 }
4248
4249 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4250 }
John McCall14606042011-06-30 08:33:18 +00004251
4252 case TemplateName::SubstTemplateTemplateParm: {
4253 SubstTemplateTemplateParmStorage *subst
4254 = From.getAsSubstTemplateTemplateParm();
4255 TemplateTemplateParmDecl *param
4256 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4257 if (!param)
4258 return TemplateName();
4259
4260 TemplateName replacement = Import(subst->getReplacement());
4261 if (replacement.isNull()) return TemplateName();
4262
4263 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4264 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00004265
4266 case TemplateName::SubstTemplateTemplateParmPack: {
4267 SubstTemplateTemplateParmPackStorage *SubstPack
4268 = From.getAsSubstTemplateTemplateParmPack();
4269 TemplateTemplateParmDecl *Param
4270 = cast_or_null<TemplateTemplateParmDecl>(
4271 Import(SubstPack->getParameterPack()));
4272 if (!Param)
4273 return TemplateName();
4274
4275 ASTNodeImporter Importer(*this);
4276 TemplateArgument ArgPack
4277 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4278 if (ArgPack.isNull())
4279 return TemplateName();
4280
4281 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4282 }
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004283 }
4284
4285 llvm_unreachable("Invalid template name kind");
4286 return TemplateName();
4287}
4288
Douglas Gregor9bed8792010-02-09 19:21:46 +00004289SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4290 if (FromLoc.isInvalid())
4291 return SourceLocation();
4292
Douglas Gregor88523732010-02-10 00:15:17 +00004293 SourceManager &FromSM = FromContext.getSourceManager();
4294
4295 // For now, map everything down to its spelling location, so that we
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004296 // don't have to import macro expansions.
4297 // FIXME: Import macro expansions!
Douglas Gregor88523732010-02-10 00:15:17 +00004298 FromLoc = FromSM.getSpellingLoc(FromLoc);
4299 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4300 SourceManager &ToSM = ToContext.getSourceManager();
4301 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
4302 .getFileLocWithOffset(Decomposed.second);
Douglas Gregor9bed8792010-02-09 19:21:46 +00004303}
4304
4305SourceRange ASTImporter::Import(SourceRange FromRange) {
4306 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4307}
4308
Douglas Gregor88523732010-02-10 00:15:17 +00004309FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl535a3e22010-09-30 01:03:06 +00004310 llvm::DenseMap<FileID, FileID>::iterator Pos
4311 = ImportedFileIDs.find(FromID);
Douglas Gregor88523732010-02-10 00:15:17 +00004312 if (Pos != ImportedFileIDs.end())
4313 return Pos->second;
4314
4315 SourceManager &FromSM = FromContext.getSourceManager();
4316 SourceManager &ToSM = ToContext.getSourceManager();
4317 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruthb10aa3e2011-07-15 00:04:35 +00004318 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor88523732010-02-10 00:15:17 +00004319
4320 // Include location of this file.
4321 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4322
4323 // Map the FileID for to the "to" source manager.
4324 FileID ToID;
4325 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004326 if (Cache->OrigEntry) {
Douglas Gregor88523732010-02-10 00:15:17 +00004327 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4328 // disk again
4329 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4330 // than mmap the files several times.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00004331 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor88523732010-02-10 00:15:17 +00004332 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4333 FromSLoc.getFile().getFileCharacteristic());
4334 } else {
4335 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004336 const llvm::MemoryBuffer *
4337 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor88523732010-02-10 00:15:17 +00004338 llvm::MemoryBuffer *ToBuf
Chris Lattnera0a270c2010-04-05 22:42:27 +00004339 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor88523732010-02-10 00:15:17 +00004340 FromBuf->getBufferIdentifier());
4341 ToID = ToSM.createFileIDForMemBuffer(ToBuf);
4342 }
4343
4344
Sebastian Redl535a3e22010-09-30 01:03:06 +00004345 ImportedFileIDs[FromID] = ToID;
Douglas Gregor88523732010-02-10 00:15:17 +00004346 return ToID;
4347}
4348
Douglas Gregord8868a62011-01-18 03:11:38 +00004349void ASTImporter::ImportDefinition(Decl *From) {
4350 Decl *To = Import(From);
4351 if (!To)
4352 return;
4353
4354 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4355 ASTNodeImporter Importer(*this);
Sean Callanan673e7752011-07-19 22:38:25 +00004356
4357 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4358 if (!ToRecord->getDefinition()) {
4359 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
4360 /*ForceImport=*/true);
4361 return;
4362 }
4363 }
Douglas Gregor1cf038c2011-07-29 23:31:30 +00004364
4365 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4366 if (!ToEnum->getDefinition()) {
4367 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
4368 /*ForceImport=*/true);
4369 return;
4370 }
4371 }
4372
Douglas Gregord8868a62011-01-18 03:11:38 +00004373 Importer.ImportDeclContext(FromDC, true);
4374 }
4375}
4376
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004377DeclarationName ASTImporter::Import(DeclarationName FromName) {
4378 if (!FromName)
4379 return DeclarationName();
4380
4381 switch (FromName.getNameKind()) {
4382 case DeclarationName::Identifier:
4383 return Import(FromName.getAsIdentifierInfo());
4384
4385 case DeclarationName::ObjCZeroArgSelector:
4386 case DeclarationName::ObjCOneArgSelector:
4387 case DeclarationName::ObjCMultiArgSelector:
4388 return Import(FromName.getObjCSelector());
4389
4390 case DeclarationName::CXXConstructorName: {
4391 QualType T = Import(FromName.getCXXNameType());
4392 if (T.isNull())
4393 return DeclarationName();
4394
4395 return ToContext.DeclarationNames.getCXXConstructorName(
4396 ToContext.getCanonicalType(T));
4397 }
4398
4399 case DeclarationName::CXXDestructorName: {
4400 QualType T = Import(FromName.getCXXNameType());
4401 if (T.isNull())
4402 return DeclarationName();
4403
4404 return ToContext.DeclarationNames.getCXXDestructorName(
4405 ToContext.getCanonicalType(T));
4406 }
4407
4408 case DeclarationName::CXXConversionFunctionName: {
4409 QualType T = Import(FromName.getCXXNameType());
4410 if (T.isNull())
4411 return DeclarationName();
4412
4413 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4414 ToContext.getCanonicalType(T));
4415 }
4416
4417 case DeclarationName::CXXOperatorName:
4418 return ToContext.DeclarationNames.getCXXOperatorName(
4419 FromName.getCXXOverloadedOperator());
4420
4421 case DeclarationName::CXXLiteralOperatorName:
4422 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4423 Import(FromName.getCXXLiteralIdentifier()));
4424
4425 case DeclarationName::CXXUsingDirective:
4426 // FIXME: STATICS!
4427 return DeclarationName::getUsingDirectiveName();
4428 }
4429
4430 // Silence bogus GCC warning
4431 return DeclarationName();
4432}
4433
Douglas Gregord5dc83a2010-12-01 01:36:18 +00004434IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor1b2949d2010-02-05 17:54:41 +00004435 if (!FromId)
4436 return 0;
4437
4438 return &ToContext.Idents.get(FromId->getName());
4439}
Douglas Gregor089459a2010-02-08 21:09:39 +00004440
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004441Selector ASTImporter::Import(Selector FromSel) {
4442 if (FromSel.isNull())
4443 return Selector();
4444
Chris Lattner5f9e2722011-07-23 10:55:15 +00004445 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregorc3f2d2b2010-02-17 02:12:47 +00004446 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4447 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4448 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4449 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4450}
4451
Douglas Gregor089459a2010-02-08 21:09:39 +00004452DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4453 DeclContext *DC,
4454 unsigned IDNS,
4455 NamedDecl **Decls,
4456 unsigned NumDecls) {
4457 return Name;
4458}
4459
4460DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004461 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004462}
4463
4464DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004465 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor089459a2010-02-08 21:09:39 +00004466}
Douglas Gregor5ce5dab2010-02-12 23:44:20 +00004467
4468Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4469 ImportedDecls[From] = To;
4470 return To;
Daniel Dunbaraf667582010-02-13 20:24:39 +00004471}
Douglas Gregorea35d112010-02-15 23:54:17 +00004472
4473bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To) {
John McCallf4c73712011-01-19 06:33:43 +00004474 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorea35d112010-02-15 23:54:17 +00004475 = ImportedTypes.find(From.getTypePtr());
4476 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4477 return true;
4478
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +00004479 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls);
Benjamin Kramerbb2d1762010-02-18 13:02:13 +00004480 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorea35d112010-02-15 23:54:17 +00004481}