blob: 740b4ff7210f7747f9953ed40f5edd6777b01639 [file] [log] [blame]
Douglas Gregor96e578d2010-02-05 17:54:41 +00001//===--- ASTImporter.cpp - Importing ASTs from other Contexts ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ASTImporter class which imports AST nodes from one
11// context into another context.
12//
13//===----------------------------------------------------------------------===//
14#include "clang/AST/ASTImporter.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000015#include "clang/AST/ASTContext.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000016#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor5c73e912010-02-11 00:48:18 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000019#include "clang/AST/DeclVisitor.h"
Douglas Gregor7eeb5972010-02-11 19:21:55 +000020#include "clang/AST/StmtVisitor.h"
Douglas Gregor96e578d2010-02-05 17:54:41 +000021#include "clang/AST/TypeVisitor.h"
Douglas Gregor811663e2010-02-10 00:15:17 +000022#include "clang/Basic/FileManager.h"
23#include "clang/Basic/SourceManager.h"
24#include "llvm/Support/MemoryBuffer.h"
Douglas Gregor3996e242010-02-15 22:01:00 +000025#include <deque>
Douglas Gregor96e578d2010-02-05 17:54:41 +000026
Douglas Gregor3c2404b2011-11-03 18:07:07 +000027namespace clang {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000028 class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, QualType>,
Douglas Gregor7eeb5972010-02-11 19:21:55 +000029 public DeclVisitor<ASTNodeImporter, Decl *>,
30 public StmtVisitor<ASTNodeImporter, Stmt *> {
Douglas Gregor96e578d2010-02-05 17:54:41 +000031 ASTImporter &Importer;
32
33 public:
34 explicit ASTNodeImporter(ASTImporter &Importer) : Importer(Importer) { }
35
36 using TypeVisitor<ASTNodeImporter, QualType>::Visit;
Douglas Gregor62d311f2010-02-09 19:21:46 +000037 using DeclVisitor<ASTNodeImporter, Decl *>::Visit;
Douglas Gregor7eeb5972010-02-11 19:21:55 +000038 using StmtVisitor<ASTNodeImporter, Stmt *>::Visit;
Douglas Gregor96e578d2010-02-05 17:54:41 +000039
40 // Importing types
John McCall424cec92011-01-19 06:33:43 +000041 QualType VisitType(const Type *T);
42 QualType VisitBuiltinType(const BuiltinType *T);
43 QualType VisitComplexType(const ComplexType *T);
44 QualType VisitPointerType(const PointerType *T);
45 QualType VisitBlockPointerType(const BlockPointerType *T);
46 QualType VisitLValueReferenceType(const LValueReferenceType *T);
47 QualType VisitRValueReferenceType(const RValueReferenceType *T);
48 QualType VisitMemberPointerType(const MemberPointerType *T);
49 QualType VisitConstantArrayType(const ConstantArrayType *T);
50 QualType VisitIncompleteArrayType(const IncompleteArrayType *T);
51 QualType VisitVariableArrayType(const VariableArrayType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000052 // FIXME: DependentSizedArrayType
53 // FIXME: DependentSizedExtVectorType
John McCall424cec92011-01-19 06:33:43 +000054 QualType VisitVectorType(const VectorType *T);
55 QualType VisitExtVectorType(const ExtVectorType *T);
56 QualType VisitFunctionNoProtoType(const FunctionNoProtoType *T);
57 QualType VisitFunctionProtoType(const FunctionProtoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000058 // FIXME: UnresolvedUsingType
Sean Callananda6df8a2011-08-11 16:56:07 +000059 QualType VisitParenType(const ParenType *T);
John McCall424cec92011-01-19 06:33:43 +000060 QualType VisitTypedefType(const TypedefType *T);
61 QualType VisitTypeOfExprType(const TypeOfExprType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000062 // FIXME: DependentTypeOfExprType
John McCall424cec92011-01-19 06:33:43 +000063 QualType VisitTypeOfType(const TypeOfType *T);
64 QualType VisitDecltypeType(const DecltypeType *T);
Alexis Hunte852b102011-05-24 22:41:36 +000065 QualType VisitUnaryTransformType(const UnaryTransformType *T);
Richard Smith30482bc2011-02-20 03:19:35 +000066 QualType VisitAutoType(const AutoType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000067 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000068 QualType VisitRecordType(const RecordType *T);
69 QualType VisitEnumType(const EnumType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000070 // FIXME: TemplateTypeParmType
71 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000072 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
73 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000074 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000075 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000076 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
77 QualType VisitObjCObjectType(const ObjCObjectType *T);
78 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000079
Douglas Gregor95d82832012-01-24 18:36:04 +000080 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000081 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
82 DeclContext *&LexicalDC, DeclarationName &Name,
Douglas Gregorf18a2c72010-02-21 18:26:36 +000083 SourceLocation &Loc);
Douglas Gregord451ea92011-07-29 23:31:30 +000084 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = 0);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000085 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
86 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000087 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000088
Douglas Gregor95d82832012-01-24 18:36:04 +000089 /// \brief What we should import from the definition.
90 enum ImportDefinitionKind {
91 /// \brief Import the default subset of the definition, which might be
92 /// nothing (if minimal import is set) or might be everything (if minimal
93 /// import is not set).
94 IDK_Default,
95 /// \brief Import everything.
96 IDK_Everything,
97 /// \brief Import only the bare bones needed to establish a valid
98 /// DeclContext.
99 IDK_Basic
100 };
101
Douglas Gregor2e15c842012-02-01 21:00:38 +0000102 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
103 return IDK == IDK_Everything ||
104 (IDK == IDK_Default && !Importer.isMinimalImport());
105 }
106
Douglas Gregord451ea92011-07-29 23:31:30 +0000107 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000108 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000109 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000110 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000111 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000112 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000113 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000115 TemplateParameterList *ImportTemplateParameterList(
116 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000117 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
118 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
119 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000120 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000121 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
122 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000123 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000124 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000125 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000126 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000127 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000128 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000129 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000130 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000131 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000132 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000133 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000134 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000135 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000136 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
137 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
138 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
139 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000141 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000142 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000143 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000144 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000145 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000146 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000147 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000148 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000149 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000150 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000151 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000152 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000153 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000154 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
155 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
156 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
157 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000158 Decl *VisitClassTemplateSpecializationDecl(
159 ClassTemplateSpecializationDecl *D);
Douglas Gregor06537af2010-02-18 02:04:09 +0000160
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000161 // Importing statements
162 Stmt *VisitStmt(Stmt *S);
163
164 // Importing expressions
165 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000166 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000167 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000168 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000169 Expr *VisitParenExpr(ParenExpr *E);
170 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000171 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000172 Expr *VisitBinaryOperator(BinaryOperator *E);
173 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000174 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000175 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000176 };
177}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000178using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000179
180//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000181// Structural Equivalence
182//----------------------------------------------------------------------------
183
184namespace {
185 struct StructuralEquivalenceContext {
186 /// \brief AST contexts for which we are checking structural equivalence.
187 ASTContext &C1, &C2;
188
Douglas Gregor3996e242010-02-15 22:01:00 +0000189 /// \brief The set of "tentative" equivalences between two canonical
190 /// declarations, mapping from a declaration in the first context to the
191 /// declaration in the second context that we believe to be equivalent.
192 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
193
194 /// \brief Queue of declarations in the first context whose equivalence
195 /// with a declaration in the second context still needs to be verified.
196 std::deque<Decl *> DeclsToCheck;
197
Douglas Gregorb4964f72010-02-15 23:54:17 +0000198 /// \brief Declaration (from, to) pairs that are known not to be equivalent
199 /// (which we have already complained about).
200 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
201
Douglas Gregor3996e242010-02-15 22:01:00 +0000202 /// \brief Whether we're being strict about the spelling of types when
203 /// unifying two types.
204 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000205
206 /// \brief Whether to complain about failures.
207 bool Complain;
208
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000209 /// \brief \c true if the last diagnostic came from C2.
210 bool LastDiagFromC2;
211
Douglas Gregor3996e242010-02-15 22:01:00 +0000212 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000213 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000214 bool StrictTypeSpelling = false,
215 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000216 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000217 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
218 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000219
220 /// \brief Determine whether the two declarations are structurally
221 /// equivalent.
222 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
223
224 /// \brief Determine whether the two types are structurally equivalent.
225 bool IsStructurallyEquivalent(QualType T1, QualType T2);
226
227 private:
228 /// \brief Finish checking all of the structural equivalences.
229 ///
230 /// \returns true if an error occurred, false otherwise.
231 bool Finish();
232
233 public:
234 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000235 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000236 if (LastDiagFromC2)
237 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
238 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000239 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000240 }
241
242 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000243 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000244 if (!LastDiagFromC2)
245 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
246 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000247 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000248 }
249 };
250}
251
252static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
253 QualType T1, QualType T2);
254static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
255 Decl *D1, Decl *D2);
256
Douglas Gregor3996e242010-02-15 22:01:00 +0000257/// \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 Gregore2e50d332010-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());
Eli Friedmanb826a002012-09-26 02:36:12 +0000297
Douglas Gregore2e50d332010-12-01 01:36:18 +0000298 case TemplateArgument::Integral:
299 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
300 Arg2.getIntegralType()))
301 return false;
302
Eric Christopher6dcc3762012-07-15 00:23:57 +0000303 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000304
305 case TemplateArgument::Declaration:
306 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000307
308 case TemplateArgument::NullPtr:
309 return true; // FIXME: Is this correct?
310
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311 case TemplateArgument::Template:
312 return IsStructurallyEquivalent(Context,
313 Arg1.getAsTemplate(),
314 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000315
316 case TemplateArgument::TemplateExpansion:
317 return IsStructurallyEquivalent(Context,
318 Arg1.getAsTemplateOrTemplatePattern(),
319 Arg2.getAsTemplateOrTemplatePattern());
320
Douglas Gregore2e50d332010-12-01 01:36:18 +0000321 case TemplateArgument::Expression:
322 return IsStructurallyEquivalent(Context,
323 Arg1.getAsExpr(), Arg2.getAsExpr());
324
325 case TemplateArgument::Pack:
326 if (Arg1.pack_size() != Arg2.pack_size())
327 return false;
328
329 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
330 if (!IsStructurallyEquivalent(Context,
331 Arg1.pack_begin()[I],
332 Arg2.pack_begin()[I]))
333 return false;
334
335 return true;
336 }
337
338 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000339}
340
341/// \brief Determine structural equivalence for the common part of array
342/// types.
343static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
344 const ArrayType *Array1,
345 const ArrayType *Array2) {
346 if (!IsStructurallyEquivalent(Context,
347 Array1->getElementType(),
348 Array2->getElementType()))
349 return false;
350 if (Array1->getSizeModifier() != Array2->getSizeModifier())
351 return false;
352 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
353 return false;
354
355 return true;
356}
357
358/// \brief Determine structural equivalence of two types.
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 QualType T1, QualType T2) {
361 if (T1.isNull() || T2.isNull())
362 return T1.isNull() && T2.isNull();
363
364 if (!Context.StrictTypeSpelling) {
365 // We aren't being strict about token-to-token equivalence of types,
366 // so map down to the canonical type.
367 T1 = Context.C1.getCanonicalType(T1);
368 T2 = Context.C2.getCanonicalType(T2);
369 }
370
371 if (T1.getQualifiers() != T2.getQualifiers())
372 return false;
373
Douglas Gregorb4964f72010-02-15 23:54:17 +0000374 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000375
Douglas Gregorb4964f72010-02-15 23:54:17 +0000376 if (T1->getTypeClass() != T2->getTypeClass()) {
377 // Compare function types with prototypes vs. without prototypes as if
378 // both did not have prototypes.
379 if (T1->getTypeClass() == Type::FunctionProto &&
380 T2->getTypeClass() == Type::FunctionNoProto)
381 TC = Type::FunctionNoProto;
382 else if (T1->getTypeClass() == Type::FunctionNoProto &&
383 T2->getTypeClass() == Type::FunctionProto)
384 TC = Type::FunctionNoProto;
385 else
386 return false;
387 }
388
389 switch (TC) {
390 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000391 // FIXME: Deal with Char_S/Char_U.
392 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
393 return false;
394 break;
395
396 case Type::Complex:
397 if (!IsStructurallyEquivalent(Context,
398 cast<ComplexType>(T1)->getElementType(),
399 cast<ComplexType>(T2)->getElementType()))
400 return false;
401 break;
402
403 case Type::Pointer:
404 if (!IsStructurallyEquivalent(Context,
405 cast<PointerType>(T1)->getPointeeType(),
406 cast<PointerType>(T2)->getPointeeType()))
407 return false;
408 break;
409
410 case Type::BlockPointer:
411 if (!IsStructurallyEquivalent(Context,
412 cast<BlockPointerType>(T1)->getPointeeType(),
413 cast<BlockPointerType>(T2)->getPointeeType()))
414 return false;
415 break;
416
417 case Type::LValueReference:
418 case Type::RValueReference: {
419 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
420 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
421 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
422 return false;
423 if (Ref1->isInnerRef() != Ref2->isInnerRef())
424 return false;
425 if (!IsStructurallyEquivalent(Context,
426 Ref1->getPointeeTypeAsWritten(),
427 Ref2->getPointeeTypeAsWritten()))
428 return false;
429 break;
430 }
431
432 case Type::MemberPointer: {
433 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
434 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
435 if (!IsStructurallyEquivalent(Context,
436 MemPtr1->getPointeeType(),
437 MemPtr2->getPointeeType()))
438 return false;
439 if (!IsStructurallyEquivalent(Context,
440 QualType(MemPtr1->getClass(), 0),
441 QualType(MemPtr2->getClass(), 0)))
442 return false;
443 break;
444 }
445
446 case Type::ConstantArray: {
447 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
448 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000449 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000450 return false;
451
452 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
453 return false;
454 break;
455 }
456
457 case Type::IncompleteArray:
458 if (!IsArrayStructurallyEquivalent(Context,
459 cast<ArrayType>(T1),
460 cast<ArrayType>(T2)))
461 return false;
462 break;
463
464 case Type::VariableArray: {
465 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
466 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
467 if (!IsStructurallyEquivalent(Context,
468 Array1->getSizeExpr(), Array2->getSizeExpr()))
469 return false;
470
471 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
472 return false;
473
474 break;
475 }
476
477 case Type::DependentSizedArray: {
478 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
479 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
480 if (!IsStructurallyEquivalent(Context,
481 Array1->getSizeExpr(), Array2->getSizeExpr()))
482 return false;
483
484 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
485 return false;
486
487 break;
488 }
489
490 case Type::DependentSizedExtVector: {
491 const DependentSizedExtVectorType *Vec1
492 = cast<DependentSizedExtVectorType>(T1);
493 const DependentSizedExtVectorType *Vec2
494 = cast<DependentSizedExtVectorType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
497 return false;
498 if (!IsStructurallyEquivalent(Context,
499 Vec1->getElementType(),
500 Vec2->getElementType()))
501 return false;
502 break;
503 }
504
505 case Type::Vector:
506 case Type::ExtVector: {
507 const VectorType *Vec1 = cast<VectorType>(T1);
508 const VectorType *Vec2 = cast<VectorType>(T2);
509 if (!IsStructurallyEquivalent(Context,
510 Vec1->getElementType(),
511 Vec2->getElementType()))
512 return false;
513 if (Vec1->getNumElements() != Vec2->getNumElements())
514 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000515 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000516 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000517 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000518 }
519
520 case Type::FunctionProto: {
521 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
522 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
523 if (Proto1->getNumArgs() != Proto2->getNumArgs())
524 return false;
525 for (unsigned I = 0, N = Proto1->getNumArgs(); I != N; ++I) {
526 if (!IsStructurallyEquivalent(Context,
527 Proto1->getArgType(I),
528 Proto2->getArgType(I)))
529 return false;
530 }
531 if (Proto1->isVariadic() != Proto2->isVariadic())
532 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000533 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000534 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000535 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
536 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
537 return false;
538 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
539 if (!IsStructurallyEquivalent(Context,
540 Proto1->getExceptionType(I),
541 Proto2->getExceptionType(I)))
542 return false;
543 }
544 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000545 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000546 Proto1->getNoexceptExpr(),
547 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000548 return false;
549 }
550 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
551 return false;
552
553 // Fall through to check the bits common with FunctionNoProtoType.
554 }
555
556 case Type::FunctionNoProto: {
557 const FunctionType *Function1 = cast<FunctionType>(T1);
558 const FunctionType *Function2 = cast<FunctionType>(T2);
559 if (!IsStructurallyEquivalent(Context,
560 Function1->getResultType(),
561 Function2->getResultType()))
562 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000563 if (Function1->getExtInfo() != Function2->getExtInfo())
564 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000565 break;
566 }
567
568 case Type::UnresolvedUsing:
569 if (!IsStructurallyEquivalent(Context,
570 cast<UnresolvedUsingType>(T1)->getDecl(),
571 cast<UnresolvedUsingType>(T2)->getDecl()))
572 return false;
573
574 break;
John McCall81904512011-01-06 01:58:22 +0000575
576 case Type::Attributed:
577 if (!IsStructurallyEquivalent(Context,
578 cast<AttributedType>(T1)->getModifiedType(),
579 cast<AttributedType>(T2)->getModifiedType()))
580 return false;
581 if (!IsStructurallyEquivalent(Context,
582 cast<AttributedType>(T1)->getEquivalentType(),
583 cast<AttributedType>(T2)->getEquivalentType()))
584 return false;
585 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000586
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000587 case Type::Paren:
588 if (!IsStructurallyEquivalent(Context,
589 cast<ParenType>(T1)->getInnerType(),
590 cast<ParenType>(T2)->getInnerType()))
591 return false;
592 break;
593
Douglas Gregor3996e242010-02-15 22:01:00 +0000594 case Type::Typedef:
595 if (!IsStructurallyEquivalent(Context,
596 cast<TypedefType>(T1)->getDecl(),
597 cast<TypedefType>(T2)->getDecl()))
598 return false;
599 break;
600
601 case Type::TypeOfExpr:
602 if (!IsStructurallyEquivalent(Context,
603 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
604 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
605 return false;
606 break;
607
608 case Type::TypeOf:
609 if (!IsStructurallyEquivalent(Context,
610 cast<TypeOfType>(T1)->getUnderlyingType(),
611 cast<TypeOfType>(T2)->getUnderlyingType()))
612 return false;
613 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000614
615 case Type::UnaryTransform:
616 if (!IsStructurallyEquivalent(Context,
617 cast<UnaryTransformType>(T1)->getUnderlyingType(),
618 cast<UnaryTransformType>(T1)->getUnderlyingType()))
619 return false;
620 break;
621
Douglas Gregor3996e242010-02-15 22:01:00 +0000622 case Type::Decltype:
623 if (!IsStructurallyEquivalent(Context,
624 cast<DecltypeType>(T1)->getUnderlyingExpr(),
625 cast<DecltypeType>(T2)->getUnderlyingExpr()))
626 return false;
627 break;
628
Richard Smith30482bc2011-02-20 03:19:35 +0000629 case Type::Auto:
630 if (!IsStructurallyEquivalent(Context,
631 cast<AutoType>(T1)->getDeducedType(),
632 cast<AutoType>(T2)->getDeducedType()))
633 return false;
634 break;
635
Douglas Gregor3996e242010-02-15 22:01:00 +0000636 case Type::Record:
637 case Type::Enum:
638 if (!IsStructurallyEquivalent(Context,
639 cast<TagType>(T1)->getDecl(),
640 cast<TagType>(T2)->getDecl()))
641 return false;
642 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000643
Douglas Gregor3996e242010-02-15 22:01:00 +0000644 case Type::TemplateTypeParm: {
645 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
646 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
647 if (Parm1->getDepth() != Parm2->getDepth())
648 return false;
649 if (Parm1->getIndex() != Parm2->getIndex())
650 return false;
651 if (Parm1->isParameterPack() != Parm2->isParameterPack())
652 return false;
653
654 // Names of template type parameters are never significant.
655 break;
656 }
657
658 case Type::SubstTemplateTypeParm: {
659 const SubstTemplateTypeParmType *Subst1
660 = cast<SubstTemplateTypeParmType>(T1);
661 const SubstTemplateTypeParmType *Subst2
662 = cast<SubstTemplateTypeParmType>(T2);
663 if (!IsStructurallyEquivalent(Context,
664 QualType(Subst1->getReplacedParameter(), 0),
665 QualType(Subst2->getReplacedParameter(), 0)))
666 return false;
667 if (!IsStructurallyEquivalent(Context,
668 Subst1->getReplacementType(),
669 Subst2->getReplacementType()))
670 return false;
671 break;
672 }
673
Douglas Gregorfb322d82011-01-14 05:11:40 +0000674 case Type::SubstTemplateTypeParmPack: {
675 const SubstTemplateTypeParmPackType *Subst1
676 = cast<SubstTemplateTypeParmPackType>(T1);
677 const SubstTemplateTypeParmPackType *Subst2
678 = cast<SubstTemplateTypeParmPackType>(T2);
679 if (!IsStructurallyEquivalent(Context,
680 QualType(Subst1->getReplacedParameter(), 0),
681 QualType(Subst2->getReplacedParameter(), 0)))
682 return false;
683 if (!IsStructurallyEquivalent(Context,
684 Subst1->getArgumentPack(),
685 Subst2->getArgumentPack()))
686 return false;
687 break;
688 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000689 case Type::TemplateSpecialization: {
690 const TemplateSpecializationType *Spec1
691 = cast<TemplateSpecializationType>(T1);
692 const TemplateSpecializationType *Spec2
693 = cast<TemplateSpecializationType>(T2);
694 if (!IsStructurallyEquivalent(Context,
695 Spec1->getTemplateName(),
696 Spec2->getTemplateName()))
697 return false;
698 if (Spec1->getNumArgs() != Spec2->getNumArgs())
699 return false;
700 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
701 if (!IsStructurallyEquivalent(Context,
702 Spec1->getArg(I), Spec2->getArg(I)))
703 return false;
704 }
705 break;
706 }
707
Abramo Bagnara6150c882010-05-11 21:36:43 +0000708 case Type::Elaborated: {
709 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
710 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
711 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
712 if (Elab1->getKeyword() != Elab2->getKeyword())
713 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000714 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000715 Elab1->getQualifier(),
716 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000717 return false;
718 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000719 Elab1->getNamedType(),
720 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000721 return false;
722 break;
723 }
724
John McCalle78aac42010-03-10 03:28:59 +0000725 case Type::InjectedClassName: {
726 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
727 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
728 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000729 Inj1->getInjectedSpecializationType(),
730 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000731 return false;
732 break;
733 }
734
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000735 case Type::DependentName: {
736 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
737 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000738 if (!IsStructurallyEquivalent(Context,
739 Typename1->getQualifier(),
740 Typename2->getQualifier()))
741 return false;
742 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
743 Typename2->getIdentifier()))
744 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000745
746 break;
747 }
748
John McCallc392f372010-06-11 00:33:02 +0000749 case Type::DependentTemplateSpecialization: {
750 const DependentTemplateSpecializationType *Spec1 =
751 cast<DependentTemplateSpecializationType>(T1);
752 const DependentTemplateSpecializationType *Spec2 =
753 cast<DependentTemplateSpecializationType>(T2);
754 if (!IsStructurallyEquivalent(Context,
755 Spec1->getQualifier(),
756 Spec2->getQualifier()))
757 return false;
758 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
759 Spec2->getIdentifier()))
760 return false;
761 if (Spec1->getNumArgs() != Spec2->getNumArgs())
762 return false;
763 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
764 if (!IsStructurallyEquivalent(Context,
765 Spec1->getArg(I), Spec2->getArg(I)))
766 return false;
767 }
768 break;
769 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000770
771 case Type::PackExpansion:
772 if (!IsStructurallyEquivalent(Context,
773 cast<PackExpansionType>(T1)->getPattern(),
774 cast<PackExpansionType>(T2)->getPattern()))
775 return false;
776 break;
777
Douglas Gregor3996e242010-02-15 22:01:00 +0000778 case Type::ObjCInterface: {
779 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
780 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
781 if (!IsStructurallyEquivalent(Context,
782 Iface1->getDecl(), Iface2->getDecl()))
783 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000784 break;
785 }
786
787 case Type::ObjCObject: {
788 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
789 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
790 if (!IsStructurallyEquivalent(Context,
791 Obj1->getBaseType(),
792 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000793 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000794 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
795 return false;
796 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000797 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000798 Obj1->getProtocol(I),
799 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000800 return false;
801 }
802 break;
803 }
804
805 case Type::ObjCObjectPointer: {
806 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
807 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
808 if (!IsStructurallyEquivalent(Context,
809 Ptr1->getPointeeType(),
810 Ptr2->getPointeeType()))
811 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000812 break;
813 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000814
815 case Type::Atomic: {
816 if (!IsStructurallyEquivalent(Context,
817 cast<AtomicType>(T1)->getValueType(),
818 cast<AtomicType>(T2)->getValueType()))
819 return false;
820 break;
821 }
822
Douglas Gregor3996e242010-02-15 22:01:00 +0000823 } // end switch
824
825 return true;
826}
827
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000828/// \brief Determine structural equivalence of two fields.
829static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
830 FieldDecl *Field1, FieldDecl *Field2) {
831 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000832
833 // For anonymous structs/unions, match up the anonymous struct/union type
834 // declarations directly, so that we don't go off searching for anonymous
835 // types
836 if (Field1->isAnonymousStructOrUnion() &&
837 Field2->isAnonymousStructOrUnion()) {
838 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
839 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
840 return IsStructurallyEquivalent(Context, D1, D2);
841 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000842
843 // Check for equivalent field names.
844 IdentifierInfo *Name1 = Field1->getIdentifier();
845 IdentifierInfo *Name2 = Field2->getIdentifier();
846 if (!::IsStructurallyEquivalent(Name1, Name2))
847 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000848
849 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000850 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000851 if (Context.Complain) {
852 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
853 << Context.C2.getTypeDeclType(Owner2);
854 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
855 << Field2->getDeclName() << Field2->getType();
856 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
857 << Field1->getDeclName() << Field1->getType();
858 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000859 return false;
860 }
861
862 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000863 if (Context.Complain) {
864 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
865 << Context.C2.getTypeDeclType(Owner2);
866 if (Field1->isBitField()) {
867 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
868 << Field1->getDeclName() << Field1->getType()
869 << Field1->getBitWidthValue(Context.C1);
870 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
871 << Field2->getDeclName();
872 } else {
873 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
874 << Field2->getDeclName() << Field2->getType()
875 << Field2->getBitWidthValue(Context.C2);
876 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
877 << Field1->getDeclName();
878 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000879 }
880 return false;
881 }
882
883 if (Field1->isBitField()) {
884 // Make sure that the bit-fields are the same length.
885 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
886 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
887
888 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000889 if (Context.Complain) {
890 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
891 << Context.C2.getTypeDeclType(Owner2);
892 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
893 << Field2->getDeclName() << Field2->getType() << Bits2;
894 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
895 << Field1->getDeclName() << Field1->getType() << Bits1;
896 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000897 return false;
898 }
899 }
900
901 return true;
902}
903
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000904/// \brief Find the index of the given anonymous struct/union within its
905/// context.
906///
907/// \returns Returns the index of this anonymous struct/union in its context,
908/// including the next assigned index (if none of them match). Returns an
909/// empty option if the context is not a record, i.e.. if the anonymous
910/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000911static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000912 ASTContext &Context = Anon->getASTContext();
913 QualType AnonTy = Context.getRecordType(Anon);
914
915 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
916 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000917 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000918
919 unsigned Index = 0;
920 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
921 DEnd = Owner->noload_decls_end();
922 D != DEnd; ++D) {
923 FieldDecl *F = dyn_cast<FieldDecl>(*D);
924 if (!F || !F->isAnonymousStructOrUnion())
925 continue;
926
927 if (Context.hasSameType(F->getType(), AnonTy))
928 break;
929
930 ++Index;
931 }
932
933 return Index;
934}
935
Douglas Gregor3996e242010-02-15 22:01:00 +0000936/// \brief Determine structural equivalence of two records.
937static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
938 RecordDecl *D1, RecordDecl *D2) {
939 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000940 if (Context.Complain) {
941 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
942 << Context.C2.getTypeDeclType(D2);
943 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
944 << D1->getDeclName() << (unsigned)D1->getTagKind();
945 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000946 return false;
947 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000948
949 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
950 // If both anonymous structs/unions are in a record context, make sure
951 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +0000952 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
953 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000954 if (*Index1 != *Index2)
955 return false;
956 }
957 }
958 }
959
Douglas Gregore2e50d332010-12-01 01:36:18 +0000960 // If both declarations are class template specializations, we know
961 // the ODR applies, so check the template and template arguments.
962 ClassTemplateSpecializationDecl *Spec1
963 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
964 ClassTemplateSpecializationDecl *Spec2
965 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
966 if (Spec1 && Spec2) {
967 // Check that the specialized templates are the same.
968 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
969 Spec2->getSpecializedTemplate()))
970 return false;
971
972 // Check that the template arguments are the same.
973 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
974 return false;
975
976 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
977 if (!IsStructurallyEquivalent(Context,
978 Spec1->getTemplateArgs().get(I),
979 Spec2->getTemplateArgs().get(I)))
980 return false;
981 }
982 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000983 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000984 else if (Spec1 || Spec2)
985 return false;
986
Douglas Gregorb4964f72010-02-15 23:54:17 +0000987 // Compare the definitions of these two records. If either or both are
988 // incomplete, we assume that they are equivalent.
989 D1 = D1->getDefinition();
990 D2 = D2->getDefinition();
991 if (!D1 || !D2)
992 return true;
993
Douglas Gregor3996e242010-02-15 22:01:00 +0000994 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
995 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
996 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000997 if (Context.Complain) {
998 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
999 << Context.C2.getTypeDeclType(D2);
1000 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1001 << D2CXX->getNumBases();
1002 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1003 << D1CXX->getNumBases();
1004 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001005 return false;
1006 }
1007
1008 // Check the base classes.
1009 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1010 BaseEnd1 = D1CXX->bases_end(),
1011 Base2 = D2CXX->bases_begin();
1012 Base1 != BaseEnd1;
1013 ++Base1, ++Base2) {
1014 if (!IsStructurallyEquivalent(Context,
1015 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001016 if (Context.Complain) {
1017 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1018 << Context.C2.getTypeDeclType(D2);
1019 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1020 << Base2->getType()
1021 << Base2->getSourceRange();
1022 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1023 << Base1->getType()
1024 << Base1->getSourceRange();
1025 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001026 return false;
1027 }
1028
1029 // Check virtual vs. non-virtual inheritance mismatch.
1030 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001031 if (Context.Complain) {
1032 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1033 << Context.C2.getTypeDeclType(D2);
1034 Context.Diag2(Base2->getLocStart(),
1035 diag::note_odr_virtual_base)
1036 << Base2->isVirtual() << Base2->getSourceRange();
1037 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1038 << Base1->isVirtual()
1039 << Base1->getSourceRange();
1040 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001041 return false;
1042 }
1043 }
1044 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001045 if (Context.Complain) {
1046 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1047 << Context.C2.getTypeDeclType(D2);
1048 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1049 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1050 << Base1->getType()
1051 << Base1->getSourceRange();
1052 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1053 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001054 return false;
1055 }
1056 }
1057
1058 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001059 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001060 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001061 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001062 Field1End = D1->field_end();
1063 Field1 != Field1End;
1064 ++Field1, ++Field2) {
1065 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001066 if (Context.Complain) {
1067 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1068 << Context.C2.getTypeDeclType(D2);
1069 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1070 << Field1->getDeclName() << Field1->getType();
1071 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1072 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001073 return false;
1074 }
1075
David Blaikie40ed2972012-06-06 20:45:41 +00001076 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001077 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001078 }
1079
1080 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001081 if (Context.Complain) {
1082 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1083 << Context.C2.getTypeDeclType(D2);
1084 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1085 << Field2->getDeclName() << Field2->getType();
1086 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1087 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001088 return false;
1089 }
1090
1091 return true;
1092}
1093
1094/// \brief Determine structural equivalence of two enums.
1095static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1096 EnumDecl *D1, EnumDecl *D2) {
1097 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1098 EC2End = D2->enumerator_end();
1099 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1100 EC1End = D1->enumerator_end();
1101 EC1 != EC1End; ++EC1, ++EC2) {
1102 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001103 if (Context.Complain) {
1104 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1105 << Context.C2.getTypeDeclType(D2);
1106 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1107 << EC1->getDeclName()
1108 << EC1->getInitVal().toString(10);
1109 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1110 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001111 return false;
1112 }
1113
1114 llvm::APSInt Val1 = EC1->getInitVal();
1115 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001116 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001117 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001118 if (Context.Complain) {
1119 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1120 << Context.C2.getTypeDeclType(D2);
1121 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1122 << EC2->getDeclName()
1123 << EC2->getInitVal().toString(10);
1124 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1125 << EC1->getDeclName()
1126 << EC1->getInitVal().toString(10);
1127 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001128 return false;
1129 }
1130 }
1131
1132 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001133 if (Context.Complain) {
1134 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1135 << Context.C2.getTypeDeclType(D2);
1136 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1137 << EC2->getDeclName()
1138 << EC2->getInitVal().toString(10);
1139 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1140 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001141 return false;
1142 }
1143
1144 return true;
1145}
Douglas Gregora082a492010-11-30 19:14:50 +00001146
1147static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1148 TemplateParameterList *Params1,
1149 TemplateParameterList *Params2) {
1150 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001151 if (Context.Complain) {
1152 Context.Diag2(Params2->getTemplateLoc(),
1153 diag::err_odr_different_num_template_parameters)
1154 << Params1->size() << Params2->size();
1155 Context.Diag1(Params1->getTemplateLoc(),
1156 diag::note_odr_template_parameter_list);
1157 }
Douglas Gregora082a492010-11-30 19:14:50 +00001158 return false;
1159 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001160
Douglas Gregora082a492010-11-30 19:14:50 +00001161 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1162 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001163 if (Context.Complain) {
1164 Context.Diag2(Params2->getParam(I)->getLocation(),
1165 diag::err_odr_different_template_parameter_kind);
1166 Context.Diag1(Params1->getParam(I)->getLocation(),
1167 diag::note_odr_template_parameter_here);
1168 }
Douglas Gregora082a492010-11-30 19:14:50 +00001169 return false;
1170 }
1171
1172 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1173 Params2->getParam(I))) {
1174
1175 return false;
1176 }
1177 }
1178
1179 return true;
1180}
1181
1182static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1183 TemplateTypeParmDecl *D1,
1184 TemplateTypeParmDecl *D2) {
1185 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001186 if (Context.Complain) {
1187 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1188 << D2->isParameterPack();
1189 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1190 << D1->isParameterPack();
1191 }
Douglas Gregora082a492010-11-30 19:14:50 +00001192 return false;
1193 }
1194
1195 return true;
1196}
1197
1198static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1199 NonTypeTemplateParmDecl *D1,
1200 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001201 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001202 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001203 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1204 << D2->isParameterPack();
1205 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1206 << D1->isParameterPack();
1207 }
Douglas Gregora082a492010-11-30 19:14:50 +00001208 return false;
1209 }
Douglas Gregora082a492010-11-30 19:14:50 +00001210
1211 // Check types.
1212 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001213 if (Context.Complain) {
1214 Context.Diag2(D2->getLocation(),
1215 diag::err_odr_non_type_parameter_type_inconsistent)
1216 << D2->getType() << D1->getType();
1217 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1218 << D1->getType();
1219 }
Douglas Gregora082a492010-11-30 19:14:50 +00001220 return false;
1221 }
1222
1223 return true;
1224}
1225
1226static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1227 TemplateTemplateParmDecl *D1,
1228 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001229 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001230 if (Context.Complain) {
1231 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1232 << D2->isParameterPack();
1233 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1234 << D1->isParameterPack();
1235 }
Douglas Gregora082a492010-11-30 19:14:50 +00001236 return false;
1237 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001238
Douglas Gregora082a492010-11-30 19:14:50 +00001239 // Check template parameter lists.
1240 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1241 D2->getTemplateParameters());
1242}
1243
1244static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1245 ClassTemplateDecl *D1,
1246 ClassTemplateDecl *D2) {
1247 // Check template parameters.
1248 if (!IsStructurallyEquivalent(Context,
1249 D1->getTemplateParameters(),
1250 D2->getTemplateParameters()))
1251 return false;
1252
1253 // Check the templated declaration.
1254 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1255 D2->getTemplatedDecl());
1256}
1257
Douglas Gregor3996e242010-02-15 22:01:00 +00001258/// \brief Determine structural equivalence of two declarations.
1259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1260 Decl *D1, Decl *D2) {
1261 // FIXME: Check for known structural equivalences via a callback of some sort.
1262
Douglas Gregorb4964f72010-02-15 23:54:17 +00001263 // Check whether we already know that these two declarations are not
1264 // structurally equivalent.
1265 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1266 D2->getCanonicalDecl())))
1267 return false;
1268
Douglas Gregor3996e242010-02-15 22:01:00 +00001269 // Determine whether we've already produced a tentative equivalence for D1.
1270 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1271 if (EquivToD1)
1272 return EquivToD1 == D2->getCanonicalDecl();
1273
1274 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1275 EquivToD1 = D2->getCanonicalDecl();
1276 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1277 return true;
1278}
1279
1280bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1281 Decl *D2) {
1282 if (!::IsStructurallyEquivalent(*this, D1, D2))
1283 return false;
1284
1285 return !Finish();
1286}
1287
1288bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1289 QualType T2) {
1290 if (!::IsStructurallyEquivalent(*this, T1, T2))
1291 return false;
1292
1293 return !Finish();
1294}
1295
1296bool StructuralEquivalenceContext::Finish() {
1297 while (!DeclsToCheck.empty()) {
1298 // Check the next declaration.
1299 Decl *D1 = DeclsToCheck.front();
1300 DeclsToCheck.pop_front();
1301
1302 Decl *D2 = TentativeEquivalences[D1];
1303 assert(D2 && "Unrecorded tentative equivalence?");
1304
Douglas Gregorb4964f72010-02-15 23:54:17 +00001305 bool Equivalent = true;
1306
Douglas Gregor3996e242010-02-15 22:01:00 +00001307 // FIXME: Switch on all declaration kinds. For now, we're just going to
1308 // check the obvious ones.
1309 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1310 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1311 // Check for equivalent structure names.
1312 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001313 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1314 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001315 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001316 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1317 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001318 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1319 !::IsStructurallyEquivalent(*this, Record1, Record2))
1320 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001321 } else {
1322 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001323 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001324 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001325 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001326 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1327 // Check for equivalent enum names.
1328 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001329 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1330 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001331 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001332 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1333 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001334 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1335 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1336 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001337 } else {
1338 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001339 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001340 }
Richard Smithdda56e42011-04-15 14:24:37 +00001341 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1342 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001343 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001344 Typedef2->getIdentifier()) ||
1345 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001346 Typedef1->getUnderlyingType(),
1347 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001348 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001349 } else {
1350 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001351 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001352 }
Douglas Gregora082a492010-11-30 19:14:50 +00001353 } else if (ClassTemplateDecl *ClassTemplate1
1354 = dyn_cast<ClassTemplateDecl>(D1)) {
1355 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1356 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1357 ClassTemplate2->getIdentifier()) ||
1358 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1359 Equivalent = false;
1360 } else {
1361 // Class template/non-class-template mismatch.
1362 Equivalent = false;
1363 }
1364 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1365 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1366 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1367 Equivalent = false;
1368 } else {
1369 // Kind mismatch.
1370 Equivalent = false;
1371 }
1372 } else if (NonTypeTemplateParmDecl *NTTP1
1373 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1374 if (NonTypeTemplateParmDecl *NTTP2
1375 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1376 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1377 Equivalent = false;
1378 } else {
1379 // Kind mismatch.
1380 Equivalent = false;
1381 }
1382 } else if (TemplateTemplateParmDecl *TTP1
1383 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1384 if (TemplateTemplateParmDecl *TTP2
1385 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1386 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1387 Equivalent = false;
1388 } else {
1389 // Kind mismatch.
1390 Equivalent = false;
1391 }
1392 }
1393
Douglas Gregorb4964f72010-02-15 23:54:17 +00001394 if (!Equivalent) {
1395 // Note that these two declarations are not equivalent (and we already
1396 // know about it).
1397 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1398 D2->getCanonicalDecl()));
1399 return true;
1400 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001401 // FIXME: Check other declaration kinds!
1402 }
1403
1404 return false;
1405}
1406
1407//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001408// Import Types
1409//----------------------------------------------------------------------------
1410
John McCall424cec92011-01-19 06:33:43 +00001411QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001412 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1413 << T->getTypeClassName();
1414 return QualType();
1415}
1416
John McCall424cec92011-01-19 06:33:43 +00001417QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001418 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001419#define SHARED_SINGLETON_TYPE(Expansion)
1420#define BUILTIN_TYPE(Id, SingletonId) \
1421 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1422#include "clang/AST/BuiltinTypes.def"
1423
1424 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1425 // context supports C++.
1426
1427 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1428 // context supports ObjC.
1429
Douglas Gregor96e578d2010-02-05 17:54:41 +00001430 case BuiltinType::Char_U:
1431 // The context we're importing from has an unsigned 'char'. If we're
1432 // importing into a context with a signed 'char', translate to
1433 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001434 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001435 return Importer.getToContext().UnsignedCharTy;
1436
1437 return Importer.getToContext().CharTy;
1438
Douglas Gregor96e578d2010-02-05 17:54:41 +00001439 case BuiltinType::Char_S:
1440 // The context we're importing from has an unsigned 'char'. If we're
1441 // importing into a context with a signed 'char', translate to
1442 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001443 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001444 return Importer.getToContext().SignedCharTy;
1445
1446 return Importer.getToContext().CharTy;
1447
Chris Lattnerad3467e2010-12-25 23:25:43 +00001448 case BuiltinType::WChar_S:
1449 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001450 // FIXME: If not in C++, shall we translate to the C equivalent of
1451 // wchar_t?
1452 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001453 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001454
1455 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001456}
1457
John McCall424cec92011-01-19 06:33:43 +00001458QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001459 QualType ToElementType = Importer.Import(T->getElementType());
1460 if (ToElementType.isNull())
1461 return QualType();
1462
1463 return Importer.getToContext().getComplexType(ToElementType);
1464}
1465
John McCall424cec92011-01-19 06:33:43 +00001466QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001467 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1468 if (ToPointeeType.isNull())
1469 return QualType();
1470
1471 return Importer.getToContext().getPointerType(ToPointeeType);
1472}
1473
John McCall424cec92011-01-19 06:33:43 +00001474QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001475 // FIXME: Check for blocks support in "to" context.
1476 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1477 if (ToPointeeType.isNull())
1478 return QualType();
1479
1480 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1481}
1482
John McCall424cec92011-01-19 06:33:43 +00001483QualType
1484ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001485 // FIXME: Check for C++ support in "to" context.
1486 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1487 if (ToPointeeType.isNull())
1488 return QualType();
1489
1490 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1491}
1492
John McCall424cec92011-01-19 06:33:43 +00001493QualType
1494ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495 // FIXME: Check for C++0x support in "to" context.
1496 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1497 if (ToPointeeType.isNull())
1498 return QualType();
1499
1500 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1501}
1502
John McCall424cec92011-01-19 06:33:43 +00001503QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504 // FIXME: Check for C++ support in "to" context.
1505 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1506 if (ToPointeeType.isNull())
1507 return QualType();
1508
1509 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1510 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1511 ClassType.getTypePtr());
1512}
1513
John McCall424cec92011-01-19 06:33:43 +00001514QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 QualType ToElementType = Importer.Import(T->getElementType());
1516 if (ToElementType.isNull())
1517 return QualType();
1518
1519 return Importer.getToContext().getConstantArrayType(ToElementType,
1520 T->getSize(),
1521 T->getSizeModifier(),
1522 T->getIndexTypeCVRQualifiers());
1523}
1524
John McCall424cec92011-01-19 06:33:43 +00001525QualType
1526ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001527 QualType ToElementType = Importer.Import(T->getElementType());
1528 if (ToElementType.isNull())
1529 return QualType();
1530
1531 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1532 T->getSizeModifier(),
1533 T->getIndexTypeCVRQualifiers());
1534}
1535
John McCall424cec92011-01-19 06:33:43 +00001536QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001537 QualType ToElementType = Importer.Import(T->getElementType());
1538 if (ToElementType.isNull())
1539 return QualType();
1540
1541 Expr *Size = Importer.Import(T->getSizeExpr());
1542 if (!Size)
1543 return QualType();
1544
1545 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1546 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1547 T->getSizeModifier(),
1548 T->getIndexTypeCVRQualifiers(),
1549 Brackets);
1550}
1551
John McCall424cec92011-01-19 06:33:43 +00001552QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001553 QualType ToElementType = Importer.Import(T->getElementType());
1554 if (ToElementType.isNull())
1555 return QualType();
1556
1557 return Importer.getToContext().getVectorType(ToElementType,
1558 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001559 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560}
1561
John McCall424cec92011-01-19 06:33:43 +00001562QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001563 QualType ToElementType = Importer.Import(T->getElementType());
1564 if (ToElementType.isNull())
1565 return QualType();
1566
1567 return Importer.getToContext().getExtVectorType(ToElementType,
1568 T->getNumElements());
1569}
1570
John McCall424cec92011-01-19 06:33:43 +00001571QualType
1572ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001573 // FIXME: What happens if we're importing a function without a prototype
1574 // into C++? Should we make it variadic?
1575 QualType ToResultType = Importer.Import(T->getResultType());
1576 if (ToResultType.isNull())
1577 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001578
Douglas Gregor96e578d2010-02-05 17:54:41 +00001579 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001580 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001581}
1582
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001583QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001584 QualType ToResultType = Importer.Import(T->getResultType());
1585 if (ToResultType.isNull())
1586 return QualType();
1587
1588 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001589 SmallVector<QualType, 4> ArgTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001590 for (FunctionProtoType::arg_type_iterator A = T->arg_type_begin(),
1591 AEnd = T->arg_type_end();
1592 A != AEnd; ++A) {
1593 QualType ArgType = Importer.Import(*A);
1594 if (ArgType.isNull())
1595 return QualType();
1596 ArgTypes.push_back(ArgType);
1597 }
1598
1599 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001600 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001601 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1602 EEnd = T->exception_end();
1603 E != EEnd; ++E) {
1604 QualType ExceptionType = Importer.Import(*E);
1605 if (ExceptionType.isNull())
1606 return QualType();
1607 ExceptionTypes.push_back(ExceptionType);
1608 }
John McCalldb40c7f2010-12-14 08:05:40 +00001609
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001610 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1611 FunctionProtoType::ExtProtoInfo ToEPI;
1612
1613 ToEPI.ExtInfo = FromEPI.ExtInfo;
1614 ToEPI.Variadic = FromEPI.Variadic;
1615 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1616 ToEPI.TypeQuals = FromEPI.TypeQuals;
1617 ToEPI.RefQualifier = FromEPI.RefQualifier;
1618 ToEPI.NumExceptions = ExceptionTypes.size();
1619 ToEPI.Exceptions = ExceptionTypes.data();
1620 ToEPI.ConsumedArguments = FromEPI.ConsumedArguments;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001621 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1622 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1623 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001624 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001625 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001626 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001627
Jordan Rose5c382722013-03-08 21:51:21 +00001628 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001629}
1630
Sean Callananda6df8a2011-08-11 16:56:07 +00001631QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1632 QualType ToInnerType = Importer.Import(T->getInnerType());
1633 if (ToInnerType.isNull())
1634 return QualType();
1635
1636 return Importer.getToContext().getParenType(ToInnerType);
1637}
1638
John McCall424cec92011-01-19 06:33:43 +00001639QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001640 TypedefNameDecl *ToDecl
1641 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001642 if (!ToDecl)
1643 return QualType();
1644
1645 return Importer.getToContext().getTypeDeclType(ToDecl);
1646}
1647
John McCall424cec92011-01-19 06:33:43 +00001648QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001649 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1650 if (!ToExpr)
1651 return QualType();
1652
1653 return Importer.getToContext().getTypeOfExprType(ToExpr);
1654}
1655
John McCall424cec92011-01-19 06:33:43 +00001656QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1658 if (ToUnderlyingType.isNull())
1659 return QualType();
1660
1661 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1662}
1663
John McCall424cec92011-01-19 06:33:43 +00001664QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001665 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001666 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1667 if (!ToExpr)
1668 return QualType();
1669
Douglas Gregor81495f32012-02-12 18:42:33 +00001670 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1671 if (UnderlyingType.isNull())
1672 return QualType();
1673
1674 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001675}
1676
Alexis Hunte852b102011-05-24 22:41:36 +00001677QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1678 QualType ToBaseType = Importer.Import(T->getBaseType());
1679 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1680 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1681 return QualType();
1682
1683 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1684 ToUnderlyingType,
1685 T->getUTTKind());
1686}
1687
Richard Smith30482bc2011-02-20 03:19:35 +00001688QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001689 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001690 QualType FromDeduced = T->getDeducedType();
1691 QualType ToDeduced;
1692 if (!FromDeduced.isNull()) {
1693 ToDeduced = Importer.Import(FromDeduced);
1694 if (ToDeduced.isNull())
1695 return QualType();
1696 }
1697
Richard Smith74aeef52013-04-26 16:15:35 +00001698 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto());
Richard Smith30482bc2011-02-20 03:19:35 +00001699}
1700
John McCall424cec92011-01-19 06:33:43 +00001701QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 RecordDecl *ToDecl
1703 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1704 if (!ToDecl)
1705 return QualType();
1706
1707 return Importer.getToContext().getTagDeclType(ToDecl);
1708}
1709
John McCall424cec92011-01-19 06:33:43 +00001710QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001711 EnumDecl *ToDecl
1712 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1713 if (!ToDecl)
1714 return QualType();
1715
1716 return Importer.getToContext().getTagDeclType(ToDecl);
1717}
1718
Douglas Gregore2e50d332010-12-01 01:36:18 +00001719QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001720 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001721 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1722 if (ToTemplate.isNull())
1723 return QualType();
1724
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001725 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001726 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1727 return QualType();
1728
1729 QualType ToCanonType;
1730 if (!QualType(T, 0).isCanonical()) {
1731 QualType FromCanonType
1732 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1733 ToCanonType =Importer.Import(FromCanonType);
1734 if (ToCanonType.isNull())
1735 return QualType();
1736 }
1737 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1738 ToTemplateArgs.data(),
1739 ToTemplateArgs.size(),
1740 ToCanonType);
1741}
1742
John McCall424cec92011-01-19 06:33:43 +00001743QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001744 NestedNameSpecifier *ToQualifier = 0;
1745 // Note: the qualifier in an ElaboratedType is optional.
1746 if (T->getQualifier()) {
1747 ToQualifier = Importer.Import(T->getQualifier());
1748 if (!ToQualifier)
1749 return QualType();
1750 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001751
1752 QualType ToNamedType = Importer.Import(T->getNamedType());
1753 if (ToNamedType.isNull())
1754 return QualType();
1755
Abramo Bagnara6150c882010-05-11 21:36:43 +00001756 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1757 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758}
1759
John McCall424cec92011-01-19 06:33:43 +00001760QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001761 ObjCInterfaceDecl *Class
1762 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1763 if (!Class)
1764 return QualType();
1765
John McCall8b07ec22010-05-15 11:32:37 +00001766 return Importer.getToContext().getObjCInterfaceType(Class);
1767}
1768
John McCall424cec92011-01-19 06:33:43 +00001769QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001770 QualType ToBaseType = Importer.Import(T->getBaseType());
1771 if (ToBaseType.isNull())
1772 return QualType();
1773
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001774 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001775 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001776 PEnd = T->qual_end();
1777 P != PEnd; ++P) {
1778 ObjCProtocolDecl *Protocol
1779 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1780 if (!Protocol)
1781 return QualType();
1782 Protocols.push_back(Protocol);
1783 }
1784
John McCall8b07ec22010-05-15 11:32:37 +00001785 return Importer.getToContext().getObjCObjectType(ToBaseType,
1786 Protocols.data(),
1787 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001788}
1789
John McCall424cec92011-01-19 06:33:43 +00001790QualType
1791ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001792 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1793 if (ToPointeeType.isNull())
1794 return QualType();
1795
John McCall8b07ec22010-05-15 11:32:37 +00001796 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001797}
1798
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001799//----------------------------------------------------------------------------
1800// Import Declarations
1801//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001802bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1803 DeclContext *&LexicalDC,
1804 DeclarationName &Name,
1805 SourceLocation &Loc) {
1806 // Import the context of this declaration.
1807 DC = Importer.ImportContext(D->getDeclContext());
1808 if (!DC)
1809 return true;
1810
1811 LexicalDC = DC;
1812 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1813 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1814 if (!LexicalDC)
1815 return true;
1816 }
1817
1818 // Import the name of this declaration.
1819 Name = Importer.Import(D->getDeclName());
1820 if (D->getDeclName() && !Name)
1821 return true;
1822
1823 // Import the location of this declaration.
1824 Loc = Importer.Import(D->getLocation());
1825 return false;
1826}
1827
Douglas Gregord451ea92011-07-29 23:31:30 +00001828void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1829 if (!FromD)
1830 return;
1831
1832 if (!ToD) {
1833 ToD = Importer.Import(FromD);
1834 if (!ToD)
1835 return;
1836 }
1837
1838 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1839 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001840 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001841 ImportDefinition(FromRecord, ToRecord);
1842 }
1843 }
1844 return;
1845 }
1846
1847 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1848 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1849 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1850 ImportDefinition(FromEnum, ToEnum);
1851 }
1852 }
1853 return;
1854 }
1855}
1856
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001857void
1858ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1859 DeclarationNameInfo& To) {
1860 // NOTE: To.Name and To.Loc are already imported.
1861 // We only have to import To.LocInfo.
1862 switch (To.getName().getNameKind()) {
1863 case DeclarationName::Identifier:
1864 case DeclarationName::ObjCZeroArgSelector:
1865 case DeclarationName::ObjCOneArgSelector:
1866 case DeclarationName::ObjCMultiArgSelector:
1867 case DeclarationName::CXXUsingDirective:
1868 return;
1869
1870 case DeclarationName::CXXOperatorName: {
1871 SourceRange Range = From.getCXXOperatorNameRange();
1872 To.setCXXOperatorNameRange(Importer.Import(Range));
1873 return;
1874 }
1875 case DeclarationName::CXXLiteralOperatorName: {
1876 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1877 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1878 return;
1879 }
1880 case DeclarationName::CXXConstructorName:
1881 case DeclarationName::CXXDestructorName:
1882 case DeclarationName::CXXConversionFunctionName: {
1883 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1884 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1885 return;
1886 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001887 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001888 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001889}
1890
Douglas Gregor2e15c842012-02-01 21:00:38 +00001891void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001892 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001893 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001894 return;
1895 }
1896
Douglas Gregor968d6332010-02-21 18:24:45 +00001897 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1898 FromEnd = FromDC->decls_end();
1899 From != FromEnd;
1900 ++From)
1901 Importer.Import(*From);
1902}
1903
Douglas Gregord451ea92011-07-29 23:31:30 +00001904bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001905 ImportDefinitionKind Kind) {
1906 if (To->getDefinition() || To->isBeingDefined()) {
1907 if (Kind == IDK_Everything)
1908 ImportDeclContext(From, /*ForceImport=*/true);
1909
Douglas Gregore2e50d332010-12-01 01:36:18 +00001910 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001911 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001912
1913 To->startDefinition();
1914
1915 // Add base classes.
1916 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1917 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001918
1919 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1920 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1921 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001922 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001923 ToData.Aggregate = FromData.Aggregate;
1924 ToData.PlainOldData = FromData.PlainOldData;
1925 ToData.Empty = FromData.Empty;
1926 ToData.Polymorphic = FromData.Polymorphic;
1927 ToData.Abstract = FromData.Abstract;
1928 ToData.IsStandardLayout = FromData.IsStandardLayout;
1929 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1930 ToData.HasPrivateFields = FromData.HasPrivateFields;
1931 ToData.HasProtectedFields = FromData.HasProtectedFields;
1932 ToData.HasPublicFields = FromData.HasPublicFields;
1933 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smith561fb152012-02-25 07:33:38 +00001934 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001935 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001936 ToData.HasUninitializedReferenceMember
1937 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00001938 ToData.NeedOverloadResolutionForMoveConstructor
1939 = FromData.NeedOverloadResolutionForMoveConstructor;
1940 ToData.NeedOverloadResolutionForMoveAssignment
1941 = FromData.NeedOverloadResolutionForMoveAssignment;
1942 ToData.NeedOverloadResolutionForDestructor
1943 = FromData.NeedOverloadResolutionForDestructor;
1944 ToData.DefaultedMoveConstructorIsDeleted
1945 = FromData.DefaultedMoveConstructorIsDeleted;
1946 ToData.DefaultedMoveAssignmentIsDeleted
1947 = FromData.DefaultedMoveAssignmentIsDeleted;
1948 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001949 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1950 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001951 ToData.HasConstexprNonCopyMoveConstructor
1952 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001953 ToData.DefaultedDefaultConstructorIsConstexpr
1954 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001955 ToData.HasConstexprDefaultConstructor
1956 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001957 ToData.HasNonLiteralTypeFieldsOrBases
1958 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001959 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001960 ToData.UserProvidedDefaultConstructor
1961 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001962 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00001963 ToData.ImplicitCopyConstructorHasConstParam
1964 = FromData.ImplicitCopyConstructorHasConstParam;
1965 ToData.ImplicitCopyAssignmentHasConstParam
1966 = FromData.ImplicitCopyAssignmentHasConstParam;
1967 ToData.HasDeclaredCopyConstructorWithConstParam
1968 = FromData.HasDeclaredCopyConstructorWithConstParam;
1969 ToData.HasDeclaredCopyAssignmentWithConstParam
1970 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001971 ToData.FailedImplicitMoveConstructor
1972 = FromData.FailedImplicitMoveConstructor;
1973 ToData.FailedImplicitMoveAssignment = FromData.FailedImplicitMoveAssignment;
Richard Smith561fb152012-02-25 07:33:38 +00001974 ToData.IsLambda = FromData.IsLambda;
1975
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001976 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001977 for (CXXRecordDecl::base_class_iterator
1978 Base1 = FromCXX->bases_begin(),
1979 FromBaseEnd = FromCXX->bases_end();
1980 Base1 != FromBaseEnd;
1981 ++Base1) {
1982 QualType T = Importer.Import(Base1->getType());
1983 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001984 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001985
1986 SourceLocation EllipsisLoc;
1987 if (Base1->isPackExpansion())
1988 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00001989
1990 // Ensure that we have a definition for the base.
1991 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
1992
Douglas Gregore2e50d332010-12-01 01:36:18 +00001993 Bases.push_back(
1994 new (Importer.getToContext())
1995 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
1996 Base1->isVirtual(),
1997 Base1->isBaseOfClass(),
1998 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00001999 Importer.Import(Base1->getTypeSourceInfo()),
2000 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002001 }
2002 if (!Bases.empty())
2003 ToCXX->setBases(Bases.data(), Bases.size());
2004 }
2005
Douglas Gregor2e15c842012-02-01 21:00:38 +00002006 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002007 ImportDeclContext(From, /*ForceImport=*/true);
2008
Douglas Gregore2e50d332010-12-01 01:36:18 +00002009 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002010 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002011}
2012
Douglas Gregord451ea92011-07-29 23:31:30 +00002013bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002014 ImportDefinitionKind Kind) {
2015 if (To->getDefinition() || To->isBeingDefined()) {
2016 if (Kind == IDK_Everything)
2017 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002018 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002019 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002020
2021 To->startDefinition();
2022
2023 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2024 if (T.isNull())
2025 return true;
2026
2027 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2028 if (ToPromotionType.isNull())
2029 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002030
2031 if (shouldForceImportDeclContext(Kind))
2032 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002033
2034 // FIXME: we might need to merge the number of positive or negative bits
2035 // if the enumerator lists don't match.
2036 To->completeDefinition(T, ToPromotionType,
2037 From->getNumPositiveBits(),
2038 From->getNumNegativeBits());
2039 return false;
2040}
2041
Douglas Gregora082a492010-11-30 19:14:50 +00002042TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2043 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002044 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002045 ToParams.reserve(Params->size());
2046 for (TemplateParameterList::iterator P = Params->begin(),
2047 PEnd = Params->end();
2048 P != PEnd; ++P) {
2049 Decl *To = Importer.Import(*P);
2050 if (!To)
2051 return 0;
2052
2053 ToParams.push_back(cast<NamedDecl>(To));
2054 }
2055
2056 return TemplateParameterList::Create(Importer.getToContext(),
2057 Importer.Import(Params->getTemplateLoc()),
2058 Importer.Import(Params->getLAngleLoc()),
2059 ToParams.data(), ToParams.size(),
2060 Importer.Import(Params->getRAngleLoc()));
2061}
2062
Douglas Gregore2e50d332010-12-01 01:36:18 +00002063TemplateArgument
2064ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2065 switch (From.getKind()) {
2066 case TemplateArgument::Null:
2067 return TemplateArgument();
2068
2069 case TemplateArgument::Type: {
2070 QualType ToType = Importer.Import(From.getAsType());
2071 if (ToType.isNull())
2072 return TemplateArgument();
2073 return TemplateArgument(ToType);
2074 }
2075
2076 case TemplateArgument::Integral: {
2077 QualType ToType = Importer.Import(From.getIntegralType());
2078 if (ToType.isNull())
2079 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002080 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002081 }
2082
Eli Friedmanb826a002012-09-26 02:36:12 +00002083 case TemplateArgument::Declaration: {
2084 ValueDecl *FromD = From.getAsDecl();
2085 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2086 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002087 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002088 }
2089
2090 case TemplateArgument::NullPtr: {
2091 QualType ToType = Importer.Import(From.getNullPtrType());
2092 if (ToType.isNull())
2093 return TemplateArgument();
2094 return TemplateArgument(ToType, /*isNullPtr*/true);
2095 }
2096
Douglas Gregore2e50d332010-12-01 01:36:18 +00002097 case TemplateArgument::Template: {
2098 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2099 if (ToTemplate.isNull())
2100 return TemplateArgument();
2101
2102 return TemplateArgument(ToTemplate);
2103 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002104
2105 case TemplateArgument::TemplateExpansion: {
2106 TemplateName ToTemplate
2107 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2108 if (ToTemplate.isNull())
2109 return TemplateArgument();
2110
Douglas Gregore1d60df2011-01-14 23:41:42 +00002111 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002112 }
2113
Douglas Gregore2e50d332010-12-01 01:36:18 +00002114 case TemplateArgument::Expression:
2115 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2116 return TemplateArgument(ToExpr);
2117 return TemplateArgument();
2118
2119 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002120 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002121 ToPack.reserve(From.pack_size());
2122 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2123 return TemplateArgument();
2124
2125 TemplateArgument *ToArgs
2126 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2127 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2128 return TemplateArgument(ToArgs, ToPack.size());
2129 }
2130 }
2131
2132 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002133}
2134
2135bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2136 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002137 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002138 for (unsigned I = 0; I != NumFromArgs; ++I) {
2139 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2140 if (To.isNull() && !FromArgs[I].isNull())
2141 return true;
2142
2143 ToArgs.push_back(To);
2144 }
2145
2146 return false;
2147}
2148
Douglas Gregor5c73e912010-02-11 00:48:18 +00002149bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002150 RecordDecl *ToRecord, bool Complain) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002151 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002152 Importer.getToContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002153 Importer.getNonEquivalentDecls(),
2154 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002155 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002156}
2157
Douglas Gregor98c10182010-02-12 22:17:39 +00002158bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002159 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002160 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002161 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002162 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002163}
2164
Douglas Gregor91155082012-11-14 22:29:20 +00002165bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2166 EnumConstantDecl *ToEC)
2167{
2168 const llvm::APSInt &FromVal = FromEC->getInitVal();
2169 const llvm::APSInt &ToVal = ToEC->getInitVal();
2170
2171 return FromVal.isSigned() == ToVal.isSigned() &&
2172 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2173 FromVal == ToVal;
2174}
2175
2176bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002177 ClassTemplateDecl *To) {
2178 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2179 Importer.getToContext(),
2180 Importer.getNonEquivalentDecls());
2181 return Ctx.IsStructurallyEquivalent(From, To);
2182}
2183
Douglas Gregore4c83e42010-02-09 22:48:33 +00002184Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002185 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002186 << D->getDeclKindName();
2187 return 0;
2188}
2189
Sean Callanan65198272011-11-17 23:20:56 +00002190Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2191 TranslationUnitDecl *ToD =
2192 Importer.getToContext().getTranslationUnitDecl();
2193
2194 Importer.Imported(D, ToD);
2195
2196 return ToD;
2197}
2198
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002199Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2200 // Import the major distinguishing characteristics of this namespace.
2201 DeclContext *DC, *LexicalDC;
2202 DeclarationName Name;
2203 SourceLocation Loc;
2204 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2205 return 0;
2206
2207 NamespaceDecl *MergeWithNamespace = 0;
2208 if (!Name) {
2209 // This is an anonymous namespace. Adopt an existing anonymous
2210 // namespace if we can.
2211 // FIXME: Not testable.
2212 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2213 MergeWithNamespace = TU->getAnonymousNamespace();
2214 else
2215 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2216 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002217 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002218 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002219 DC->localUncachedLookup(Name, FoundDecls);
2220 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2221 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002222 continue;
2223
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002224 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002225 MergeWithNamespace = FoundNS;
2226 ConflictingDecls.clear();
2227 break;
2228 }
2229
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002230 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002231 }
2232
2233 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002234 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002235 ConflictingDecls.data(),
2236 ConflictingDecls.size());
2237 }
2238 }
2239
2240 // Create the "to" namespace, if needed.
2241 NamespaceDecl *ToNamespace = MergeWithNamespace;
2242 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002243 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002244 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002245 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002246 Loc, Name.getAsIdentifierInfo(),
2247 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002248 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002249 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002250
2251 // If this is an anonymous namespace, register it as the anonymous
2252 // namespace within its context.
2253 if (!Name) {
2254 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2255 TU->setAnonymousNamespace(ToNamespace);
2256 else
2257 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2258 }
2259 }
2260 Importer.Imported(D, ToNamespace);
2261
2262 ImportDeclContext(D);
2263
2264 return ToNamespace;
2265}
2266
Richard Smithdda56e42011-04-15 14:24:37 +00002267Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002268 // Import the major distinguishing characteristics of this typedef.
2269 DeclContext *DC, *LexicalDC;
2270 DeclarationName Name;
2271 SourceLocation Loc;
2272 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2273 return 0;
2274
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002275 // If this typedef is not in block scope, determine whether we've
2276 // seen a typedef with the same name (that we can merge with) or any
2277 // other entity by that name (which name lookup could conflict with).
2278 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002279 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002280 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002281 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002282 DC->localUncachedLookup(Name, FoundDecls);
2283 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2284 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002285 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002286 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002287 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002288 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2289 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002290 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002291 }
2292
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002293 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002294 }
2295
2296 if (!ConflictingDecls.empty()) {
2297 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2298 ConflictingDecls.data(),
2299 ConflictingDecls.size());
2300 if (!Name)
2301 return 0;
2302 }
2303 }
2304
Douglas Gregorb4964f72010-02-15 23:54:17 +00002305 // Import the underlying type of this typedef;
2306 QualType T = Importer.Import(D->getUnderlyingType());
2307 if (T.isNull())
2308 return 0;
2309
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002310 // Create the new typedef node.
2311 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002312 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002313 TypedefNameDecl *ToTypedef;
2314 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002315 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2316 StartL, Loc,
2317 Name.getAsIdentifierInfo(),
2318 TInfo);
2319 else
Richard Smithdda56e42011-04-15 14:24:37 +00002320 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2321 StartL, Loc,
2322 Name.getAsIdentifierInfo(),
2323 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002324
Douglas Gregordd483172010-02-22 17:42:47 +00002325 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002326 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002327 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002328 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002329
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002330 return ToTypedef;
2331}
2332
Richard Smithdda56e42011-04-15 14:24:37 +00002333Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2334 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2335}
2336
2337Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2338 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2339}
2340
Douglas Gregor98c10182010-02-12 22:17:39 +00002341Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2342 // Import the major distinguishing characteristics of this enum.
2343 DeclContext *DC, *LexicalDC;
2344 DeclarationName Name;
2345 SourceLocation Loc;
2346 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2347 return 0;
2348
2349 // Figure out what enum name we're looking for.
2350 unsigned IDNS = Decl::IDNS_Tag;
2351 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002352 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2353 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002354 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002355 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002356 IDNS |= Decl::IDNS_Ordinary;
2357
2358 // We may already have an enum of the same name; try to find and match it.
2359 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002360 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002361 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002362 DC->localUncachedLookup(SearchName, FoundDecls);
2363 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2364 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002365 continue;
2366
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002367 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002368 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002369 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2370 Found = Tag->getDecl();
2371 }
2372
2373 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002374 if (IsStructuralMatch(D, FoundEnum))
2375 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002376 }
2377
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002378 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002379 }
2380
2381 if (!ConflictingDecls.empty()) {
2382 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2383 ConflictingDecls.data(),
2384 ConflictingDecls.size());
2385 }
2386 }
2387
2388 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002389 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2390 Importer.Import(D->getLocStart()),
2391 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002392 D->isScoped(), D->isScopedUsingClassTag(),
2393 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002394 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002395 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002396 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002397 D2->setLexicalDeclContext(LexicalDC);
2398 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002399 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002400
2401 // Import the integer type.
2402 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2403 if (ToIntegerType.isNull())
2404 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002405 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002406
2407 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002408 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002409 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002410
Douglas Gregor3996e242010-02-15 22:01:00 +00002411 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002412}
2413
Douglas Gregor5c73e912010-02-11 00:48:18 +00002414Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2415 // If this record has a definition in the translation unit we're coming from,
2416 // but this particular declaration is not that definition, import the
2417 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002418 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002419 if (Definition && Definition != D) {
2420 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002421 if (!ImportedDef)
2422 return 0;
2423
2424 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002425 }
2426
2427 // Import the major distinguishing characteristics of this record.
2428 DeclContext *DC, *LexicalDC;
2429 DeclarationName Name;
2430 SourceLocation Loc;
2431 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2432 return 0;
2433
2434 // Figure out what structure name we're looking for.
2435 unsigned IDNS = Decl::IDNS_Tag;
2436 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002437 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2438 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002439 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002440 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002441 IDNS |= Decl::IDNS_Ordinary;
2442
2443 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002444 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002445 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002446 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002447 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002448 DC->localUncachedLookup(SearchName, FoundDecls);
2449 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2450 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002451 continue;
2452
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002453 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002454 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002455 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2456 Found = Tag->getDecl();
2457 }
2458
2459 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002460 if (D->isAnonymousStructOrUnion() &&
2461 FoundRecord->isAnonymousStructOrUnion()) {
2462 // If both anonymous structs/unions are in a record context, make sure
2463 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002464 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002465 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002466 if (Optional<unsigned> Index2 =
2467 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002468 if (*Index1 != *Index2)
2469 continue;
2470 }
2471 }
2472 }
2473
Douglas Gregor25791052010-02-12 00:09:27 +00002474 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002475 if ((SearchName && !D->isCompleteDefinition())
2476 || (D->isCompleteDefinition() &&
2477 D->isAnonymousStructOrUnion()
2478 == FoundDef->isAnonymousStructOrUnion() &&
2479 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002480 // The record types structurally match, or the "from" translation
2481 // unit only had a forward declaration anyway; call it the same
2482 // function.
2483 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002484 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002485 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002486 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002487 // We have a forward declaration of this type, so adopt that forward
2488 // declaration rather than building a new one.
2489 AdoptDecl = FoundRecord;
2490 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002491 } else if (!SearchName) {
2492 continue;
2493 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002494 }
2495
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002496 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002497 }
2498
Douglas Gregordd6006f2012-07-17 21:16:27 +00002499 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002500 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2501 ConflictingDecls.data(),
2502 ConflictingDecls.size());
2503 }
2504 }
2505
2506 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002507 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002508 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002509 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002510 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002511 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002512 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002513 DC, StartLoc, Loc,
2514 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002515 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002516 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002517 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002518 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002519 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002520 }
Douglas Gregor14454802011-02-25 02:25:35 +00002521
2522 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002523 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002524 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002525 if (D->isAnonymousStructOrUnion())
2526 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002527 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002528
Douglas Gregor3996e242010-02-15 22:01:00 +00002529 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002530
Douglas Gregor95d82832012-01-24 18:36:04 +00002531 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002532 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002533
Douglas Gregor3996e242010-02-15 22:01:00 +00002534 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002535}
2536
Douglas Gregor98c10182010-02-12 22:17:39 +00002537Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2538 // Import the major distinguishing characteristics of this enumerator.
2539 DeclContext *DC, *LexicalDC;
2540 DeclarationName Name;
2541 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002542 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002543 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002544
2545 QualType T = Importer.Import(D->getType());
2546 if (T.isNull())
2547 return 0;
2548
Douglas Gregor98c10182010-02-12 22:17:39 +00002549 // Determine whether there are any other declarations with the same name and
2550 // in the same context.
2551 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002552 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002553 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002554 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002555 DC->localUncachedLookup(Name, FoundDecls);
2556 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2557 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002558 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002559
2560 if (EnumConstantDecl *FoundEnumConstant
2561 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2562 if (IsStructuralMatch(D, FoundEnumConstant))
2563 return Importer.Imported(D, FoundEnumConstant);
2564 }
2565
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002566 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002567 }
2568
2569 if (!ConflictingDecls.empty()) {
2570 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2571 ConflictingDecls.data(),
2572 ConflictingDecls.size());
2573 if (!Name)
2574 return 0;
2575 }
2576 }
2577
2578 Expr *Init = Importer.Import(D->getInitExpr());
2579 if (D->getInitExpr() && !Init)
2580 return 0;
2581
2582 EnumConstantDecl *ToEnumerator
2583 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2584 Name.getAsIdentifierInfo(), T,
2585 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002586 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002587 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002588 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002589 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002590 return ToEnumerator;
2591}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002592
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002593Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2594 // Import the major distinguishing characteristics of this function.
2595 DeclContext *DC, *LexicalDC;
2596 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002597 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002598 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002599 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002600
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002601 // Try to find a function in our own ("to") context with the same name, same
2602 // type, and in the same context as the function we're importing.
2603 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002604 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002605 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002606 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002607 DC->localUncachedLookup(Name, FoundDecls);
2608 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2609 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002610 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002611
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002612 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002613 if (FoundFunction->hasExternalFormalLinkage() &&
2614 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002615 if (Importer.IsStructurallyEquivalent(D->getType(),
2616 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002617 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002618 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002619 }
2620
2621 // FIXME: Check for overloading more carefully, e.g., by boosting
2622 // Sema::IsOverload out to the AST library.
2623
2624 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002625 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002626 continue;
2627
2628 // Complain about inconsistent function types.
2629 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002630 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002631 Importer.ToDiag(FoundFunction->getLocation(),
2632 diag::note_odr_value_here)
2633 << FoundFunction->getType();
2634 }
2635 }
2636
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002637 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002638 }
2639
2640 if (!ConflictingDecls.empty()) {
2641 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2642 ConflictingDecls.data(),
2643 ConflictingDecls.size());
2644 if (!Name)
2645 return 0;
2646 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002647 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002648
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002649 DeclarationNameInfo NameInfo(Name, Loc);
2650 // Import additional name location/type info.
2651 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2652
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002653 QualType FromTy = D->getType();
2654 bool usedDifferentExceptionSpec = false;
2655
2656 if (const FunctionProtoType *
2657 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2658 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2659 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2660 // FunctionDecl that we are importing the FunctionProtoType for.
2661 // To avoid an infinite recursion when importing, create the FunctionDecl
2662 // with a simplified function type and update it afterwards.
2663 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2664 FromEPI.NoexceptExpr) {
2665 FunctionProtoType::ExtProtoInfo DefaultEPI;
2666 FromTy = Importer.getFromContext().getFunctionType(
2667 FromFPT->getResultType(),
Jordan Rose5c382722013-03-08 21:51:21 +00002668 ArrayRef<QualType>(FromFPT->arg_type_begin(),
2669 FromFPT->getNumArgs()),
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002670 DefaultEPI);
2671 usedDifferentExceptionSpec = true;
2672 }
2673 }
2674
Douglas Gregorb4964f72010-02-15 23:54:17 +00002675 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002676 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002677 if (T.isNull())
2678 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002679
2680 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002681 SmallVector<ParmVarDecl *, 8> Parameters;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002682 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
2683 P != PEnd; ++P) {
2684 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*P));
2685 if (!ToP)
2686 return 0;
2687
2688 Parameters.push_back(ToP);
2689 }
2690
2691 // Create the imported function.
2692 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002693 FunctionDecl *ToFunction = 0;
2694 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2695 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2696 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002697 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002698 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002699 FromConstructor->isExplicit(),
2700 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002701 D->isImplicit(),
2702 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002703 } else if (isa<CXXDestructorDecl>(D)) {
2704 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2705 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002706 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002707 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002708 D->isInlineSpecified(),
2709 D->isImplicit());
2710 } else if (CXXConversionDecl *FromConversion
2711 = dyn_cast<CXXConversionDecl>(D)) {
2712 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2713 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002714 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002715 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002716 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002717 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002718 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002719 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002720 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2721 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2722 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002723 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002724 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002725 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002726 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002727 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002728 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002729 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002730 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002731 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002732 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002733 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002734 D->hasWrittenPrototype(),
2735 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002736 }
John McCall3e11ebe2010-03-15 10:12:16 +00002737
2738 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002739 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002740 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002741 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002742 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2743 ToFunction->setTrivial(D->isTrivial());
2744 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002745 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002746
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002747 // Set the parameters.
2748 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002749 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002750 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002751 }
David Blaikie9c70e042011-09-21 18:16:56 +00002752 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002753
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002754 if (usedDifferentExceptionSpec) {
2755 // Update FunctionProtoType::ExtProtoInfo.
2756 QualType T = Importer.Import(D->getType());
2757 if (T.isNull())
2758 return 0;
2759 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002760 }
2761
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002762 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002763
2764 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002765 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002766
Douglas Gregor43f54792010-02-17 02:12:47 +00002767 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002768}
2769
Douglas Gregor00eace12010-02-21 18:29:16 +00002770Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2771 return VisitFunctionDecl(D);
2772}
2773
2774Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2775 return VisitCXXMethodDecl(D);
2776}
2777
2778Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2779 return VisitCXXMethodDecl(D);
2780}
2781
2782Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2783 return VisitCXXMethodDecl(D);
2784}
2785
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002786static unsigned getFieldIndex(Decl *F) {
2787 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2788 if (!Owner)
2789 return 0;
2790
2791 unsigned Index = 1;
2792 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
2793 DEnd = Owner->noload_decls_end();
2794 D != DEnd; ++D) {
2795 if (*D == F)
2796 return Index;
2797
2798 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2799 ++Index;
2800 }
2801
2802 return Index;
2803}
2804
Douglas Gregor5c73e912010-02-11 00:48:18 +00002805Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2806 // Import the major distinguishing characteristics of a variable.
2807 DeclContext *DC, *LexicalDC;
2808 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002809 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002810 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2811 return 0;
2812
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002813 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002814 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002815 DC->localUncachedLookup(Name, FoundDecls);
2816 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2817 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002818 // For anonymous fields, match up by index.
2819 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2820 continue;
2821
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002822 if (Importer.IsStructurallyEquivalent(D->getType(),
2823 FoundField->getType())) {
2824 Importer.Imported(D, FoundField);
2825 return FoundField;
2826 }
2827
2828 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2829 << Name << D->getType() << FoundField->getType();
2830 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2831 << FoundField->getType();
2832 return 0;
2833 }
2834 }
2835
Douglas Gregorb4964f72010-02-15 23:54:17 +00002836 // Import the type.
2837 QualType T = Importer.Import(D->getType());
2838 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002839 return 0;
2840
2841 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2842 Expr *BitWidth = Importer.Import(D->getBitWidth());
2843 if (!BitWidth && D->getBitWidth())
2844 return 0;
2845
Abramo Bagnaradff19302011-03-08 08:55:46 +00002846 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2847 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002848 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002849 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002850 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002851 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002852 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002853 if (ToField->hasInClassInitializer())
2854 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002855 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002856 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002857 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002858 return ToField;
2859}
2860
Francois Pichet783dd6e2010-11-21 06:08:52 +00002861Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2862 // Import the major distinguishing characteristics of a variable.
2863 DeclContext *DC, *LexicalDC;
2864 DeclarationName Name;
2865 SourceLocation Loc;
2866 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2867 return 0;
2868
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002869 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002870 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002871 DC->localUncachedLookup(Name, FoundDecls);
2872 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002873 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002874 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002875 // For anonymous indirect fields, match up by index.
2876 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2877 continue;
2878
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002879 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002880 FoundField->getType(),
2881 Name)) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002882 Importer.Imported(D, FoundField);
2883 return FoundField;
2884 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002885
2886 // If there are more anonymous fields to check, continue.
2887 if (!Name && I < N-1)
2888 continue;
2889
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002890 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2891 << Name << D->getType() << FoundField->getType();
2892 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2893 << FoundField->getType();
2894 return 0;
2895 }
2896 }
2897
Francois Pichet783dd6e2010-11-21 06:08:52 +00002898 // Import the type.
2899 QualType T = Importer.Import(D->getType());
2900 if (T.isNull())
2901 return 0;
2902
2903 NamedDecl **NamedChain =
2904 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2905
2906 unsigned i = 0;
2907 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2908 PE = D->chain_end(); PI != PE; ++PI) {
2909 Decl* D = Importer.Import(*PI);
2910 if (!D)
2911 return 0;
2912 NamedChain[i++] = cast<NamedDecl>(D);
2913 }
2914
2915 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2916 Importer.getToContext(), DC,
2917 Loc, Name.getAsIdentifierInfo(), T,
2918 NamedChain, D->getChainingSize());
2919 ToIndirectField->setAccess(D->getAccess());
2920 ToIndirectField->setLexicalDeclContext(LexicalDC);
2921 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002922 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002923 return ToIndirectField;
2924}
2925
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002926Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2927 // Import the major distinguishing characteristics of an ivar.
2928 DeclContext *DC, *LexicalDC;
2929 DeclarationName Name;
2930 SourceLocation Loc;
2931 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2932 return 0;
2933
2934 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002935 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002936 DC->localUncachedLookup(Name, FoundDecls);
2937 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2938 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002939 if (Importer.IsStructurallyEquivalent(D->getType(),
2940 FoundIvar->getType())) {
2941 Importer.Imported(D, FoundIvar);
2942 return FoundIvar;
2943 }
2944
2945 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
2946 << Name << D->getType() << FoundIvar->getType();
2947 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
2948 << FoundIvar->getType();
2949 return 0;
2950 }
2951 }
2952
2953 // Import the type.
2954 QualType T = Importer.Import(D->getType());
2955 if (T.isNull())
2956 return 0;
2957
2958 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2959 Expr *BitWidth = Importer.Import(D->getBitWidth());
2960 if (!BitWidth && D->getBitWidth())
2961 return 0;
2962
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00002963 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
2964 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002965 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002966 Loc, Name.getAsIdentifierInfo(),
2967 T, TInfo, D->getAccessControl(),
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +00002968 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002969 ToIvar->setLexicalDeclContext(LexicalDC);
2970 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00002971 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002972 return ToIvar;
2973
2974}
2975
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002976Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
2977 // Import the major distinguishing characteristics of a variable.
2978 DeclContext *DC, *LexicalDC;
2979 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002980 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002981 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002982 return 0;
2983
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002984 // Try to find a variable in our own ("to") context with the same name and
2985 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00002986 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002987 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002988 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002989 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002990 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002991 DC->localUncachedLookup(Name, FoundDecls);
2992 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2993 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002994 continue;
2995
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002996 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002997 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00002998 if (FoundVar->hasExternalFormalLinkage() &&
2999 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003000 if (Importer.IsStructurallyEquivalent(D->getType(),
3001 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003002 MergeWithVar = FoundVar;
3003 break;
3004 }
3005
Douglas Gregor56521c52010-02-12 17:23:39 +00003006 const ArrayType *FoundArray
3007 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3008 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003009 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003010 if (FoundArray && TArray) {
3011 if (isa<IncompleteArrayType>(FoundArray) &&
3012 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003013 // Import the type.
3014 QualType T = Importer.Import(D->getType());
3015 if (T.isNull())
3016 return 0;
3017
Douglas Gregor56521c52010-02-12 17:23:39 +00003018 FoundVar->setType(T);
3019 MergeWithVar = FoundVar;
3020 break;
3021 } else if (isa<IncompleteArrayType>(TArray) &&
3022 isa<ConstantArrayType>(FoundArray)) {
3023 MergeWithVar = FoundVar;
3024 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003025 }
3026 }
3027
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003028 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003029 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003030 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3031 << FoundVar->getType();
3032 }
3033 }
3034
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003035 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003036 }
3037
3038 if (MergeWithVar) {
3039 // An equivalent variable with external linkage has been found. Link
3040 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003041 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003042
3043 if (VarDecl *DDef = D->getDefinition()) {
3044 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3045 Importer.ToDiag(ExistingDef->getLocation(),
3046 diag::err_odr_variable_multiple_def)
3047 << Name;
3048 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3049 } else {
3050 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003051 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003052 if (DDef->isInitKnownICE()) {
3053 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3054 Eval->CheckedICE = true;
3055 Eval->IsICE = DDef->isInitICE();
3056 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003057 }
3058 }
3059
3060 return MergeWithVar;
3061 }
3062
3063 if (!ConflictingDecls.empty()) {
3064 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3065 ConflictingDecls.data(),
3066 ConflictingDecls.size());
3067 if (!Name)
3068 return 0;
3069 }
3070 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003071
Douglas Gregorb4964f72010-02-15 23:54:17 +00003072 // Import the type.
3073 QualType T = Importer.Import(D->getType());
3074 if (T.isNull())
3075 return 0;
3076
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003077 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003078 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003079 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3080 Importer.Import(D->getInnerLocStart()),
3081 Loc, Name.getAsIdentifierInfo(),
3082 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003083 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003084 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003085 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003086 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003087 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003088 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003089
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003090 // Merge the initializer.
3091 // FIXME: Can we really import any initializer? Alternatively, we could force
3092 // ourselves to import every declaration of a variable and then only use
3093 // getInit() here.
Douglas Gregord5058122010-02-11 01:19:42 +00003094 ToVar->setInit(Importer.Import(const_cast<Expr *>(D->getAnyInitializer())));
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003095
3096 // FIXME: Other bits to merge?
3097
3098 return ToVar;
3099}
3100
Douglas Gregor8b228d72010-02-17 21:22:52 +00003101Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3102 // Parameters are created in the translation unit's context, then moved
3103 // into the function declaration's context afterward.
3104 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3105
3106 // Import the name of this declaration.
3107 DeclarationName Name = Importer.Import(D->getDeclName());
3108 if (D->getDeclName() && !Name)
3109 return 0;
3110
3111 // Import the location of this declaration.
3112 SourceLocation Loc = Importer.Import(D->getLocation());
3113
3114 // Import the parameter's type.
3115 QualType T = Importer.Import(D->getType());
3116 if (T.isNull())
3117 return 0;
3118
3119 // Create the imported parameter.
3120 ImplicitParamDecl *ToParm
3121 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3122 Loc, Name.getAsIdentifierInfo(),
3123 T);
3124 return Importer.Imported(D, ToParm);
3125}
3126
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003127Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3128 // Parameters are created in the translation unit's context, then moved
3129 // into the function declaration's context afterward.
3130 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3131
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003132 // Import the name of this declaration.
3133 DeclarationName Name = Importer.Import(D->getDeclName());
3134 if (D->getDeclName() && !Name)
3135 return 0;
3136
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003137 // Import the location of this declaration.
3138 SourceLocation Loc = Importer.Import(D->getLocation());
3139
3140 // Import the parameter's type.
3141 QualType T = Importer.Import(D->getType());
3142 if (T.isNull())
3143 return 0;
3144
3145 // Create the imported parameter.
3146 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3147 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003148 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003149 Loc, Name.getAsIdentifierInfo(),
3150 T, TInfo, D->getStorageClass(),
3151 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00003152 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003153 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003154}
3155
Douglas Gregor43f54792010-02-17 02:12:47 +00003156Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3157 // Import the major distinguishing characteristics of a method.
3158 DeclContext *DC, *LexicalDC;
3159 DeclarationName Name;
3160 SourceLocation Loc;
3161 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3162 return 0;
3163
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003164 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003165 DC->localUncachedLookup(Name, FoundDecls);
3166 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3167 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003168 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3169 continue;
3170
3171 // Check return types.
3172 if (!Importer.IsStructurallyEquivalent(D->getResultType(),
3173 FoundMethod->getResultType())) {
3174 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
3175 << D->isInstanceMethod() << Name
3176 << D->getResultType() << FoundMethod->getResultType();
3177 Importer.ToDiag(FoundMethod->getLocation(),
3178 diag::note_odr_objc_method_here)
3179 << D->isInstanceMethod() << Name;
3180 return 0;
3181 }
3182
3183 // Check the number of parameters.
3184 if (D->param_size() != FoundMethod->param_size()) {
3185 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3186 << D->isInstanceMethod() << Name
3187 << D->param_size() << FoundMethod->param_size();
3188 Importer.ToDiag(FoundMethod->getLocation(),
3189 diag::note_odr_objc_method_here)
3190 << D->isInstanceMethod() << Name;
3191 return 0;
3192 }
3193
3194 // Check parameter types.
3195 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3196 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3197 P != PEnd; ++P, ++FoundP) {
3198 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3199 (*FoundP)->getType())) {
3200 Importer.FromDiag((*P)->getLocation(),
3201 diag::err_odr_objc_method_param_type_inconsistent)
3202 << D->isInstanceMethod() << Name
3203 << (*P)->getType() << (*FoundP)->getType();
3204 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3205 << (*FoundP)->getType();
3206 return 0;
3207 }
3208 }
3209
3210 // Check variadic/non-variadic.
3211 // Check the number of parameters.
3212 if (D->isVariadic() != FoundMethod->isVariadic()) {
3213 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3214 << D->isInstanceMethod() << Name;
3215 Importer.ToDiag(FoundMethod->getLocation(),
3216 diag::note_odr_objc_method_here)
3217 << D->isInstanceMethod() << Name;
3218 return 0;
3219 }
3220
3221 // FIXME: Any other bits we need to merge?
3222 return Importer.Imported(D, FoundMethod);
3223 }
3224 }
3225
3226 // Import the result type.
3227 QualType ResultTy = Importer.Import(D->getResultType());
3228 if (ResultTy.isNull())
3229 return 0;
3230
Douglas Gregor12852d92010-03-08 14:59:44 +00003231 TypeSourceInfo *ResultTInfo = Importer.Import(D->getResultTypeSourceInfo());
3232
Douglas Gregor43f54792010-02-17 02:12:47 +00003233 ObjCMethodDecl *ToMethod
3234 = ObjCMethodDecl::Create(Importer.getToContext(),
3235 Loc,
3236 Importer.Import(D->getLocEnd()),
3237 Name.getObjCSelector(),
Douglas Gregor12852d92010-03-08 14:59:44 +00003238 ResultTy, ResultTInfo, DC,
Douglas Gregor43f54792010-02-17 02:12:47 +00003239 D->isInstanceMethod(),
3240 D->isVariadic(),
Jordan Rosed01e83a2012-10-10 16:42:25 +00003241 D->isPropertyAccessor(),
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003242 D->isImplicit(),
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003243 D->isDefined(),
Douglas Gregor33823722011-06-11 01:09:30 +00003244 D->getImplementationControl(),
3245 D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003246
3247 // FIXME: When we decide to merge method definitions, we'll need to
3248 // deal with implicit parameters.
3249
3250 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003251 SmallVector<ParmVarDecl *, 5> ToParams;
Douglas Gregor43f54792010-02-17 02:12:47 +00003252 for (ObjCMethodDecl::param_iterator FromP = D->param_begin(),
3253 FromPEnd = D->param_end();
3254 FromP != FromPEnd;
3255 ++FromP) {
3256 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(*FromP));
3257 if (!ToP)
3258 return 0;
3259
3260 ToParams.push_back(ToP);
3261 }
3262
3263 // Set the parameters.
3264 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3265 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003266 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003267 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003268 SmallVector<SourceLocation, 12> SelLocs;
3269 D->getSelectorLocs(SelLocs);
3270 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003271
3272 ToMethod->setLexicalDeclContext(LexicalDC);
3273 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003274 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003275 return ToMethod;
3276}
3277
Douglas Gregor84c51c32010-02-18 01:47:50 +00003278Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3279 // Import the major distinguishing characteristics of a category.
3280 DeclContext *DC, *LexicalDC;
3281 DeclarationName Name;
3282 SourceLocation Loc;
3283 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3284 return 0;
3285
3286 ObjCInterfaceDecl *ToInterface
3287 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3288 if (!ToInterface)
3289 return 0;
3290
3291 // Determine if we've already encountered this category.
3292 ObjCCategoryDecl *MergeWithCategory
3293 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3294 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3295 if (!ToCategory) {
3296 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003297 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003298 Loc,
3299 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003300 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003301 ToInterface,
3302 Importer.Import(D->getIvarLBraceLoc()),
3303 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003304 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003305 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003306 Importer.Imported(D, ToCategory);
3307
Douglas Gregor84c51c32010-02-18 01:47:50 +00003308 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003309 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3310 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003311 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3312 = D->protocol_loc_begin();
3313 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3314 FromProtoEnd = D->protocol_end();
3315 FromProto != FromProtoEnd;
3316 ++FromProto, ++FromProtoLoc) {
3317 ObjCProtocolDecl *ToProto
3318 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3319 if (!ToProto)
3320 return 0;
3321 Protocols.push_back(ToProto);
3322 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3323 }
3324
3325 // FIXME: If we're merging, make sure that the protocol list is the same.
3326 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3327 ProtocolLocs.data(), Importer.getToContext());
3328
3329 } else {
3330 Importer.Imported(D, ToCategory);
3331 }
3332
3333 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003334 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003335
3336 // If we have an implementation, import it as well.
3337 if (D->getImplementation()) {
3338 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003339 = cast_or_null<ObjCCategoryImplDecl>(
3340 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003341 if (!Impl)
3342 return 0;
3343
3344 ToCategory->setImplementation(Impl);
3345 }
3346
3347 return ToCategory;
3348}
3349
Douglas Gregor2aa53772012-01-24 17:42:07 +00003350bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3351 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003352 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003353 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003354 if (shouldForceImportDeclContext(Kind))
3355 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003356 return false;
3357 }
3358
3359 // Start the protocol definition
3360 To->startDefinition();
3361
3362 // Import protocols
3363 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3364 SmallVector<SourceLocation, 4> ProtocolLocs;
3365 ObjCProtocolDecl::protocol_loc_iterator
3366 FromProtoLoc = From->protocol_loc_begin();
3367 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3368 FromProtoEnd = From->protocol_end();
3369 FromProto != FromProtoEnd;
3370 ++FromProto, ++FromProtoLoc) {
3371 ObjCProtocolDecl *ToProto
3372 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3373 if (!ToProto)
3374 return true;
3375 Protocols.push_back(ToProto);
3376 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3377 }
3378
3379 // FIXME: If we're merging, make sure that the protocol list is the same.
3380 To->setProtocolList(Protocols.data(), Protocols.size(),
3381 ProtocolLocs.data(), Importer.getToContext());
3382
Douglas Gregor2e15c842012-02-01 21:00:38 +00003383 if (shouldForceImportDeclContext(Kind)) {
3384 // Import all of the members of this protocol.
3385 ImportDeclContext(From, /*ForceImport=*/true);
3386 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003387 return false;
3388}
3389
Douglas Gregor98d156a2010-02-17 16:12:00 +00003390Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003391 // If this protocol has a definition in the translation unit we're coming
3392 // from, but this particular declaration is not that definition, import the
3393 // definition and map to that.
3394 ObjCProtocolDecl *Definition = D->getDefinition();
3395 if (Definition && Definition != D) {
3396 Decl *ImportedDef = Importer.Import(Definition);
3397 if (!ImportedDef)
3398 return 0;
3399
3400 return Importer.Imported(D, ImportedDef);
3401 }
3402
Douglas Gregor84c51c32010-02-18 01:47:50 +00003403 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003404 DeclContext *DC, *LexicalDC;
3405 DeclarationName Name;
3406 SourceLocation Loc;
3407 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3408 return 0;
3409
3410 ObjCProtocolDecl *MergeWithProtocol = 0;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003411 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003412 DC->localUncachedLookup(Name, FoundDecls);
3413 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3414 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003415 continue;
3416
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003417 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003418 break;
3419 }
3420
3421 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003422 if (!ToProto) {
3423 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3424 Name.getAsIdentifierInfo(), Loc,
3425 Importer.Import(D->getAtStartLoc()),
3426 /*PrevDecl=*/0);
3427 ToProto->setLexicalDeclContext(LexicalDC);
3428 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003429 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003430
3431 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003432
Douglas Gregor2aa53772012-01-24 17:42:07 +00003433 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3434 return 0;
3435
Douglas Gregor98d156a2010-02-17 16:12:00 +00003436 return ToProto;
3437}
3438
Douglas Gregor2aa53772012-01-24 17:42:07 +00003439bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3440 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003441 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003442 if (To->getDefinition()) {
3443 // Check consistency of superclass.
3444 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3445 if (FromSuper) {
3446 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3447 if (!FromSuper)
3448 return true;
3449 }
3450
3451 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3452 if ((bool)FromSuper != (bool)ToSuper ||
3453 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3454 Importer.ToDiag(To->getLocation(),
3455 diag::err_odr_objc_superclass_inconsistent)
3456 << To->getDeclName();
3457 if (ToSuper)
3458 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3459 << To->getSuperClass()->getDeclName();
3460 else
3461 Importer.ToDiag(To->getLocation(),
3462 diag::note_odr_objc_missing_superclass);
3463 if (From->getSuperClass())
3464 Importer.FromDiag(From->getSuperClassLoc(),
3465 diag::note_odr_objc_superclass)
3466 << From->getSuperClass()->getDeclName();
3467 else
3468 Importer.FromDiag(From->getLocation(),
3469 diag::note_odr_objc_missing_superclass);
3470 }
3471
Douglas Gregor2e15c842012-02-01 21:00:38 +00003472 if (shouldForceImportDeclContext(Kind))
3473 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003474 return false;
3475 }
3476
3477 // Start the definition.
3478 To->startDefinition();
3479
3480 // If this class has a superclass, import it.
3481 if (From->getSuperClass()) {
3482 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3483 Importer.Import(From->getSuperClass()));
3484 if (!Super)
3485 return true;
3486
3487 To->setSuperClass(Super);
3488 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3489 }
3490
3491 // Import protocols
3492 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3493 SmallVector<SourceLocation, 4> ProtocolLocs;
3494 ObjCInterfaceDecl::protocol_loc_iterator
3495 FromProtoLoc = From->protocol_loc_begin();
3496
3497 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3498 FromProtoEnd = From->protocol_end();
3499 FromProto != FromProtoEnd;
3500 ++FromProto, ++FromProtoLoc) {
3501 ObjCProtocolDecl *ToProto
3502 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3503 if (!ToProto)
3504 return true;
3505 Protocols.push_back(ToProto);
3506 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3507 }
3508
3509 // FIXME: If we're merging, make sure that the protocol list is the same.
3510 To->setProtocolList(Protocols.data(), Protocols.size(),
3511 ProtocolLocs.data(), Importer.getToContext());
3512
3513 // Import categories. When the categories themselves are imported, they'll
3514 // hook themselves into this interface.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003515 for (ObjCInterfaceDecl::known_categories_iterator
3516 Cat = From->known_categories_begin(),
3517 CatEnd = From->known_categories_end();
3518 Cat != CatEnd; ++Cat) {
3519 Importer.Import(*Cat);
3520 }
3521
Douglas Gregor2aa53772012-01-24 17:42:07 +00003522 // If we have an @implementation, import it as well.
3523 if (From->getImplementation()) {
3524 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3525 Importer.Import(From->getImplementation()));
3526 if (!Impl)
3527 return true;
3528
3529 To->setImplementation(Impl);
3530 }
3531
Douglas Gregor2e15c842012-02-01 21:00:38 +00003532 if (shouldForceImportDeclContext(Kind)) {
3533 // Import all of the members of this class.
3534 ImportDeclContext(From, /*ForceImport=*/true);
3535 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003536 return false;
3537}
3538
Douglas Gregor45635322010-02-16 01:20:57 +00003539Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003540 // If this class has a definition in the translation unit we're coming from,
3541 // but this particular declaration is not that definition, import the
3542 // definition and map to that.
3543 ObjCInterfaceDecl *Definition = D->getDefinition();
3544 if (Definition && Definition != D) {
3545 Decl *ImportedDef = Importer.Import(Definition);
3546 if (!ImportedDef)
3547 return 0;
3548
3549 return Importer.Imported(D, ImportedDef);
3550 }
3551
Douglas Gregor45635322010-02-16 01:20:57 +00003552 // Import the major distinguishing characteristics of an @interface.
3553 DeclContext *DC, *LexicalDC;
3554 DeclarationName Name;
3555 SourceLocation Loc;
3556 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3557 return 0;
3558
Douglas Gregor2aa53772012-01-24 17:42:07 +00003559 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003560 ObjCInterfaceDecl *MergeWithIface = 0;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003561 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003562 DC->localUncachedLookup(Name, FoundDecls);
3563 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3564 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003565 continue;
3566
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003567 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003568 break;
3569 }
3570
Douglas Gregor2aa53772012-01-24 17:42:07 +00003571 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003572 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003573 if (!ToIface) {
3574 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3575 Importer.Import(D->getAtStartLoc()),
3576 Name.getAsIdentifierInfo(),
3577 /*PrevDecl=*/0,Loc,
3578 D->isImplicitInterfaceDecl());
3579 ToIface->setLexicalDeclContext(LexicalDC);
3580 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003581 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003582 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003583
Douglas Gregor2aa53772012-01-24 17:42:07 +00003584 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3585 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003586
Douglas Gregor98d156a2010-02-17 16:12:00 +00003587 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003588}
3589
Douglas Gregor4da9d682010-12-07 15:32:12 +00003590Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3591 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3592 Importer.Import(D->getCategoryDecl()));
3593 if (!Category)
3594 return 0;
3595
3596 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3597 if (!ToImpl) {
3598 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3599 if (!DC)
3600 return 0;
3601
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003602 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003603 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003604 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003605 Category->getClassInterface(),
3606 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003607 Importer.Import(D->getAtStartLoc()),
3608 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003609
3610 DeclContext *LexicalDC = DC;
3611 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3612 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3613 if (!LexicalDC)
3614 return 0;
3615
3616 ToImpl->setLexicalDeclContext(LexicalDC);
3617 }
3618
Sean Callanan95e74be2011-10-21 02:57:43 +00003619 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003620 Category->setImplementation(ToImpl);
3621 }
3622
3623 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003624 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003625 return ToImpl;
3626}
3627
Douglas Gregorda8025c2010-12-07 01:26:03 +00003628Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3629 // Find the corresponding interface.
3630 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3631 Importer.Import(D->getClassInterface()));
3632 if (!Iface)
3633 return 0;
3634
3635 // Import the superclass, if any.
3636 ObjCInterfaceDecl *Super = 0;
3637 if (D->getSuperClass()) {
3638 Super = cast_or_null<ObjCInterfaceDecl>(
3639 Importer.Import(D->getSuperClass()));
3640 if (!Super)
3641 return 0;
3642 }
3643
3644 ObjCImplementationDecl *Impl = Iface->getImplementation();
3645 if (!Impl) {
3646 // We haven't imported an implementation yet. Create a new @implementation
3647 // now.
3648 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3649 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003650 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003651 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003652 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003653 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003654 Importer.Import(D->getIvarLBraceLoc()),
3655 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003656
3657 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3658 DeclContext *LexicalDC
3659 = Importer.ImportContext(D->getLexicalDeclContext());
3660 if (!LexicalDC)
3661 return 0;
3662 Impl->setLexicalDeclContext(LexicalDC);
3663 }
3664
3665 // Associate the implementation with the class it implements.
3666 Iface->setImplementation(Impl);
3667 Importer.Imported(D, Iface->getImplementation());
3668 } else {
3669 Importer.Imported(D, Iface->getImplementation());
3670
3671 // Verify that the existing @implementation has the same superclass.
3672 if ((Super && !Impl->getSuperClass()) ||
3673 (!Super && Impl->getSuperClass()) ||
3674 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003675 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003676 Importer.ToDiag(Impl->getLocation(),
3677 diag::err_odr_objc_superclass_inconsistent)
3678 << Iface->getDeclName();
3679 // FIXME: It would be nice to have the location of the superclass
3680 // below.
3681 if (Impl->getSuperClass())
3682 Importer.ToDiag(Impl->getLocation(),
3683 diag::note_odr_objc_superclass)
3684 << Impl->getSuperClass()->getDeclName();
3685 else
3686 Importer.ToDiag(Impl->getLocation(),
3687 diag::note_odr_objc_missing_superclass);
3688 if (D->getSuperClass())
3689 Importer.FromDiag(D->getLocation(),
3690 diag::note_odr_objc_superclass)
3691 << D->getSuperClass()->getDeclName();
3692 else
3693 Importer.FromDiag(D->getLocation(),
3694 diag::note_odr_objc_missing_superclass);
3695 return 0;
3696 }
3697 }
3698
3699 // Import all of the members of this @implementation.
3700 ImportDeclContext(D);
3701
3702 return Impl;
3703}
3704
Douglas Gregora11c4582010-02-17 18:02:10 +00003705Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3706 // Import the major distinguishing characteristics of an @property.
3707 DeclContext *DC, *LexicalDC;
3708 DeclarationName Name;
3709 SourceLocation Loc;
3710 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3711 return 0;
3712
3713 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003714 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003715 DC->localUncachedLookup(Name, FoundDecls);
3716 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003717 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003718 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003719 // Check property types.
3720 if (!Importer.IsStructurallyEquivalent(D->getType(),
3721 FoundProp->getType())) {
3722 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3723 << Name << D->getType() << FoundProp->getType();
3724 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3725 << FoundProp->getType();
3726 return 0;
3727 }
3728
3729 // FIXME: Check property attributes, getters, setters, etc.?
3730
3731 // Consider these properties to be equivalent.
3732 Importer.Imported(D, FoundProp);
3733 return FoundProp;
3734 }
3735 }
3736
3737 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003738 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3739 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003740 return 0;
3741
3742 // Create the new property.
3743 ObjCPropertyDecl *ToProperty
3744 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3745 Name.getAsIdentifierInfo(),
3746 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003747 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003748 T,
3749 D->getPropertyImplementation());
3750 Importer.Imported(D, ToProperty);
3751 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003752 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003753
3754 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003755 ToProperty->setPropertyAttributesAsWritten(
3756 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003757 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3758 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3759 ToProperty->setGetterMethodDecl(
3760 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3761 ToProperty->setSetterMethodDecl(
3762 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3763 ToProperty->setPropertyIvarDecl(
3764 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3765 return ToProperty;
3766}
3767
Douglas Gregor14a49e22010-12-07 18:32:03 +00003768Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3769 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3770 Importer.Import(D->getPropertyDecl()));
3771 if (!Property)
3772 return 0;
3773
3774 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3775 if (!DC)
3776 return 0;
3777
3778 // Import the lexical declaration context.
3779 DeclContext *LexicalDC = DC;
3780 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3781 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3782 if (!LexicalDC)
3783 return 0;
3784 }
3785
3786 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3787 if (!InImpl)
3788 return 0;
3789
3790 // Import the ivar (for an @synthesize).
3791 ObjCIvarDecl *Ivar = 0;
3792 if (D->getPropertyIvarDecl()) {
3793 Ivar = cast_or_null<ObjCIvarDecl>(
3794 Importer.Import(D->getPropertyIvarDecl()));
3795 if (!Ivar)
3796 return 0;
3797 }
3798
3799 ObjCPropertyImplDecl *ToImpl
3800 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3801 if (!ToImpl) {
3802 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3803 Importer.Import(D->getLocStart()),
3804 Importer.Import(D->getLocation()),
3805 Property,
3806 D->getPropertyImplementation(),
3807 Ivar,
3808 Importer.Import(D->getPropertyIvarDeclLoc()));
3809 ToImpl->setLexicalDeclContext(LexicalDC);
3810 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003811 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003812 } else {
3813 // Check that we have the same kind of property implementation (@synthesize
3814 // vs. @dynamic).
3815 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3816 Importer.ToDiag(ToImpl->getLocation(),
3817 diag::err_odr_objc_property_impl_kind_inconsistent)
3818 << Property->getDeclName()
3819 << (ToImpl->getPropertyImplementation()
3820 == ObjCPropertyImplDecl::Dynamic);
3821 Importer.FromDiag(D->getLocation(),
3822 diag::note_odr_objc_property_impl_kind)
3823 << D->getPropertyDecl()->getDeclName()
3824 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3825 return 0;
3826 }
3827
3828 // For @synthesize, check that we have the same
3829 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3830 Ivar != ToImpl->getPropertyIvarDecl()) {
3831 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3832 diag::err_odr_objc_synthesize_ivar_inconsistent)
3833 << Property->getDeclName()
3834 << ToImpl->getPropertyIvarDecl()->getDeclName()
3835 << Ivar->getDeclName();
3836 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3837 diag::note_odr_objc_synthesize_ivar_here)
3838 << D->getPropertyIvarDecl()->getDeclName();
3839 return 0;
3840 }
3841
3842 // Merge the existing implementation with the new implementation.
3843 Importer.Imported(D, ToImpl);
3844 }
3845
3846 return ToImpl;
3847}
3848
Douglas Gregora082a492010-11-30 19:14:50 +00003849Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3850 // For template arguments, we adopt the translation unit as our declaration
3851 // context. This context will be fixed when the actual template declaration
3852 // is created.
3853
3854 // FIXME: Import default argument.
3855 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3856 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003857 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003858 Importer.Import(D->getLocation()),
3859 D->getDepth(),
3860 D->getIndex(),
3861 Importer.Import(D->getIdentifier()),
3862 D->wasDeclaredWithTypename(),
3863 D->isParameterPack());
3864}
3865
3866Decl *
3867ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3868 // Import the name of this declaration.
3869 DeclarationName Name = Importer.Import(D->getDeclName());
3870 if (D->getDeclName() && !Name)
3871 return 0;
3872
3873 // Import the location of this declaration.
3874 SourceLocation Loc = Importer.Import(D->getLocation());
3875
3876 // Import the type of this declaration.
3877 QualType T = Importer.Import(D->getType());
3878 if (T.isNull())
3879 return 0;
3880
3881 // Import type-source information.
3882 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3883 if (D->getTypeSourceInfo() && !TInfo)
3884 return 0;
3885
3886 // FIXME: Import default argument.
3887
3888 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3889 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003890 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003891 Loc, D->getDepth(), D->getPosition(),
3892 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003893 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003894}
3895
3896Decl *
3897ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3898 // Import the name of this declaration.
3899 DeclarationName Name = Importer.Import(D->getDeclName());
3900 if (D->getDeclName() && !Name)
3901 return 0;
3902
3903 // Import the location of this declaration.
3904 SourceLocation Loc = Importer.Import(D->getLocation());
3905
3906 // Import template parameters.
3907 TemplateParameterList *TemplateParams
3908 = ImportTemplateParameterList(D->getTemplateParameters());
3909 if (!TemplateParams)
3910 return 0;
3911
3912 // FIXME: Import default argument.
3913
3914 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3915 Importer.getToContext().getTranslationUnitDecl(),
3916 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003917 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003918 Name.getAsIdentifierInfo(),
3919 TemplateParams);
3920}
3921
3922Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3923 // If this record has a definition in the translation unit we're coming from,
3924 // but this particular declaration is not that definition, import the
3925 // definition and map to that.
3926 CXXRecordDecl *Definition
3927 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3928 if (Definition && Definition != D->getTemplatedDecl()) {
3929 Decl *ImportedDef
3930 = Importer.Import(Definition->getDescribedClassTemplate());
3931 if (!ImportedDef)
3932 return 0;
3933
3934 return Importer.Imported(D, ImportedDef);
3935 }
3936
3937 // Import the major distinguishing characteristics of this class template.
3938 DeclContext *DC, *LexicalDC;
3939 DeclarationName Name;
3940 SourceLocation Loc;
3941 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3942 return 0;
3943
3944 // We may already have a template of the same name; try to find and match it.
3945 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003946 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003947 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003948 DC->localUncachedLookup(Name, FoundDecls);
3949 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3950 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003951 continue;
3952
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003953 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00003954 if (ClassTemplateDecl *FoundTemplate
3955 = dyn_cast<ClassTemplateDecl>(Found)) {
3956 if (IsStructuralMatch(D, FoundTemplate)) {
3957 // The class templates structurally match; call it the same template.
3958 // FIXME: We may be filling in a forward declaration here. Handle
3959 // this case!
3960 Importer.Imported(D->getTemplatedDecl(),
3961 FoundTemplate->getTemplatedDecl());
3962 return Importer.Imported(D, FoundTemplate);
3963 }
3964 }
3965
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003966 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00003967 }
3968
3969 if (!ConflictingDecls.empty()) {
3970 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
3971 ConflictingDecls.data(),
3972 ConflictingDecls.size());
3973 }
3974
3975 if (!Name)
3976 return 0;
3977 }
3978
3979 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
3980
3981 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003982 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
3983 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00003984 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
3985 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00003986 DC, StartLoc, IdLoc,
3987 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00003988 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00003989 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00003990 D2Templated->setLexicalDeclContext(LexicalDC);
3991
3992 // Create the class template declaration itself.
3993 TemplateParameterList *TemplateParams
3994 = ImportTemplateParameterList(D->getTemplateParameters());
3995 if (!TemplateParams)
3996 return 0;
3997
3998 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
3999 Loc, Name, TemplateParams,
4000 D2Templated,
4001 /*PrevDecl=*/0);
4002 D2Templated->setDescribedClassTemplate(D2);
4003
4004 D2->setAccess(D->getAccess());
4005 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004006 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004007
4008 // Note the relationship between the class templates.
4009 Importer.Imported(D, D2);
4010 Importer.Imported(DTemplated, D2Templated);
4011
John McCallf937c022011-10-07 06:10:15 +00004012 if (DTemplated->isCompleteDefinition() &&
4013 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004014 // FIXME: Import definition!
4015 }
4016
4017 return D2;
4018}
4019
Douglas Gregore2e50d332010-12-01 01:36:18 +00004020Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4021 ClassTemplateSpecializationDecl *D) {
4022 // If this record has a definition in the translation unit we're coming from,
4023 // but this particular declaration is not that definition, import the
4024 // definition and map to that.
4025 TagDecl *Definition = D->getDefinition();
4026 if (Definition && Definition != D) {
4027 Decl *ImportedDef = Importer.Import(Definition);
4028 if (!ImportedDef)
4029 return 0;
4030
4031 return Importer.Imported(D, ImportedDef);
4032 }
4033
4034 ClassTemplateDecl *ClassTemplate
4035 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4036 D->getSpecializedTemplate()));
4037 if (!ClassTemplate)
4038 return 0;
4039
4040 // Import the context of this declaration.
4041 DeclContext *DC = ClassTemplate->getDeclContext();
4042 if (!DC)
4043 return 0;
4044
4045 DeclContext *LexicalDC = DC;
4046 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4047 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4048 if (!LexicalDC)
4049 return 0;
4050 }
4051
4052 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004053 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4054 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004055
4056 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004057 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004058 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4059 D->getTemplateArgs().size(),
4060 TemplateArgs))
4061 return 0;
4062
4063 // Try to find an existing specialization with these template arguments.
4064 void *InsertPos = 0;
4065 ClassTemplateSpecializationDecl *D2
4066 = ClassTemplate->findSpecialization(TemplateArgs.data(),
4067 TemplateArgs.size(), InsertPos);
4068 if (D2) {
4069 // We already have a class template specialization with these template
4070 // arguments.
4071
4072 // FIXME: Check for specialization vs. instantiation errors.
4073
4074 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004075 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004076 // The record types structurally match, or the "from" translation
4077 // unit only had a forward declaration anyway; call it the same
4078 // function.
4079 return Importer.Imported(D, FoundDef);
4080 }
4081 }
4082 } else {
4083 // Create a new specialization.
4084 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4085 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004086 StartLoc, IdLoc,
4087 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004088 TemplateArgs.data(),
4089 TemplateArgs.size(),
4090 /*PrevDecl=*/0);
4091 D2->setSpecializationKind(D->getSpecializationKind());
4092
4093 // Add this specialization to the class template.
4094 ClassTemplate->AddSpecialization(D2, InsertPos);
4095
4096 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004097 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004098
4099 // Add the specialization to this context.
4100 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004101 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004102 }
4103 Importer.Imported(D, D2);
4104
John McCallf937c022011-10-07 06:10:15 +00004105 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00004106 return 0;
4107
4108 return D2;
4109}
4110
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004111//----------------------------------------------------------------------------
4112// Import Statements
4113//----------------------------------------------------------------------------
4114
4115Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4116 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4117 << S->getStmtClassName();
4118 return 0;
4119}
4120
4121//----------------------------------------------------------------------------
4122// Import Expressions
4123//----------------------------------------------------------------------------
4124Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4125 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4126 << E->getStmtClassName();
4127 return 0;
4128}
4129
Douglas Gregor52f820e2010-02-19 01:17:02 +00004130Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004131 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4132 if (!ToD)
4133 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004134
4135 NamedDecl *FoundD = 0;
4136 if (E->getDecl() != E->getFoundDecl()) {
4137 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4138 if (!FoundD)
4139 return 0;
4140 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004141
4142 QualType T = Importer.Import(E->getType());
4143 if (T.isNull())
4144 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004145
4146 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4147 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004148 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004149 ToD,
John McCall113bee02012-03-10 09:33:50 +00004150 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004151 Importer.Import(E->getLocation()),
4152 T, E->getValueKind(),
4153 FoundD,
4154 /*FIXME:TemplateArgs=*/0);
4155 if (E->hadMultipleCandidates())
4156 DRE->setHadMultipleCandidates(true);
4157 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004158}
4159
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004160Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4161 QualType T = Importer.Import(E->getType());
4162 if (T.isNull())
4163 return 0;
4164
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004165 return IntegerLiteral::Create(Importer.getToContext(),
4166 E->getValue(), T,
4167 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004168}
4169
Douglas Gregor623421d2010-02-18 02:21:22 +00004170Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4171 QualType T = Importer.Import(E->getType());
4172 if (T.isNull())
4173 return 0;
4174
Douglas Gregorfb65e592011-07-27 05:40:30 +00004175 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4176 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004177 Importer.Import(E->getLocation()));
4178}
4179
Douglas Gregorc74247e2010-02-19 01:07:06 +00004180Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4181 Expr *SubExpr = Importer.Import(E->getSubExpr());
4182 if (!SubExpr)
4183 return 0;
4184
4185 return new (Importer.getToContext())
4186 ParenExpr(Importer.Import(E->getLParen()),
4187 Importer.Import(E->getRParen()),
4188 SubExpr);
4189}
4190
4191Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4192 QualType T = Importer.Import(E->getType());
4193 if (T.isNull())
4194 return 0;
4195
4196 Expr *SubExpr = Importer.Import(E->getSubExpr());
4197 if (!SubExpr)
4198 return 0;
4199
4200 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004201 T, E->getValueKind(),
4202 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004203 Importer.Import(E->getOperatorLoc()));
4204}
4205
Peter Collingbournee190dee2011-03-11 19:24:49 +00004206Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4207 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004208 QualType ResultType = Importer.Import(E->getType());
4209
4210 if (E->isArgumentType()) {
4211 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4212 if (!TInfo)
4213 return 0;
4214
Peter Collingbournee190dee2011-03-11 19:24:49 +00004215 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4216 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004217 Importer.Import(E->getOperatorLoc()),
4218 Importer.Import(E->getRParenLoc()));
4219 }
4220
4221 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4222 if (!SubExpr)
4223 return 0;
4224
Peter Collingbournee190dee2011-03-11 19:24:49 +00004225 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4226 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004227 Importer.Import(E->getOperatorLoc()),
4228 Importer.Import(E->getRParenLoc()));
4229}
4230
Douglas Gregorc74247e2010-02-19 01:07:06 +00004231Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4232 QualType T = Importer.Import(E->getType());
4233 if (T.isNull())
4234 return 0;
4235
4236 Expr *LHS = Importer.Import(E->getLHS());
4237 if (!LHS)
4238 return 0;
4239
4240 Expr *RHS = Importer.Import(E->getRHS());
4241 if (!RHS)
4242 return 0;
4243
4244 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004245 T, E->getValueKind(),
4246 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004247 Importer.Import(E->getOperatorLoc()),
4248 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004249}
4250
4251Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4252 QualType T = Importer.Import(E->getType());
4253 if (T.isNull())
4254 return 0;
4255
4256 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4257 if (CompLHSType.isNull())
4258 return 0;
4259
4260 QualType CompResultType = Importer.Import(E->getComputationResultType());
4261 if (CompResultType.isNull())
4262 return 0;
4263
4264 Expr *LHS = Importer.Import(E->getLHS());
4265 if (!LHS)
4266 return 0;
4267
4268 Expr *RHS = Importer.Import(E->getRHS());
4269 if (!RHS)
4270 return 0;
4271
4272 return new (Importer.getToContext())
4273 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004274 T, E->getValueKind(),
4275 E->getObjectKind(),
4276 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004277 Importer.Import(E->getOperatorLoc()),
4278 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004279}
4280
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004281static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004282 if (E->path_empty()) return false;
4283
4284 // TODO: import cast paths
4285 return true;
4286}
4287
Douglas Gregor98c10182010-02-12 22:17:39 +00004288Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4289 QualType T = Importer.Import(E->getType());
4290 if (T.isNull())
4291 return 0;
4292
4293 Expr *SubExpr = Importer.Import(E->getSubExpr());
4294 if (!SubExpr)
4295 return 0;
John McCallcf142162010-08-07 06:22:56 +00004296
4297 CXXCastPath BasePath;
4298 if (ImportCastPath(E, BasePath))
4299 return 0;
4300
4301 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004302 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004303}
4304
Douglas Gregor5481d322010-02-19 01:32:14 +00004305Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4306 QualType T = Importer.Import(E->getType());
4307 if (T.isNull())
4308 return 0;
4309
4310 Expr *SubExpr = Importer.Import(E->getSubExpr());
4311 if (!SubExpr)
4312 return 0;
4313
4314 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4315 if (!TInfo && E->getTypeInfoAsWritten())
4316 return 0;
4317
John McCallcf142162010-08-07 06:22:56 +00004318 CXXCastPath BasePath;
4319 if (ImportCastPath(E, BasePath))
4320 return 0;
4321
John McCall7decc9e2010-11-18 06:31:45 +00004322 return CStyleCastExpr::Create(Importer.getToContext(), T,
4323 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004324 SubExpr, &BasePath, TInfo,
4325 Importer.Import(E->getLParenLoc()),
4326 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004327}
4328
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004329ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004330 ASTContext &FromContext, FileManager &FromFileManager,
4331 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004332 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004333 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00004334 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00004335{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004336 ImportedDecls[FromContext.getTranslationUnitDecl()]
4337 = ToContext.getTranslationUnitDecl();
4338}
4339
4340ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004341
4342QualType ASTImporter::Import(QualType FromT) {
4343 if (FromT.isNull())
4344 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004345
4346 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004347
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004348 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004349 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4350 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004351 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004352 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004353
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004354 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004355 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004356 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004357 if (ToT.isNull())
4358 return ToT;
4359
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004360 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004361 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004362
John McCall424cec92011-01-19 06:33:43 +00004363 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004364}
4365
Douglas Gregor62d311f2010-02-09 19:21:46 +00004366TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004367 if (!FromTSI)
4368 return FromTSI;
4369
4370 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004371 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004372 QualType T = Import(FromTSI->getType());
4373 if (T.isNull())
4374 return 0;
4375
4376 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004377 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004378}
4379
4380Decl *ASTImporter::Import(Decl *FromD) {
4381 if (!FromD)
4382 return 0;
4383
Douglas Gregord451ea92011-07-29 23:31:30 +00004384 ASTNodeImporter Importer(*this);
4385
Douglas Gregor62d311f2010-02-09 19:21:46 +00004386 // Check whether we've already imported this declaration.
4387 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004388 if (Pos != ImportedDecls.end()) {
4389 Decl *ToD = Pos->second;
4390 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4391 return ToD;
4392 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004393
4394 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004395 Decl *ToD = Importer.Visit(FromD);
4396 if (!ToD)
4397 return 0;
4398
4399 // Record the imported declaration.
4400 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004401
4402 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4403 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004404 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004405 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004406 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004407 // When we've finished transforming a typedef, see whether it was the
4408 // typedef for an anonymous tag.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004409 for (SmallVector<TagDecl *, 4>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004410 FromTag = AnonTagsWithPendingTypedefs.begin(),
4411 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4412 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004413 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004414 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4415 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004416 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004417 AnonTagsWithPendingTypedefs.erase(FromTag);
4418 break;
4419 }
4420 }
4421 }
4422 }
4423
Douglas Gregor62d311f2010-02-09 19:21:46 +00004424 return ToD;
4425}
4426
4427DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4428 if (!FromDC)
4429 return FromDC;
4430
Douglas Gregor95d82832012-01-24 18:36:04 +00004431 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004432 if (!ToDC)
4433 return 0;
4434
4435 // When we're using a record/enum/Objective-C class/protocol as a context, we
4436 // need it to have a definition.
4437 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004438 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004439 if (ToRecord->isCompleteDefinition()) {
4440 // Do nothing.
4441 } else if (FromRecord->isCompleteDefinition()) {
4442 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4443 ASTNodeImporter::IDK_Basic);
4444 } else {
4445 CompleteDecl(ToRecord);
4446 }
4447 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4448 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4449 if (ToEnum->isCompleteDefinition()) {
4450 // Do nothing.
4451 } else if (FromEnum->isCompleteDefinition()) {
4452 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4453 ASTNodeImporter::IDK_Basic);
4454 } else {
4455 CompleteDecl(ToEnum);
4456 }
4457 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4458 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4459 if (ToClass->getDefinition()) {
4460 // Do nothing.
4461 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4462 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4463 ASTNodeImporter::IDK_Basic);
4464 } else {
4465 CompleteDecl(ToClass);
4466 }
4467 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4468 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4469 if (ToProto->getDefinition()) {
4470 // Do nothing.
4471 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4472 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4473 ASTNodeImporter::IDK_Basic);
4474 } else {
4475 CompleteDecl(ToProto);
4476 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004477 }
4478
4479 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004480}
4481
4482Expr *ASTImporter::Import(Expr *FromE) {
4483 if (!FromE)
4484 return 0;
4485
4486 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4487}
4488
4489Stmt *ASTImporter::Import(Stmt *FromS) {
4490 if (!FromS)
4491 return 0;
4492
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004493 // Check whether we've already imported this declaration.
4494 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4495 if (Pos != ImportedStmts.end())
4496 return Pos->second;
4497
4498 // Import the type
4499 ASTNodeImporter Importer(*this);
4500 Stmt *ToS = Importer.Visit(FromS);
4501 if (!ToS)
4502 return 0;
4503
4504 // Record the imported declaration.
4505 ImportedStmts[FromS] = ToS;
4506 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004507}
4508
4509NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4510 if (!FromNNS)
4511 return 0;
4512
Douglas Gregor90ebf252011-04-27 16:48:40 +00004513 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4514
4515 switch (FromNNS->getKind()) {
4516 case NestedNameSpecifier::Identifier:
4517 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4518 return NestedNameSpecifier::Create(ToContext, prefix, II);
4519 }
4520 return 0;
4521
4522 case NestedNameSpecifier::Namespace:
4523 if (NamespaceDecl *NS =
4524 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4525 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4526 }
4527 return 0;
4528
4529 case NestedNameSpecifier::NamespaceAlias:
4530 if (NamespaceAliasDecl *NSAD =
4531 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4532 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4533 }
4534 return 0;
4535
4536 case NestedNameSpecifier::Global:
4537 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4538
4539 case NestedNameSpecifier::TypeSpec:
4540 case NestedNameSpecifier::TypeSpecWithTemplate: {
4541 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4542 if (!T.isNull()) {
4543 bool bTemplate = FromNNS->getKind() ==
4544 NestedNameSpecifier::TypeSpecWithTemplate;
4545 return NestedNameSpecifier::Create(ToContext, prefix,
4546 bTemplate, T.getTypePtr());
4547 }
4548 }
4549 return 0;
4550 }
4551
4552 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004553}
4554
Douglas Gregor14454802011-02-25 02:25:35 +00004555NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4556 // FIXME: Implement!
4557 return NestedNameSpecifierLoc();
4558}
4559
Douglas Gregore2e50d332010-12-01 01:36:18 +00004560TemplateName ASTImporter::Import(TemplateName From) {
4561 switch (From.getKind()) {
4562 case TemplateName::Template:
4563 if (TemplateDecl *ToTemplate
4564 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4565 return TemplateName(ToTemplate);
4566
4567 return TemplateName();
4568
4569 case TemplateName::OverloadedTemplate: {
4570 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4571 UnresolvedSet<2> ToTemplates;
4572 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4573 E = FromStorage->end();
4574 I != E; ++I) {
4575 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4576 ToTemplates.addDecl(To);
4577 else
4578 return TemplateName();
4579 }
4580 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4581 ToTemplates.end());
4582 }
4583
4584 case TemplateName::QualifiedTemplate: {
4585 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4586 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4587 if (!Qualifier)
4588 return TemplateName();
4589
4590 if (TemplateDecl *ToTemplate
4591 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4592 return ToContext.getQualifiedTemplateName(Qualifier,
4593 QTN->hasTemplateKeyword(),
4594 ToTemplate);
4595
4596 return TemplateName();
4597 }
4598
4599 case TemplateName::DependentTemplate: {
4600 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4601 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4602 if (!Qualifier)
4603 return TemplateName();
4604
4605 if (DTN->isIdentifier()) {
4606 return ToContext.getDependentTemplateName(Qualifier,
4607 Import(DTN->getIdentifier()));
4608 }
4609
4610 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4611 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004612
4613 case TemplateName::SubstTemplateTemplateParm: {
4614 SubstTemplateTemplateParmStorage *subst
4615 = From.getAsSubstTemplateTemplateParm();
4616 TemplateTemplateParmDecl *param
4617 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4618 if (!param)
4619 return TemplateName();
4620
4621 TemplateName replacement = Import(subst->getReplacement());
4622 if (replacement.isNull()) return TemplateName();
4623
4624 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4625 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004626
4627 case TemplateName::SubstTemplateTemplateParmPack: {
4628 SubstTemplateTemplateParmPackStorage *SubstPack
4629 = From.getAsSubstTemplateTemplateParmPack();
4630 TemplateTemplateParmDecl *Param
4631 = cast_or_null<TemplateTemplateParmDecl>(
4632 Import(SubstPack->getParameterPack()));
4633 if (!Param)
4634 return TemplateName();
4635
4636 ASTNodeImporter Importer(*this);
4637 TemplateArgument ArgPack
4638 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4639 if (ArgPack.isNull())
4640 return TemplateName();
4641
4642 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4643 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004644 }
4645
4646 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004647}
4648
Douglas Gregor62d311f2010-02-09 19:21:46 +00004649SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4650 if (FromLoc.isInvalid())
4651 return SourceLocation();
4652
Douglas Gregor811663e2010-02-10 00:15:17 +00004653 SourceManager &FromSM = FromContext.getSourceManager();
4654
4655 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004656 // don't have to import macro expansions.
4657 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004658 FromLoc = FromSM.getSpellingLoc(FromLoc);
4659 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4660 SourceManager &ToSM = ToContext.getSourceManager();
4661 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004662 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004663}
4664
4665SourceRange ASTImporter::Import(SourceRange FromRange) {
4666 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4667}
4668
Douglas Gregor811663e2010-02-10 00:15:17 +00004669FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004670 llvm::DenseMap<FileID, FileID>::iterator Pos
4671 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004672 if (Pos != ImportedFileIDs.end())
4673 return Pos->second;
4674
4675 SourceManager &FromSM = FromContext.getSourceManager();
4676 SourceManager &ToSM = ToContext.getSourceManager();
4677 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004678 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004679
4680 // Include location of this file.
4681 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4682
4683 // Map the FileID for to the "to" source manager.
4684 FileID ToID;
4685 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004686 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004687 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4688 // disk again
4689 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4690 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004691 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004692 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4693 FromSLoc.getFile().getFileCharacteristic());
4694 } else {
4695 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004696 const llvm::MemoryBuffer *
4697 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004698 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004699 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004700 FromBuf->getBufferIdentifier());
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +00004701 ToID = ToSM.createFileIDForMemBuffer(ToBuf,
4702 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00004703 }
4704
4705
Sebastian Redl99219f12010-09-30 01:03:06 +00004706 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004707 return ToID;
4708}
4709
Douglas Gregor0a791672011-01-18 03:11:38 +00004710void ASTImporter::ImportDefinition(Decl *From) {
4711 Decl *To = Import(From);
4712 if (!To)
4713 return;
4714
4715 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4716 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004717
4718 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4719 if (!ToRecord->getDefinition()) {
4720 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004721 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004722 return;
4723 }
4724 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004725
4726 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4727 if (!ToEnum->getDefinition()) {
4728 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004729 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004730 return;
4731 }
4732 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004733
4734 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4735 if (!ToIFace->getDefinition()) {
4736 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004737 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004738 return;
4739 }
4740 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004741
Douglas Gregor2aa53772012-01-24 17:42:07 +00004742 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4743 if (!ToProto->getDefinition()) {
4744 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004745 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004746 return;
4747 }
4748 }
4749
Douglas Gregor0a791672011-01-18 03:11:38 +00004750 Importer.ImportDeclContext(FromDC, true);
4751 }
4752}
4753
Douglas Gregor96e578d2010-02-05 17:54:41 +00004754DeclarationName ASTImporter::Import(DeclarationName FromName) {
4755 if (!FromName)
4756 return DeclarationName();
4757
4758 switch (FromName.getNameKind()) {
4759 case DeclarationName::Identifier:
4760 return Import(FromName.getAsIdentifierInfo());
4761
4762 case DeclarationName::ObjCZeroArgSelector:
4763 case DeclarationName::ObjCOneArgSelector:
4764 case DeclarationName::ObjCMultiArgSelector:
4765 return Import(FromName.getObjCSelector());
4766
4767 case DeclarationName::CXXConstructorName: {
4768 QualType T = Import(FromName.getCXXNameType());
4769 if (T.isNull())
4770 return DeclarationName();
4771
4772 return ToContext.DeclarationNames.getCXXConstructorName(
4773 ToContext.getCanonicalType(T));
4774 }
4775
4776 case DeclarationName::CXXDestructorName: {
4777 QualType T = Import(FromName.getCXXNameType());
4778 if (T.isNull())
4779 return DeclarationName();
4780
4781 return ToContext.DeclarationNames.getCXXDestructorName(
4782 ToContext.getCanonicalType(T));
4783 }
4784
4785 case DeclarationName::CXXConversionFunctionName: {
4786 QualType T = Import(FromName.getCXXNameType());
4787 if (T.isNull())
4788 return DeclarationName();
4789
4790 return ToContext.DeclarationNames.getCXXConversionFunctionName(
4791 ToContext.getCanonicalType(T));
4792 }
4793
4794 case DeclarationName::CXXOperatorName:
4795 return ToContext.DeclarationNames.getCXXOperatorName(
4796 FromName.getCXXOverloadedOperator());
4797
4798 case DeclarationName::CXXLiteralOperatorName:
4799 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
4800 Import(FromName.getCXXLiteralIdentifier()));
4801
4802 case DeclarationName::CXXUsingDirective:
4803 // FIXME: STATICS!
4804 return DeclarationName::getUsingDirectiveName();
4805 }
4806
David Blaikiee4d798f2012-01-20 21:50:17 +00004807 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00004808}
4809
Douglas Gregore2e50d332010-12-01 01:36:18 +00004810IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00004811 if (!FromId)
4812 return 0;
4813
4814 return &ToContext.Idents.get(FromId->getName());
4815}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004816
Douglas Gregor43f54792010-02-17 02:12:47 +00004817Selector ASTImporter::Import(Selector FromSel) {
4818 if (FromSel.isNull())
4819 return Selector();
4820
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004821 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00004822 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
4823 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
4824 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
4825 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
4826}
4827
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004828DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
4829 DeclContext *DC,
4830 unsigned IDNS,
4831 NamedDecl **Decls,
4832 unsigned NumDecls) {
4833 return Name;
4834}
4835
4836DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00004837 if (LastDiagFromFrom)
4838 ToContext.getDiagnostics().notePriorDiagnosticFrom(
4839 FromContext.getDiagnostics());
4840 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004841 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004842}
4843
4844DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00004845 if (!LastDiagFromFrom)
4846 FromContext.getDiagnostics().notePriorDiagnosticFrom(
4847 ToContext.getDiagnostics());
4848 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004849 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00004850}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004851
Douglas Gregor2e15c842012-02-01 21:00:38 +00004852void ASTImporter::CompleteDecl (Decl *D) {
4853 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
4854 if (!ID->getDefinition())
4855 ID->startDefinition();
4856 }
4857 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
4858 if (!PD->getDefinition())
4859 PD->startDefinition();
4860 }
4861 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
4862 if (!TD->getDefinition() && !TD->isBeingDefined()) {
4863 TD->startDefinition();
4864 TD->setCompleteDefinition(true);
4865 }
4866 }
4867 else {
4868 assert (0 && "CompleteDecl called on a Decl that can't be completed");
4869 }
4870}
4871
Douglas Gregor8cdbe642010-02-12 23:44:20 +00004872Decl *ASTImporter::Imported(Decl *From, Decl *To) {
4873 ImportedDecls[From] = To;
4874 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00004875}
Douglas Gregorb4964f72010-02-15 23:54:17 +00004876
Douglas Gregordd6006f2012-07-17 21:16:27 +00004877bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
4878 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00004879 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00004880 = ImportedTypes.find(From.getTypePtr());
4881 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
4882 return true;
4883
Douglas Gregordd6006f2012-07-17 21:16:27 +00004884 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
4885 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00004886 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00004887}