blob: 676228bb081f73a47336a112c6d7d1b0f7242bad [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);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000109 bool ImportDefinition(VarDecl *From, VarDecl *To,
110 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000111 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000112 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000113 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000114 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000115 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000116 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000117 TemplateParameterList *ImportTemplateParameterList(
118 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000119 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
120 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
121 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000122 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000123 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
124 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000125 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
126 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000127 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000128 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000129 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000130 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000131 Decl *VisitDecl(Decl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000132 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000133 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000134 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000135 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000136 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000137 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000138 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000139 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000140 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000141 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
142 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
143 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
144 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000145 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000146 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000147 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000148 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000149 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000150 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000151 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000152 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000153 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Douglas Gregor45635322010-02-16 01:20:57 +0000154 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000155 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000156 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000157 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000158 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000159 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
160 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
161 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
162 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000163 Decl *VisitClassTemplateSpecializationDecl(
164 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000165 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
166 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
167
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000168 // Importing statements
169 Stmt *VisitStmt(Stmt *S);
170
171 // Importing expressions
172 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000173 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000174 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000175 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000176 Expr *VisitParenExpr(ParenExpr *E);
177 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000178 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000179 Expr *VisitBinaryOperator(BinaryOperator *E);
180 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000181 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000182 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000183 };
184}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000185using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000186
187//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000188// Structural Equivalence
189//----------------------------------------------------------------------------
190
191namespace {
192 struct StructuralEquivalenceContext {
193 /// \brief AST contexts for which we are checking structural equivalence.
194 ASTContext &C1, &C2;
195
Douglas Gregor3996e242010-02-15 22:01:00 +0000196 /// \brief The set of "tentative" equivalences between two canonical
197 /// declarations, mapping from a declaration in the first context to the
198 /// declaration in the second context that we believe to be equivalent.
199 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
200
201 /// \brief Queue of declarations in the first context whose equivalence
202 /// with a declaration in the second context still needs to be verified.
203 std::deque<Decl *> DeclsToCheck;
204
Douglas Gregorb4964f72010-02-15 23:54:17 +0000205 /// \brief Declaration (from, to) pairs that are known not to be equivalent
206 /// (which we have already complained about).
207 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
208
Douglas Gregor3996e242010-02-15 22:01:00 +0000209 /// \brief Whether we're being strict about the spelling of types when
210 /// unifying two types.
211 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000212
213 /// \brief Whether to complain about failures.
214 bool Complain;
215
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000216 /// \brief \c true if the last diagnostic came from C2.
217 bool LastDiagFromC2;
218
Douglas Gregor3996e242010-02-15 22:01:00 +0000219 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000220 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000221 bool StrictTypeSpelling = false,
222 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000223 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000224 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
225 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000226
227 /// \brief Determine whether the two declarations are structurally
228 /// equivalent.
229 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
230
231 /// \brief Determine whether the two types are structurally equivalent.
232 bool IsStructurallyEquivalent(QualType T1, QualType T2);
233
234 private:
235 /// \brief Finish checking all of the structural equivalences.
236 ///
237 /// \returns true if an error occurred, false otherwise.
238 bool Finish();
239
240 public:
241 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000242 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000243 if (LastDiagFromC2)
244 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
245 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000246 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000247 }
248
249 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000250 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000251 if (!LastDiagFromC2)
252 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
253 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000254 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000255 }
256 };
257}
258
259static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
260 QualType T1, QualType T2);
261static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
262 Decl *D1, Decl *D2);
263
Douglas Gregor3996e242010-02-15 22:01:00 +0000264/// \brief Determine structural equivalence of two expressions.
265static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
266 Expr *E1, Expr *E2) {
267 if (!E1 || !E2)
268 return E1 == E2;
269
270 // FIXME: Actually perform a structural comparison!
271 return true;
272}
273
274/// \brief Determine whether two identifiers are equivalent.
275static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
276 const IdentifierInfo *Name2) {
277 if (!Name1 || !Name2)
278 return Name1 == Name2;
279
280 return Name1->getName() == Name2->getName();
281}
282
283/// \brief Determine whether two nested-name-specifiers are equivalent.
284static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
285 NestedNameSpecifier *NNS1,
286 NestedNameSpecifier *NNS2) {
287 // FIXME: Implement!
288 return true;
289}
290
291/// \brief Determine whether two template arguments are equivalent.
292static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
293 const TemplateArgument &Arg1,
294 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000295 if (Arg1.getKind() != Arg2.getKind())
296 return false;
297
298 switch (Arg1.getKind()) {
299 case TemplateArgument::Null:
300 return true;
301
302 case TemplateArgument::Type:
303 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000304
Douglas Gregore2e50d332010-12-01 01:36:18 +0000305 case TemplateArgument::Integral:
306 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
307 Arg2.getIntegralType()))
308 return false;
309
Eric Christopher6dcc3762012-07-15 00:23:57 +0000310 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000311
312 case TemplateArgument::Declaration:
313 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000314
315 case TemplateArgument::NullPtr:
316 return true; // FIXME: Is this correct?
317
Douglas Gregore2e50d332010-12-01 01:36:18 +0000318 case TemplateArgument::Template:
319 return IsStructurallyEquivalent(Context,
320 Arg1.getAsTemplate(),
321 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000322
323 case TemplateArgument::TemplateExpansion:
324 return IsStructurallyEquivalent(Context,
325 Arg1.getAsTemplateOrTemplatePattern(),
326 Arg2.getAsTemplateOrTemplatePattern());
327
Douglas Gregore2e50d332010-12-01 01:36:18 +0000328 case TemplateArgument::Expression:
329 return IsStructurallyEquivalent(Context,
330 Arg1.getAsExpr(), Arg2.getAsExpr());
331
332 case TemplateArgument::Pack:
333 if (Arg1.pack_size() != Arg2.pack_size())
334 return false;
335
336 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
337 if (!IsStructurallyEquivalent(Context,
338 Arg1.pack_begin()[I],
339 Arg2.pack_begin()[I]))
340 return false;
341
342 return true;
343 }
344
345 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000346}
347
348/// \brief Determine structural equivalence for the common part of array
349/// types.
350static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
351 const ArrayType *Array1,
352 const ArrayType *Array2) {
353 if (!IsStructurallyEquivalent(Context,
354 Array1->getElementType(),
355 Array2->getElementType()))
356 return false;
357 if (Array1->getSizeModifier() != Array2->getSizeModifier())
358 return false;
359 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
360 return false;
361
362 return true;
363}
364
365/// \brief Determine structural equivalence of two types.
366static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
367 QualType T1, QualType T2) {
368 if (T1.isNull() || T2.isNull())
369 return T1.isNull() && T2.isNull();
370
371 if (!Context.StrictTypeSpelling) {
372 // We aren't being strict about token-to-token equivalence of types,
373 // so map down to the canonical type.
374 T1 = Context.C1.getCanonicalType(T1);
375 T2 = Context.C2.getCanonicalType(T2);
376 }
377
378 if (T1.getQualifiers() != T2.getQualifiers())
379 return false;
380
Douglas Gregorb4964f72010-02-15 23:54:17 +0000381 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000382
Douglas Gregorb4964f72010-02-15 23:54:17 +0000383 if (T1->getTypeClass() != T2->getTypeClass()) {
384 // Compare function types with prototypes vs. without prototypes as if
385 // both did not have prototypes.
386 if (T1->getTypeClass() == Type::FunctionProto &&
387 T2->getTypeClass() == Type::FunctionNoProto)
388 TC = Type::FunctionNoProto;
389 else if (T1->getTypeClass() == Type::FunctionNoProto &&
390 T2->getTypeClass() == Type::FunctionProto)
391 TC = Type::FunctionNoProto;
392 else
393 return false;
394 }
395
396 switch (TC) {
397 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000398 // FIXME: Deal with Char_S/Char_U.
399 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
400 return false;
401 break;
402
403 case Type::Complex:
404 if (!IsStructurallyEquivalent(Context,
405 cast<ComplexType>(T1)->getElementType(),
406 cast<ComplexType>(T2)->getElementType()))
407 return false;
408 break;
409
Reid Kleckner0503a872013-12-05 01:23:43 +0000410 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000411 case Type::Decayed:
412 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000413 cast<AdjustedType>(T1)->getOriginalType(),
414 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000415 return false;
416 break;
417
Douglas Gregor3996e242010-02-15 22:01:00 +0000418 case Type::Pointer:
419 if (!IsStructurallyEquivalent(Context,
420 cast<PointerType>(T1)->getPointeeType(),
421 cast<PointerType>(T2)->getPointeeType()))
422 return false;
423 break;
424
425 case Type::BlockPointer:
426 if (!IsStructurallyEquivalent(Context,
427 cast<BlockPointerType>(T1)->getPointeeType(),
428 cast<BlockPointerType>(T2)->getPointeeType()))
429 return false;
430 break;
431
432 case Type::LValueReference:
433 case Type::RValueReference: {
434 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
435 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
436 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
437 return false;
438 if (Ref1->isInnerRef() != Ref2->isInnerRef())
439 return false;
440 if (!IsStructurallyEquivalent(Context,
441 Ref1->getPointeeTypeAsWritten(),
442 Ref2->getPointeeTypeAsWritten()))
443 return false;
444 break;
445 }
446
447 case Type::MemberPointer: {
448 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
449 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
450 if (!IsStructurallyEquivalent(Context,
451 MemPtr1->getPointeeType(),
452 MemPtr2->getPointeeType()))
453 return false;
454 if (!IsStructurallyEquivalent(Context,
455 QualType(MemPtr1->getClass(), 0),
456 QualType(MemPtr2->getClass(), 0)))
457 return false;
458 break;
459 }
460
461 case Type::ConstantArray: {
462 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
463 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000464 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000465 return false;
466
467 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
468 return false;
469 break;
470 }
471
472 case Type::IncompleteArray:
473 if (!IsArrayStructurallyEquivalent(Context,
474 cast<ArrayType>(T1),
475 cast<ArrayType>(T2)))
476 return false;
477 break;
478
479 case Type::VariableArray: {
480 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
481 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
482 if (!IsStructurallyEquivalent(Context,
483 Array1->getSizeExpr(), Array2->getSizeExpr()))
484 return false;
485
486 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
487 return false;
488
489 break;
490 }
491
492 case Type::DependentSizedArray: {
493 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
494 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
495 if (!IsStructurallyEquivalent(Context,
496 Array1->getSizeExpr(), Array2->getSizeExpr()))
497 return false;
498
499 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
500 return false;
501
502 break;
503 }
504
505 case Type::DependentSizedExtVector: {
506 const DependentSizedExtVectorType *Vec1
507 = cast<DependentSizedExtVectorType>(T1);
508 const DependentSizedExtVectorType *Vec2
509 = cast<DependentSizedExtVectorType>(T2);
510 if (!IsStructurallyEquivalent(Context,
511 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
512 return false;
513 if (!IsStructurallyEquivalent(Context,
514 Vec1->getElementType(),
515 Vec2->getElementType()))
516 return false;
517 break;
518 }
519
520 case Type::Vector:
521 case Type::ExtVector: {
522 const VectorType *Vec1 = cast<VectorType>(T1);
523 const VectorType *Vec2 = cast<VectorType>(T2);
524 if (!IsStructurallyEquivalent(Context,
525 Vec1->getElementType(),
526 Vec2->getElementType()))
527 return false;
528 if (Vec1->getNumElements() != Vec2->getNumElements())
529 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000530 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000531 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000532 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000533 }
534
535 case Type::FunctionProto: {
536 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
537 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000538 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000539 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000540 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
541 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
542 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000543 return false;
544 }
545 if (Proto1->isVariadic() != Proto2->isVariadic())
546 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000547 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000548 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000549 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
550 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
551 return false;
552 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
553 if (!IsStructurallyEquivalent(Context,
554 Proto1->getExceptionType(I),
555 Proto2->getExceptionType(I)))
556 return false;
557 }
558 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000559 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000560 Proto1->getNoexceptExpr(),
561 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000562 return false;
563 }
564 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
565 return false;
566
567 // Fall through to check the bits common with FunctionNoProtoType.
568 }
569
570 case Type::FunctionNoProto: {
571 const FunctionType *Function1 = cast<FunctionType>(T1);
572 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000573 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
574 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000575 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000576 if (Function1->getExtInfo() != Function2->getExtInfo())
577 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000578 break;
579 }
580
581 case Type::UnresolvedUsing:
582 if (!IsStructurallyEquivalent(Context,
583 cast<UnresolvedUsingType>(T1)->getDecl(),
584 cast<UnresolvedUsingType>(T2)->getDecl()))
585 return false;
586
587 break;
John McCall81904512011-01-06 01:58:22 +0000588
589 case Type::Attributed:
590 if (!IsStructurallyEquivalent(Context,
591 cast<AttributedType>(T1)->getModifiedType(),
592 cast<AttributedType>(T2)->getModifiedType()))
593 return false;
594 if (!IsStructurallyEquivalent(Context,
595 cast<AttributedType>(T1)->getEquivalentType(),
596 cast<AttributedType>(T2)->getEquivalentType()))
597 return false;
598 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000599
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000600 case Type::Paren:
601 if (!IsStructurallyEquivalent(Context,
602 cast<ParenType>(T1)->getInnerType(),
603 cast<ParenType>(T2)->getInnerType()))
604 return false;
605 break;
606
Douglas Gregor3996e242010-02-15 22:01:00 +0000607 case Type::Typedef:
608 if (!IsStructurallyEquivalent(Context,
609 cast<TypedefType>(T1)->getDecl(),
610 cast<TypedefType>(T2)->getDecl()))
611 return false;
612 break;
613
614 case Type::TypeOfExpr:
615 if (!IsStructurallyEquivalent(Context,
616 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
617 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
618 return false;
619 break;
620
621 case Type::TypeOf:
622 if (!IsStructurallyEquivalent(Context,
623 cast<TypeOfType>(T1)->getUnderlyingType(),
624 cast<TypeOfType>(T2)->getUnderlyingType()))
625 return false;
626 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000627
628 case Type::UnaryTransform:
629 if (!IsStructurallyEquivalent(Context,
630 cast<UnaryTransformType>(T1)->getUnderlyingType(),
631 cast<UnaryTransformType>(T1)->getUnderlyingType()))
632 return false;
633 break;
634
Douglas Gregor3996e242010-02-15 22:01:00 +0000635 case Type::Decltype:
636 if (!IsStructurallyEquivalent(Context,
637 cast<DecltypeType>(T1)->getUnderlyingExpr(),
638 cast<DecltypeType>(T2)->getUnderlyingExpr()))
639 return false;
640 break;
641
Richard Smith30482bc2011-02-20 03:19:35 +0000642 case Type::Auto:
643 if (!IsStructurallyEquivalent(Context,
644 cast<AutoType>(T1)->getDeducedType(),
645 cast<AutoType>(T2)->getDeducedType()))
646 return false;
647 break;
648
Douglas Gregor3996e242010-02-15 22:01:00 +0000649 case Type::Record:
650 case Type::Enum:
651 if (!IsStructurallyEquivalent(Context,
652 cast<TagType>(T1)->getDecl(),
653 cast<TagType>(T2)->getDecl()))
654 return false;
655 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000656
Douglas Gregor3996e242010-02-15 22:01:00 +0000657 case Type::TemplateTypeParm: {
658 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
659 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
660 if (Parm1->getDepth() != Parm2->getDepth())
661 return false;
662 if (Parm1->getIndex() != Parm2->getIndex())
663 return false;
664 if (Parm1->isParameterPack() != Parm2->isParameterPack())
665 return false;
666
667 // Names of template type parameters are never significant.
668 break;
669 }
670
671 case Type::SubstTemplateTypeParm: {
672 const SubstTemplateTypeParmType *Subst1
673 = cast<SubstTemplateTypeParmType>(T1);
674 const SubstTemplateTypeParmType *Subst2
675 = cast<SubstTemplateTypeParmType>(T2);
676 if (!IsStructurallyEquivalent(Context,
677 QualType(Subst1->getReplacedParameter(), 0),
678 QualType(Subst2->getReplacedParameter(), 0)))
679 return false;
680 if (!IsStructurallyEquivalent(Context,
681 Subst1->getReplacementType(),
682 Subst2->getReplacementType()))
683 return false;
684 break;
685 }
686
Douglas Gregorfb322d82011-01-14 05:11:40 +0000687 case Type::SubstTemplateTypeParmPack: {
688 const SubstTemplateTypeParmPackType *Subst1
689 = cast<SubstTemplateTypeParmPackType>(T1);
690 const SubstTemplateTypeParmPackType *Subst2
691 = cast<SubstTemplateTypeParmPackType>(T2);
692 if (!IsStructurallyEquivalent(Context,
693 QualType(Subst1->getReplacedParameter(), 0),
694 QualType(Subst2->getReplacedParameter(), 0)))
695 return false;
696 if (!IsStructurallyEquivalent(Context,
697 Subst1->getArgumentPack(),
698 Subst2->getArgumentPack()))
699 return false;
700 break;
701 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000702 case Type::TemplateSpecialization: {
703 const TemplateSpecializationType *Spec1
704 = cast<TemplateSpecializationType>(T1);
705 const TemplateSpecializationType *Spec2
706 = cast<TemplateSpecializationType>(T2);
707 if (!IsStructurallyEquivalent(Context,
708 Spec1->getTemplateName(),
709 Spec2->getTemplateName()))
710 return false;
711 if (Spec1->getNumArgs() != Spec2->getNumArgs())
712 return false;
713 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
714 if (!IsStructurallyEquivalent(Context,
715 Spec1->getArg(I), Spec2->getArg(I)))
716 return false;
717 }
718 break;
719 }
720
Abramo Bagnara6150c882010-05-11 21:36:43 +0000721 case Type::Elaborated: {
722 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
723 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
724 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
725 if (Elab1->getKeyword() != Elab2->getKeyword())
726 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000727 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000728 Elab1->getQualifier(),
729 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000730 return false;
731 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000732 Elab1->getNamedType(),
733 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000734 return false;
735 break;
736 }
737
John McCalle78aac42010-03-10 03:28:59 +0000738 case Type::InjectedClassName: {
739 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
740 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
741 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000742 Inj1->getInjectedSpecializationType(),
743 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000744 return false;
745 break;
746 }
747
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000748 case Type::DependentName: {
749 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
750 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000751 if (!IsStructurallyEquivalent(Context,
752 Typename1->getQualifier(),
753 Typename2->getQualifier()))
754 return false;
755 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
756 Typename2->getIdentifier()))
757 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000758
759 break;
760 }
761
John McCallc392f372010-06-11 00:33:02 +0000762 case Type::DependentTemplateSpecialization: {
763 const DependentTemplateSpecializationType *Spec1 =
764 cast<DependentTemplateSpecializationType>(T1);
765 const DependentTemplateSpecializationType *Spec2 =
766 cast<DependentTemplateSpecializationType>(T2);
767 if (!IsStructurallyEquivalent(Context,
768 Spec1->getQualifier(),
769 Spec2->getQualifier()))
770 return false;
771 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
772 Spec2->getIdentifier()))
773 return false;
774 if (Spec1->getNumArgs() != Spec2->getNumArgs())
775 return false;
776 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
777 if (!IsStructurallyEquivalent(Context,
778 Spec1->getArg(I), Spec2->getArg(I)))
779 return false;
780 }
781 break;
782 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000783
784 case Type::PackExpansion:
785 if (!IsStructurallyEquivalent(Context,
786 cast<PackExpansionType>(T1)->getPattern(),
787 cast<PackExpansionType>(T2)->getPattern()))
788 return false;
789 break;
790
Douglas Gregor3996e242010-02-15 22:01:00 +0000791 case Type::ObjCInterface: {
792 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
793 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
794 if (!IsStructurallyEquivalent(Context,
795 Iface1->getDecl(), Iface2->getDecl()))
796 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000797 break;
798 }
799
800 case Type::ObjCObject: {
801 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
802 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
803 if (!IsStructurallyEquivalent(Context,
804 Obj1->getBaseType(),
805 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000806 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000807 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
808 return false;
809 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000810 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000811 Obj1->getProtocol(I),
812 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000813 return false;
814 }
815 break;
816 }
817
818 case Type::ObjCObjectPointer: {
819 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
820 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
821 if (!IsStructurallyEquivalent(Context,
822 Ptr1->getPointeeType(),
823 Ptr2->getPointeeType()))
824 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000825 break;
826 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000827
828 case Type::Atomic: {
829 if (!IsStructurallyEquivalent(Context,
830 cast<AtomicType>(T1)->getValueType(),
831 cast<AtomicType>(T2)->getValueType()))
832 return false;
833 break;
834 }
835
Douglas Gregor3996e242010-02-15 22:01:00 +0000836 } // end switch
837
838 return true;
839}
840
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000841/// \brief Determine structural equivalence of two fields.
842static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
843 FieldDecl *Field1, FieldDecl *Field2) {
844 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000845
846 // For anonymous structs/unions, match up the anonymous struct/union type
847 // declarations directly, so that we don't go off searching for anonymous
848 // types
849 if (Field1->isAnonymousStructOrUnion() &&
850 Field2->isAnonymousStructOrUnion()) {
851 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
852 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
853 return IsStructurallyEquivalent(Context, D1, D2);
854 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000855
856 // Check for equivalent field names.
857 IdentifierInfo *Name1 = Field1->getIdentifier();
858 IdentifierInfo *Name2 = Field2->getIdentifier();
859 if (!::IsStructurallyEquivalent(Name1, Name2))
860 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000861
862 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000863 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000864 if (Context.Complain) {
865 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
866 << Context.C2.getTypeDeclType(Owner2);
867 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
868 << Field2->getDeclName() << Field2->getType();
869 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
870 << Field1->getDeclName() << Field1->getType();
871 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000872 return false;
873 }
874
875 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000876 if (Context.Complain) {
877 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
878 << Context.C2.getTypeDeclType(Owner2);
879 if (Field1->isBitField()) {
880 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
881 << Field1->getDeclName() << Field1->getType()
882 << Field1->getBitWidthValue(Context.C1);
883 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
884 << Field2->getDeclName();
885 } else {
886 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
887 << Field2->getDeclName() << Field2->getType()
888 << Field2->getBitWidthValue(Context.C2);
889 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
890 << Field1->getDeclName();
891 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000892 }
893 return false;
894 }
895
896 if (Field1->isBitField()) {
897 // Make sure that the bit-fields are the same length.
898 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
899 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
900
901 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000902 if (Context.Complain) {
903 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
904 << Context.C2.getTypeDeclType(Owner2);
905 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
906 << Field2->getDeclName() << Field2->getType() << Bits2;
907 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
908 << Field1->getDeclName() << Field1->getType() << Bits1;
909 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000910 return false;
911 }
912 }
913
914 return true;
915}
916
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000917/// \brief Find the index of the given anonymous struct/union within its
918/// context.
919///
920/// \returns Returns the index of this anonymous struct/union in its context,
921/// including the next assigned index (if none of them match). Returns an
922/// empty option if the context is not a record, i.e.. if the anonymous
923/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000924static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000925 ASTContext &Context = Anon->getASTContext();
926 QualType AnonTy = Context.getRecordType(Anon);
927
928 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
929 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000930 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000931
932 unsigned Index = 0;
933 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
934 DEnd = Owner->noload_decls_end();
935 D != DEnd; ++D) {
936 FieldDecl *F = dyn_cast<FieldDecl>(*D);
937 if (!F || !F->isAnonymousStructOrUnion())
938 continue;
939
940 if (Context.hasSameType(F->getType(), AnonTy))
941 break;
942
943 ++Index;
944 }
945
946 return Index;
947}
948
Douglas Gregor3996e242010-02-15 22:01:00 +0000949/// \brief Determine structural equivalence of two records.
950static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
951 RecordDecl *D1, RecordDecl *D2) {
952 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000953 if (Context.Complain) {
954 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
955 << Context.C2.getTypeDeclType(D2);
956 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
957 << D1->getDeclName() << (unsigned)D1->getTagKind();
958 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000959 return false;
960 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000961
962 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
963 // If both anonymous structs/unions are in a record context, make sure
964 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +0000965 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
966 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000967 if (*Index1 != *Index2)
968 return false;
969 }
970 }
971 }
972
Douglas Gregore2e50d332010-12-01 01:36:18 +0000973 // If both declarations are class template specializations, we know
974 // the ODR applies, so check the template and template arguments.
975 ClassTemplateSpecializationDecl *Spec1
976 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
977 ClassTemplateSpecializationDecl *Spec2
978 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
979 if (Spec1 && Spec2) {
980 // Check that the specialized templates are the same.
981 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
982 Spec2->getSpecializedTemplate()))
983 return false;
984
985 // Check that the template arguments are the same.
986 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
987 return false;
988
989 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
990 if (!IsStructurallyEquivalent(Context,
991 Spec1->getTemplateArgs().get(I),
992 Spec2->getTemplateArgs().get(I)))
993 return false;
994 }
995 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +0000996 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +0000997 else if (Spec1 || Spec2)
998 return false;
999
Douglas Gregorb4964f72010-02-15 23:54:17 +00001000 // Compare the definitions of these two records. If either or both are
1001 // incomplete, we assume that they are equivalent.
1002 D1 = D1->getDefinition();
1003 D2 = D2->getDefinition();
1004 if (!D1 || !D2)
1005 return true;
1006
Douglas Gregor3996e242010-02-15 22:01:00 +00001007 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1008 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1009 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001010 if (Context.Complain) {
1011 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1012 << Context.C2.getTypeDeclType(D2);
1013 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1014 << D2CXX->getNumBases();
1015 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1016 << D1CXX->getNumBases();
1017 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001018 return false;
1019 }
1020
1021 // Check the base classes.
1022 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1023 BaseEnd1 = D1CXX->bases_end(),
1024 Base2 = D2CXX->bases_begin();
1025 Base1 != BaseEnd1;
1026 ++Base1, ++Base2) {
1027 if (!IsStructurallyEquivalent(Context,
1028 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001029 if (Context.Complain) {
1030 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1031 << Context.C2.getTypeDeclType(D2);
1032 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1033 << Base2->getType()
1034 << Base2->getSourceRange();
1035 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1036 << Base1->getType()
1037 << Base1->getSourceRange();
1038 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001039 return false;
1040 }
1041
1042 // Check virtual vs. non-virtual inheritance mismatch.
1043 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001044 if (Context.Complain) {
1045 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1046 << Context.C2.getTypeDeclType(D2);
1047 Context.Diag2(Base2->getLocStart(),
1048 diag::note_odr_virtual_base)
1049 << Base2->isVirtual() << Base2->getSourceRange();
1050 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1051 << Base1->isVirtual()
1052 << Base1->getSourceRange();
1053 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001054 return false;
1055 }
1056 }
1057 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001058 if (Context.Complain) {
1059 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1060 << Context.C2.getTypeDeclType(D2);
1061 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1062 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1063 << Base1->getType()
1064 << Base1->getSourceRange();
1065 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1066 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001067 return false;
1068 }
1069 }
1070
1071 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001072 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001073 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001074 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001075 Field1End = D1->field_end();
1076 Field1 != Field1End;
1077 ++Field1, ++Field2) {
1078 if (Field2 == Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001079 if (Context.Complain) {
1080 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1081 << Context.C2.getTypeDeclType(D2);
1082 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
1083 << Field1->getDeclName() << Field1->getType();
1084 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1085 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001086 return false;
1087 }
1088
David Blaikie40ed2972012-06-06 20:45:41 +00001089 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001090 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001091 }
1092
1093 if (Field2 != Field2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001094 if (Context.Complain) {
1095 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1096 << Context.C2.getTypeDeclType(D2);
1097 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
1098 << Field2->getDeclName() << Field2->getType();
1099 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1100 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001101 return false;
1102 }
1103
1104 return true;
1105}
1106
1107/// \brief Determine structural equivalence of two enums.
1108static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1109 EnumDecl *D1, EnumDecl *D2) {
1110 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1111 EC2End = D2->enumerator_end();
1112 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1113 EC1End = D1->enumerator_end();
1114 EC1 != EC1End; ++EC1, ++EC2) {
1115 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001116 if (Context.Complain) {
1117 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1118 << Context.C2.getTypeDeclType(D2);
1119 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1120 << EC1->getDeclName()
1121 << EC1->getInitVal().toString(10);
1122 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1123 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001124 return false;
1125 }
1126
1127 llvm::APSInt Val1 = EC1->getInitVal();
1128 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001129 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001130 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001131 if (Context.Complain) {
1132 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1133 << Context.C2.getTypeDeclType(D2);
1134 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1135 << EC2->getDeclName()
1136 << EC2->getInitVal().toString(10);
1137 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1138 << EC1->getDeclName()
1139 << EC1->getInitVal().toString(10);
1140 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001141 return false;
1142 }
1143 }
1144
1145 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001146 if (Context.Complain) {
1147 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1148 << Context.C2.getTypeDeclType(D2);
1149 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1150 << EC2->getDeclName()
1151 << EC2->getInitVal().toString(10);
1152 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1153 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001154 return false;
1155 }
1156
1157 return true;
1158}
Douglas Gregora082a492010-11-30 19:14:50 +00001159
1160static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1161 TemplateParameterList *Params1,
1162 TemplateParameterList *Params2) {
1163 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001164 if (Context.Complain) {
1165 Context.Diag2(Params2->getTemplateLoc(),
1166 diag::err_odr_different_num_template_parameters)
1167 << Params1->size() << Params2->size();
1168 Context.Diag1(Params1->getTemplateLoc(),
1169 diag::note_odr_template_parameter_list);
1170 }
Douglas Gregora082a492010-11-30 19:14:50 +00001171 return false;
1172 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001173
Douglas Gregora082a492010-11-30 19:14:50 +00001174 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1175 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001176 if (Context.Complain) {
1177 Context.Diag2(Params2->getParam(I)->getLocation(),
1178 diag::err_odr_different_template_parameter_kind);
1179 Context.Diag1(Params1->getParam(I)->getLocation(),
1180 diag::note_odr_template_parameter_here);
1181 }
Douglas Gregora082a492010-11-30 19:14:50 +00001182 return false;
1183 }
1184
1185 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1186 Params2->getParam(I))) {
1187
1188 return false;
1189 }
1190 }
1191
1192 return true;
1193}
1194
1195static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1196 TemplateTypeParmDecl *D1,
1197 TemplateTypeParmDecl *D2) {
1198 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001199 if (Context.Complain) {
1200 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1201 << D2->isParameterPack();
1202 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1203 << D1->isParameterPack();
1204 }
Douglas Gregora082a492010-11-30 19:14:50 +00001205 return false;
1206 }
1207
1208 return true;
1209}
1210
1211static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1212 NonTypeTemplateParmDecl *D1,
1213 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001214 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001215 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001216 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1217 << D2->isParameterPack();
1218 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1219 << D1->isParameterPack();
1220 }
Douglas Gregora082a492010-11-30 19:14:50 +00001221 return false;
1222 }
Douglas Gregora082a492010-11-30 19:14:50 +00001223
1224 // Check types.
1225 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001226 if (Context.Complain) {
1227 Context.Diag2(D2->getLocation(),
1228 diag::err_odr_non_type_parameter_type_inconsistent)
1229 << D2->getType() << D1->getType();
1230 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1231 << D1->getType();
1232 }
Douglas Gregora082a492010-11-30 19:14:50 +00001233 return false;
1234 }
1235
1236 return true;
1237}
1238
1239static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1240 TemplateTemplateParmDecl *D1,
1241 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001242 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001243 if (Context.Complain) {
1244 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1245 << D2->isParameterPack();
1246 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1247 << D1->isParameterPack();
1248 }
Douglas Gregora082a492010-11-30 19:14:50 +00001249 return false;
1250 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001251
Douglas Gregora082a492010-11-30 19:14:50 +00001252 // Check template parameter lists.
1253 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1254 D2->getTemplateParameters());
1255}
1256
1257static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1258 ClassTemplateDecl *D1,
1259 ClassTemplateDecl *D2) {
1260 // Check template parameters.
1261 if (!IsStructurallyEquivalent(Context,
1262 D1->getTemplateParameters(),
1263 D2->getTemplateParameters()))
1264 return false;
1265
1266 // Check the templated declaration.
1267 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1268 D2->getTemplatedDecl());
1269}
1270
Douglas Gregor3996e242010-02-15 22:01:00 +00001271/// \brief Determine structural equivalence of two declarations.
1272static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1273 Decl *D1, Decl *D2) {
1274 // FIXME: Check for known structural equivalences via a callback of some sort.
1275
Douglas Gregorb4964f72010-02-15 23:54:17 +00001276 // Check whether we already know that these two declarations are not
1277 // structurally equivalent.
1278 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1279 D2->getCanonicalDecl())))
1280 return false;
1281
Douglas Gregor3996e242010-02-15 22:01:00 +00001282 // Determine whether we've already produced a tentative equivalence for D1.
1283 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1284 if (EquivToD1)
1285 return EquivToD1 == D2->getCanonicalDecl();
1286
1287 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1288 EquivToD1 = D2->getCanonicalDecl();
1289 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1290 return true;
1291}
1292
1293bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1294 Decl *D2) {
1295 if (!::IsStructurallyEquivalent(*this, D1, D2))
1296 return false;
1297
1298 return !Finish();
1299}
1300
1301bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1302 QualType T2) {
1303 if (!::IsStructurallyEquivalent(*this, T1, T2))
1304 return false;
1305
1306 return !Finish();
1307}
1308
1309bool StructuralEquivalenceContext::Finish() {
1310 while (!DeclsToCheck.empty()) {
1311 // Check the next declaration.
1312 Decl *D1 = DeclsToCheck.front();
1313 DeclsToCheck.pop_front();
1314
1315 Decl *D2 = TentativeEquivalences[D1];
1316 assert(D2 && "Unrecorded tentative equivalence?");
1317
Douglas Gregorb4964f72010-02-15 23:54:17 +00001318 bool Equivalent = true;
1319
Douglas Gregor3996e242010-02-15 22:01:00 +00001320 // FIXME: Switch on all declaration kinds. For now, we're just going to
1321 // check the obvious ones.
1322 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1323 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1324 // Check for equivalent structure names.
1325 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001326 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1327 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001328 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001329 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1330 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001331 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1332 !::IsStructurallyEquivalent(*this, Record1, Record2))
1333 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001334 } else {
1335 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001336 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001337 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001338 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001339 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1340 // Check for equivalent enum names.
1341 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001342 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1343 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001344 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001345 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1346 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001347 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1348 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1349 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001350 } else {
1351 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001352 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001353 }
Richard Smithdda56e42011-04-15 14:24:37 +00001354 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1355 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001356 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001357 Typedef2->getIdentifier()) ||
1358 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001359 Typedef1->getUnderlyingType(),
1360 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001361 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001362 } else {
1363 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001364 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001365 }
Douglas Gregora082a492010-11-30 19:14:50 +00001366 } else if (ClassTemplateDecl *ClassTemplate1
1367 = dyn_cast<ClassTemplateDecl>(D1)) {
1368 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1369 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1370 ClassTemplate2->getIdentifier()) ||
1371 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1372 Equivalent = false;
1373 } else {
1374 // Class template/non-class-template mismatch.
1375 Equivalent = false;
1376 }
1377 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1378 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1379 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1380 Equivalent = false;
1381 } else {
1382 // Kind mismatch.
1383 Equivalent = false;
1384 }
1385 } else if (NonTypeTemplateParmDecl *NTTP1
1386 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1387 if (NonTypeTemplateParmDecl *NTTP2
1388 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1389 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1390 Equivalent = false;
1391 } else {
1392 // Kind mismatch.
1393 Equivalent = false;
1394 }
1395 } else if (TemplateTemplateParmDecl *TTP1
1396 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1397 if (TemplateTemplateParmDecl *TTP2
1398 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1399 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1400 Equivalent = false;
1401 } else {
1402 // Kind mismatch.
1403 Equivalent = false;
1404 }
1405 }
1406
Douglas Gregorb4964f72010-02-15 23:54:17 +00001407 if (!Equivalent) {
1408 // Note that these two declarations are not equivalent (and we already
1409 // know about it).
1410 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1411 D2->getCanonicalDecl()));
1412 return true;
1413 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001414 // FIXME: Check other declaration kinds!
1415 }
1416
1417 return false;
1418}
1419
1420//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001421// Import Types
1422//----------------------------------------------------------------------------
1423
John McCall424cec92011-01-19 06:33:43 +00001424QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001425 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1426 << T->getTypeClassName();
1427 return QualType();
1428}
1429
John McCall424cec92011-01-19 06:33:43 +00001430QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001431 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001432#define SHARED_SINGLETON_TYPE(Expansion)
1433#define BUILTIN_TYPE(Id, SingletonId) \
1434 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1435#include "clang/AST/BuiltinTypes.def"
1436
1437 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1438 // context supports C++.
1439
1440 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1441 // context supports ObjC.
1442
Douglas Gregor96e578d2010-02-05 17:54:41 +00001443 case BuiltinType::Char_U:
1444 // The context we're importing from has an unsigned 'char'. If we're
1445 // importing into a context with a signed 'char', translate to
1446 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001447 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001448 return Importer.getToContext().UnsignedCharTy;
1449
1450 return Importer.getToContext().CharTy;
1451
Douglas Gregor96e578d2010-02-05 17:54:41 +00001452 case BuiltinType::Char_S:
1453 // The context we're importing from has an unsigned 'char'. If we're
1454 // importing into a context with a signed 'char', translate to
1455 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001456 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001457 return Importer.getToContext().SignedCharTy;
1458
1459 return Importer.getToContext().CharTy;
1460
Chris Lattnerad3467e2010-12-25 23:25:43 +00001461 case BuiltinType::WChar_S:
1462 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001463 // FIXME: If not in C++, shall we translate to the C equivalent of
1464 // wchar_t?
1465 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001466 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001467
1468 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001469}
1470
John McCall424cec92011-01-19 06:33:43 +00001471QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001472 QualType ToElementType = Importer.Import(T->getElementType());
1473 if (ToElementType.isNull())
1474 return QualType();
1475
1476 return Importer.getToContext().getComplexType(ToElementType);
1477}
1478
John McCall424cec92011-01-19 06:33:43 +00001479QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001480 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1481 if (ToPointeeType.isNull())
1482 return QualType();
1483
1484 return Importer.getToContext().getPointerType(ToPointeeType);
1485}
1486
John McCall424cec92011-01-19 06:33:43 +00001487QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001488 // FIXME: Check for blocks support in "to" context.
1489 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1490 if (ToPointeeType.isNull())
1491 return QualType();
1492
1493 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1494}
1495
John McCall424cec92011-01-19 06:33:43 +00001496QualType
1497ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001498 // FIXME: Check for C++ support in "to" context.
1499 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1500 if (ToPointeeType.isNull())
1501 return QualType();
1502
1503 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1504}
1505
John McCall424cec92011-01-19 06:33:43 +00001506QualType
1507ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001508 // FIXME: Check for C++0x support in "to" context.
1509 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1510 if (ToPointeeType.isNull())
1511 return QualType();
1512
1513 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1514}
1515
John McCall424cec92011-01-19 06:33:43 +00001516QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001517 // FIXME: Check for C++ support in "to" context.
1518 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1519 if (ToPointeeType.isNull())
1520 return QualType();
1521
1522 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1523 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1524 ClassType.getTypePtr());
1525}
1526
John McCall424cec92011-01-19 06:33:43 +00001527QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001528 QualType ToElementType = Importer.Import(T->getElementType());
1529 if (ToElementType.isNull())
1530 return QualType();
1531
1532 return Importer.getToContext().getConstantArrayType(ToElementType,
1533 T->getSize(),
1534 T->getSizeModifier(),
1535 T->getIndexTypeCVRQualifiers());
1536}
1537
John McCall424cec92011-01-19 06:33:43 +00001538QualType
1539ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 QualType ToElementType = Importer.Import(T->getElementType());
1541 if (ToElementType.isNull())
1542 return QualType();
1543
1544 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1545 T->getSizeModifier(),
1546 T->getIndexTypeCVRQualifiers());
1547}
1548
John McCall424cec92011-01-19 06:33:43 +00001549QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 QualType ToElementType = Importer.Import(T->getElementType());
1551 if (ToElementType.isNull())
1552 return QualType();
1553
1554 Expr *Size = Importer.Import(T->getSizeExpr());
1555 if (!Size)
1556 return QualType();
1557
1558 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1559 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1560 T->getSizeModifier(),
1561 T->getIndexTypeCVRQualifiers(),
1562 Brackets);
1563}
1564
John McCall424cec92011-01-19 06:33:43 +00001565QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001566 QualType ToElementType = Importer.Import(T->getElementType());
1567 if (ToElementType.isNull())
1568 return QualType();
1569
1570 return Importer.getToContext().getVectorType(ToElementType,
1571 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001572 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001573}
1574
John McCall424cec92011-01-19 06:33:43 +00001575QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001576 QualType ToElementType = Importer.Import(T->getElementType());
1577 if (ToElementType.isNull())
1578 return QualType();
1579
1580 return Importer.getToContext().getExtVectorType(ToElementType,
1581 T->getNumElements());
1582}
1583
John McCall424cec92011-01-19 06:33:43 +00001584QualType
1585ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001586 // FIXME: What happens if we're importing a function without a prototype
1587 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001588 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001589 if (ToResultType.isNull())
1590 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001591
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001593 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001594}
1595
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001596QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001597 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001598 if (ToResultType.isNull())
1599 return QualType();
1600
1601 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001602 SmallVector<QualType, 4> ArgTypes;
Alp Toker9cacbab2014-01-20 20:26:09 +00001603 for (FunctionProtoType::param_type_iterator A = T->param_type_begin(),
1604 AEnd = T->param_type_end();
Douglas Gregor96e578d2010-02-05 17:54:41 +00001605 A != AEnd; ++A) {
1606 QualType ArgType = Importer.Import(*A);
1607 if (ArgType.isNull())
1608 return QualType();
1609 ArgTypes.push_back(ArgType);
1610 }
1611
1612 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001613 SmallVector<QualType, 4> ExceptionTypes;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001614 for (FunctionProtoType::exception_iterator E = T->exception_begin(),
1615 EEnd = T->exception_end();
1616 E != EEnd; ++E) {
1617 QualType ExceptionType = Importer.Import(*E);
1618 if (ExceptionType.isNull())
1619 return QualType();
1620 ExceptionTypes.push_back(ExceptionType);
1621 }
John McCalldb40c7f2010-12-14 08:05:40 +00001622
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001623 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1624 FunctionProtoType::ExtProtoInfo ToEPI;
1625
1626 ToEPI.ExtInfo = FromEPI.ExtInfo;
1627 ToEPI.Variadic = FromEPI.Variadic;
1628 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1629 ToEPI.TypeQuals = FromEPI.TypeQuals;
1630 ToEPI.RefQualifier = FromEPI.RefQualifier;
1631 ToEPI.NumExceptions = ExceptionTypes.size();
1632 ToEPI.Exceptions = ExceptionTypes.data();
Alp Tokerb3fd5cf2014-01-21 00:32:38 +00001633 ToEPI.ConsumedParameters = FromEPI.ConsumedParameters;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001634 ToEPI.ExceptionSpecType = FromEPI.ExceptionSpecType;
1635 ToEPI.NoexceptExpr = Importer.Import(FromEPI.NoexceptExpr);
1636 ToEPI.ExceptionSpecDecl = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001637 Importer.Import(FromEPI.ExceptionSpecDecl));
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001638 ToEPI.ExceptionSpecTemplate = cast_or_null<FunctionDecl>(
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001639 Importer.Import(FromEPI.ExceptionSpecTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001640
Jordan Rose5c382722013-03-08 21:51:21 +00001641 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001642}
1643
Sean Callananda6df8a2011-08-11 16:56:07 +00001644QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1645 QualType ToInnerType = Importer.Import(T->getInnerType());
1646 if (ToInnerType.isNull())
1647 return QualType();
1648
1649 return Importer.getToContext().getParenType(ToInnerType);
1650}
1651
John McCall424cec92011-01-19 06:33:43 +00001652QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001653 TypedefNameDecl *ToDecl
1654 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001655 if (!ToDecl)
1656 return QualType();
1657
1658 return Importer.getToContext().getTypeDeclType(ToDecl);
1659}
1660
John McCall424cec92011-01-19 06:33:43 +00001661QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001662 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1663 if (!ToExpr)
1664 return QualType();
1665
1666 return Importer.getToContext().getTypeOfExprType(ToExpr);
1667}
1668
John McCall424cec92011-01-19 06:33:43 +00001669QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001670 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1671 if (ToUnderlyingType.isNull())
1672 return QualType();
1673
1674 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1675}
1676
John McCall424cec92011-01-19 06:33:43 +00001677QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001678 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001679 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1680 if (!ToExpr)
1681 return QualType();
1682
Douglas Gregor81495f32012-02-12 18:42:33 +00001683 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1684 if (UnderlyingType.isNull())
1685 return QualType();
1686
1687 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001688}
1689
Alexis Hunte852b102011-05-24 22:41:36 +00001690QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1691 QualType ToBaseType = Importer.Import(T->getBaseType());
1692 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1693 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1694 return QualType();
1695
1696 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1697 ToUnderlyingType,
1698 T->getUTTKind());
1699}
1700
Richard Smith30482bc2011-02-20 03:19:35 +00001701QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001702 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001703 QualType FromDeduced = T->getDeducedType();
1704 QualType ToDeduced;
1705 if (!FromDeduced.isNull()) {
1706 ToDeduced = Importer.Import(FromDeduced);
1707 if (ToDeduced.isNull())
1708 return QualType();
1709 }
1710
Faisal Vali2b391ab2013-09-26 19:54:12 +00001711 return Importer.getToContext().getAutoType(ToDeduced, T->isDecltypeAuto(),
1712 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001713}
1714
John McCall424cec92011-01-19 06:33:43 +00001715QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001716 RecordDecl *ToDecl
1717 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1718 if (!ToDecl)
1719 return QualType();
1720
1721 return Importer.getToContext().getTagDeclType(ToDecl);
1722}
1723
John McCall424cec92011-01-19 06:33:43 +00001724QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001725 EnumDecl *ToDecl
1726 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1727 if (!ToDecl)
1728 return QualType();
1729
1730 return Importer.getToContext().getTagDeclType(ToDecl);
1731}
1732
Douglas Gregore2e50d332010-12-01 01:36:18 +00001733QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001734 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001735 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1736 if (ToTemplate.isNull())
1737 return QualType();
1738
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001739 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001740 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1741 return QualType();
1742
1743 QualType ToCanonType;
1744 if (!QualType(T, 0).isCanonical()) {
1745 QualType FromCanonType
1746 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1747 ToCanonType =Importer.Import(FromCanonType);
1748 if (ToCanonType.isNull())
1749 return QualType();
1750 }
1751 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1752 ToTemplateArgs.data(),
1753 ToTemplateArgs.size(),
1754 ToCanonType);
1755}
1756
John McCall424cec92011-01-19 06:33:43 +00001757QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +00001758 NestedNameSpecifier *ToQualifier = 0;
1759 // Note: the qualifier in an ElaboratedType is optional.
1760 if (T->getQualifier()) {
1761 ToQualifier = Importer.Import(T->getQualifier());
1762 if (!ToQualifier)
1763 return QualType();
1764 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765
1766 QualType ToNamedType = Importer.Import(T->getNamedType());
1767 if (ToNamedType.isNull())
1768 return QualType();
1769
Abramo Bagnara6150c882010-05-11 21:36:43 +00001770 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1771 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001772}
1773
John McCall424cec92011-01-19 06:33:43 +00001774QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001775 ObjCInterfaceDecl *Class
1776 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1777 if (!Class)
1778 return QualType();
1779
John McCall8b07ec22010-05-15 11:32:37 +00001780 return Importer.getToContext().getObjCInterfaceType(Class);
1781}
1782
John McCall424cec92011-01-19 06:33:43 +00001783QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001784 QualType ToBaseType = Importer.Import(T->getBaseType());
1785 if (ToBaseType.isNull())
1786 return QualType();
1787
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001788 SmallVector<ObjCProtocolDecl *, 4> Protocols;
John McCall8b07ec22010-05-15 11:32:37 +00001789 for (ObjCObjectType::qual_iterator P = T->qual_begin(),
Douglas Gregor96e578d2010-02-05 17:54:41 +00001790 PEnd = T->qual_end();
1791 P != PEnd; ++P) {
1792 ObjCProtocolDecl *Protocol
1793 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(*P));
1794 if (!Protocol)
1795 return QualType();
1796 Protocols.push_back(Protocol);
1797 }
1798
John McCall8b07ec22010-05-15 11:32:37 +00001799 return Importer.getToContext().getObjCObjectType(ToBaseType,
1800 Protocols.data(),
1801 Protocols.size());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001802}
1803
John McCall424cec92011-01-19 06:33:43 +00001804QualType
1805ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001806 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1807 if (ToPointeeType.isNull())
1808 return QualType();
1809
John McCall8b07ec22010-05-15 11:32:37 +00001810 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001811}
1812
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001813//----------------------------------------------------------------------------
1814// Import Declarations
1815//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001816bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1817 DeclContext *&LexicalDC,
1818 DeclarationName &Name,
1819 SourceLocation &Loc) {
1820 // Import the context of this declaration.
1821 DC = Importer.ImportContext(D->getDeclContext());
1822 if (!DC)
1823 return true;
1824
1825 LexicalDC = DC;
1826 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1827 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1828 if (!LexicalDC)
1829 return true;
1830 }
1831
1832 // Import the name of this declaration.
1833 Name = Importer.Import(D->getDeclName());
1834 if (D->getDeclName() && !Name)
1835 return true;
1836
1837 // Import the location of this declaration.
1838 Loc = Importer.Import(D->getLocation());
1839 return false;
1840}
1841
Douglas Gregord451ea92011-07-29 23:31:30 +00001842void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1843 if (!FromD)
1844 return;
1845
1846 if (!ToD) {
1847 ToD = Importer.Import(FromD);
1848 if (!ToD)
1849 return;
1850 }
1851
1852 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1853 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001854 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001855 ImportDefinition(FromRecord, ToRecord);
1856 }
1857 }
1858 return;
1859 }
1860
1861 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1862 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1863 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1864 ImportDefinition(FromEnum, ToEnum);
1865 }
1866 }
1867 return;
1868 }
1869}
1870
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001871void
1872ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1873 DeclarationNameInfo& To) {
1874 // NOTE: To.Name and To.Loc are already imported.
1875 // We only have to import To.LocInfo.
1876 switch (To.getName().getNameKind()) {
1877 case DeclarationName::Identifier:
1878 case DeclarationName::ObjCZeroArgSelector:
1879 case DeclarationName::ObjCOneArgSelector:
1880 case DeclarationName::ObjCMultiArgSelector:
1881 case DeclarationName::CXXUsingDirective:
1882 return;
1883
1884 case DeclarationName::CXXOperatorName: {
1885 SourceRange Range = From.getCXXOperatorNameRange();
1886 To.setCXXOperatorNameRange(Importer.Import(Range));
1887 return;
1888 }
1889 case DeclarationName::CXXLiteralOperatorName: {
1890 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1891 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1892 return;
1893 }
1894 case DeclarationName::CXXConstructorName:
1895 case DeclarationName::CXXDestructorName:
1896 case DeclarationName::CXXConversionFunctionName: {
1897 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1898 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1899 return;
1900 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001901 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001902 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001903}
1904
Douglas Gregor2e15c842012-02-01 21:00:38 +00001905void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001906 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001907 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001908 return;
1909 }
1910
Douglas Gregor968d6332010-02-21 18:24:45 +00001911 for (DeclContext::decl_iterator From = FromDC->decls_begin(),
1912 FromEnd = FromDC->decls_end();
1913 From != FromEnd;
1914 ++From)
1915 Importer.Import(*From);
1916}
1917
Douglas Gregord451ea92011-07-29 23:31:30 +00001918bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001919 ImportDefinitionKind Kind) {
1920 if (To->getDefinition() || To->isBeingDefined()) {
1921 if (Kind == IDK_Everything)
1922 ImportDeclContext(From, /*ForceImport=*/true);
1923
Douglas Gregore2e50d332010-12-01 01:36:18 +00001924 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001925 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00001926
1927 To->startDefinition();
1928
1929 // Add base classes.
1930 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
1931 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001932
1933 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
1934 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
1935 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001936 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001937 ToData.Aggregate = FromData.Aggregate;
1938 ToData.PlainOldData = FromData.PlainOldData;
1939 ToData.Empty = FromData.Empty;
1940 ToData.Polymorphic = FromData.Polymorphic;
1941 ToData.Abstract = FromData.Abstract;
1942 ToData.IsStandardLayout = FromData.IsStandardLayout;
1943 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
1944 ToData.HasPrivateFields = FromData.HasPrivateFields;
1945 ToData.HasProtectedFields = FromData.HasProtectedFields;
1946 ToData.HasPublicFields = FromData.HasPublicFields;
1947 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00001948 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00001949 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00001950 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00001951 ToData.HasUninitializedReferenceMember
1952 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00001953 ToData.NeedOverloadResolutionForMoveConstructor
1954 = FromData.NeedOverloadResolutionForMoveConstructor;
1955 ToData.NeedOverloadResolutionForMoveAssignment
1956 = FromData.NeedOverloadResolutionForMoveAssignment;
1957 ToData.NeedOverloadResolutionForDestructor
1958 = FromData.NeedOverloadResolutionForDestructor;
1959 ToData.DefaultedMoveConstructorIsDeleted
1960 = FromData.DefaultedMoveConstructorIsDeleted;
1961 ToData.DefaultedMoveAssignmentIsDeleted
1962 = FromData.DefaultedMoveAssignmentIsDeleted;
1963 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00001964 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
1965 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001966 ToData.HasConstexprNonCopyMoveConstructor
1967 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00001968 ToData.DefaultedDefaultConstructorIsConstexpr
1969 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00001970 ToData.HasConstexprDefaultConstructor
1971 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001972 ToData.HasNonLiteralTypeFieldsOrBases
1973 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00001974 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00001975 ToData.UserProvidedDefaultConstructor
1976 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00001977 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00001978 ToData.ImplicitCopyConstructorHasConstParam
1979 = FromData.ImplicitCopyConstructorHasConstParam;
1980 ToData.ImplicitCopyAssignmentHasConstParam
1981 = FromData.ImplicitCopyAssignmentHasConstParam;
1982 ToData.HasDeclaredCopyConstructorWithConstParam
1983 = FromData.HasDeclaredCopyConstructorWithConstParam;
1984 ToData.HasDeclaredCopyAssignmentWithConstParam
1985 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00001986 ToData.IsLambda = FromData.IsLambda;
1987
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001988 SmallVector<CXXBaseSpecifier *, 4> Bases;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001989 for (CXXRecordDecl::base_class_iterator
1990 Base1 = FromCXX->bases_begin(),
1991 FromBaseEnd = FromCXX->bases_end();
1992 Base1 != FromBaseEnd;
1993 ++Base1) {
1994 QualType T = Importer.Import(Base1->getType());
1995 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00001996 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00001997
1998 SourceLocation EllipsisLoc;
1999 if (Base1->isPackExpansion())
2000 EllipsisLoc = Importer.Import(Base1->getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002001
2002 // Ensure that we have a definition for the base.
2003 ImportDefinitionIfNeeded(Base1->getType()->getAsCXXRecordDecl());
2004
Douglas Gregore2e50d332010-12-01 01:36:18 +00002005 Bases.push_back(
2006 new (Importer.getToContext())
2007 CXXBaseSpecifier(Importer.Import(Base1->getSourceRange()),
2008 Base1->isVirtual(),
2009 Base1->isBaseOfClass(),
2010 Base1->getAccessSpecifierAsWritten(),
Douglas Gregor752a5952011-01-03 22:36:02 +00002011 Importer.Import(Base1->getTypeSourceInfo()),
2012 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002013 }
2014 if (!Bases.empty())
2015 ToCXX->setBases(Bases.data(), Bases.size());
2016 }
2017
Douglas Gregor2e15c842012-02-01 21:00:38 +00002018 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002019 ImportDeclContext(From, /*ForceImport=*/true);
2020
Douglas Gregore2e50d332010-12-01 01:36:18 +00002021 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002022 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002023}
2024
Larisse Voufo39a1e502013-08-06 01:03:05 +00002025bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2026 ImportDefinitionKind Kind) {
2027 if (To->getDefinition())
2028 return false;
2029
2030 // FIXME: Can we really import any initializer? Alternatively, we could force
2031 // ourselves to import every declaration of a variable and then only use
2032 // getInit() here.
2033 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2034
2035 // FIXME: Other bits to merge?
2036
2037 return false;
2038}
2039
Douglas Gregord451ea92011-07-29 23:31:30 +00002040bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002041 ImportDefinitionKind Kind) {
2042 if (To->getDefinition() || To->isBeingDefined()) {
2043 if (Kind == IDK_Everything)
2044 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002045 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002046 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002047
2048 To->startDefinition();
2049
2050 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2051 if (T.isNull())
2052 return true;
2053
2054 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2055 if (ToPromotionType.isNull())
2056 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002057
2058 if (shouldForceImportDeclContext(Kind))
2059 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002060
2061 // FIXME: we might need to merge the number of positive or negative bits
2062 // if the enumerator lists don't match.
2063 To->completeDefinition(T, ToPromotionType,
2064 From->getNumPositiveBits(),
2065 From->getNumNegativeBits());
2066 return false;
2067}
2068
Douglas Gregora082a492010-11-30 19:14:50 +00002069TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2070 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002071 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002072 ToParams.reserve(Params->size());
2073 for (TemplateParameterList::iterator P = Params->begin(),
2074 PEnd = Params->end();
2075 P != PEnd; ++P) {
2076 Decl *To = Importer.Import(*P);
2077 if (!To)
2078 return 0;
2079
2080 ToParams.push_back(cast<NamedDecl>(To));
2081 }
2082
2083 return TemplateParameterList::Create(Importer.getToContext(),
2084 Importer.Import(Params->getTemplateLoc()),
2085 Importer.Import(Params->getLAngleLoc()),
2086 ToParams.data(), ToParams.size(),
2087 Importer.Import(Params->getRAngleLoc()));
2088}
2089
Douglas Gregore2e50d332010-12-01 01:36:18 +00002090TemplateArgument
2091ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2092 switch (From.getKind()) {
2093 case TemplateArgument::Null:
2094 return TemplateArgument();
2095
2096 case TemplateArgument::Type: {
2097 QualType ToType = Importer.Import(From.getAsType());
2098 if (ToType.isNull())
2099 return TemplateArgument();
2100 return TemplateArgument(ToType);
2101 }
2102
2103 case TemplateArgument::Integral: {
2104 QualType ToType = Importer.Import(From.getIntegralType());
2105 if (ToType.isNull())
2106 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002107 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002108 }
2109
Eli Friedmanb826a002012-09-26 02:36:12 +00002110 case TemplateArgument::Declaration: {
2111 ValueDecl *FromD = From.getAsDecl();
2112 if (ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(FromD)))
2113 return TemplateArgument(To, From.isDeclForReferenceParam());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002114 return TemplateArgument();
Eli Friedmanb826a002012-09-26 02:36:12 +00002115 }
2116
2117 case TemplateArgument::NullPtr: {
2118 QualType ToType = Importer.Import(From.getNullPtrType());
2119 if (ToType.isNull())
2120 return TemplateArgument();
2121 return TemplateArgument(ToType, /*isNullPtr*/true);
2122 }
2123
Douglas Gregore2e50d332010-12-01 01:36:18 +00002124 case TemplateArgument::Template: {
2125 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2126 if (ToTemplate.isNull())
2127 return TemplateArgument();
2128
2129 return TemplateArgument(ToTemplate);
2130 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002131
2132 case TemplateArgument::TemplateExpansion: {
2133 TemplateName ToTemplate
2134 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2135 if (ToTemplate.isNull())
2136 return TemplateArgument();
2137
Douglas Gregore1d60df2011-01-14 23:41:42 +00002138 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002139 }
2140
Douglas Gregore2e50d332010-12-01 01:36:18 +00002141 case TemplateArgument::Expression:
2142 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2143 return TemplateArgument(ToExpr);
2144 return TemplateArgument();
2145
2146 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002147 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002148 ToPack.reserve(From.pack_size());
2149 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2150 return TemplateArgument();
2151
2152 TemplateArgument *ToArgs
2153 = new (Importer.getToContext()) TemplateArgument[ToPack.size()];
2154 std::copy(ToPack.begin(), ToPack.end(), ToArgs);
2155 return TemplateArgument(ToArgs, ToPack.size());
2156 }
2157 }
2158
2159 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002160}
2161
2162bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2163 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002164 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002165 for (unsigned I = 0; I != NumFromArgs; ++I) {
2166 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2167 if (To.isNull() && !FromArgs[I].isNull())
2168 return true;
2169
2170 ToArgs.push_back(To);
2171 }
2172
2173 return false;
2174}
2175
Douglas Gregor5c73e912010-02-11 00:48:18 +00002176bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002177 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002178 // Eliminate a potential failure point where we attempt to re-import
2179 // something we're trying to import while completing ToRecord.
2180 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2181 if (ToOrigin) {
2182 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2183 if (ToOriginRecord)
2184 ToRecord = ToOriginRecord;
2185 }
2186
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002187 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002188 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002189 Importer.getNonEquivalentDecls(),
2190 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002191 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002192}
2193
Larisse Voufo39a1e502013-08-06 01:03:05 +00002194bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2195 bool Complain) {
2196 StructuralEquivalenceContext Ctx(
2197 Importer.getFromContext(), Importer.getToContext(),
2198 Importer.getNonEquivalentDecls(), false, Complain);
2199 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2200}
2201
Douglas Gregor98c10182010-02-12 22:17:39 +00002202bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002203 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002204 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002205 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002206 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002207}
2208
Douglas Gregor91155082012-11-14 22:29:20 +00002209bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2210 EnumConstantDecl *ToEC)
2211{
2212 const llvm::APSInt &FromVal = FromEC->getInitVal();
2213 const llvm::APSInt &ToVal = ToEC->getInitVal();
2214
2215 return FromVal.isSigned() == ToVal.isSigned() &&
2216 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2217 FromVal == ToVal;
2218}
2219
2220bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002221 ClassTemplateDecl *To) {
2222 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2223 Importer.getToContext(),
2224 Importer.getNonEquivalentDecls());
2225 return Ctx.IsStructurallyEquivalent(From, To);
2226}
2227
Larisse Voufo39a1e502013-08-06 01:03:05 +00002228bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2229 VarTemplateDecl *To) {
2230 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2231 Importer.getToContext(),
2232 Importer.getNonEquivalentDecls());
2233 return Ctx.IsStructurallyEquivalent(From, To);
2234}
2235
Douglas Gregore4c83e42010-02-09 22:48:33 +00002236Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002237 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002238 << D->getDeclKindName();
2239 return 0;
2240}
2241
Sean Callanan65198272011-11-17 23:20:56 +00002242Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2243 TranslationUnitDecl *ToD =
2244 Importer.getToContext().getTranslationUnitDecl();
2245
2246 Importer.Imported(D, ToD);
2247
2248 return ToD;
2249}
2250
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002251Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2252 // Import the major distinguishing characteristics of this namespace.
2253 DeclContext *DC, *LexicalDC;
2254 DeclarationName Name;
2255 SourceLocation Loc;
2256 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2257 return 0;
2258
2259 NamespaceDecl *MergeWithNamespace = 0;
2260 if (!Name) {
2261 // This is an anonymous namespace. Adopt an existing anonymous
2262 // namespace if we can.
2263 // FIXME: Not testable.
2264 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2265 MergeWithNamespace = TU->getAnonymousNamespace();
2266 else
2267 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2268 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002269 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002270 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002271 DC->localUncachedLookup(Name, FoundDecls);
2272 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2273 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002274 continue;
2275
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002276 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002277 MergeWithNamespace = FoundNS;
2278 ConflictingDecls.clear();
2279 break;
2280 }
2281
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002282 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002283 }
2284
2285 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002286 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002287 ConflictingDecls.data(),
2288 ConflictingDecls.size());
2289 }
2290 }
2291
2292 // Create the "to" namespace, if needed.
2293 NamespaceDecl *ToNamespace = MergeWithNamespace;
2294 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002295 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002296 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002297 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002298 Loc, Name.getAsIdentifierInfo(),
2299 /*PrevDecl=*/0);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002300 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002301 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002302
2303 // If this is an anonymous namespace, register it as the anonymous
2304 // namespace within its context.
2305 if (!Name) {
2306 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2307 TU->setAnonymousNamespace(ToNamespace);
2308 else
2309 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2310 }
2311 }
2312 Importer.Imported(D, ToNamespace);
2313
2314 ImportDeclContext(D);
2315
2316 return ToNamespace;
2317}
2318
Richard Smithdda56e42011-04-15 14:24:37 +00002319Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002320 // Import the major distinguishing characteristics of this typedef.
2321 DeclContext *DC, *LexicalDC;
2322 DeclarationName Name;
2323 SourceLocation Loc;
2324 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2325 return 0;
2326
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002327 // If this typedef is not in block scope, determine whether we've
2328 // seen a typedef with the same name (that we can merge with) or any
2329 // other entity by that name (which name lookup could conflict with).
2330 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002331 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002332 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002333 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002334 DC->localUncachedLookup(Name, FoundDecls);
2335 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2336 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002337 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002338 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002339 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002340 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2341 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002342 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002343 }
2344
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002345 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002346 }
2347
2348 if (!ConflictingDecls.empty()) {
2349 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2350 ConflictingDecls.data(),
2351 ConflictingDecls.size());
2352 if (!Name)
2353 return 0;
2354 }
2355 }
2356
Douglas Gregorb4964f72010-02-15 23:54:17 +00002357 // Import the underlying type of this typedef;
2358 QualType T = Importer.Import(D->getUnderlyingType());
2359 if (T.isNull())
2360 return 0;
2361
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002362 // Create the new typedef node.
2363 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002364 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002365 TypedefNameDecl *ToTypedef;
2366 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002367 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2368 StartL, Loc,
2369 Name.getAsIdentifierInfo(),
2370 TInfo);
2371 else
Richard Smithdda56e42011-04-15 14:24:37 +00002372 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2373 StartL, Loc,
2374 Name.getAsIdentifierInfo(),
2375 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002376
Douglas Gregordd483172010-02-22 17:42:47 +00002377 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002378 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002379 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002380 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002381
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002382 return ToTypedef;
2383}
2384
Richard Smithdda56e42011-04-15 14:24:37 +00002385Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2386 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2387}
2388
2389Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2390 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2391}
2392
Douglas Gregor98c10182010-02-12 22:17:39 +00002393Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2394 // Import the major distinguishing characteristics of this enum.
2395 DeclContext *DC, *LexicalDC;
2396 DeclarationName Name;
2397 SourceLocation Loc;
2398 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2399 return 0;
2400
2401 // Figure out what enum name we're looking for.
2402 unsigned IDNS = Decl::IDNS_Tag;
2403 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002404 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2405 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002406 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002407 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002408 IDNS |= Decl::IDNS_Ordinary;
2409
2410 // We may already have an enum of the same name; try to find and match it.
2411 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002412 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002413 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002414 DC->localUncachedLookup(SearchName, FoundDecls);
2415 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2416 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002417 continue;
2418
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002419 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002420 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002421 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2422 Found = Tag->getDecl();
2423 }
2424
2425 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002426 if (IsStructuralMatch(D, FoundEnum))
2427 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002428 }
2429
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002430 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002431 }
2432
2433 if (!ConflictingDecls.empty()) {
2434 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2435 ConflictingDecls.data(),
2436 ConflictingDecls.size());
2437 }
2438 }
2439
2440 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002441 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2442 Importer.Import(D->getLocStart()),
2443 Loc, Name.getAsIdentifierInfo(), 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002444 D->isScoped(), D->isScopedUsingClassTag(),
2445 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002446 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002447 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002448 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002449 D2->setLexicalDeclContext(LexicalDC);
2450 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002451 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002452
2453 // Import the integer type.
2454 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2455 if (ToIntegerType.isNull())
2456 return 0;
Douglas Gregor3996e242010-02-15 22:01:00 +00002457 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002458
2459 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002460 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregord451ea92011-07-29 23:31:30 +00002461 return 0;
Douglas Gregor98c10182010-02-12 22:17:39 +00002462
Douglas Gregor3996e242010-02-15 22:01:00 +00002463 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002464}
2465
Douglas Gregor5c73e912010-02-11 00:48:18 +00002466Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2467 // If this record has a definition in the translation unit we're coming from,
2468 // but this particular declaration is not that definition, import the
2469 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002470 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002471 if (Definition && Definition != D) {
2472 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002473 if (!ImportedDef)
2474 return 0;
2475
2476 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002477 }
2478
2479 // Import the major distinguishing characteristics of this record.
2480 DeclContext *DC, *LexicalDC;
2481 DeclarationName Name;
2482 SourceLocation Loc;
2483 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2484 return 0;
2485
2486 // Figure out what structure name we're looking for.
2487 unsigned IDNS = Decl::IDNS_Tag;
2488 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002489 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2490 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002491 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002492 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002493 IDNS |= Decl::IDNS_Ordinary;
2494
2495 // We may already have a record of the same name; try to find and match it.
Douglas Gregor25791052010-02-12 00:09:27 +00002496 RecordDecl *AdoptDecl = 0;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002497 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002498 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002499 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002500 DC->localUncachedLookup(SearchName, FoundDecls);
2501 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2502 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002503 continue;
2504
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002505 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002506 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002507 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2508 Found = Tag->getDecl();
2509 }
2510
2511 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002512 if (D->isAnonymousStructOrUnion() &&
2513 FoundRecord->isAnonymousStructOrUnion()) {
2514 // If both anonymous structs/unions are in a record context, make sure
2515 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002516 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002517 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002518 if (Optional<unsigned> Index2 =
2519 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002520 if (*Index1 != *Index2)
2521 continue;
2522 }
2523 }
2524 }
2525
Douglas Gregor25791052010-02-12 00:09:27 +00002526 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002527 if ((SearchName && !D->isCompleteDefinition())
2528 || (D->isCompleteDefinition() &&
2529 D->isAnonymousStructOrUnion()
2530 == FoundDef->isAnonymousStructOrUnion() &&
2531 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002532 // The record types structurally match, or the "from" translation
2533 // unit only had a forward declaration anyway; call it the same
2534 // function.
2535 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002536 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002537 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002538 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002539 // We have a forward declaration of this type, so adopt that forward
2540 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002541
2542 // If one or both can be completed from external storage then try one
2543 // last time to complete and compare them before doing this.
2544
2545 if (FoundRecord->hasExternalLexicalStorage() &&
2546 !FoundRecord->isCompleteDefinition())
2547 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2548 if (D->hasExternalLexicalStorage())
2549 D->getASTContext().getExternalSource()->CompleteType(D);
2550
2551 if (FoundRecord->isCompleteDefinition() &&
2552 D->isCompleteDefinition() &&
2553 !IsStructuralMatch(D, FoundRecord))
2554 continue;
2555
Douglas Gregor25791052010-02-12 00:09:27 +00002556 AdoptDecl = FoundRecord;
2557 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002558 } else if (!SearchName) {
2559 continue;
2560 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002561 }
2562
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002563 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002564 }
2565
Douglas Gregordd6006f2012-07-17 21:16:27 +00002566 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002567 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2568 ConflictingDecls.data(),
2569 ConflictingDecls.size());
2570 }
2571 }
2572
2573 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002574 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002575 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002576 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002577 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002578 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002579 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002580 DC, StartLoc, Loc,
2581 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002582 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002583 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002584 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002585 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002586 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002587 }
Douglas Gregor14454802011-02-25 02:25:35 +00002588
2589 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002590 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002591 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002592 if (D->isAnonymousStructOrUnion())
2593 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002594 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002595
Douglas Gregor3996e242010-02-15 22:01:00 +00002596 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002597
Douglas Gregor95d82832012-01-24 18:36:04 +00002598 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Douglas Gregore2e50d332010-12-01 01:36:18 +00002599 return 0;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002600
Douglas Gregor3996e242010-02-15 22:01:00 +00002601 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002602}
2603
Douglas Gregor98c10182010-02-12 22:17:39 +00002604Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2605 // Import the major distinguishing characteristics of this enumerator.
2606 DeclContext *DC, *LexicalDC;
2607 DeclarationName Name;
2608 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002609 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor98c10182010-02-12 22:17:39 +00002610 return 0;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002611
2612 QualType T = Importer.Import(D->getType());
2613 if (T.isNull())
2614 return 0;
2615
Douglas Gregor98c10182010-02-12 22:17:39 +00002616 // Determine whether there are any other declarations with the same name and
2617 // in the same context.
2618 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002619 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002620 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002621 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002622 DC->localUncachedLookup(Name, FoundDecls);
2623 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2624 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002625 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002626
2627 if (EnumConstantDecl *FoundEnumConstant
2628 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2629 if (IsStructuralMatch(D, FoundEnumConstant))
2630 return Importer.Imported(D, FoundEnumConstant);
2631 }
2632
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002633 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002634 }
2635
2636 if (!ConflictingDecls.empty()) {
2637 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2638 ConflictingDecls.data(),
2639 ConflictingDecls.size());
2640 if (!Name)
2641 return 0;
2642 }
2643 }
2644
2645 Expr *Init = Importer.Import(D->getInitExpr());
2646 if (D->getInitExpr() && !Init)
2647 return 0;
2648
2649 EnumConstantDecl *ToEnumerator
2650 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2651 Name.getAsIdentifierInfo(), T,
2652 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002653 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002654 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002655 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002656 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002657 return ToEnumerator;
2658}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002659
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002660Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2661 // Import the major distinguishing characteristics of this function.
2662 DeclContext *DC, *LexicalDC;
2663 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002664 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002665 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002666 return 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002667
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002668 // Try to find a function in our own ("to") context with the same name, same
2669 // type, and in the same context as the function we're importing.
2670 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002671 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002672 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002673 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002674 DC->localUncachedLookup(Name, FoundDecls);
2675 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2676 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002677 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002678
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002679 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002680 if (FoundFunction->hasExternalFormalLinkage() &&
2681 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002682 if (Importer.IsStructurallyEquivalent(D->getType(),
2683 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002684 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002685 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002686 }
2687
2688 // FIXME: Check for overloading more carefully, e.g., by boosting
2689 // Sema::IsOverload out to the AST library.
2690
2691 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002692 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002693 continue;
2694
2695 // Complain about inconsistent function types.
2696 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002697 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002698 Importer.ToDiag(FoundFunction->getLocation(),
2699 diag::note_odr_value_here)
2700 << FoundFunction->getType();
2701 }
2702 }
2703
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002704 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002705 }
2706
2707 if (!ConflictingDecls.empty()) {
2708 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2709 ConflictingDecls.data(),
2710 ConflictingDecls.size());
2711 if (!Name)
2712 return 0;
2713 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002714 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002715
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002716 DeclarationNameInfo NameInfo(Name, Loc);
2717 // Import additional name location/type info.
2718 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2719
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002720 QualType FromTy = D->getType();
2721 bool usedDifferentExceptionSpec = false;
2722
2723 if (const FunctionProtoType *
2724 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2725 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2726 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2727 // FunctionDecl that we are importing the FunctionProtoType for.
2728 // To avoid an infinite recursion when importing, create the FunctionDecl
2729 // with a simplified function type and update it afterwards.
2730 if (FromEPI.ExceptionSpecDecl || FromEPI.ExceptionSpecTemplate ||
2731 FromEPI.NoexceptExpr) {
2732 FunctionProtoType::ExtProtoInfo DefaultEPI;
2733 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002734 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002735 usedDifferentExceptionSpec = true;
2736 }
2737 }
2738
Douglas Gregorb4964f72010-02-15 23:54:17 +00002739 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002740 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002741 if (T.isNull())
2742 return 0;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002743
2744 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002745 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002746 for (auto P : D->params()) {
2747 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002748 if (!ToP)
2749 return 0;
2750
2751 Parameters.push_back(ToP);
2752 }
2753
2754 // Create the imported function.
2755 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Douglas Gregor00eace12010-02-21 18:29:16 +00002756 FunctionDecl *ToFunction = 0;
2757 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2758 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2759 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002760 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002761 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002762 FromConstructor->isExplicit(),
2763 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002764 D->isImplicit(),
2765 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002766 } else if (isa<CXXDestructorDecl>(D)) {
2767 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2768 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002769 D->getInnerLocStart(),
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002770 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002771 D->isInlineSpecified(),
2772 D->isImplicit());
2773 } else if (CXXConversionDecl *FromConversion
2774 = dyn_cast<CXXConversionDecl>(D)) {
2775 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2776 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002777 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002778 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002779 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002780 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002781 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002782 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002783 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2784 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2785 cast<CXXRecordDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002786 D->getInnerLocStart(),
Douglas Gregora50ad132010-11-29 16:04:58 +00002787 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002788 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002789 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002790 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002791 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002792 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002793 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002794 D->getInnerLocStart(),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002795 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002796 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002797 D->hasWrittenPrototype(),
2798 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002799 }
John McCall3e11ebe2010-03-15 10:12:16 +00002800
2801 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002802 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002803 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002804 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002805 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2806 ToFunction->setTrivial(D->isTrivial());
2807 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002808 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002809
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002810 // Set the parameters.
2811 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002812 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002813 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002814 }
David Blaikie9c70e042011-09-21 18:16:56 +00002815 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002816
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002817 if (usedDifferentExceptionSpec) {
2818 // Update FunctionProtoType::ExtProtoInfo.
2819 QualType T = Importer.Import(D->getType());
2820 if (T.isNull())
2821 return 0;
2822 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002823 }
2824
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002825 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002826
2827 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002828 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002829
Douglas Gregor43f54792010-02-17 02:12:47 +00002830 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002831}
2832
Douglas Gregor00eace12010-02-21 18:29:16 +00002833Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2834 return VisitFunctionDecl(D);
2835}
2836
2837Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2838 return VisitCXXMethodDecl(D);
2839}
2840
2841Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2842 return VisitCXXMethodDecl(D);
2843}
2844
2845Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2846 return VisitCXXMethodDecl(D);
2847}
2848
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002849static unsigned getFieldIndex(Decl *F) {
2850 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2851 if (!Owner)
2852 return 0;
2853
2854 unsigned Index = 1;
2855 for (DeclContext::decl_iterator D = Owner->noload_decls_begin(),
2856 DEnd = Owner->noload_decls_end();
2857 D != DEnd; ++D) {
2858 if (*D == F)
2859 return Index;
2860
2861 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2862 ++Index;
2863 }
2864
2865 return Index;
2866}
2867
Douglas Gregor5c73e912010-02-11 00:48:18 +00002868Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2869 // Import the major distinguishing characteristics of a variable.
2870 DeclContext *DC, *LexicalDC;
2871 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002872 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002873 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2874 return 0;
2875
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002876 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002877 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002878 DC->localUncachedLookup(Name, FoundDecls);
2879 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2880 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002881 // For anonymous fields, match up by index.
2882 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2883 continue;
2884
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002885 if (Importer.IsStructurallyEquivalent(D->getType(),
2886 FoundField->getType())) {
2887 Importer.Imported(D, FoundField);
2888 return FoundField;
2889 }
2890
2891 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2892 << Name << D->getType() << FoundField->getType();
2893 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2894 << FoundField->getType();
2895 return 0;
2896 }
2897 }
2898
Douglas Gregorb4964f72010-02-15 23:54:17 +00002899 // Import the type.
2900 QualType T = Importer.Import(D->getType());
2901 if (T.isNull())
Douglas Gregor5c73e912010-02-11 00:48:18 +00002902 return 0;
2903
2904 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
2905 Expr *BitWidth = Importer.Import(D->getBitWidth());
2906 if (!BitWidth && D->getBitWidth())
2907 return 0;
2908
Abramo Bagnaradff19302011-03-08 08:55:46 +00002909 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
2910 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00002911 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00002912 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00002913 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00002914 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002915 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00002916 if (ToField->hasInClassInitializer())
2917 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002918 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002919 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002920 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002921 return ToField;
2922}
2923
Francois Pichet783dd6e2010-11-21 06:08:52 +00002924Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
2925 // Import the major distinguishing characteristics of a variable.
2926 DeclContext *DC, *LexicalDC;
2927 DeclarationName Name;
2928 SourceLocation Loc;
2929 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2930 return 0;
2931
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002932 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002933 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002934 DC->localUncachedLookup(Name, FoundDecls);
2935 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002936 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002937 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002938 // For anonymous indirect fields, match up by index.
2939 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
2940 continue;
2941
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002942 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002943 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00002944 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002945 Importer.Imported(D, FoundField);
2946 return FoundField;
2947 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002948
2949 // If there are more anonymous fields to check, continue.
2950 if (!Name && I < N-1)
2951 continue;
2952
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002953 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
2954 << Name << D->getType() << FoundField->getType();
2955 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
2956 << FoundField->getType();
2957 return 0;
2958 }
2959 }
2960
Francois Pichet783dd6e2010-11-21 06:08:52 +00002961 // Import the type.
2962 QualType T = Importer.Import(D->getType());
2963 if (T.isNull())
2964 return 0;
2965
2966 NamedDecl **NamedChain =
2967 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
2968
2969 unsigned i = 0;
2970 for (IndirectFieldDecl::chain_iterator PI = D->chain_begin(),
2971 PE = D->chain_end(); PI != PE; ++PI) {
2972 Decl* D = Importer.Import(*PI);
2973 if (!D)
2974 return 0;
2975 NamedChain[i++] = cast<NamedDecl>(D);
2976 }
2977
2978 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
2979 Importer.getToContext(), DC,
2980 Loc, Name.getAsIdentifierInfo(), T,
2981 NamedChain, D->getChainingSize());
2982 ToIndirectField->setAccess(D->getAccess());
2983 ToIndirectField->setLexicalDeclContext(LexicalDC);
2984 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00002985 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00002986 return ToIndirectField;
2987}
2988
Douglas Gregor7244b0b2010-02-17 00:34:30 +00002989Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
2990 // Import the major distinguishing characteristics of an ivar.
2991 DeclContext *DC, *LexicalDC;
2992 DeclarationName Name;
2993 SourceLocation Loc;
2994 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
2995 return 0;
2996
2997 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002998 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002999 DC->localUncachedLookup(Name, FoundDecls);
3000 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3001 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003002 if (Importer.IsStructurallyEquivalent(D->getType(),
3003 FoundIvar->getType())) {
3004 Importer.Imported(D, FoundIvar);
3005 return FoundIvar;
3006 }
3007
3008 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3009 << Name << D->getType() << FoundIvar->getType();
3010 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3011 << FoundIvar->getType();
3012 return 0;
3013 }
3014 }
3015
3016 // Import the type.
3017 QualType T = Importer.Import(D->getType());
3018 if (T.isNull())
3019 return 0;
3020
3021 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3022 Expr *BitWidth = Importer.Import(D->getBitWidth());
3023 if (!BitWidth && D->getBitWidth())
3024 return 0;
3025
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003026 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3027 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003028 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003029 Loc, Name.getAsIdentifierInfo(),
3030 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003031 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003032 ToIvar->setLexicalDeclContext(LexicalDC);
3033 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003034 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003035 return ToIvar;
3036
3037}
3038
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003039Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3040 // Import the major distinguishing characteristics of a variable.
3041 DeclContext *DC, *LexicalDC;
3042 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003043 SourceLocation Loc;
Douglas Gregorb4964f72010-02-15 23:54:17 +00003044 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003045 return 0;
3046
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003047 // Try to find a variable in our own ("to") context with the same name and
3048 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003049 if (D->isFileVarDecl()) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003050 VarDecl *MergeWithVar = 0;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003051 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003052 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003053 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003054 DC->localUncachedLookup(Name, FoundDecls);
3055 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3056 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003057 continue;
3058
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003059 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003060 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003061 if (FoundVar->hasExternalFormalLinkage() &&
3062 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003063 if (Importer.IsStructurallyEquivalent(D->getType(),
3064 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003065 MergeWithVar = FoundVar;
3066 break;
3067 }
3068
Douglas Gregor56521c52010-02-12 17:23:39 +00003069 const ArrayType *FoundArray
3070 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3071 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003072 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003073 if (FoundArray && TArray) {
3074 if (isa<IncompleteArrayType>(FoundArray) &&
3075 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003076 // Import the type.
3077 QualType T = Importer.Import(D->getType());
3078 if (T.isNull())
3079 return 0;
3080
Douglas Gregor56521c52010-02-12 17:23:39 +00003081 FoundVar->setType(T);
3082 MergeWithVar = FoundVar;
3083 break;
3084 } else if (isa<IncompleteArrayType>(TArray) &&
3085 isa<ConstantArrayType>(FoundArray)) {
3086 MergeWithVar = FoundVar;
3087 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003088 }
3089 }
3090
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003091 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003092 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003093 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3094 << FoundVar->getType();
3095 }
3096 }
3097
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003098 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003099 }
3100
3101 if (MergeWithVar) {
3102 // An equivalent variable with external linkage has been found. Link
3103 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003104 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003105
3106 if (VarDecl *DDef = D->getDefinition()) {
3107 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3108 Importer.ToDiag(ExistingDef->getLocation(),
3109 diag::err_odr_variable_multiple_def)
3110 << Name;
3111 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3112 } else {
3113 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003114 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003115 if (DDef->isInitKnownICE()) {
3116 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3117 Eval->CheckedICE = true;
3118 Eval->IsICE = DDef->isInitICE();
3119 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003120 }
3121 }
3122
3123 return MergeWithVar;
3124 }
3125
3126 if (!ConflictingDecls.empty()) {
3127 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3128 ConflictingDecls.data(),
3129 ConflictingDecls.size());
3130 if (!Name)
3131 return 0;
3132 }
3133 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003134
Douglas Gregorb4964f72010-02-15 23:54:17 +00003135 // Import the type.
3136 QualType T = Importer.Import(D->getType());
3137 if (T.isNull())
3138 return 0;
3139
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003140 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003141 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003142 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3143 Importer.Import(D->getInnerLocStart()),
3144 Loc, Name.getAsIdentifierInfo(),
3145 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003146 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003147 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003148 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003149 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003150 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003151 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003152
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003153 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003154 if (ImportDefinition(D, ToVar))
3155 return 0;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003156
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003157 return ToVar;
3158}
3159
Douglas Gregor8b228d72010-02-17 21:22:52 +00003160Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3161 // Parameters are created in the translation unit's context, then moved
3162 // into the function declaration's context afterward.
3163 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3164
3165 // Import the name of this declaration.
3166 DeclarationName Name = Importer.Import(D->getDeclName());
3167 if (D->getDeclName() && !Name)
3168 return 0;
3169
3170 // Import the location of this declaration.
3171 SourceLocation Loc = Importer.Import(D->getLocation());
3172
3173 // Import the parameter's type.
3174 QualType T = Importer.Import(D->getType());
3175 if (T.isNull())
3176 return 0;
3177
3178 // Create the imported parameter.
3179 ImplicitParamDecl *ToParm
3180 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3181 Loc, Name.getAsIdentifierInfo(),
3182 T);
3183 return Importer.Imported(D, ToParm);
3184}
3185
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003186Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3187 // Parameters are created in the translation unit's context, then moved
3188 // into the function declaration's context afterward.
3189 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3190
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003191 // Import the name of this declaration.
3192 DeclarationName Name = Importer.Import(D->getDeclName());
3193 if (D->getDeclName() && !Name)
3194 return 0;
3195
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003196 // Import the location of this declaration.
3197 SourceLocation Loc = Importer.Import(D->getLocation());
3198
3199 // Import the parameter's type.
3200 QualType T = Importer.Import(D->getType());
3201 if (T.isNull())
3202 return 0;
3203
3204 // Create the imported parameter.
3205 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3206 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003207 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003208 Loc, Name.getAsIdentifierInfo(),
3209 T, TInfo, D->getStorageClass(),
3210 /*FIXME: Default argument*/ 0);
John McCallf3cd6652010-03-12 18:31:32 +00003211 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003212 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003213}
3214
Douglas Gregor43f54792010-02-17 02:12:47 +00003215Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3216 // Import the major distinguishing characteristics of a method.
3217 DeclContext *DC, *LexicalDC;
3218 DeclarationName Name;
3219 SourceLocation Loc;
3220 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3221 return 0;
3222
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003223 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003224 DC->localUncachedLookup(Name, FoundDecls);
3225 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3226 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003227 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3228 continue;
3229
3230 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003231 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3232 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003233 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003234 << D->isInstanceMethod() << Name << D->getReturnType()
3235 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003236 Importer.ToDiag(FoundMethod->getLocation(),
3237 diag::note_odr_objc_method_here)
3238 << D->isInstanceMethod() << Name;
3239 return 0;
3240 }
3241
3242 // Check the number of parameters.
3243 if (D->param_size() != FoundMethod->param_size()) {
3244 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3245 << D->isInstanceMethod() << Name
3246 << D->param_size() << FoundMethod->param_size();
3247 Importer.ToDiag(FoundMethod->getLocation(),
3248 diag::note_odr_objc_method_here)
3249 << D->isInstanceMethod() << Name;
3250 return 0;
3251 }
3252
3253 // Check parameter types.
3254 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3255 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3256 P != PEnd; ++P, ++FoundP) {
3257 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3258 (*FoundP)->getType())) {
3259 Importer.FromDiag((*P)->getLocation(),
3260 diag::err_odr_objc_method_param_type_inconsistent)
3261 << D->isInstanceMethod() << Name
3262 << (*P)->getType() << (*FoundP)->getType();
3263 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3264 << (*FoundP)->getType();
3265 return 0;
3266 }
3267 }
3268
3269 // Check variadic/non-variadic.
3270 // Check the number of parameters.
3271 if (D->isVariadic() != FoundMethod->isVariadic()) {
3272 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3273 << D->isInstanceMethod() << Name;
3274 Importer.ToDiag(FoundMethod->getLocation(),
3275 diag::note_odr_objc_method_here)
3276 << D->isInstanceMethod() << Name;
3277 return 0;
3278 }
3279
3280 // FIXME: Any other bits we need to merge?
3281 return Importer.Imported(D, FoundMethod);
3282 }
3283 }
3284
3285 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003286 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003287 if (ResultTy.isNull())
3288 return 0;
3289
Alp Toker314cc812014-01-25 16:55:45 +00003290 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003291
Alp Toker314cc812014-01-25 16:55:45 +00003292 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3293 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3294 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3295 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3296 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003297
3298 // FIXME: When we decide to merge method definitions, we'll need to
3299 // deal with implicit parameters.
3300
3301 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003302 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003303 for (auto *FromP : D->params()) {
3304 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003305 if (!ToP)
3306 return 0;
3307
3308 ToParams.push_back(ToP);
3309 }
3310
3311 // Set the parameters.
3312 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3313 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003314 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003315 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003316 SmallVector<SourceLocation, 12> SelLocs;
3317 D->getSelectorLocs(SelLocs);
3318 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003319
3320 ToMethod->setLexicalDeclContext(LexicalDC);
3321 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003322 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003323 return ToMethod;
3324}
3325
Douglas Gregor84c51c32010-02-18 01:47:50 +00003326Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3327 // Import the major distinguishing characteristics of a category.
3328 DeclContext *DC, *LexicalDC;
3329 DeclarationName Name;
3330 SourceLocation Loc;
3331 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3332 return 0;
3333
3334 ObjCInterfaceDecl *ToInterface
3335 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3336 if (!ToInterface)
3337 return 0;
3338
3339 // Determine if we've already encountered this category.
3340 ObjCCategoryDecl *MergeWithCategory
3341 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3342 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3343 if (!ToCategory) {
3344 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003345 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003346 Loc,
3347 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003348 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003349 ToInterface,
3350 Importer.Import(D->getIvarLBraceLoc()),
3351 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003352 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003353 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003354 Importer.Imported(D, ToCategory);
3355
Douglas Gregor84c51c32010-02-18 01:47:50 +00003356 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003357 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3358 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003359 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3360 = D->protocol_loc_begin();
3361 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3362 FromProtoEnd = D->protocol_end();
3363 FromProto != FromProtoEnd;
3364 ++FromProto, ++FromProtoLoc) {
3365 ObjCProtocolDecl *ToProto
3366 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3367 if (!ToProto)
3368 return 0;
3369 Protocols.push_back(ToProto);
3370 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3371 }
3372
3373 // FIXME: If we're merging, make sure that the protocol list is the same.
3374 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3375 ProtocolLocs.data(), Importer.getToContext());
3376
3377 } else {
3378 Importer.Imported(D, ToCategory);
3379 }
3380
3381 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003382 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003383
3384 // If we have an implementation, import it as well.
3385 if (D->getImplementation()) {
3386 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003387 = cast_or_null<ObjCCategoryImplDecl>(
3388 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003389 if (!Impl)
3390 return 0;
3391
3392 ToCategory->setImplementation(Impl);
3393 }
3394
3395 return ToCategory;
3396}
3397
Douglas Gregor2aa53772012-01-24 17:42:07 +00003398bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3399 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003400 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003401 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003402 if (shouldForceImportDeclContext(Kind))
3403 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003404 return false;
3405 }
3406
3407 // Start the protocol definition
3408 To->startDefinition();
3409
3410 // Import protocols
3411 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3412 SmallVector<SourceLocation, 4> ProtocolLocs;
3413 ObjCProtocolDecl::protocol_loc_iterator
3414 FromProtoLoc = From->protocol_loc_begin();
3415 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3416 FromProtoEnd = From->protocol_end();
3417 FromProto != FromProtoEnd;
3418 ++FromProto, ++FromProtoLoc) {
3419 ObjCProtocolDecl *ToProto
3420 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3421 if (!ToProto)
3422 return true;
3423 Protocols.push_back(ToProto);
3424 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3425 }
3426
3427 // FIXME: If we're merging, make sure that the protocol list is the same.
3428 To->setProtocolList(Protocols.data(), Protocols.size(),
3429 ProtocolLocs.data(), Importer.getToContext());
3430
Douglas Gregor2e15c842012-02-01 21:00:38 +00003431 if (shouldForceImportDeclContext(Kind)) {
3432 // Import all of the members of this protocol.
3433 ImportDeclContext(From, /*ForceImport=*/true);
3434 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003435 return false;
3436}
3437
Douglas Gregor98d156a2010-02-17 16:12:00 +00003438Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003439 // If this protocol has a definition in the translation unit we're coming
3440 // from, but this particular declaration is not that definition, import the
3441 // definition and map to that.
3442 ObjCProtocolDecl *Definition = D->getDefinition();
3443 if (Definition && Definition != D) {
3444 Decl *ImportedDef = Importer.Import(Definition);
3445 if (!ImportedDef)
3446 return 0;
3447
3448 return Importer.Imported(D, ImportedDef);
3449 }
3450
Douglas Gregor84c51c32010-02-18 01:47:50 +00003451 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003452 DeclContext *DC, *LexicalDC;
3453 DeclarationName Name;
3454 SourceLocation Loc;
3455 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3456 return 0;
3457
3458 ObjCProtocolDecl *MergeWithProtocol = 0;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003459 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003460 DC->localUncachedLookup(Name, FoundDecls);
3461 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3462 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003463 continue;
3464
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003465 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003466 break;
3467 }
3468
3469 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003470 if (!ToProto) {
3471 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3472 Name.getAsIdentifierInfo(), Loc,
3473 Importer.Import(D->getAtStartLoc()),
3474 /*PrevDecl=*/0);
3475 ToProto->setLexicalDeclContext(LexicalDC);
3476 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003477 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003478
3479 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003480
Douglas Gregor2aa53772012-01-24 17:42:07 +00003481 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
3482 return 0;
3483
Douglas Gregor98d156a2010-02-17 16:12:00 +00003484 return ToProto;
3485}
3486
Douglas Gregor2aa53772012-01-24 17:42:07 +00003487bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3488 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003489 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003490 if (To->getDefinition()) {
3491 // Check consistency of superclass.
3492 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3493 if (FromSuper) {
3494 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3495 if (!FromSuper)
3496 return true;
3497 }
3498
3499 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3500 if ((bool)FromSuper != (bool)ToSuper ||
3501 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3502 Importer.ToDiag(To->getLocation(),
3503 diag::err_odr_objc_superclass_inconsistent)
3504 << To->getDeclName();
3505 if (ToSuper)
3506 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3507 << To->getSuperClass()->getDeclName();
3508 else
3509 Importer.ToDiag(To->getLocation(),
3510 diag::note_odr_objc_missing_superclass);
3511 if (From->getSuperClass())
3512 Importer.FromDiag(From->getSuperClassLoc(),
3513 diag::note_odr_objc_superclass)
3514 << From->getSuperClass()->getDeclName();
3515 else
3516 Importer.FromDiag(From->getLocation(),
3517 diag::note_odr_objc_missing_superclass);
3518 }
3519
Douglas Gregor2e15c842012-02-01 21:00:38 +00003520 if (shouldForceImportDeclContext(Kind))
3521 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003522 return false;
3523 }
3524
3525 // Start the definition.
3526 To->startDefinition();
3527
3528 // If this class has a superclass, import it.
3529 if (From->getSuperClass()) {
3530 ObjCInterfaceDecl *Super = cast_or_null<ObjCInterfaceDecl>(
3531 Importer.Import(From->getSuperClass()));
3532 if (!Super)
3533 return true;
3534
3535 To->setSuperClass(Super);
3536 To->setSuperClassLoc(Importer.Import(From->getSuperClassLoc()));
3537 }
3538
3539 // Import protocols
3540 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3541 SmallVector<SourceLocation, 4> ProtocolLocs;
3542 ObjCInterfaceDecl::protocol_loc_iterator
3543 FromProtoLoc = From->protocol_loc_begin();
3544
3545 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3546 FromProtoEnd = From->protocol_end();
3547 FromProto != FromProtoEnd;
3548 ++FromProto, ++FromProtoLoc) {
3549 ObjCProtocolDecl *ToProto
3550 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3551 if (!ToProto)
3552 return true;
3553 Protocols.push_back(ToProto);
3554 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3555 }
3556
3557 // FIXME: If we're merging, make sure that the protocol list is the same.
3558 To->setProtocolList(Protocols.data(), Protocols.size(),
3559 ProtocolLocs.data(), Importer.getToContext());
3560
3561 // Import categories. When the categories themselves are imported, they'll
3562 // hook themselves into this interface.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003563 for (ObjCInterfaceDecl::known_categories_iterator
3564 Cat = From->known_categories_begin(),
3565 CatEnd = From->known_categories_end();
3566 Cat != CatEnd; ++Cat) {
3567 Importer.Import(*Cat);
3568 }
3569
Douglas Gregor2aa53772012-01-24 17:42:07 +00003570 // If we have an @implementation, import it as well.
3571 if (From->getImplementation()) {
3572 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3573 Importer.Import(From->getImplementation()));
3574 if (!Impl)
3575 return true;
3576
3577 To->setImplementation(Impl);
3578 }
3579
Douglas Gregor2e15c842012-02-01 21:00:38 +00003580 if (shouldForceImportDeclContext(Kind)) {
3581 // Import all of the members of this class.
3582 ImportDeclContext(From, /*ForceImport=*/true);
3583 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003584 return false;
3585}
3586
Douglas Gregor45635322010-02-16 01:20:57 +00003587Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003588 // If this class has a definition in the translation unit we're coming from,
3589 // but this particular declaration is not that definition, import the
3590 // definition and map to that.
3591 ObjCInterfaceDecl *Definition = D->getDefinition();
3592 if (Definition && Definition != D) {
3593 Decl *ImportedDef = Importer.Import(Definition);
3594 if (!ImportedDef)
3595 return 0;
3596
3597 return Importer.Imported(D, ImportedDef);
3598 }
3599
Douglas Gregor45635322010-02-16 01:20:57 +00003600 // Import the major distinguishing characteristics of an @interface.
3601 DeclContext *DC, *LexicalDC;
3602 DeclarationName Name;
3603 SourceLocation Loc;
3604 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3605 return 0;
3606
Douglas Gregor2aa53772012-01-24 17:42:07 +00003607 // Look for an existing interface with the same name.
Douglas Gregor45635322010-02-16 01:20:57 +00003608 ObjCInterfaceDecl *MergeWithIface = 0;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003609 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003610 DC->localUncachedLookup(Name, FoundDecls);
3611 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3612 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003613 continue;
3614
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003615 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003616 break;
3617 }
3618
Douglas Gregor2aa53772012-01-24 17:42:07 +00003619 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003620 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003621 if (!ToIface) {
3622 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3623 Importer.Import(D->getAtStartLoc()),
3624 Name.getAsIdentifierInfo(),
3625 /*PrevDecl=*/0,Loc,
3626 D->isImplicitInterfaceDecl());
3627 ToIface->setLexicalDeclContext(LexicalDC);
3628 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003629 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003630 Importer.Imported(D, ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003631
Douglas Gregor2aa53772012-01-24 17:42:07 +00003632 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
3633 return 0;
Douglas Gregor45635322010-02-16 01:20:57 +00003634
Douglas Gregor98d156a2010-02-17 16:12:00 +00003635 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003636}
3637
Douglas Gregor4da9d682010-12-07 15:32:12 +00003638Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3639 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3640 Importer.Import(D->getCategoryDecl()));
3641 if (!Category)
3642 return 0;
3643
3644 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3645 if (!ToImpl) {
3646 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3647 if (!DC)
3648 return 0;
3649
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003650 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003651 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003652 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003653 Category->getClassInterface(),
3654 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003655 Importer.Import(D->getAtStartLoc()),
3656 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003657
3658 DeclContext *LexicalDC = DC;
3659 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3660 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3661 if (!LexicalDC)
3662 return 0;
3663
3664 ToImpl->setLexicalDeclContext(LexicalDC);
3665 }
3666
Sean Callanan95e74be2011-10-21 02:57:43 +00003667 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003668 Category->setImplementation(ToImpl);
3669 }
3670
3671 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003672 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003673 return ToImpl;
3674}
3675
Douglas Gregorda8025c2010-12-07 01:26:03 +00003676Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3677 // Find the corresponding interface.
3678 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3679 Importer.Import(D->getClassInterface()));
3680 if (!Iface)
3681 return 0;
3682
3683 // Import the superclass, if any.
3684 ObjCInterfaceDecl *Super = 0;
3685 if (D->getSuperClass()) {
3686 Super = cast_or_null<ObjCInterfaceDecl>(
3687 Importer.Import(D->getSuperClass()));
3688 if (!Super)
3689 return 0;
3690 }
3691
3692 ObjCImplementationDecl *Impl = Iface->getImplementation();
3693 if (!Impl) {
3694 // We haven't imported an implementation yet. Create a new @implementation
3695 // now.
3696 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3697 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003698 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003699 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003700 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003701 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003702 Importer.Import(D->getIvarLBraceLoc()),
3703 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003704
3705 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3706 DeclContext *LexicalDC
3707 = Importer.ImportContext(D->getLexicalDeclContext());
3708 if (!LexicalDC)
3709 return 0;
3710 Impl->setLexicalDeclContext(LexicalDC);
3711 }
3712
3713 // Associate the implementation with the class it implements.
3714 Iface->setImplementation(Impl);
3715 Importer.Imported(D, Iface->getImplementation());
3716 } else {
3717 Importer.Imported(D, Iface->getImplementation());
3718
3719 // Verify that the existing @implementation has the same superclass.
3720 if ((Super && !Impl->getSuperClass()) ||
3721 (!Super && Impl->getSuperClass()) ||
3722 (Super && Impl->getSuperClass() &&
Douglas Gregor0b144e12011-12-15 00:29:59 +00003723 !declaresSameEntity(Super->getCanonicalDecl(), Impl->getSuperClass()))) {
Douglas Gregorda8025c2010-12-07 01:26:03 +00003724 Importer.ToDiag(Impl->getLocation(),
3725 diag::err_odr_objc_superclass_inconsistent)
3726 << Iface->getDeclName();
3727 // FIXME: It would be nice to have the location of the superclass
3728 // below.
3729 if (Impl->getSuperClass())
3730 Importer.ToDiag(Impl->getLocation(),
3731 diag::note_odr_objc_superclass)
3732 << Impl->getSuperClass()->getDeclName();
3733 else
3734 Importer.ToDiag(Impl->getLocation(),
3735 diag::note_odr_objc_missing_superclass);
3736 if (D->getSuperClass())
3737 Importer.FromDiag(D->getLocation(),
3738 diag::note_odr_objc_superclass)
3739 << D->getSuperClass()->getDeclName();
3740 else
3741 Importer.FromDiag(D->getLocation(),
3742 diag::note_odr_objc_missing_superclass);
3743 return 0;
3744 }
3745 }
3746
3747 // Import all of the members of this @implementation.
3748 ImportDeclContext(D);
3749
3750 return Impl;
3751}
3752
Douglas Gregora11c4582010-02-17 18:02:10 +00003753Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3754 // Import the major distinguishing characteristics of an @property.
3755 DeclContext *DC, *LexicalDC;
3756 DeclarationName Name;
3757 SourceLocation Loc;
3758 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3759 return 0;
3760
3761 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003762 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003763 DC->localUncachedLookup(Name, FoundDecls);
3764 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003765 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003766 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00003767 // Check property types.
3768 if (!Importer.IsStructurallyEquivalent(D->getType(),
3769 FoundProp->getType())) {
3770 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
3771 << Name << D->getType() << FoundProp->getType();
3772 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
3773 << FoundProp->getType();
3774 return 0;
3775 }
3776
3777 // FIXME: Check property attributes, getters, setters, etc.?
3778
3779 // Consider these properties to be equivalent.
3780 Importer.Imported(D, FoundProp);
3781 return FoundProp;
3782 }
3783 }
3784
3785 // Import the type.
John McCall339bb662010-06-04 20:50:08 +00003786 TypeSourceInfo *T = Importer.Import(D->getTypeSourceInfo());
3787 if (!T)
Douglas Gregora11c4582010-02-17 18:02:10 +00003788 return 0;
3789
3790 // Create the new property.
3791 ObjCPropertyDecl *ToProperty
3792 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
3793 Name.getAsIdentifierInfo(),
3794 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00003795 Importer.Import(D->getLParenLoc()),
Douglas Gregora11c4582010-02-17 18:02:10 +00003796 T,
3797 D->getPropertyImplementation());
3798 Importer.Imported(D, ToProperty);
3799 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003800 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00003801
3802 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00003803 ToProperty->setPropertyAttributesAsWritten(
3804 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00003805 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
3806 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
3807 ToProperty->setGetterMethodDecl(
3808 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
3809 ToProperty->setSetterMethodDecl(
3810 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
3811 ToProperty->setPropertyIvarDecl(
3812 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
3813 return ToProperty;
3814}
3815
Douglas Gregor14a49e22010-12-07 18:32:03 +00003816Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
3817 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
3818 Importer.Import(D->getPropertyDecl()));
3819 if (!Property)
3820 return 0;
3821
3822 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3823 if (!DC)
3824 return 0;
3825
3826 // Import the lexical declaration context.
3827 DeclContext *LexicalDC = DC;
3828 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3829 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3830 if (!LexicalDC)
3831 return 0;
3832 }
3833
3834 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
3835 if (!InImpl)
3836 return 0;
3837
3838 // Import the ivar (for an @synthesize).
3839 ObjCIvarDecl *Ivar = 0;
3840 if (D->getPropertyIvarDecl()) {
3841 Ivar = cast_or_null<ObjCIvarDecl>(
3842 Importer.Import(D->getPropertyIvarDecl()));
3843 if (!Ivar)
3844 return 0;
3845 }
3846
3847 ObjCPropertyImplDecl *ToImpl
3848 = InImpl->FindPropertyImplDecl(Property->getIdentifier());
3849 if (!ToImpl) {
3850 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
3851 Importer.Import(D->getLocStart()),
3852 Importer.Import(D->getLocation()),
3853 Property,
3854 D->getPropertyImplementation(),
3855 Ivar,
3856 Importer.Import(D->getPropertyIvarDeclLoc()));
3857 ToImpl->setLexicalDeclContext(LexicalDC);
3858 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00003859 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00003860 } else {
3861 // Check that we have the same kind of property implementation (@synthesize
3862 // vs. @dynamic).
3863 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
3864 Importer.ToDiag(ToImpl->getLocation(),
3865 diag::err_odr_objc_property_impl_kind_inconsistent)
3866 << Property->getDeclName()
3867 << (ToImpl->getPropertyImplementation()
3868 == ObjCPropertyImplDecl::Dynamic);
3869 Importer.FromDiag(D->getLocation(),
3870 diag::note_odr_objc_property_impl_kind)
3871 << D->getPropertyDecl()->getDeclName()
3872 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
3873 return 0;
3874 }
3875
3876 // For @synthesize, check that we have the same
3877 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
3878 Ivar != ToImpl->getPropertyIvarDecl()) {
3879 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
3880 diag::err_odr_objc_synthesize_ivar_inconsistent)
3881 << Property->getDeclName()
3882 << ToImpl->getPropertyIvarDecl()->getDeclName()
3883 << Ivar->getDeclName();
3884 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
3885 diag::note_odr_objc_synthesize_ivar_here)
3886 << D->getPropertyIvarDecl()->getDeclName();
3887 return 0;
3888 }
3889
3890 // Merge the existing implementation with the new implementation.
3891 Importer.Imported(D, ToImpl);
3892 }
3893
3894 return ToImpl;
3895}
3896
Douglas Gregora082a492010-11-30 19:14:50 +00003897Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
3898 // For template arguments, we adopt the translation unit as our declaration
3899 // context. This context will be fixed when the actual template declaration
3900 // is created.
3901
3902 // FIXME: Import default argument.
3903 return TemplateTypeParmDecl::Create(Importer.getToContext(),
3904 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003905 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003906 Importer.Import(D->getLocation()),
3907 D->getDepth(),
3908 D->getIndex(),
3909 Importer.Import(D->getIdentifier()),
3910 D->wasDeclaredWithTypename(),
3911 D->isParameterPack());
3912}
3913
3914Decl *
3915ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
3916 // Import the name of this declaration.
3917 DeclarationName Name = Importer.Import(D->getDeclName());
3918 if (D->getDeclName() && !Name)
3919 return 0;
3920
3921 // Import the location of this declaration.
3922 SourceLocation Loc = Importer.Import(D->getLocation());
3923
3924 // Import the type of this declaration.
3925 QualType T = Importer.Import(D->getType());
3926 if (T.isNull())
3927 return 0;
3928
3929 // Import type-source information.
3930 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3931 if (D->getTypeSourceInfo() && !TInfo)
3932 return 0;
3933
3934 // FIXME: Import default argument.
3935
3936 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
3937 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003938 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00003939 Loc, D->getDepth(), D->getPosition(),
3940 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00003941 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00003942}
3943
3944Decl *
3945ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
3946 // Import the name of this declaration.
3947 DeclarationName Name = Importer.Import(D->getDeclName());
3948 if (D->getDeclName() && !Name)
3949 return 0;
3950
3951 // Import the location of this declaration.
3952 SourceLocation Loc = Importer.Import(D->getLocation());
3953
3954 // Import template parameters.
3955 TemplateParameterList *TemplateParams
3956 = ImportTemplateParameterList(D->getTemplateParameters());
3957 if (!TemplateParams)
3958 return 0;
3959
3960 // FIXME: Import default argument.
3961
3962 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
3963 Importer.getToContext().getTranslationUnitDecl(),
3964 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00003965 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00003966 Name.getAsIdentifierInfo(),
3967 TemplateParams);
3968}
3969
3970Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
3971 // If this record has a definition in the translation unit we're coming from,
3972 // but this particular declaration is not that definition, import the
3973 // definition and map to that.
3974 CXXRecordDecl *Definition
3975 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
3976 if (Definition && Definition != D->getTemplatedDecl()) {
3977 Decl *ImportedDef
3978 = Importer.Import(Definition->getDescribedClassTemplate());
3979 if (!ImportedDef)
3980 return 0;
3981
3982 return Importer.Imported(D, ImportedDef);
3983 }
3984
3985 // Import the major distinguishing characteristics of this class template.
3986 DeclContext *DC, *LexicalDC;
3987 DeclarationName Name;
3988 SourceLocation Loc;
3989 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
3990 return 0;
3991
3992 // We may already have a template of the same name; try to find and match it.
3993 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003994 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003995 SmallVector<NamedDecl *, 2> FoundDecls;
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003996 DC->localUncachedLookup(Name, FoundDecls);
3997 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3998 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00003999 continue;
4000
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004001 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004002 if (ClassTemplateDecl *FoundTemplate
4003 = dyn_cast<ClassTemplateDecl>(Found)) {
4004 if (IsStructuralMatch(D, FoundTemplate)) {
4005 // The class templates structurally match; call it the same template.
4006 // FIXME: We may be filling in a forward declaration here. Handle
4007 // this case!
4008 Importer.Imported(D->getTemplatedDecl(),
4009 FoundTemplate->getTemplatedDecl());
4010 return Importer.Imported(D, FoundTemplate);
4011 }
4012 }
4013
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004014 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004015 }
4016
4017 if (!ConflictingDecls.empty()) {
4018 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4019 ConflictingDecls.data(),
4020 ConflictingDecls.size());
4021 }
4022
4023 if (!Name)
4024 return 0;
4025 }
4026
4027 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4028
4029 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004030 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4031 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004032 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4033 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004034 DC, StartLoc, IdLoc,
4035 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004036 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004037 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004038 D2Templated->setLexicalDeclContext(LexicalDC);
4039
4040 // Create the class template declaration itself.
4041 TemplateParameterList *TemplateParams
4042 = ImportTemplateParameterList(D->getTemplateParameters());
4043 if (!TemplateParams)
4044 return 0;
4045
4046 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4047 Loc, Name, TemplateParams,
4048 D2Templated,
4049 /*PrevDecl=*/0);
4050 D2Templated->setDescribedClassTemplate(D2);
4051
4052 D2->setAccess(D->getAccess());
4053 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004054 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004055
4056 // Note the relationship between the class templates.
4057 Importer.Imported(D, D2);
4058 Importer.Imported(DTemplated, D2Templated);
4059
John McCallf937c022011-10-07 06:10:15 +00004060 if (DTemplated->isCompleteDefinition() &&
4061 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004062 // FIXME: Import definition!
4063 }
4064
4065 return D2;
4066}
4067
Douglas Gregore2e50d332010-12-01 01:36:18 +00004068Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4069 ClassTemplateSpecializationDecl *D) {
4070 // If this record has a definition in the translation unit we're coming from,
4071 // but this particular declaration is not that definition, import the
4072 // definition and map to that.
4073 TagDecl *Definition = D->getDefinition();
4074 if (Definition && Definition != D) {
4075 Decl *ImportedDef = Importer.Import(Definition);
4076 if (!ImportedDef)
4077 return 0;
4078
4079 return Importer.Imported(D, ImportedDef);
4080 }
4081
4082 ClassTemplateDecl *ClassTemplate
4083 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4084 D->getSpecializedTemplate()));
4085 if (!ClassTemplate)
4086 return 0;
4087
4088 // Import the context of this declaration.
4089 DeclContext *DC = ClassTemplate->getDeclContext();
4090 if (!DC)
4091 return 0;
4092
4093 DeclContext *LexicalDC = DC;
4094 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4095 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4096 if (!LexicalDC)
4097 return 0;
4098 }
4099
4100 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004101 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4102 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004103
4104 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004105 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004106 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4107 D->getTemplateArgs().size(),
4108 TemplateArgs))
4109 return 0;
4110
4111 // Try to find an existing specialization with these template arguments.
4112 void *InsertPos = 0;
4113 ClassTemplateSpecializationDecl *D2
4114 = ClassTemplate->findSpecialization(TemplateArgs.data(),
4115 TemplateArgs.size(), InsertPos);
4116 if (D2) {
4117 // We already have a class template specialization with these template
4118 // arguments.
4119
4120 // FIXME: Check for specialization vs. instantiation errors.
4121
4122 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004123 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004124 // The record types structurally match, or the "from" translation
4125 // unit only had a forward declaration anyway; call it the same
4126 // function.
4127 return Importer.Imported(D, FoundDef);
4128 }
4129 }
4130 } else {
4131 // Create a new specialization.
4132 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4133 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004134 StartLoc, IdLoc,
4135 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004136 TemplateArgs.data(),
4137 TemplateArgs.size(),
4138 /*PrevDecl=*/0);
4139 D2->setSpecializationKind(D->getSpecializationKind());
4140
4141 // Add this specialization to the class template.
4142 ClassTemplate->AddSpecialization(D2, InsertPos);
4143
4144 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004145 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004146
4147 // Add the specialization to this context.
4148 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004149 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004150 }
4151 Importer.Imported(D, D2);
4152
John McCallf937c022011-10-07 06:10:15 +00004153 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Douglas Gregore2e50d332010-12-01 01:36:18 +00004154 return 0;
4155
4156 return D2;
4157}
4158
Larisse Voufo39a1e502013-08-06 01:03:05 +00004159Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4160 // If this variable has a definition in the translation unit we're coming
4161 // from,
4162 // but this particular declaration is not that definition, import the
4163 // definition and map to that.
4164 VarDecl *Definition =
4165 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4166 if (Definition && Definition != D->getTemplatedDecl()) {
4167 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4168 if (!ImportedDef)
4169 return 0;
4170
4171 return Importer.Imported(D, ImportedDef);
4172 }
4173
4174 // Import the major distinguishing characteristics of this variable template.
4175 DeclContext *DC, *LexicalDC;
4176 DeclarationName Name;
4177 SourceLocation Loc;
4178 if (ImportDeclParts(D, DC, LexicalDC, Name, Loc))
4179 return 0;
4180
4181 // We may already have a template of the same name; try to find and match it.
4182 assert(!DC->isFunctionOrMethod() &&
4183 "Variable templates cannot be declared at function scope");
4184 SmallVector<NamedDecl *, 4> ConflictingDecls;
4185 SmallVector<NamedDecl *, 2> FoundDecls;
4186 DC->localUncachedLookup(Name, FoundDecls);
4187 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4188 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4189 continue;
4190
4191 Decl *Found = FoundDecls[I];
4192 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4193 if (IsStructuralMatch(D, FoundTemplate)) {
4194 // The variable templates structurally match; call it the same template.
4195 Importer.Imported(D->getTemplatedDecl(),
4196 FoundTemplate->getTemplatedDecl());
4197 return Importer.Imported(D, FoundTemplate);
4198 }
4199 }
4200
4201 ConflictingDecls.push_back(FoundDecls[I]);
4202 }
4203
4204 if (!ConflictingDecls.empty()) {
4205 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4206 ConflictingDecls.data(),
4207 ConflictingDecls.size());
4208 }
4209
4210 if (!Name)
4211 return 0;
4212
4213 VarDecl *DTemplated = D->getTemplatedDecl();
4214
4215 // Import the type.
4216 QualType T = Importer.Import(DTemplated->getType());
4217 if (T.isNull())
4218 return 0;
4219
4220 // Create the declaration that is being templated.
4221 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4222 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4223 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4224 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4225 IdLoc, Name.getAsIdentifierInfo(), T,
4226 TInfo, DTemplated->getStorageClass());
4227 D2Templated->setAccess(DTemplated->getAccess());
4228 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4229 D2Templated->setLexicalDeclContext(LexicalDC);
4230
4231 // Importer.Imported(DTemplated, D2Templated);
4232 // LexicalDC->addDeclInternal(D2Templated);
4233
4234 // Merge the initializer.
4235 if (ImportDefinition(DTemplated, D2Templated))
4236 return 0;
4237
4238 // Create the variable template declaration itself.
4239 TemplateParameterList *TemplateParams =
4240 ImportTemplateParameterList(D->getTemplateParameters());
4241 if (!TemplateParams)
4242 return 0;
4243
4244 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004245 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004246 D2Templated->setDescribedVarTemplate(D2);
4247
4248 D2->setAccess(D->getAccess());
4249 D2->setLexicalDeclContext(LexicalDC);
4250 LexicalDC->addDeclInternal(D2);
4251
4252 // Note the relationship between the variable templates.
4253 Importer.Imported(D, D2);
4254 Importer.Imported(DTemplated, D2Templated);
4255
4256 if (DTemplated->isThisDeclarationADefinition() &&
4257 !D2Templated->isThisDeclarationADefinition()) {
4258 // FIXME: Import definition!
4259 }
4260
4261 return D2;
4262}
4263
4264Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4265 VarTemplateSpecializationDecl *D) {
4266 // If this record has a definition in the translation unit we're coming from,
4267 // but this particular declaration is not that definition, import the
4268 // definition and map to that.
4269 VarDecl *Definition = D->getDefinition();
4270 if (Definition && Definition != D) {
4271 Decl *ImportedDef = Importer.Import(Definition);
4272 if (!ImportedDef)
4273 return 0;
4274
4275 return Importer.Imported(D, ImportedDef);
4276 }
4277
4278 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4279 Importer.Import(D->getSpecializedTemplate()));
4280 if (!VarTemplate)
4281 return 0;
4282
4283 // Import the context of this declaration.
4284 DeclContext *DC = VarTemplate->getDeclContext();
4285 if (!DC)
4286 return 0;
4287
4288 DeclContext *LexicalDC = DC;
4289 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4290 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4291 if (!LexicalDC)
4292 return 0;
4293 }
4294
4295 // Import the location of this declaration.
4296 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4297 SourceLocation IdLoc = Importer.Import(D->getLocation());
4298
4299 // Import template arguments.
4300 SmallVector<TemplateArgument, 2> TemplateArgs;
4301 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4302 D->getTemplateArgs().size(), TemplateArgs))
4303 return 0;
4304
4305 // Try to find an existing specialization with these template arguments.
4306 void *InsertPos = 0;
4307 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
4308 TemplateArgs.data(), TemplateArgs.size(), InsertPos);
4309 if (D2) {
4310 // We already have a variable template specialization with these template
4311 // arguments.
4312
4313 // FIXME: Check for specialization vs. instantiation errors.
4314
4315 if (VarDecl *FoundDef = D2->getDefinition()) {
4316 if (!D->isThisDeclarationADefinition() ||
4317 IsStructuralMatch(D, FoundDef)) {
4318 // The record types structurally match, or the "from" translation
4319 // unit only had a forward declaration anyway; call it the same
4320 // variable.
4321 return Importer.Imported(D, FoundDef);
4322 }
4323 }
4324 } else {
4325
4326 // Import the type.
4327 QualType T = Importer.Import(D->getType());
4328 if (T.isNull())
4329 return 0;
4330 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4331
4332 // Create a new specialization.
4333 D2 = VarTemplateSpecializationDecl::Create(
4334 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4335 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4336 D2->setSpecializationKind(D->getSpecializationKind());
4337 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4338
4339 // Add this specialization to the class template.
4340 VarTemplate->AddSpecialization(D2, InsertPos);
4341
4342 // Import the qualifier, if any.
4343 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4344
4345 // Add the specialization to this context.
4346 D2->setLexicalDeclContext(LexicalDC);
4347 LexicalDC->addDeclInternal(D2);
4348 }
4349 Importer.Imported(D, D2);
4350
4351 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
4352 return 0;
4353
4354 return D2;
4355}
4356
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004357//----------------------------------------------------------------------------
4358// Import Statements
4359//----------------------------------------------------------------------------
4360
4361Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4362 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4363 << S->getStmtClassName();
4364 return 0;
4365}
4366
4367//----------------------------------------------------------------------------
4368// Import Expressions
4369//----------------------------------------------------------------------------
4370Expr *ASTNodeImporter::VisitExpr(Expr *E) {
4371 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
4372 << E->getStmtClassName();
4373 return 0;
4374}
4375
Douglas Gregor52f820e2010-02-19 01:17:02 +00004376Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00004377 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
4378 if (!ToD)
4379 return 0;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00004380
4381 NamedDecl *FoundD = 0;
4382 if (E->getDecl() != E->getFoundDecl()) {
4383 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
4384 if (!FoundD)
4385 return 0;
4386 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00004387
4388 QualType T = Importer.Import(E->getType());
4389 if (T.isNull())
4390 return 0;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004391
4392 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
4393 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004394 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004395 ToD,
John McCall113bee02012-03-10 09:33:50 +00004396 E->refersToEnclosingLocal(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00004397 Importer.Import(E->getLocation()),
4398 T, E->getValueKind(),
4399 FoundD,
4400 /*FIXME:TemplateArgs=*/0);
4401 if (E->hadMultipleCandidates())
4402 DRE->setHadMultipleCandidates(true);
4403 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00004404}
4405
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004406Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
4407 QualType T = Importer.Import(E->getType());
4408 if (T.isNull())
4409 return 0;
4410
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00004411 return IntegerLiteral::Create(Importer.getToContext(),
4412 E->getValue(), T,
4413 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004414}
4415
Douglas Gregor623421d2010-02-18 02:21:22 +00004416Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
4417 QualType T = Importer.Import(E->getType());
4418 if (T.isNull())
4419 return 0;
4420
Douglas Gregorfb65e592011-07-27 05:40:30 +00004421 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
4422 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00004423 Importer.Import(E->getLocation()));
4424}
4425
Douglas Gregorc74247e2010-02-19 01:07:06 +00004426Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
4427 Expr *SubExpr = Importer.Import(E->getSubExpr());
4428 if (!SubExpr)
4429 return 0;
4430
4431 return new (Importer.getToContext())
4432 ParenExpr(Importer.Import(E->getLParen()),
4433 Importer.Import(E->getRParen()),
4434 SubExpr);
4435}
4436
4437Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
4438 QualType T = Importer.Import(E->getType());
4439 if (T.isNull())
4440 return 0;
4441
4442 Expr *SubExpr = Importer.Import(E->getSubExpr());
4443 if (!SubExpr)
4444 return 0;
4445
4446 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004447 T, E->getValueKind(),
4448 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00004449 Importer.Import(E->getOperatorLoc()));
4450}
4451
Peter Collingbournee190dee2011-03-11 19:24:49 +00004452Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
4453 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00004454 QualType ResultType = Importer.Import(E->getType());
4455
4456 if (E->isArgumentType()) {
4457 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
4458 if (!TInfo)
4459 return 0;
4460
Peter Collingbournee190dee2011-03-11 19:24:49 +00004461 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4462 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004463 Importer.Import(E->getOperatorLoc()),
4464 Importer.Import(E->getRParenLoc()));
4465 }
4466
4467 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
4468 if (!SubExpr)
4469 return 0;
4470
Peter Collingbournee190dee2011-03-11 19:24:49 +00004471 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
4472 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00004473 Importer.Import(E->getOperatorLoc()),
4474 Importer.Import(E->getRParenLoc()));
4475}
4476
Douglas Gregorc74247e2010-02-19 01:07:06 +00004477Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
4478 QualType T = Importer.Import(E->getType());
4479 if (T.isNull())
4480 return 0;
4481
4482 Expr *LHS = Importer.Import(E->getLHS());
4483 if (!LHS)
4484 return 0;
4485
4486 Expr *RHS = Importer.Import(E->getRHS());
4487 if (!RHS)
4488 return 0;
4489
4490 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004491 T, E->getValueKind(),
4492 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00004493 Importer.Import(E->getOperatorLoc()),
4494 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004495}
4496
4497Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
4498 QualType T = Importer.Import(E->getType());
4499 if (T.isNull())
4500 return 0;
4501
4502 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
4503 if (CompLHSType.isNull())
4504 return 0;
4505
4506 QualType CompResultType = Importer.Import(E->getComputationResultType());
4507 if (CompResultType.isNull())
4508 return 0;
4509
4510 Expr *LHS = Importer.Import(E->getLHS());
4511 if (!LHS)
4512 return 0;
4513
4514 Expr *RHS = Importer.Import(E->getRHS());
4515 if (!RHS)
4516 return 0;
4517
4518 return new (Importer.getToContext())
4519 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00004520 T, E->getValueKind(),
4521 E->getObjectKind(),
4522 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00004523 Importer.Import(E->getOperatorLoc()),
4524 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00004525}
4526
Benjamin Kramer8aef5962011-03-26 12:38:21 +00004527static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00004528 if (E->path_empty()) return false;
4529
4530 // TODO: import cast paths
4531 return true;
4532}
4533
Douglas Gregor98c10182010-02-12 22:17:39 +00004534Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
4535 QualType T = Importer.Import(E->getType());
4536 if (T.isNull())
4537 return 0;
4538
4539 Expr *SubExpr = Importer.Import(E->getSubExpr());
4540 if (!SubExpr)
4541 return 0;
John McCallcf142162010-08-07 06:22:56 +00004542
4543 CXXCastPath BasePath;
4544 if (ImportCastPath(E, BasePath))
4545 return 0;
4546
4547 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00004548 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00004549}
4550
Douglas Gregor5481d322010-02-19 01:32:14 +00004551Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
4552 QualType T = Importer.Import(E->getType());
4553 if (T.isNull())
4554 return 0;
4555
4556 Expr *SubExpr = Importer.Import(E->getSubExpr());
4557 if (!SubExpr)
4558 return 0;
4559
4560 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
4561 if (!TInfo && E->getTypeInfoAsWritten())
4562 return 0;
4563
John McCallcf142162010-08-07 06:22:56 +00004564 CXXCastPath BasePath;
4565 if (ImportCastPath(E, BasePath))
4566 return 0;
4567
John McCall7decc9e2010-11-18 06:31:45 +00004568 return CStyleCastExpr::Create(Importer.getToContext(), T,
4569 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00004570 SubExpr, &BasePath, TInfo,
4571 Importer.Import(E->getLParenLoc()),
4572 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00004573}
4574
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004575ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00004576 ASTContext &FromContext, FileManager &FromFileManager,
4577 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00004578 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00004579 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00004580 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00004581{
Douglas Gregor62d311f2010-02-09 19:21:46 +00004582 ImportedDecls[FromContext.getTranslationUnitDecl()]
4583 = ToContext.getTranslationUnitDecl();
4584}
4585
4586ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00004587
4588QualType ASTImporter::Import(QualType FromT) {
4589 if (FromT.isNull())
4590 return QualType();
John McCall424cec92011-01-19 06:33:43 +00004591
4592 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00004593
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004594 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00004595 llvm::DenseMap<const Type *, const Type *>::iterator Pos
4596 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004597 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00004598 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004599
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004600 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00004601 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00004602 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00004603 if (ToT.isNull())
4604 return ToT;
4605
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004606 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00004607 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00004608
John McCall424cec92011-01-19 06:33:43 +00004609 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00004610}
4611
Douglas Gregor62d311f2010-02-09 19:21:46 +00004612TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004613 if (!FromTSI)
4614 return FromTSI;
4615
4616 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00004617 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00004618 QualType T = Import(FromTSI->getType());
4619 if (T.isNull())
4620 return 0;
4621
4622 return ToContext.getTrivialTypeSourceInfo(T,
Daniel Dunbar62ee6412012-03-09 18:35:03 +00004623 FromTSI->getTypeLoc().getLocStart());
Douglas Gregor62d311f2010-02-09 19:21:46 +00004624}
4625
4626Decl *ASTImporter::Import(Decl *FromD) {
4627 if (!FromD)
4628 return 0;
4629
Douglas Gregord451ea92011-07-29 23:31:30 +00004630 ASTNodeImporter Importer(*this);
4631
Douglas Gregor62d311f2010-02-09 19:21:46 +00004632 // Check whether we've already imported this declaration.
4633 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00004634 if (Pos != ImportedDecls.end()) {
4635 Decl *ToD = Pos->second;
4636 Importer.ImportDefinitionIfNeeded(FromD, ToD);
4637 return ToD;
4638 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00004639
4640 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00004641 Decl *ToD = Importer.Visit(FromD);
4642 if (!ToD)
4643 return 0;
4644
4645 // Record the imported declaration.
4646 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00004647
4648 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
4649 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00004650 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00004651 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00004652 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004653 // When we've finished transforming a typedef, see whether it was the
4654 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00004655 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00004656 FromTag = AnonTagsWithPendingTypedefs.begin(),
4657 FromTagEnd = AnonTagsWithPendingTypedefs.end();
4658 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00004659 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00004660 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
4661 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00004662 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00004663 AnonTagsWithPendingTypedefs.erase(FromTag);
4664 break;
4665 }
4666 }
4667 }
4668 }
4669
Douglas Gregor62d311f2010-02-09 19:21:46 +00004670 return ToD;
4671}
4672
4673DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
4674 if (!FromDC)
4675 return FromDC;
4676
Douglas Gregor95d82832012-01-24 18:36:04 +00004677 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00004678 if (!ToDC)
4679 return 0;
4680
4681 // When we're using a record/enum/Objective-C class/protocol as a context, we
4682 // need it to have a definition.
4683 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00004684 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00004685 if (ToRecord->isCompleteDefinition()) {
4686 // Do nothing.
4687 } else if (FromRecord->isCompleteDefinition()) {
4688 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
4689 ASTNodeImporter::IDK_Basic);
4690 } else {
4691 CompleteDecl(ToRecord);
4692 }
4693 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
4694 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
4695 if (ToEnum->isCompleteDefinition()) {
4696 // Do nothing.
4697 } else if (FromEnum->isCompleteDefinition()) {
4698 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
4699 ASTNodeImporter::IDK_Basic);
4700 } else {
4701 CompleteDecl(ToEnum);
4702 }
4703 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
4704 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
4705 if (ToClass->getDefinition()) {
4706 // Do nothing.
4707 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
4708 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
4709 ASTNodeImporter::IDK_Basic);
4710 } else {
4711 CompleteDecl(ToClass);
4712 }
4713 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
4714 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
4715 if (ToProto->getDefinition()) {
4716 // Do nothing.
4717 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
4718 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
4719 ASTNodeImporter::IDK_Basic);
4720 } else {
4721 CompleteDecl(ToProto);
4722 }
Douglas Gregor95d82832012-01-24 18:36:04 +00004723 }
4724
4725 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004726}
4727
4728Expr *ASTImporter::Import(Expr *FromE) {
4729 if (!FromE)
4730 return 0;
4731
4732 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
4733}
4734
4735Stmt *ASTImporter::Import(Stmt *FromS) {
4736 if (!FromS)
4737 return 0;
4738
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004739 // Check whether we've already imported this declaration.
4740 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
4741 if (Pos != ImportedStmts.end())
4742 return Pos->second;
4743
4744 // Import the type
4745 ASTNodeImporter Importer(*this);
4746 Stmt *ToS = Importer.Visit(FromS);
4747 if (!ToS)
4748 return 0;
4749
4750 // Record the imported declaration.
4751 ImportedStmts[FromS] = ToS;
4752 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00004753}
4754
4755NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
4756 if (!FromNNS)
4757 return 0;
4758
Douglas Gregor90ebf252011-04-27 16:48:40 +00004759 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
4760
4761 switch (FromNNS->getKind()) {
4762 case NestedNameSpecifier::Identifier:
4763 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
4764 return NestedNameSpecifier::Create(ToContext, prefix, II);
4765 }
4766 return 0;
4767
4768 case NestedNameSpecifier::Namespace:
4769 if (NamespaceDecl *NS =
4770 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
4771 return NestedNameSpecifier::Create(ToContext, prefix, NS);
4772 }
4773 return 0;
4774
4775 case NestedNameSpecifier::NamespaceAlias:
4776 if (NamespaceAliasDecl *NSAD =
4777 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
4778 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
4779 }
4780 return 0;
4781
4782 case NestedNameSpecifier::Global:
4783 return NestedNameSpecifier::GlobalSpecifier(ToContext);
4784
4785 case NestedNameSpecifier::TypeSpec:
4786 case NestedNameSpecifier::TypeSpecWithTemplate: {
4787 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
4788 if (!T.isNull()) {
4789 bool bTemplate = FromNNS->getKind() ==
4790 NestedNameSpecifier::TypeSpecWithTemplate;
4791 return NestedNameSpecifier::Create(ToContext, prefix,
4792 bTemplate, T.getTypePtr());
4793 }
4794 }
4795 return 0;
4796 }
4797
4798 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00004799}
4800
Douglas Gregor14454802011-02-25 02:25:35 +00004801NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
4802 // FIXME: Implement!
4803 return NestedNameSpecifierLoc();
4804}
4805
Douglas Gregore2e50d332010-12-01 01:36:18 +00004806TemplateName ASTImporter::Import(TemplateName From) {
4807 switch (From.getKind()) {
4808 case TemplateName::Template:
4809 if (TemplateDecl *ToTemplate
4810 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4811 return TemplateName(ToTemplate);
4812
4813 return TemplateName();
4814
4815 case TemplateName::OverloadedTemplate: {
4816 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
4817 UnresolvedSet<2> ToTemplates;
4818 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
4819 E = FromStorage->end();
4820 I != E; ++I) {
4821 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
4822 ToTemplates.addDecl(To);
4823 else
4824 return TemplateName();
4825 }
4826 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
4827 ToTemplates.end());
4828 }
4829
4830 case TemplateName::QualifiedTemplate: {
4831 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
4832 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
4833 if (!Qualifier)
4834 return TemplateName();
4835
4836 if (TemplateDecl *ToTemplate
4837 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
4838 return ToContext.getQualifiedTemplateName(Qualifier,
4839 QTN->hasTemplateKeyword(),
4840 ToTemplate);
4841
4842 return TemplateName();
4843 }
4844
4845 case TemplateName::DependentTemplate: {
4846 DependentTemplateName *DTN = From.getAsDependentTemplateName();
4847 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
4848 if (!Qualifier)
4849 return TemplateName();
4850
4851 if (DTN->isIdentifier()) {
4852 return ToContext.getDependentTemplateName(Qualifier,
4853 Import(DTN->getIdentifier()));
4854 }
4855
4856 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
4857 }
John McCalld9dfe3a2011-06-30 08:33:18 +00004858
4859 case TemplateName::SubstTemplateTemplateParm: {
4860 SubstTemplateTemplateParmStorage *subst
4861 = From.getAsSubstTemplateTemplateParm();
4862 TemplateTemplateParmDecl *param
4863 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
4864 if (!param)
4865 return TemplateName();
4866
4867 TemplateName replacement = Import(subst->getReplacement());
4868 if (replacement.isNull()) return TemplateName();
4869
4870 return ToContext.getSubstTemplateTemplateParm(param, replacement);
4871 }
Douglas Gregor5590be02011-01-15 06:45:20 +00004872
4873 case TemplateName::SubstTemplateTemplateParmPack: {
4874 SubstTemplateTemplateParmPackStorage *SubstPack
4875 = From.getAsSubstTemplateTemplateParmPack();
4876 TemplateTemplateParmDecl *Param
4877 = cast_or_null<TemplateTemplateParmDecl>(
4878 Import(SubstPack->getParameterPack()));
4879 if (!Param)
4880 return TemplateName();
4881
4882 ASTNodeImporter Importer(*this);
4883 TemplateArgument ArgPack
4884 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
4885 if (ArgPack.isNull())
4886 return TemplateName();
4887
4888 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
4889 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00004890 }
4891
4892 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00004893}
4894
Douglas Gregor62d311f2010-02-09 19:21:46 +00004895SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
4896 if (FromLoc.isInvalid())
4897 return SourceLocation();
4898
Douglas Gregor811663e2010-02-10 00:15:17 +00004899 SourceManager &FromSM = FromContext.getSourceManager();
4900
4901 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00004902 // don't have to import macro expansions.
4903 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00004904 FromLoc = FromSM.getSpellingLoc(FromLoc);
4905 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
4906 SourceManager &ToSM = ToContext.getSourceManager();
4907 return ToSM.getLocForStartOfFile(Import(Decomposed.first))
Argyrios Kyrtzidise6e67de2011-09-19 20:40:19 +00004908 .getLocWithOffset(Decomposed.second);
Douglas Gregor62d311f2010-02-09 19:21:46 +00004909}
4910
4911SourceRange ASTImporter::Import(SourceRange FromRange) {
4912 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
4913}
4914
Douglas Gregor811663e2010-02-10 00:15:17 +00004915FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00004916 llvm::DenseMap<FileID, FileID>::iterator Pos
4917 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00004918 if (Pos != ImportedFileIDs.end())
4919 return Pos->second;
4920
4921 SourceManager &FromSM = FromContext.getSourceManager();
4922 SourceManager &ToSM = ToContext.getSourceManager();
4923 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00004924 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00004925
4926 // Include location of this file.
4927 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
4928
4929 // Map the FileID for to the "to" source manager.
4930 FileID ToID;
4931 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004932 if (Cache->OrigEntry) {
Douglas Gregor811663e2010-02-10 00:15:17 +00004933 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
4934 // disk again
4935 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
4936 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00004937 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Douglas Gregor811663e2010-02-10 00:15:17 +00004938 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
4939 FromSLoc.getFile().getFileCharacteristic());
4940 } else {
4941 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00004942 const llvm::MemoryBuffer *
4943 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Douglas Gregor811663e2010-02-10 00:15:17 +00004944 llvm::MemoryBuffer *ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00004945 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00004946 FromBuf->getBufferIdentifier());
Argyrios Kyrtzidis6566e232012-11-09 19:40:45 +00004947 ToID = ToSM.createFileIDForMemBuffer(ToBuf,
4948 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00004949 }
4950
4951
Sebastian Redl99219f12010-09-30 01:03:06 +00004952 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00004953 return ToID;
4954}
4955
Douglas Gregor0a791672011-01-18 03:11:38 +00004956void ASTImporter::ImportDefinition(Decl *From) {
4957 Decl *To = Import(From);
4958 if (!To)
4959 return;
4960
4961 if (DeclContext *FromDC = cast<DeclContext>(From)) {
4962 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004963
4964 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
4965 if (!ToRecord->getDefinition()) {
4966 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00004967 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00004968 return;
4969 }
4970 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004971
4972 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
4973 if (!ToEnum->getDefinition()) {
4974 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004975 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00004976 return;
4977 }
4978 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004979
4980 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
4981 if (!ToIFace->getDefinition()) {
4982 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004983 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004984 return;
4985 }
4986 }
Douglas Gregord451ea92011-07-29 23:31:30 +00004987
Douglas Gregor2aa53772012-01-24 17:42:07 +00004988 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
4989 if (!ToProto->getDefinition()) {
4990 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00004991 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00004992 return;
4993 }
4994 }
4995
Douglas Gregor0a791672011-01-18 03:11:38 +00004996 Importer.ImportDeclContext(FromDC, true);
4997 }
4998}
4999
Douglas Gregor96e578d2010-02-05 17:54:41 +00005000DeclarationName ASTImporter::Import(DeclarationName FromName) {
5001 if (!FromName)
5002 return DeclarationName();
5003
5004 switch (FromName.getNameKind()) {
5005 case DeclarationName::Identifier:
5006 return Import(FromName.getAsIdentifierInfo());
5007
5008 case DeclarationName::ObjCZeroArgSelector:
5009 case DeclarationName::ObjCOneArgSelector:
5010 case DeclarationName::ObjCMultiArgSelector:
5011 return Import(FromName.getObjCSelector());
5012
5013 case DeclarationName::CXXConstructorName: {
5014 QualType T = Import(FromName.getCXXNameType());
5015 if (T.isNull())
5016 return DeclarationName();
5017
5018 return ToContext.DeclarationNames.getCXXConstructorName(
5019 ToContext.getCanonicalType(T));
5020 }
5021
5022 case DeclarationName::CXXDestructorName: {
5023 QualType T = Import(FromName.getCXXNameType());
5024 if (T.isNull())
5025 return DeclarationName();
5026
5027 return ToContext.DeclarationNames.getCXXDestructorName(
5028 ToContext.getCanonicalType(T));
5029 }
5030
5031 case DeclarationName::CXXConversionFunctionName: {
5032 QualType T = Import(FromName.getCXXNameType());
5033 if (T.isNull())
5034 return DeclarationName();
5035
5036 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5037 ToContext.getCanonicalType(T));
5038 }
5039
5040 case DeclarationName::CXXOperatorName:
5041 return ToContext.DeclarationNames.getCXXOperatorName(
5042 FromName.getCXXOverloadedOperator());
5043
5044 case DeclarationName::CXXLiteralOperatorName:
5045 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5046 Import(FromName.getCXXLiteralIdentifier()));
5047
5048 case DeclarationName::CXXUsingDirective:
5049 // FIXME: STATICS!
5050 return DeclarationName::getUsingDirectiveName();
5051 }
5052
David Blaikiee4d798f2012-01-20 21:50:17 +00005053 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005054}
5055
Douglas Gregore2e50d332010-12-01 01:36:18 +00005056IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005057 if (!FromId)
5058 return 0;
5059
5060 return &ToContext.Idents.get(FromId->getName());
5061}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005062
Douglas Gregor43f54792010-02-17 02:12:47 +00005063Selector ASTImporter::Import(Selector FromSel) {
5064 if (FromSel.isNull())
5065 return Selector();
5066
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005067 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005068 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5069 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5070 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5071 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5072}
5073
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005074DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5075 DeclContext *DC,
5076 unsigned IDNS,
5077 NamedDecl **Decls,
5078 unsigned NumDecls) {
5079 return Name;
5080}
5081
5082DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005083 if (LastDiagFromFrom)
5084 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5085 FromContext.getDiagnostics());
5086 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005087 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005088}
5089
5090DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005091 if (!LastDiagFromFrom)
5092 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5093 ToContext.getDiagnostics());
5094 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005095 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005096}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005097
Douglas Gregor2e15c842012-02-01 21:00:38 +00005098void ASTImporter::CompleteDecl (Decl *D) {
5099 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5100 if (!ID->getDefinition())
5101 ID->startDefinition();
5102 }
5103 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5104 if (!PD->getDefinition())
5105 PD->startDefinition();
5106 }
5107 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5108 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5109 TD->startDefinition();
5110 TD->setCompleteDefinition(true);
5111 }
5112 }
5113 else {
5114 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5115 }
5116}
5117
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005118Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5119 ImportedDecls[From] = To;
5120 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005121}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005122
Douglas Gregordd6006f2012-07-17 21:16:27 +00005123bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5124 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005125 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005126 = ImportedTypes.find(From.getTypePtr());
5127 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5128 return true;
5129
Douglas Gregordd6006f2012-07-17 21:16:27 +00005130 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5131 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005132 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005133}