blob: 039ea20eedf97ed19aae8628ea81a247f667f439 [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);
Sean Callanan72fe0852015-04-02 23:50:08 +000070 QualType VisitAttributedType(const AttributedType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000071 // FIXME: TemplateTypeParmType
72 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000073 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
74 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000075 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000076 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000077 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
78 QualType VisitObjCObjectType(const ObjCObjectType *T);
79 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000080
Douglas Gregor95d82832012-01-24 18:36:04 +000081 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000082 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
83 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000084 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000085 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000086 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
87 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000088 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Douglas Gregor2e15c842012-02-01 21:00:38 +000089
Douglas Gregor95d82832012-01-24 18:36:04 +000090 /// \brief What we should import from the definition.
91 enum ImportDefinitionKind {
92 /// \brief Import the default subset of the definition, which might be
93 /// nothing (if minimal import is set) or might be everything (if minimal
94 /// import is not set).
95 IDK_Default,
96 /// \brief Import everything.
97 IDK_Everything,
98 /// \brief Import only the bare bones needed to establish a valid
99 /// DeclContext.
100 IDK_Basic
101 };
102
Douglas Gregor2e15c842012-02-01 21:00:38 +0000103 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
104 return IDK == IDK_Everything ||
105 (IDK == IDK_Default && !Importer.isMinimalImport());
106 }
107
Douglas Gregord451ea92011-07-29 23:31:30 +0000108 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000109 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000110 bool ImportDefinition(VarDecl *From, VarDecl *To,
111 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000112 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000113 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000114 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000115 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000116 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000117 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000118 TemplateParameterList *ImportTemplateParameterList(
119 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000120 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
121 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
122 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000123 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000124 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
125 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000126 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
127 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000128 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000129 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000130 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000132 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000133 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000134 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000135 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000136 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000137 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000138 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000139 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000140 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000141 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000142 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000143 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
144 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
145 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
146 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000147 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000148 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000149 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000150 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000151 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000152 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000153 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000154 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000155 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000156 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000157 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000158
159 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000160 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000161 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000162 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000163 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000164 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000165 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
166 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
167 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
168 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000169 Decl *VisitClassTemplateSpecializationDecl(
170 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000171 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
172 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
173
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000174 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000175 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
176
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000177 Stmt *VisitStmt(Stmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000178 Stmt *VisitDeclStmt(DeclStmt *S);
179 Stmt *VisitNullStmt(NullStmt *S);
180 Stmt *VisitCompoundStmt(CompoundStmt *S);
181 Stmt *VisitCaseStmt(CaseStmt *S);
182 Stmt *VisitDefaultStmt(DefaultStmt *S);
183 Stmt *VisitLabelStmt(LabelStmt *S);
184 Stmt *VisitAttributedStmt(AttributedStmt *S);
185 Stmt *VisitIfStmt(IfStmt *S);
186 Stmt *VisitSwitchStmt(SwitchStmt *S);
187 Stmt *VisitWhileStmt(WhileStmt *S);
188 Stmt *VisitDoStmt(DoStmt *S);
189 Stmt *VisitForStmt(ForStmt *S);
190 Stmt *VisitGotoStmt(GotoStmt *S);
191 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
192 Stmt *VisitContinueStmt(ContinueStmt *S);
193 Stmt *VisitBreakStmt(BreakStmt *S);
194 Stmt *VisitReturnStmt(ReturnStmt *S);
195 // FIXME: GCCAsmStmt
196 // FIXME: MSAsmStmt
197 // FIXME: SEHExceptStmt
198 // FIXME: SEHFinallyStmt
199 // FIXME: SEHTryStmt
200 // FIXME: SEHLeaveStmt
201 // FIXME: CapturedStmt
202 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
203 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
204 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
205 // FIXME: MSDependentExistsStmt
206 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
207 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
208 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
209 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
210 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
211 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
212 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000213
214 // Importing expressions
215 Expr *VisitExpr(Expr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000216 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000217 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000218 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000219 Expr *VisitParenExpr(ParenExpr *E);
220 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000221 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000222 Expr *VisitBinaryOperator(BinaryOperator *E);
223 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000224 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000225 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000226 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
227 Expr *VisitMemberExpr(MemberExpr *E);
228 Expr *VisitCallExpr(CallExpr *E);
Douglas Gregor96e578d2010-02-05 17:54:41 +0000229 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000230}
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000231using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000232
233//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000234// Structural Equivalence
235//----------------------------------------------------------------------------
236
237namespace {
238 struct StructuralEquivalenceContext {
239 /// \brief AST contexts for which we are checking structural equivalence.
240 ASTContext &C1, &C2;
241
Douglas Gregor3996e242010-02-15 22:01:00 +0000242 /// \brief The set of "tentative" equivalences between two canonical
243 /// declarations, mapping from a declaration in the first context to the
244 /// declaration in the second context that we believe to be equivalent.
245 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
246
247 /// \brief Queue of declarations in the first context whose equivalence
248 /// with a declaration in the second context still needs to be verified.
249 std::deque<Decl *> DeclsToCheck;
250
Douglas Gregorb4964f72010-02-15 23:54:17 +0000251 /// \brief Declaration (from, to) pairs that are known not to be equivalent
252 /// (which we have already complained about).
253 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
254
Douglas Gregor3996e242010-02-15 22:01:00 +0000255 /// \brief Whether we're being strict about the spelling of types when
256 /// unifying two types.
257 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000258
259 /// \brief Whether to complain about failures.
260 bool Complain;
261
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000262 /// \brief \c true if the last diagnostic came from C2.
263 bool LastDiagFromC2;
264
Douglas Gregor3996e242010-02-15 22:01:00 +0000265 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000266 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000267 bool StrictTypeSpelling = false,
268 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000269 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000270 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
271 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000272
273 /// \brief Determine whether the two declarations are structurally
274 /// equivalent.
275 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
276
277 /// \brief Determine whether the two types are structurally equivalent.
278 bool IsStructurallyEquivalent(QualType T1, QualType T2);
279
280 private:
281 /// \brief Finish checking all of the structural equivalences.
282 ///
283 /// \returns true if an error occurred, false otherwise.
284 bool Finish();
285
286 public:
287 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000288 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000289 if (LastDiagFromC2)
290 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
291 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000292 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000293 }
294
295 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000296 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000297 if (!LastDiagFromC2)
298 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
299 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000300 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000301 }
302 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000303}
Douglas Gregor3996e242010-02-15 22:01:00 +0000304
305static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
306 QualType T1, QualType T2);
307static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
308 Decl *D1, Decl *D2);
309
Douglas Gregor3996e242010-02-15 22:01:00 +0000310/// \brief Determine structural equivalence of two expressions.
311static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
312 Expr *E1, Expr *E2) {
313 if (!E1 || !E2)
314 return E1 == E2;
315
316 // FIXME: Actually perform a structural comparison!
317 return true;
318}
319
320/// \brief Determine whether two identifiers are equivalent.
321static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
322 const IdentifierInfo *Name2) {
323 if (!Name1 || !Name2)
324 return Name1 == Name2;
325
326 return Name1->getName() == Name2->getName();
327}
328
329/// \brief Determine whether two nested-name-specifiers are equivalent.
330static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
331 NestedNameSpecifier *NNS1,
332 NestedNameSpecifier *NNS2) {
333 // FIXME: Implement!
334 return true;
335}
336
337/// \brief Determine whether two template arguments are equivalent.
338static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
339 const TemplateArgument &Arg1,
340 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000341 if (Arg1.getKind() != Arg2.getKind())
342 return false;
343
344 switch (Arg1.getKind()) {
345 case TemplateArgument::Null:
346 return true;
347
348 case TemplateArgument::Type:
349 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000350
Douglas Gregore2e50d332010-12-01 01:36:18 +0000351 case TemplateArgument::Integral:
352 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
353 Arg2.getIntegralType()))
354 return false;
355
Eric Christopher6dcc3762012-07-15 00:23:57 +0000356 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000357
358 case TemplateArgument::Declaration:
359 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000360
361 case TemplateArgument::NullPtr:
362 return true; // FIXME: Is this correct?
363
Douglas Gregore2e50d332010-12-01 01:36:18 +0000364 case TemplateArgument::Template:
365 return IsStructurallyEquivalent(Context,
366 Arg1.getAsTemplate(),
367 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000368
369 case TemplateArgument::TemplateExpansion:
370 return IsStructurallyEquivalent(Context,
371 Arg1.getAsTemplateOrTemplatePattern(),
372 Arg2.getAsTemplateOrTemplatePattern());
373
Douglas Gregore2e50d332010-12-01 01:36:18 +0000374 case TemplateArgument::Expression:
375 return IsStructurallyEquivalent(Context,
376 Arg1.getAsExpr(), Arg2.getAsExpr());
377
378 case TemplateArgument::Pack:
379 if (Arg1.pack_size() != Arg2.pack_size())
380 return false;
381
382 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
383 if (!IsStructurallyEquivalent(Context,
384 Arg1.pack_begin()[I],
385 Arg2.pack_begin()[I]))
386 return false;
387
388 return true;
389 }
390
391 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000392}
393
394/// \brief Determine structural equivalence for the common part of array
395/// types.
396static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
397 const ArrayType *Array1,
398 const ArrayType *Array2) {
399 if (!IsStructurallyEquivalent(Context,
400 Array1->getElementType(),
401 Array2->getElementType()))
402 return false;
403 if (Array1->getSizeModifier() != Array2->getSizeModifier())
404 return false;
405 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
406 return false;
407
408 return true;
409}
410
411/// \brief Determine structural equivalence of two types.
412static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
413 QualType T1, QualType T2) {
414 if (T1.isNull() || T2.isNull())
415 return T1.isNull() && T2.isNull();
416
417 if (!Context.StrictTypeSpelling) {
418 // We aren't being strict about token-to-token equivalence of types,
419 // so map down to the canonical type.
420 T1 = Context.C1.getCanonicalType(T1);
421 T2 = Context.C2.getCanonicalType(T2);
422 }
423
424 if (T1.getQualifiers() != T2.getQualifiers())
425 return false;
426
Douglas Gregorb4964f72010-02-15 23:54:17 +0000427 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000428
Douglas Gregorb4964f72010-02-15 23:54:17 +0000429 if (T1->getTypeClass() != T2->getTypeClass()) {
430 // Compare function types with prototypes vs. without prototypes as if
431 // both did not have prototypes.
432 if (T1->getTypeClass() == Type::FunctionProto &&
433 T2->getTypeClass() == Type::FunctionNoProto)
434 TC = Type::FunctionNoProto;
435 else if (T1->getTypeClass() == Type::FunctionNoProto &&
436 T2->getTypeClass() == Type::FunctionProto)
437 TC = Type::FunctionNoProto;
438 else
439 return false;
440 }
441
442 switch (TC) {
443 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000444 // FIXME: Deal with Char_S/Char_U.
445 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
446 return false;
447 break;
448
449 case Type::Complex:
450 if (!IsStructurallyEquivalent(Context,
451 cast<ComplexType>(T1)->getElementType(),
452 cast<ComplexType>(T2)->getElementType()))
453 return false;
454 break;
455
Reid Kleckner0503a872013-12-05 01:23:43 +0000456 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000457 case Type::Decayed:
458 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000459 cast<AdjustedType>(T1)->getOriginalType(),
460 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000461 return false;
462 break;
463
Douglas Gregor3996e242010-02-15 22:01:00 +0000464 case Type::Pointer:
465 if (!IsStructurallyEquivalent(Context,
466 cast<PointerType>(T1)->getPointeeType(),
467 cast<PointerType>(T2)->getPointeeType()))
468 return false;
469 break;
470
471 case Type::BlockPointer:
472 if (!IsStructurallyEquivalent(Context,
473 cast<BlockPointerType>(T1)->getPointeeType(),
474 cast<BlockPointerType>(T2)->getPointeeType()))
475 return false;
476 break;
477
478 case Type::LValueReference:
479 case Type::RValueReference: {
480 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
481 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
482 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
483 return false;
484 if (Ref1->isInnerRef() != Ref2->isInnerRef())
485 return false;
486 if (!IsStructurallyEquivalent(Context,
487 Ref1->getPointeeTypeAsWritten(),
488 Ref2->getPointeeTypeAsWritten()))
489 return false;
490 break;
491 }
492
493 case Type::MemberPointer: {
494 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
495 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
496 if (!IsStructurallyEquivalent(Context,
497 MemPtr1->getPointeeType(),
498 MemPtr2->getPointeeType()))
499 return false;
500 if (!IsStructurallyEquivalent(Context,
501 QualType(MemPtr1->getClass(), 0),
502 QualType(MemPtr2->getClass(), 0)))
503 return false;
504 break;
505 }
506
507 case Type::ConstantArray: {
508 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
509 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000510 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000511 return false;
512
513 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
514 return false;
515 break;
516 }
517
518 case Type::IncompleteArray:
519 if (!IsArrayStructurallyEquivalent(Context,
520 cast<ArrayType>(T1),
521 cast<ArrayType>(T2)))
522 return false;
523 break;
524
525 case Type::VariableArray: {
526 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
527 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
528 if (!IsStructurallyEquivalent(Context,
529 Array1->getSizeExpr(), Array2->getSizeExpr()))
530 return false;
531
532 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
533 return false;
534
535 break;
536 }
537
538 case Type::DependentSizedArray: {
539 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
540 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
541 if (!IsStructurallyEquivalent(Context,
542 Array1->getSizeExpr(), Array2->getSizeExpr()))
543 return false;
544
545 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
546 return false;
547
548 break;
549 }
550
551 case Type::DependentSizedExtVector: {
552 const DependentSizedExtVectorType *Vec1
553 = cast<DependentSizedExtVectorType>(T1);
554 const DependentSizedExtVectorType *Vec2
555 = cast<DependentSizedExtVectorType>(T2);
556 if (!IsStructurallyEquivalent(Context,
557 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
558 return false;
559 if (!IsStructurallyEquivalent(Context,
560 Vec1->getElementType(),
561 Vec2->getElementType()))
562 return false;
563 break;
564 }
565
566 case Type::Vector:
567 case Type::ExtVector: {
568 const VectorType *Vec1 = cast<VectorType>(T1);
569 const VectorType *Vec2 = cast<VectorType>(T2);
570 if (!IsStructurallyEquivalent(Context,
571 Vec1->getElementType(),
572 Vec2->getElementType()))
573 return false;
574 if (Vec1->getNumElements() != Vec2->getNumElements())
575 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000576 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000577 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000578 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000579 }
580
581 case Type::FunctionProto: {
582 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
583 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000584 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000585 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000586 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
587 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
588 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000589 return false;
590 }
591 if (Proto1->isVariadic() != Proto2->isVariadic())
592 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000593 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000594 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000595 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
596 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
597 return false;
598 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
599 if (!IsStructurallyEquivalent(Context,
600 Proto1->getExceptionType(I),
601 Proto2->getExceptionType(I)))
602 return false;
603 }
604 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000605 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000606 Proto1->getNoexceptExpr(),
607 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000608 return false;
609 }
610 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
611 return false;
612
613 // Fall through to check the bits common with FunctionNoProtoType.
614 }
615
616 case Type::FunctionNoProto: {
617 const FunctionType *Function1 = cast<FunctionType>(T1);
618 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000619 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
620 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000621 return false;
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000622 if (Function1->getExtInfo() != Function2->getExtInfo())
623 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000624 break;
625 }
626
627 case Type::UnresolvedUsing:
628 if (!IsStructurallyEquivalent(Context,
629 cast<UnresolvedUsingType>(T1)->getDecl(),
630 cast<UnresolvedUsingType>(T2)->getDecl()))
631 return false;
632
633 break;
John McCall81904512011-01-06 01:58:22 +0000634
635 case Type::Attributed:
636 if (!IsStructurallyEquivalent(Context,
637 cast<AttributedType>(T1)->getModifiedType(),
638 cast<AttributedType>(T2)->getModifiedType()))
639 return false;
640 if (!IsStructurallyEquivalent(Context,
641 cast<AttributedType>(T1)->getEquivalentType(),
642 cast<AttributedType>(T2)->getEquivalentType()))
643 return false;
644 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000645
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000646 case Type::Paren:
647 if (!IsStructurallyEquivalent(Context,
648 cast<ParenType>(T1)->getInnerType(),
649 cast<ParenType>(T2)->getInnerType()))
650 return false;
651 break;
652
Douglas Gregor3996e242010-02-15 22:01:00 +0000653 case Type::Typedef:
654 if (!IsStructurallyEquivalent(Context,
655 cast<TypedefType>(T1)->getDecl(),
656 cast<TypedefType>(T2)->getDecl()))
657 return false;
658 break;
659
660 case Type::TypeOfExpr:
661 if (!IsStructurallyEquivalent(Context,
662 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
663 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
664 return false;
665 break;
666
667 case Type::TypeOf:
668 if (!IsStructurallyEquivalent(Context,
669 cast<TypeOfType>(T1)->getUnderlyingType(),
670 cast<TypeOfType>(T2)->getUnderlyingType()))
671 return false;
672 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000673
674 case Type::UnaryTransform:
675 if (!IsStructurallyEquivalent(Context,
676 cast<UnaryTransformType>(T1)->getUnderlyingType(),
677 cast<UnaryTransformType>(T1)->getUnderlyingType()))
678 return false;
679 break;
680
Douglas Gregor3996e242010-02-15 22:01:00 +0000681 case Type::Decltype:
682 if (!IsStructurallyEquivalent(Context,
683 cast<DecltypeType>(T1)->getUnderlyingExpr(),
684 cast<DecltypeType>(T2)->getUnderlyingExpr()))
685 return false;
686 break;
687
Richard Smith30482bc2011-02-20 03:19:35 +0000688 case Type::Auto:
689 if (!IsStructurallyEquivalent(Context,
690 cast<AutoType>(T1)->getDeducedType(),
691 cast<AutoType>(T2)->getDeducedType()))
692 return false;
693 break;
694
Douglas Gregor3996e242010-02-15 22:01:00 +0000695 case Type::Record:
696 case Type::Enum:
697 if (!IsStructurallyEquivalent(Context,
698 cast<TagType>(T1)->getDecl(),
699 cast<TagType>(T2)->getDecl()))
700 return false;
701 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000702
Douglas Gregor3996e242010-02-15 22:01:00 +0000703 case Type::TemplateTypeParm: {
704 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
705 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
706 if (Parm1->getDepth() != Parm2->getDepth())
707 return false;
708 if (Parm1->getIndex() != Parm2->getIndex())
709 return false;
710 if (Parm1->isParameterPack() != Parm2->isParameterPack())
711 return false;
712
713 // Names of template type parameters are never significant.
714 break;
715 }
716
717 case Type::SubstTemplateTypeParm: {
718 const SubstTemplateTypeParmType *Subst1
719 = cast<SubstTemplateTypeParmType>(T1);
720 const SubstTemplateTypeParmType *Subst2
721 = cast<SubstTemplateTypeParmType>(T2);
722 if (!IsStructurallyEquivalent(Context,
723 QualType(Subst1->getReplacedParameter(), 0),
724 QualType(Subst2->getReplacedParameter(), 0)))
725 return false;
726 if (!IsStructurallyEquivalent(Context,
727 Subst1->getReplacementType(),
728 Subst2->getReplacementType()))
729 return false;
730 break;
731 }
732
Douglas Gregorfb322d82011-01-14 05:11:40 +0000733 case Type::SubstTemplateTypeParmPack: {
734 const SubstTemplateTypeParmPackType *Subst1
735 = cast<SubstTemplateTypeParmPackType>(T1);
736 const SubstTemplateTypeParmPackType *Subst2
737 = cast<SubstTemplateTypeParmPackType>(T2);
738 if (!IsStructurallyEquivalent(Context,
739 QualType(Subst1->getReplacedParameter(), 0),
740 QualType(Subst2->getReplacedParameter(), 0)))
741 return false;
742 if (!IsStructurallyEquivalent(Context,
743 Subst1->getArgumentPack(),
744 Subst2->getArgumentPack()))
745 return false;
746 break;
747 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000748 case Type::TemplateSpecialization: {
749 const TemplateSpecializationType *Spec1
750 = cast<TemplateSpecializationType>(T1);
751 const TemplateSpecializationType *Spec2
752 = cast<TemplateSpecializationType>(T2);
753 if (!IsStructurallyEquivalent(Context,
754 Spec1->getTemplateName(),
755 Spec2->getTemplateName()))
756 return false;
757 if (Spec1->getNumArgs() != Spec2->getNumArgs())
758 return false;
759 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
760 if (!IsStructurallyEquivalent(Context,
761 Spec1->getArg(I), Spec2->getArg(I)))
762 return false;
763 }
764 break;
765 }
766
Abramo Bagnara6150c882010-05-11 21:36:43 +0000767 case Type::Elaborated: {
768 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
769 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
770 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
771 if (Elab1->getKeyword() != Elab2->getKeyword())
772 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000773 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000774 Elab1->getQualifier(),
775 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000776 return false;
777 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000778 Elab1->getNamedType(),
779 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000780 return false;
781 break;
782 }
783
John McCalle78aac42010-03-10 03:28:59 +0000784 case Type::InjectedClassName: {
785 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
786 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
787 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000788 Inj1->getInjectedSpecializationType(),
789 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000790 return false;
791 break;
792 }
793
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000794 case Type::DependentName: {
795 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
796 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000797 if (!IsStructurallyEquivalent(Context,
798 Typename1->getQualifier(),
799 Typename2->getQualifier()))
800 return false;
801 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
802 Typename2->getIdentifier()))
803 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000804
805 break;
806 }
807
John McCallc392f372010-06-11 00:33:02 +0000808 case Type::DependentTemplateSpecialization: {
809 const DependentTemplateSpecializationType *Spec1 =
810 cast<DependentTemplateSpecializationType>(T1);
811 const DependentTemplateSpecializationType *Spec2 =
812 cast<DependentTemplateSpecializationType>(T2);
813 if (!IsStructurallyEquivalent(Context,
814 Spec1->getQualifier(),
815 Spec2->getQualifier()))
816 return false;
817 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
818 Spec2->getIdentifier()))
819 return false;
820 if (Spec1->getNumArgs() != Spec2->getNumArgs())
821 return false;
822 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
823 if (!IsStructurallyEquivalent(Context,
824 Spec1->getArg(I), Spec2->getArg(I)))
825 return false;
826 }
827 break;
828 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000829
830 case Type::PackExpansion:
831 if (!IsStructurallyEquivalent(Context,
832 cast<PackExpansionType>(T1)->getPattern(),
833 cast<PackExpansionType>(T2)->getPattern()))
834 return false;
835 break;
836
Douglas Gregor3996e242010-02-15 22:01:00 +0000837 case Type::ObjCInterface: {
838 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
839 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
840 if (!IsStructurallyEquivalent(Context,
841 Iface1->getDecl(), Iface2->getDecl()))
842 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000843 break;
844 }
845
846 case Type::ObjCObject: {
847 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
848 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
849 if (!IsStructurallyEquivalent(Context,
850 Obj1->getBaseType(),
851 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000852 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000853 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
854 return false;
855 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000856 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000857 Obj1->getProtocol(I),
858 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000859 return false;
860 }
861 break;
862 }
863
864 case Type::ObjCObjectPointer: {
865 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
866 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
867 if (!IsStructurallyEquivalent(Context,
868 Ptr1->getPointeeType(),
869 Ptr2->getPointeeType()))
870 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000871 break;
872 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000873
874 case Type::Atomic: {
875 if (!IsStructurallyEquivalent(Context,
876 cast<AtomicType>(T1)->getValueType(),
877 cast<AtomicType>(T2)->getValueType()))
878 return false;
879 break;
880 }
881
Xiuli Pan9c14e282016-01-09 12:53:17 +0000882 case Type::Pipe: {
883 if (!IsStructurallyEquivalent(Context,
884 cast<PipeType>(T1)->getElementType(),
885 cast<PipeType>(T2)->getElementType()))
886 return false;
887 break;
888 }
889
Douglas Gregor3996e242010-02-15 22:01:00 +0000890 } // end switch
891
892 return true;
893}
894
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000895/// \brief Determine structural equivalence of two fields.
896static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
897 FieldDecl *Field1, FieldDecl *Field2) {
898 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000899
900 // For anonymous structs/unions, match up the anonymous struct/union type
901 // declarations directly, so that we don't go off searching for anonymous
902 // types
903 if (Field1->isAnonymousStructOrUnion() &&
904 Field2->isAnonymousStructOrUnion()) {
905 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
906 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
907 return IsStructurallyEquivalent(Context, D1, D2);
908 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000909
910 // Check for equivalent field names.
911 IdentifierInfo *Name1 = Field1->getIdentifier();
912 IdentifierInfo *Name2 = Field2->getIdentifier();
913 if (!::IsStructurallyEquivalent(Name1, Name2))
914 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000915
916 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000917 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000918 if (Context.Complain) {
919 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
920 << Context.C2.getTypeDeclType(Owner2);
921 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
922 << Field2->getDeclName() << Field2->getType();
923 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
924 << Field1->getDeclName() << Field1->getType();
925 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000926 return false;
927 }
928
929 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000930 if (Context.Complain) {
931 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
932 << Context.C2.getTypeDeclType(Owner2);
933 if (Field1->isBitField()) {
934 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
935 << Field1->getDeclName() << Field1->getType()
936 << Field1->getBitWidthValue(Context.C1);
937 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
938 << Field2->getDeclName();
939 } else {
940 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
941 << Field2->getDeclName() << Field2->getType()
942 << Field2->getBitWidthValue(Context.C2);
943 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
944 << Field1->getDeclName();
945 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000946 }
947 return false;
948 }
949
950 if (Field1->isBitField()) {
951 // Make sure that the bit-fields are the same length.
952 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
953 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
954
955 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000956 if (Context.Complain) {
957 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
958 << Context.C2.getTypeDeclType(Owner2);
959 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
960 << Field2->getDeclName() << Field2->getType() << Bits2;
961 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
962 << Field1->getDeclName() << Field1->getType() << Bits1;
963 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000964 return false;
965 }
966 }
967
968 return true;
969}
970
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000971/// \brief Find the index of the given anonymous struct/union within its
972/// context.
973///
974/// \returns Returns the index of this anonymous struct/union in its context,
975/// including the next assigned index (if none of them match). Returns an
976/// empty option if the context is not a record, i.e.. if the anonymous
977/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +0000978static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000979 ASTContext &Context = Anon->getASTContext();
980 QualType AnonTy = Context.getRecordType(Anon);
981
982 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
983 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +0000984 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000985
986 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +0000987 for (const auto *D : Owner->noload_decls()) {
988 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000989 if (!F || !F->isAnonymousStructOrUnion())
990 continue;
991
992 if (Context.hasSameType(F->getType(), AnonTy))
993 break;
994
995 ++Index;
996 }
997
998 return Index;
999}
1000
Douglas Gregor3996e242010-02-15 22:01:00 +00001001/// \brief Determine structural equivalence of two records.
1002static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1003 RecordDecl *D1, RecordDecl *D2) {
1004 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001005 if (Context.Complain) {
1006 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1007 << Context.C2.getTypeDeclType(D2);
1008 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1009 << D1->getDeclName() << (unsigned)D1->getTagKind();
1010 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001011 return false;
1012 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001013
1014 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1015 // If both anonymous structs/unions are in a record context, make sure
1016 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001017 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1018 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001019 if (*Index1 != *Index2)
1020 return false;
1021 }
1022 }
1023 }
1024
Douglas Gregore2e50d332010-12-01 01:36:18 +00001025 // If both declarations are class template specializations, we know
1026 // the ODR applies, so check the template and template arguments.
1027 ClassTemplateSpecializationDecl *Spec1
1028 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1029 ClassTemplateSpecializationDecl *Spec2
1030 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1031 if (Spec1 && Spec2) {
1032 // Check that the specialized templates are the same.
1033 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1034 Spec2->getSpecializedTemplate()))
1035 return false;
1036
1037 // Check that the template arguments are the same.
1038 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1039 return false;
1040
1041 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1042 if (!IsStructurallyEquivalent(Context,
1043 Spec1->getTemplateArgs().get(I),
1044 Spec2->getTemplateArgs().get(I)))
1045 return false;
1046 }
1047 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001048 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001049 else if (Spec1 || Spec2)
1050 return false;
1051
Douglas Gregorb4964f72010-02-15 23:54:17 +00001052 // Compare the definitions of these two records. If either or both are
1053 // incomplete, we assume that they are equivalent.
1054 D1 = D1->getDefinition();
1055 D2 = D2->getDefinition();
1056 if (!D1 || !D2)
1057 return true;
1058
Douglas Gregor3996e242010-02-15 22:01:00 +00001059 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1060 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1061 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001062 if (Context.Complain) {
1063 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1064 << Context.C2.getTypeDeclType(D2);
1065 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1066 << D2CXX->getNumBases();
1067 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1068 << D1CXX->getNumBases();
1069 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001070 return false;
1071 }
1072
1073 // Check the base classes.
1074 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1075 BaseEnd1 = D1CXX->bases_end(),
1076 Base2 = D2CXX->bases_begin();
1077 Base1 != BaseEnd1;
1078 ++Base1, ++Base2) {
1079 if (!IsStructurallyEquivalent(Context,
1080 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001081 if (Context.Complain) {
1082 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1083 << Context.C2.getTypeDeclType(D2);
1084 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1085 << Base2->getType()
1086 << Base2->getSourceRange();
1087 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1088 << Base1->getType()
1089 << Base1->getSourceRange();
1090 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001091 return false;
1092 }
1093
1094 // Check virtual vs. non-virtual inheritance mismatch.
1095 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001096 if (Context.Complain) {
1097 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1098 << Context.C2.getTypeDeclType(D2);
1099 Context.Diag2(Base2->getLocStart(),
1100 diag::note_odr_virtual_base)
1101 << Base2->isVirtual() << Base2->getSourceRange();
1102 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1103 << Base1->isVirtual()
1104 << Base1->getSourceRange();
1105 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001106 return false;
1107 }
1108 }
1109 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001110 if (Context.Complain) {
1111 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1112 << Context.C2.getTypeDeclType(D2);
1113 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1114 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1115 << Base1->getType()
1116 << Base1->getSourceRange();
1117 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1118 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001119 return false;
1120 }
1121 }
1122
1123 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001124 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001125 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001126 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001127 Field1End = D1->field_end();
1128 Field1 != Field1End;
1129 ++Field1, ++Field2) {
1130 if (Field2 == Field2End) {
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.Diag1(Field1->getLocation(), diag::note_odr_field)
1135 << Field1->getDeclName() << Field1->getType();
1136 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1137 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001138 return false;
1139 }
1140
David Blaikie40ed2972012-06-06 20:45:41 +00001141 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001142 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001143 }
1144
1145 if (Field2 != Field2End) {
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(Field2->getLocation(), diag::note_odr_field)
1150 << Field2->getDeclName() << Field2->getType();
1151 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1152 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001153 return false;
1154 }
1155
1156 return true;
1157}
1158
1159/// \brief Determine structural equivalence of two enums.
1160static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1161 EnumDecl *D1, EnumDecl *D2) {
1162 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1163 EC2End = D2->enumerator_end();
1164 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1165 EC1End = D1->enumerator_end();
1166 EC1 != EC1End; ++EC1, ++EC2) {
1167 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001168 if (Context.Complain) {
1169 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1170 << Context.C2.getTypeDeclType(D2);
1171 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1172 << EC1->getDeclName()
1173 << EC1->getInitVal().toString(10);
1174 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1175 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001176 return false;
1177 }
1178
1179 llvm::APSInt Val1 = EC1->getInitVal();
1180 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001181 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001182 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001183 if (Context.Complain) {
1184 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1185 << Context.C2.getTypeDeclType(D2);
1186 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1187 << EC2->getDeclName()
1188 << EC2->getInitVal().toString(10);
1189 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1190 << EC1->getDeclName()
1191 << EC1->getInitVal().toString(10);
1192 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001193 return false;
1194 }
1195 }
1196
1197 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001198 if (Context.Complain) {
1199 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1200 << Context.C2.getTypeDeclType(D2);
1201 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1202 << EC2->getDeclName()
1203 << EC2->getInitVal().toString(10);
1204 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1205 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001206 return false;
1207 }
1208
1209 return true;
1210}
Douglas Gregora082a492010-11-30 19:14:50 +00001211
1212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1213 TemplateParameterList *Params1,
1214 TemplateParameterList *Params2) {
1215 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001216 if (Context.Complain) {
1217 Context.Diag2(Params2->getTemplateLoc(),
1218 diag::err_odr_different_num_template_parameters)
1219 << Params1->size() << Params2->size();
1220 Context.Diag1(Params1->getTemplateLoc(),
1221 diag::note_odr_template_parameter_list);
1222 }
Douglas Gregora082a492010-11-30 19:14:50 +00001223 return false;
1224 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001225
Douglas Gregora082a492010-11-30 19:14:50 +00001226 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1227 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001228 if (Context.Complain) {
1229 Context.Diag2(Params2->getParam(I)->getLocation(),
1230 diag::err_odr_different_template_parameter_kind);
1231 Context.Diag1(Params1->getParam(I)->getLocation(),
1232 diag::note_odr_template_parameter_here);
1233 }
Douglas Gregora082a492010-11-30 19:14:50 +00001234 return false;
1235 }
1236
1237 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1238 Params2->getParam(I))) {
1239
1240 return false;
1241 }
1242 }
1243
1244 return true;
1245}
1246
1247static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1248 TemplateTypeParmDecl *D1,
1249 TemplateTypeParmDecl *D2) {
1250 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001251 if (Context.Complain) {
1252 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1253 << D2->isParameterPack();
1254 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1255 << D1->isParameterPack();
1256 }
Douglas Gregora082a492010-11-30 19:14:50 +00001257 return false;
1258 }
1259
1260 return true;
1261}
1262
1263static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1264 NonTypeTemplateParmDecl *D1,
1265 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001266 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001267 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001268 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1269 << D2->isParameterPack();
1270 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1271 << D1->isParameterPack();
1272 }
Douglas Gregora082a492010-11-30 19:14:50 +00001273 return false;
1274 }
Douglas Gregora082a492010-11-30 19:14:50 +00001275
1276 // Check types.
1277 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001278 if (Context.Complain) {
1279 Context.Diag2(D2->getLocation(),
1280 diag::err_odr_non_type_parameter_type_inconsistent)
1281 << D2->getType() << D1->getType();
1282 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1283 << D1->getType();
1284 }
Douglas Gregora082a492010-11-30 19:14:50 +00001285 return false;
1286 }
1287
1288 return true;
1289}
1290
1291static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1292 TemplateTemplateParmDecl *D1,
1293 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001294 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001295 if (Context.Complain) {
1296 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1297 << D2->isParameterPack();
1298 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1299 << D1->isParameterPack();
1300 }
Douglas Gregora082a492010-11-30 19:14:50 +00001301 return false;
1302 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001303
Douglas Gregora082a492010-11-30 19:14:50 +00001304 // Check template parameter lists.
1305 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1306 D2->getTemplateParameters());
1307}
1308
1309static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1310 ClassTemplateDecl *D1,
1311 ClassTemplateDecl *D2) {
1312 // Check template parameters.
1313 if (!IsStructurallyEquivalent(Context,
1314 D1->getTemplateParameters(),
1315 D2->getTemplateParameters()))
1316 return false;
1317
1318 // Check the templated declaration.
1319 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1320 D2->getTemplatedDecl());
1321}
1322
Douglas Gregor3996e242010-02-15 22:01:00 +00001323/// \brief Determine structural equivalence of two declarations.
1324static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1325 Decl *D1, Decl *D2) {
1326 // FIXME: Check for known structural equivalences via a callback of some sort.
1327
Douglas Gregorb4964f72010-02-15 23:54:17 +00001328 // Check whether we already know that these two declarations are not
1329 // structurally equivalent.
1330 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1331 D2->getCanonicalDecl())))
1332 return false;
1333
Douglas Gregor3996e242010-02-15 22:01:00 +00001334 // Determine whether we've already produced a tentative equivalence for D1.
1335 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1336 if (EquivToD1)
1337 return EquivToD1 == D2->getCanonicalDecl();
1338
1339 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1340 EquivToD1 = D2->getCanonicalDecl();
1341 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1342 return true;
1343}
1344
1345bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1346 Decl *D2) {
1347 if (!::IsStructurallyEquivalent(*this, D1, D2))
1348 return false;
1349
1350 return !Finish();
1351}
1352
1353bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1354 QualType T2) {
1355 if (!::IsStructurallyEquivalent(*this, T1, T2))
1356 return false;
1357
1358 return !Finish();
1359}
1360
1361bool StructuralEquivalenceContext::Finish() {
1362 while (!DeclsToCheck.empty()) {
1363 // Check the next declaration.
1364 Decl *D1 = DeclsToCheck.front();
1365 DeclsToCheck.pop_front();
1366
1367 Decl *D2 = TentativeEquivalences[D1];
1368 assert(D2 && "Unrecorded tentative equivalence?");
1369
Douglas Gregorb4964f72010-02-15 23:54:17 +00001370 bool Equivalent = true;
1371
Douglas Gregor3996e242010-02-15 22:01:00 +00001372 // FIXME: Switch on all declaration kinds. For now, we're just going to
1373 // check the obvious ones.
1374 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1375 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1376 // Check for equivalent structure names.
1377 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001378 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1379 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001380 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001381 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1382 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001383 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1384 !::IsStructurallyEquivalent(*this, Record1, Record2))
1385 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001386 } else {
1387 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001388 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001389 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001390 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001391 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1392 // Check for equivalent enum names.
1393 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001394 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1395 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001396 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001397 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1398 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001399 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1400 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1401 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001402 } else {
1403 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001404 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001405 }
Richard Smithdda56e42011-04-15 14:24:37 +00001406 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1407 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001408 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001409 Typedef2->getIdentifier()) ||
1410 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001411 Typedef1->getUnderlyingType(),
1412 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001413 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001414 } else {
1415 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001416 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001417 }
Douglas Gregora082a492010-11-30 19:14:50 +00001418 } else if (ClassTemplateDecl *ClassTemplate1
1419 = dyn_cast<ClassTemplateDecl>(D1)) {
1420 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1421 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1422 ClassTemplate2->getIdentifier()) ||
1423 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1424 Equivalent = false;
1425 } else {
1426 // Class template/non-class-template mismatch.
1427 Equivalent = false;
1428 }
1429 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1430 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1431 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1432 Equivalent = false;
1433 } else {
1434 // Kind mismatch.
1435 Equivalent = false;
1436 }
1437 } else if (NonTypeTemplateParmDecl *NTTP1
1438 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1439 if (NonTypeTemplateParmDecl *NTTP2
1440 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1441 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1442 Equivalent = false;
1443 } else {
1444 // Kind mismatch.
1445 Equivalent = false;
1446 }
1447 } else if (TemplateTemplateParmDecl *TTP1
1448 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1449 if (TemplateTemplateParmDecl *TTP2
1450 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1451 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1452 Equivalent = false;
1453 } else {
1454 // Kind mismatch.
1455 Equivalent = false;
1456 }
1457 }
1458
Douglas Gregorb4964f72010-02-15 23:54:17 +00001459 if (!Equivalent) {
1460 // Note that these two declarations are not equivalent (and we already
1461 // know about it).
1462 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1463 D2->getCanonicalDecl()));
1464 return true;
1465 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001466 // FIXME: Check other declaration kinds!
1467 }
1468
1469 return false;
1470}
1471
1472//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001473// Import Types
1474//----------------------------------------------------------------------------
1475
John McCall424cec92011-01-19 06:33:43 +00001476QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001477 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1478 << T->getTypeClassName();
1479 return QualType();
1480}
1481
John McCall424cec92011-01-19 06:33:43 +00001482QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001483 switch (T->getKind()) {
John McCalle314e272011-10-18 21:02:43 +00001484#define SHARED_SINGLETON_TYPE(Expansion)
1485#define BUILTIN_TYPE(Id, SingletonId) \
1486 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1487#include "clang/AST/BuiltinTypes.def"
1488
1489 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1490 // context supports C++.
1491
1492 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1493 // context supports ObjC.
1494
Douglas Gregor96e578d2010-02-05 17:54:41 +00001495 case BuiltinType::Char_U:
1496 // The context we're importing from has an unsigned 'char'. If we're
1497 // importing into a context with a signed 'char', translate to
1498 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001499 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001500 return Importer.getToContext().UnsignedCharTy;
1501
1502 return Importer.getToContext().CharTy;
1503
Douglas Gregor96e578d2010-02-05 17:54:41 +00001504 case BuiltinType::Char_S:
1505 // The context we're importing from has an unsigned 'char'. If we're
1506 // importing into a context with a signed 'char', translate to
1507 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001508 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001509 return Importer.getToContext().SignedCharTy;
1510
1511 return Importer.getToContext().CharTy;
1512
Chris Lattnerad3467e2010-12-25 23:25:43 +00001513 case BuiltinType::WChar_S:
1514 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001515 // FIXME: If not in C++, shall we translate to the C equivalent of
1516 // wchar_t?
1517 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001518 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001519
1520 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001521}
1522
John McCall424cec92011-01-19 06:33:43 +00001523QualType ASTNodeImporter::VisitComplexType(const ComplexType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001524 QualType ToElementType = Importer.Import(T->getElementType());
1525 if (ToElementType.isNull())
1526 return QualType();
1527
1528 return Importer.getToContext().getComplexType(ToElementType);
1529}
1530
John McCall424cec92011-01-19 06:33:43 +00001531QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001532 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1533 if (ToPointeeType.isNull())
1534 return QualType();
1535
1536 return Importer.getToContext().getPointerType(ToPointeeType);
1537}
1538
John McCall424cec92011-01-19 06:33:43 +00001539QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001540 // FIXME: Check for blocks support in "to" context.
1541 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1542 if (ToPointeeType.isNull())
1543 return QualType();
1544
1545 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1546}
1547
John McCall424cec92011-01-19 06:33:43 +00001548QualType
1549ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001550 // FIXME: Check for C++ support in "to" context.
1551 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1552 if (ToPointeeType.isNull())
1553 return QualType();
1554
1555 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1556}
1557
John McCall424cec92011-01-19 06:33:43 +00001558QualType
1559ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 // FIXME: Check for C++0x support in "to" context.
1561 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1562 if (ToPointeeType.isNull())
1563 return QualType();
1564
1565 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1566}
1567
John McCall424cec92011-01-19 06:33:43 +00001568QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001569 // FIXME: Check for C++ support in "to" context.
1570 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1571 if (ToPointeeType.isNull())
1572 return QualType();
1573
1574 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1575 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1576 ClassType.getTypePtr());
1577}
1578
John McCall424cec92011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001580 QualType ToElementType = Importer.Import(T->getElementType());
1581 if (ToElementType.isNull())
1582 return QualType();
1583
1584 return Importer.getToContext().getConstantArrayType(ToElementType,
1585 T->getSize(),
1586 T->getSizeModifier(),
1587 T->getIndexTypeCVRQualifiers());
1588}
1589
John McCall424cec92011-01-19 06:33:43 +00001590QualType
1591ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001592 QualType ToElementType = Importer.Import(T->getElementType());
1593 if (ToElementType.isNull())
1594 return QualType();
1595
1596 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1597 T->getSizeModifier(),
1598 T->getIndexTypeCVRQualifiers());
1599}
1600
John McCall424cec92011-01-19 06:33:43 +00001601QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001602 QualType ToElementType = Importer.Import(T->getElementType());
1603 if (ToElementType.isNull())
1604 return QualType();
1605
1606 Expr *Size = Importer.Import(T->getSizeExpr());
1607 if (!Size)
1608 return QualType();
1609
1610 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1611 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1612 T->getSizeModifier(),
1613 T->getIndexTypeCVRQualifiers(),
1614 Brackets);
1615}
1616
John McCall424cec92011-01-19 06:33:43 +00001617QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001618 QualType ToElementType = Importer.Import(T->getElementType());
1619 if (ToElementType.isNull())
1620 return QualType();
1621
1622 return Importer.getToContext().getVectorType(ToElementType,
1623 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001624 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001625}
1626
John McCall424cec92011-01-19 06:33:43 +00001627QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001628 QualType ToElementType = Importer.Import(T->getElementType());
1629 if (ToElementType.isNull())
1630 return QualType();
1631
1632 return Importer.getToContext().getExtVectorType(ToElementType,
1633 T->getNumElements());
1634}
1635
John McCall424cec92011-01-19 06:33:43 +00001636QualType
1637ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001638 // FIXME: What happens if we're importing a function without a prototype
1639 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001640 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001641 if (ToResultType.isNull())
1642 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001643
Douglas Gregor96e578d2010-02-05 17:54:41 +00001644 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001645 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001646}
1647
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001648QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001649 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001650 if (ToResultType.isNull())
1651 return QualType();
1652
1653 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001654 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001655 for (const auto &A : T->param_types()) {
1656 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001657 if (ArgType.isNull())
1658 return QualType();
1659 ArgTypes.push_back(ArgType);
1660 }
1661
1662 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001663 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001664 for (const auto &E : T->exceptions()) {
1665 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001666 if (ExceptionType.isNull())
1667 return QualType();
1668 ExceptionTypes.push_back(ExceptionType);
1669 }
John McCalldb40c7f2010-12-14 08:05:40 +00001670
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001671 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1672 FunctionProtoType::ExtProtoInfo ToEPI;
1673
1674 ToEPI.ExtInfo = FromEPI.ExtInfo;
1675 ToEPI.Variadic = FromEPI.Variadic;
1676 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1677 ToEPI.TypeQuals = FromEPI.TypeQuals;
1678 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001679 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1680 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1681 ToEPI.ExceptionSpec.NoexceptExpr =
1682 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1683 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1684 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1685 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1686 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001687
Jordan Rose5c382722013-03-08 21:51:21 +00001688 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001689}
1690
Sean Callananda6df8a2011-08-11 16:56:07 +00001691QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1692 QualType ToInnerType = Importer.Import(T->getInnerType());
1693 if (ToInnerType.isNull())
1694 return QualType();
1695
1696 return Importer.getToContext().getParenType(ToInnerType);
1697}
1698
John McCall424cec92011-01-19 06:33:43 +00001699QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001700 TypedefNameDecl *ToDecl
1701 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702 if (!ToDecl)
1703 return QualType();
1704
1705 return Importer.getToContext().getTypeDeclType(ToDecl);
1706}
1707
John McCall424cec92011-01-19 06:33:43 +00001708QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001709 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1710 if (!ToExpr)
1711 return QualType();
1712
1713 return Importer.getToContext().getTypeOfExprType(ToExpr);
1714}
1715
John McCall424cec92011-01-19 06:33:43 +00001716QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001717 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1718 if (ToUnderlyingType.isNull())
1719 return QualType();
1720
1721 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1722}
1723
John McCall424cec92011-01-19 06:33:43 +00001724QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001725 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001726 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1727 if (!ToExpr)
1728 return QualType();
1729
Douglas Gregor81495f32012-02-12 18:42:33 +00001730 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1731 if (UnderlyingType.isNull())
1732 return QualType();
1733
1734 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001735}
1736
Alexis Hunte852b102011-05-24 22:41:36 +00001737QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1738 QualType ToBaseType = Importer.Import(T->getBaseType());
1739 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1740 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1741 return QualType();
1742
1743 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1744 ToUnderlyingType,
1745 T->getUTTKind());
1746}
1747
Richard Smith30482bc2011-02-20 03:19:35 +00001748QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001749 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001750 QualType FromDeduced = T->getDeducedType();
1751 QualType ToDeduced;
1752 if (!FromDeduced.isNull()) {
1753 ToDeduced = Importer.Import(FromDeduced);
1754 if (ToDeduced.isNull())
1755 return QualType();
1756 }
1757
Richard Smithe301ba22015-11-11 02:02:15 +00001758 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001759 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001760}
1761
John McCall424cec92011-01-19 06:33:43 +00001762QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001763 RecordDecl *ToDecl
1764 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1765 if (!ToDecl)
1766 return QualType();
1767
1768 return Importer.getToContext().getTagDeclType(ToDecl);
1769}
1770
John McCall424cec92011-01-19 06:33:43 +00001771QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001772 EnumDecl *ToDecl
1773 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1774 if (!ToDecl)
1775 return QualType();
1776
1777 return Importer.getToContext().getTagDeclType(ToDecl);
1778}
1779
Sean Callanan72fe0852015-04-02 23:50:08 +00001780QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1781 QualType FromModifiedType = T->getModifiedType();
1782 QualType FromEquivalentType = T->getEquivalentType();
1783 QualType ToModifiedType;
1784 QualType ToEquivalentType;
1785
1786 if (!FromModifiedType.isNull()) {
1787 ToModifiedType = Importer.Import(FromModifiedType);
1788 if (ToModifiedType.isNull())
1789 return QualType();
1790 }
1791 if (!FromEquivalentType.isNull()) {
1792 ToEquivalentType = Importer.Import(FromEquivalentType);
1793 if (ToEquivalentType.isNull())
1794 return QualType();
1795 }
1796
1797 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1798 ToModifiedType, ToEquivalentType);
1799}
1800
Douglas Gregore2e50d332010-12-01 01:36:18 +00001801QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001802 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001803 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1804 if (ToTemplate.isNull())
1805 return QualType();
1806
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001807 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001808 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1809 return QualType();
1810
1811 QualType ToCanonType;
1812 if (!QualType(T, 0).isCanonical()) {
1813 QualType FromCanonType
1814 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1815 ToCanonType =Importer.Import(FromCanonType);
1816 if (ToCanonType.isNull())
1817 return QualType();
1818 }
1819 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1820 ToTemplateArgs.data(),
1821 ToTemplateArgs.size(),
1822 ToCanonType);
1823}
1824
John McCall424cec92011-01-19 06:33:43 +00001825QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001826 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001827 // Note: the qualifier in an ElaboratedType is optional.
1828 if (T->getQualifier()) {
1829 ToQualifier = Importer.Import(T->getQualifier());
1830 if (!ToQualifier)
1831 return QualType();
1832 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001833
1834 QualType ToNamedType = Importer.Import(T->getNamedType());
1835 if (ToNamedType.isNull())
1836 return QualType();
1837
Abramo Bagnara6150c882010-05-11 21:36:43 +00001838 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1839 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001840}
1841
John McCall424cec92011-01-19 06:33:43 +00001842QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001843 ObjCInterfaceDecl *Class
1844 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1845 if (!Class)
1846 return QualType();
1847
John McCall8b07ec22010-05-15 11:32:37 +00001848 return Importer.getToContext().getObjCInterfaceType(Class);
1849}
1850
John McCall424cec92011-01-19 06:33:43 +00001851QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001852 QualType ToBaseType = Importer.Import(T->getBaseType());
1853 if (ToBaseType.isNull())
1854 return QualType();
1855
Douglas Gregore9d95f12015-07-07 03:57:35 +00001856 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001857 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001858 QualType ImportedTypeArg = Importer.Import(TypeArg);
1859 if (ImportedTypeArg.isNull())
1860 return QualType();
1861
1862 TypeArgs.push_back(ImportedTypeArg);
1863 }
1864
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001865 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001866 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001867 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001868 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001869 if (!Protocol)
1870 return QualType();
1871 Protocols.push_back(Protocol);
1872 }
1873
Douglas Gregore9d95f12015-07-07 03:57:35 +00001874 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001875 Protocols,
1876 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001877}
1878
John McCall424cec92011-01-19 06:33:43 +00001879QualType
1880ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001881 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1882 if (ToPointeeType.isNull())
1883 return QualType();
1884
John McCall8b07ec22010-05-15 11:32:37 +00001885 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001886}
1887
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001888//----------------------------------------------------------------------------
1889// Import Declarations
1890//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001891bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1892 DeclContext *&LexicalDC,
1893 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001894 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001895 SourceLocation &Loc) {
1896 // Import the context of this declaration.
1897 DC = Importer.ImportContext(D->getDeclContext());
1898 if (!DC)
1899 return true;
1900
1901 LexicalDC = DC;
1902 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1903 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1904 if (!LexicalDC)
1905 return true;
1906 }
1907
1908 // Import the name of this declaration.
1909 Name = Importer.Import(D->getDeclName());
1910 if (D->getDeclName() && !Name)
1911 return true;
1912
1913 // Import the location of this declaration.
1914 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00001915 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001916 return false;
1917}
1918
Douglas Gregord451ea92011-07-29 23:31:30 +00001919void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
1920 if (!FromD)
1921 return;
1922
1923 if (!ToD) {
1924 ToD = Importer.Import(FromD);
1925 if (!ToD)
1926 return;
1927 }
1928
1929 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
1930 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00001931 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00001932 ImportDefinition(FromRecord, ToRecord);
1933 }
1934 }
1935 return;
1936 }
1937
1938 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
1939 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
1940 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
1941 ImportDefinition(FromEnum, ToEnum);
1942 }
1943 }
1944 return;
1945 }
1946}
1947
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001948void
1949ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
1950 DeclarationNameInfo& To) {
1951 // NOTE: To.Name and To.Loc are already imported.
1952 // We only have to import To.LocInfo.
1953 switch (To.getName().getNameKind()) {
1954 case DeclarationName::Identifier:
1955 case DeclarationName::ObjCZeroArgSelector:
1956 case DeclarationName::ObjCOneArgSelector:
1957 case DeclarationName::ObjCMultiArgSelector:
1958 case DeclarationName::CXXUsingDirective:
1959 return;
1960
1961 case DeclarationName::CXXOperatorName: {
1962 SourceRange Range = From.getCXXOperatorNameRange();
1963 To.setCXXOperatorNameRange(Importer.Import(Range));
1964 return;
1965 }
1966 case DeclarationName::CXXLiteralOperatorName: {
1967 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
1968 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
1969 return;
1970 }
1971 case DeclarationName::CXXConstructorName:
1972 case DeclarationName::CXXDestructorName:
1973 case DeclarationName::CXXConversionFunctionName: {
1974 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
1975 To.setNamedTypeInfo(Importer.Import(FromTInfo));
1976 return;
1977 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001978 }
Douglas Gregor07216d12011-11-02 20:52:01 +00001979 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001980}
1981
Douglas Gregor2e15c842012-02-01 21:00:38 +00001982void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00001983 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00001984 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00001985 return;
1986 }
1987
Aaron Ballman629afae2014-03-07 19:56:05 +00001988 for (auto *From : FromDC->decls())
1989 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00001990}
1991
Douglas Gregord451ea92011-07-29 23:31:30 +00001992bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00001993 ImportDefinitionKind Kind) {
1994 if (To->getDefinition() || To->isBeingDefined()) {
1995 if (Kind == IDK_Everything)
1996 ImportDeclContext(From, /*ForceImport=*/true);
1997
Douglas Gregore2e50d332010-12-01 01:36:18 +00001998 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00001999 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002000
2001 To->startDefinition();
2002
2003 // Add base classes.
2004 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2005 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002006
2007 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2008 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2009 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002010 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002011 ToData.Aggregate = FromData.Aggregate;
2012 ToData.PlainOldData = FromData.PlainOldData;
2013 ToData.Empty = FromData.Empty;
2014 ToData.Polymorphic = FromData.Polymorphic;
2015 ToData.Abstract = FromData.Abstract;
2016 ToData.IsStandardLayout = FromData.IsStandardLayout;
2017 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2018 ToData.HasPrivateFields = FromData.HasPrivateFields;
2019 ToData.HasProtectedFields = FromData.HasProtectedFields;
2020 ToData.HasPublicFields = FromData.HasPublicFields;
2021 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002022 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002023 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002024 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002025 ToData.HasUninitializedReferenceMember
2026 = FromData.HasUninitializedReferenceMember;
Richard Smith6b02d462012-12-08 08:32:28 +00002027 ToData.NeedOverloadResolutionForMoveConstructor
2028 = FromData.NeedOverloadResolutionForMoveConstructor;
2029 ToData.NeedOverloadResolutionForMoveAssignment
2030 = FromData.NeedOverloadResolutionForMoveAssignment;
2031 ToData.NeedOverloadResolutionForDestructor
2032 = FromData.NeedOverloadResolutionForDestructor;
2033 ToData.DefaultedMoveConstructorIsDeleted
2034 = FromData.DefaultedMoveConstructorIsDeleted;
2035 ToData.DefaultedMoveAssignmentIsDeleted
2036 = FromData.DefaultedMoveAssignmentIsDeleted;
2037 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002038 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2039 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002040 ToData.HasConstexprNonCopyMoveConstructor
2041 = FromData.HasConstexprNonCopyMoveConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002042 ToData.DefaultedDefaultConstructorIsConstexpr
2043 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002044 ToData.HasConstexprDefaultConstructor
2045 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002046 ToData.HasNonLiteralTypeFieldsOrBases
2047 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002048 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002049 ToData.UserProvidedDefaultConstructor
2050 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002051 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002052 ToData.ImplicitCopyConstructorHasConstParam
2053 = FromData.ImplicitCopyConstructorHasConstParam;
2054 ToData.ImplicitCopyAssignmentHasConstParam
2055 = FromData.ImplicitCopyAssignmentHasConstParam;
2056 ToData.HasDeclaredCopyConstructorWithConstParam
2057 = FromData.HasDeclaredCopyConstructorWithConstParam;
2058 ToData.HasDeclaredCopyAssignmentWithConstParam
2059 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002060 ToData.IsLambda = FromData.IsLambda;
2061
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002062 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002063 for (const auto &Base1 : FromCXX->bases()) {
2064 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002065 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002066 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002067
2068 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002069 if (Base1.isPackExpansion())
2070 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002071
2072 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002073 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002074
Douglas Gregore2e50d332010-12-01 01:36:18 +00002075 Bases.push_back(
2076 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002077 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2078 Base1.isVirtual(),
2079 Base1.isBaseOfClass(),
2080 Base1.getAccessSpecifierAsWritten(),
2081 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002082 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002083 }
2084 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002085 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002086 }
2087
Douglas Gregor2e15c842012-02-01 21:00:38 +00002088 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002089 ImportDeclContext(From, /*ForceImport=*/true);
2090
Douglas Gregore2e50d332010-12-01 01:36:18 +00002091 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002092 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002093}
2094
Larisse Voufo39a1e502013-08-06 01:03:05 +00002095bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2096 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002097 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002098 return false;
2099
2100 // FIXME: Can we really import any initializer? Alternatively, we could force
2101 // ourselves to import every declaration of a variable and then only use
2102 // getInit() here.
2103 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2104
2105 // FIXME: Other bits to merge?
2106
2107 return false;
2108}
2109
Douglas Gregord451ea92011-07-29 23:31:30 +00002110bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002111 ImportDefinitionKind Kind) {
2112 if (To->getDefinition() || To->isBeingDefined()) {
2113 if (Kind == IDK_Everything)
2114 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002115 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002116 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002117
2118 To->startDefinition();
2119
2120 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2121 if (T.isNull())
2122 return true;
2123
2124 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2125 if (ToPromotionType.isNull())
2126 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002127
2128 if (shouldForceImportDeclContext(Kind))
2129 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002130
2131 // FIXME: we might need to merge the number of positive or negative bits
2132 // if the enumerator lists don't match.
2133 To->completeDefinition(T, ToPromotionType,
2134 From->getNumPositiveBits(),
2135 From->getNumNegativeBits());
2136 return false;
2137}
2138
Douglas Gregora082a492010-11-30 19:14:50 +00002139TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2140 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002141 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002142 ToParams.reserve(Params->size());
2143 for (TemplateParameterList::iterator P = Params->begin(),
2144 PEnd = Params->end();
2145 P != PEnd; ++P) {
2146 Decl *To = Importer.Import(*P);
2147 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002148 return nullptr;
2149
Douglas Gregora082a492010-11-30 19:14:50 +00002150 ToParams.push_back(cast<NamedDecl>(To));
2151 }
2152
2153 return TemplateParameterList::Create(Importer.getToContext(),
2154 Importer.Import(Params->getTemplateLoc()),
2155 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002156 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002157 Importer.Import(Params->getRAngleLoc()));
2158}
2159
Douglas Gregore2e50d332010-12-01 01:36:18 +00002160TemplateArgument
2161ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2162 switch (From.getKind()) {
2163 case TemplateArgument::Null:
2164 return TemplateArgument();
2165
2166 case TemplateArgument::Type: {
2167 QualType ToType = Importer.Import(From.getAsType());
2168 if (ToType.isNull())
2169 return TemplateArgument();
2170 return TemplateArgument(ToType);
2171 }
2172
2173 case TemplateArgument::Integral: {
2174 QualType ToType = Importer.Import(From.getIntegralType());
2175 if (ToType.isNull())
2176 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002177 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002178 }
2179
Eli Friedmanb826a002012-09-26 02:36:12 +00002180 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002181 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2182 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2183 if (!To || ToType.isNull())
2184 return TemplateArgument();
2185 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002186 }
2187
2188 case TemplateArgument::NullPtr: {
2189 QualType ToType = Importer.Import(From.getNullPtrType());
2190 if (ToType.isNull())
2191 return TemplateArgument();
2192 return TemplateArgument(ToType, /*isNullPtr*/true);
2193 }
2194
Douglas Gregore2e50d332010-12-01 01:36:18 +00002195 case TemplateArgument::Template: {
2196 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2197 if (ToTemplate.isNull())
2198 return TemplateArgument();
2199
2200 return TemplateArgument(ToTemplate);
2201 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002202
2203 case TemplateArgument::TemplateExpansion: {
2204 TemplateName ToTemplate
2205 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2206 if (ToTemplate.isNull())
2207 return TemplateArgument();
2208
Douglas Gregore1d60df2011-01-14 23:41:42 +00002209 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002210 }
2211
Douglas Gregore2e50d332010-12-01 01:36:18 +00002212 case TemplateArgument::Expression:
2213 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2214 return TemplateArgument(ToExpr);
2215 return TemplateArgument();
2216
2217 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002218 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002219 ToPack.reserve(From.pack_size());
2220 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2221 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002222
2223 return TemplateArgument(
2224 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002225 }
2226 }
2227
2228 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002229}
2230
2231bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2232 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002233 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002234 for (unsigned I = 0; I != NumFromArgs; ++I) {
2235 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2236 if (To.isNull() && !FromArgs[I].isNull())
2237 return true;
2238
2239 ToArgs.push_back(To);
2240 }
2241
2242 return false;
2243}
2244
Douglas Gregor5c73e912010-02-11 00:48:18 +00002245bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002246 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002247 // Eliminate a potential failure point where we attempt to re-import
2248 // something we're trying to import while completing ToRecord.
2249 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2250 if (ToOrigin) {
2251 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2252 if (ToOriginRecord)
2253 ToRecord = ToOriginRecord;
2254 }
2255
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002256 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002257 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002258 Importer.getNonEquivalentDecls(),
2259 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002260 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002261}
2262
Larisse Voufo39a1e502013-08-06 01:03:05 +00002263bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2264 bool Complain) {
2265 StructuralEquivalenceContext Ctx(
2266 Importer.getFromContext(), Importer.getToContext(),
2267 Importer.getNonEquivalentDecls(), false, Complain);
2268 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2269}
2270
Douglas Gregor98c10182010-02-12 22:17:39 +00002271bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002272 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002273 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002274 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002275 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002276}
2277
Douglas Gregor91155082012-11-14 22:29:20 +00002278bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2279 EnumConstantDecl *ToEC)
2280{
2281 const llvm::APSInt &FromVal = FromEC->getInitVal();
2282 const llvm::APSInt &ToVal = ToEC->getInitVal();
2283
2284 return FromVal.isSigned() == ToVal.isSigned() &&
2285 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2286 FromVal == ToVal;
2287}
2288
2289bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002290 ClassTemplateDecl *To) {
2291 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2292 Importer.getToContext(),
2293 Importer.getNonEquivalentDecls());
2294 return Ctx.IsStructurallyEquivalent(From, To);
2295}
2296
Larisse Voufo39a1e502013-08-06 01:03:05 +00002297bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2298 VarTemplateDecl *To) {
2299 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2300 Importer.getToContext(),
2301 Importer.getNonEquivalentDecls());
2302 return Ctx.IsStructurallyEquivalent(From, To);
2303}
2304
Douglas Gregore4c83e42010-02-09 22:48:33 +00002305Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002306 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002307 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002308 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002309}
2310
Sean Callanan65198272011-11-17 23:20:56 +00002311Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2312 TranslationUnitDecl *ToD =
2313 Importer.getToContext().getTranslationUnitDecl();
2314
2315 Importer.Imported(D, ToD);
2316
2317 return ToD;
2318}
2319
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002320Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2321
2322 SourceLocation Loc = Importer.Import(D->getLocation());
2323 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2324
2325 // Import the context of this declaration.
2326 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2327 if (!DC)
2328 return nullptr;
2329
2330 AccessSpecDecl *accessSpecDecl
2331 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2332 DC, Loc, ColonLoc);
2333
2334 if (!accessSpecDecl)
2335 return nullptr;
2336
2337 // Lexical DeclContext and Semantic DeclContext
2338 // is always the same for the accessSpec.
2339 accessSpecDecl->setLexicalDeclContext(DC);
2340 DC->addDeclInternal(accessSpecDecl);
2341
2342 return accessSpecDecl;
2343}
2344
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002345Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2346 // Import the major distinguishing characteristics of this namespace.
2347 DeclContext *DC, *LexicalDC;
2348 DeclarationName Name;
2349 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002350 NamedDecl *ToD;
2351 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002352 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002353 if (ToD)
2354 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002355
2356 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002357 if (!Name) {
2358 // This is an anonymous namespace. Adopt an existing anonymous
2359 // namespace if we can.
2360 // FIXME: Not testable.
2361 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2362 MergeWithNamespace = TU->getAnonymousNamespace();
2363 else
2364 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2365 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002366 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002367 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002368 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002369 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2370 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002371 continue;
2372
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002373 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002374 MergeWithNamespace = FoundNS;
2375 ConflictingDecls.clear();
2376 break;
2377 }
2378
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002379 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002380 }
2381
2382 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002383 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002384 ConflictingDecls.data(),
2385 ConflictingDecls.size());
2386 }
2387 }
2388
2389 // Create the "to" namespace, if needed.
2390 NamespaceDecl *ToNamespace = MergeWithNamespace;
2391 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002392 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002393 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002394 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002395 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002396 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002397 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002398 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002399
2400 // If this is an anonymous namespace, register it as the anonymous
2401 // namespace within its context.
2402 if (!Name) {
2403 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2404 TU->setAnonymousNamespace(ToNamespace);
2405 else
2406 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2407 }
2408 }
2409 Importer.Imported(D, ToNamespace);
2410
2411 ImportDeclContext(D);
2412
2413 return ToNamespace;
2414}
2415
Richard Smithdda56e42011-04-15 14:24:37 +00002416Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002417 // Import the major distinguishing characteristics of this typedef.
2418 DeclContext *DC, *LexicalDC;
2419 DeclarationName Name;
2420 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002421 NamedDecl *ToD;
2422 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002423 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002424 if (ToD)
2425 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002426
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002427 // If this typedef is not in block scope, determine whether we've
2428 // seen a typedef with the same name (that we can merge with) or any
2429 // other entity by that name (which name lookup could conflict with).
2430 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002431 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002432 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002433 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002434 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002435 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2436 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002437 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002438 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002439 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002440 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2441 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002442 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002443 }
2444
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002445 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002446 }
2447
2448 if (!ConflictingDecls.empty()) {
2449 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2450 ConflictingDecls.data(),
2451 ConflictingDecls.size());
2452 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002453 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002454 }
2455 }
2456
Douglas Gregorb4964f72010-02-15 23:54:17 +00002457 // Import the underlying type of this typedef;
2458 QualType T = Importer.Import(D->getUnderlyingType());
2459 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002460 return nullptr;
2461
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002462 // Create the new typedef node.
2463 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002464 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002465 TypedefNameDecl *ToTypedef;
2466 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002467 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2468 StartL, Loc,
2469 Name.getAsIdentifierInfo(),
2470 TInfo);
2471 else
Richard Smithdda56e42011-04-15 14:24:37 +00002472 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2473 StartL, Loc,
2474 Name.getAsIdentifierInfo(),
2475 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002476
Douglas Gregordd483172010-02-22 17:42:47 +00002477 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002478 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002479 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002480 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002481
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002482 return ToTypedef;
2483}
2484
Richard Smithdda56e42011-04-15 14:24:37 +00002485Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2486 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2487}
2488
2489Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2490 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2491}
2492
Douglas Gregor98c10182010-02-12 22:17:39 +00002493Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2494 // Import the major distinguishing characteristics of this enum.
2495 DeclContext *DC, *LexicalDC;
2496 DeclarationName Name;
2497 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002498 NamedDecl *ToD;
2499 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002500 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002501 if (ToD)
2502 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002503
Douglas Gregor98c10182010-02-12 22:17:39 +00002504 // Figure out what enum name we're looking for.
2505 unsigned IDNS = Decl::IDNS_Tag;
2506 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002507 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2508 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002509 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002510 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002511 IDNS |= Decl::IDNS_Ordinary;
2512
2513 // We may already have an enum of the same name; try to find and match it.
2514 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002515 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002516 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002517 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002518 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2519 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002520 continue;
2521
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002522 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002523 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002524 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2525 Found = Tag->getDecl();
2526 }
2527
2528 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002529 if (IsStructuralMatch(D, FoundEnum))
2530 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002531 }
2532
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002533 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002534 }
2535
2536 if (!ConflictingDecls.empty()) {
2537 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2538 ConflictingDecls.data(),
2539 ConflictingDecls.size());
2540 }
2541 }
2542
2543 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002544 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2545 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002546 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002547 D->isScoped(), D->isScopedUsingClassTag(),
2548 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002549 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002550 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002551 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002552 D2->setLexicalDeclContext(LexicalDC);
2553 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002554 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002555
2556 // Import the integer type.
2557 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2558 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002559 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002560 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002561
2562 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002563 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002564 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002565
Douglas Gregor3996e242010-02-15 22:01:00 +00002566 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002567}
2568
Douglas Gregor5c73e912010-02-11 00:48:18 +00002569Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2570 // If this record has a definition in the translation unit we're coming from,
2571 // but this particular declaration is not that definition, import the
2572 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002573 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002574 if (Definition && Definition != D) {
2575 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002576 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002577 return nullptr;
2578
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002579 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002580 }
2581
2582 // Import the major distinguishing characteristics of this record.
2583 DeclContext *DC, *LexicalDC;
2584 DeclarationName Name;
2585 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002586 NamedDecl *ToD;
2587 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002588 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002589 if (ToD)
2590 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002591
Douglas Gregor5c73e912010-02-11 00:48:18 +00002592 // Figure out what structure name we're looking for.
2593 unsigned IDNS = Decl::IDNS_Tag;
2594 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002595 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2596 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002597 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002598 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002599 IDNS |= Decl::IDNS_Ordinary;
2600
2601 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002602 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002603 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002604 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002605 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002606 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002607 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2608 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002609 continue;
2610
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002611 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002612 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002613 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2614 Found = Tag->getDecl();
2615 }
2616
2617 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002618 if (D->isAnonymousStructOrUnion() &&
2619 FoundRecord->isAnonymousStructOrUnion()) {
2620 // If both anonymous structs/unions are in a record context, make sure
2621 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002622 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002623 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002624 if (Optional<unsigned> Index2 =
2625 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002626 if (*Index1 != *Index2)
2627 continue;
2628 }
2629 }
2630 }
2631
Douglas Gregor25791052010-02-12 00:09:27 +00002632 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002633 if ((SearchName && !D->isCompleteDefinition())
2634 || (D->isCompleteDefinition() &&
2635 D->isAnonymousStructOrUnion()
2636 == FoundDef->isAnonymousStructOrUnion() &&
2637 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002638 // The record types structurally match, or the "from" translation
2639 // unit only had a forward declaration anyway; call it the same
2640 // function.
2641 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002642 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002643 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002644 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002645 // We have a forward declaration of this type, so adopt that forward
2646 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002647
2648 // If one or both can be completed from external storage then try one
2649 // last time to complete and compare them before doing this.
2650
2651 if (FoundRecord->hasExternalLexicalStorage() &&
2652 !FoundRecord->isCompleteDefinition())
2653 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2654 if (D->hasExternalLexicalStorage())
2655 D->getASTContext().getExternalSource()->CompleteType(D);
2656
2657 if (FoundRecord->isCompleteDefinition() &&
2658 D->isCompleteDefinition() &&
2659 !IsStructuralMatch(D, FoundRecord))
2660 continue;
2661
Douglas Gregor25791052010-02-12 00:09:27 +00002662 AdoptDecl = FoundRecord;
2663 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002664 } else if (!SearchName) {
2665 continue;
2666 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002667 }
2668
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002669 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002670 }
2671
Douglas Gregordd6006f2012-07-17 21:16:27 +00002672 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002673 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2674 ConflictingDecls.data(),
2675 ConflictingDecls.size());
2676 }
2677 }
2678
2679 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002680 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002681 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002682 if (!D2) {
John McCall1c70e992010-06-03 19:28:45 +00002683 if (isa<CXXRecordDecl>(D)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00002684 CXXRecordDecl *D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
Douglas Gregor25791052010-02-12 00:09:27 +00002685 D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002686 DC, StartLoc, Loc,
2687 Name.getAsIdentifierInfo());
Douglas Gregor3996e242010-02-15 22:01:00 +00002688 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002689 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002690 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002691 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002692 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002693 }
Douglas Gregor14454802011-02-25 02:25:35 +00002694
2695 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002696 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002697 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002698 if (D->isAnonymousStructOrUnion())
2699 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002700 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002701
Douglas Gregor3996e242010-02-15 22:01:00 +00002702 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002703
Douglas Gregor95d82832012-01-24 18:36:04 +00002704 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002705 return nullptr;
2706
Douglas Gregor3996e242010-02-15 22:01:00 +00002707 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002708}
2709
Douglas Gregor98c10182010-02-12 22:17:39 +00002710Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2711 // Import the major distinguishing characteristics of this enumerator.
2712 DeclContext *DC, *LexicalDC;
2713 DeclarationName Name;
2714 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002715 NamedDecl *ToD;
2716 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002717 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002718 if (ToD)
2719 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002720
2721 QualType T = Importer.Import(D->getType());
2722 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002723 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002724
Douglas Gregor98c10182010-02-12 22:17:39 +00002725 // Determine whether there are any other declarations with the same name and
2726 // in the same context.
2727 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002728 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002729 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002730 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002731 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002732 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2733 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002734 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002735
2736 if (EnumConstantDecl *FoundEnumConstant
2737 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2738 if (IsStructuralMatch(D, FoundEnumConstant))
2739 return Importer.Imported(D, FoundEnumConstant);
2740 }
2741
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002742 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002743 }
2744
2745 if (!ConflictingDecls.empty()) {
2746 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2747 ConflictingDecls.data(),
2748 ConflictingDecls.size());
2749 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002750 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002751 }
2752 }
2753
2754 Expr *Init = Importer.Import(D->getInitExpr());
2755 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002756 return nullptr;
2757
Douglas Gregor98c10182010-02-12 22:17:39 +00002758 EnumConstantDecl *ToEnumerator
2759 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2760 Name.getAsIdentifierInfo(), T,
2761 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002762 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002763 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002764 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002765 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002766 return ToEnumerator;
2767}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002768
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002769Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2770 // Import the major distinguishing characteristics of this function.
2771 DeclContext *DC, *LexicalDC;
2772 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002773 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002774 NamedDecl *ToD;
2775 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002776 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002777 if (ToD)
2778 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002779
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002780 // Try to find a function in our own ("to") context with the same name, same
2781 // type, and in the same context as the function we're importing.
2782 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002783 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002784 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002785 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002786 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002787 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2788 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002789 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002790
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002791 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002792 if (FoundFunction->hasExternalFormalLinkage() &&
2793 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002794 if (Importer.IsStructurallyEquivalent(D->getType(),
2795 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002796 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002797 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002798 }
2799
2800 // FIXME: Check for overloading more carefully, e.g., by boosting
2801 // Sema::IsOverload out to the AST library.
2802
2803 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002804 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002805 continue;
2806
2807 // Complain about inconsistent function types.
2808 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002809 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002810 Importer.ToDiag(FoundFunction->getLocation(),
2811 diag::note_odr_value_here)
2812 << FoundFunction->getType();
2813 }
2814 }
2815
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002816 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002817 }
2818
2819 if (!ConflictingDecls.empty()) {
2820 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2821 ConflictingDecls.data(),
2822 ConflictingDecls.size());
2823 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002824 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002825 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002826 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002827
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002828 DeclarationNameInfo NameInfo(Name, Loc);
2829 // Import additional name location/type info.
2830 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2831
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002832 QualType FromTy = D->getType();
2833 bool usedDifferentExceptionSpec = false;
2834
2835 if (const FunctionProtoType *
2836 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2837 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2838 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2839 // FunctionDecl that we are importing the FunctionProtoType for.
2840 // To avoid an infinite recursion when importing, create the FunctionDecl
2841 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002842 if (FromEPI.ExceptionSpec.SourceDecl ||
2843 FromEPI.ExceptionSpec.SourceTemplate ||
2844 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002845 FunctionProtoType::ExtProtoInfo DefaultEPI;
2846 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002847 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002848 usedDifferentExceptionSpec = true;
2849 }
2850 }
2851
Douglas Gregorb4964f72010-02-15 23:54:17 +00002852 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002853 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002854 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002855 return nullptr;
2856
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002857 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002858 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00002859 for (auto P : D->params()) {
2860 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002861 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00002862 return nullptr;
2863
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002864 Parameters.push_back(ToP);
2865 }
2866
2867 // Create the imported function.
2868 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00002869 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002870 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00002871 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
2872 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
2873 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002874 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002875 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002876 FromConstructor->isExplicit(),
2877 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002878 D->isImplicit(),
2879 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002880 } else if (isa<CXXDestructorDecl>(D)) {
2881 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
2882 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002883 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00002884 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002885 D->isInlineSpecified(),
2886 D->isImplicit());
2887 } else if (CXXConversionDecl *FromConversion
2888 = dyn_cast<CXXConversionDecl>(D)) {
2889 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
2890 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002891 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002892 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00002893 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002894 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002895 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002896 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00002897 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
2898 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
2899 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00002900 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00002901 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00002902 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002903 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002904 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00002905 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00002906 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002907 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00002908 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002909 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00002910 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00002911 D->hasWrittenPrototype(),
2912 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00002913 }
John McCall3e11ebe2010-03-15 10:12:16 +00002914
2915 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002916 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002917 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00002918 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00002919 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
2920 ToFunction->setTrivial(D->isTrivial());
2921 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00002922 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00002923
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002924 // Set the parameters.
2925 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00002926 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00002927 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002928 }
David Blaikie9c70e042011-09-21 18:16:56 +00002929 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002930
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002931 if (usedDifferentExceptionSpec) {
2932 // Update FunctionProtoType::ExtProtoInfo.
2933 QualType T = Importer.Import(D->getType());
2934 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002935 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002936 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00002937 }
2938
Sean Callanan59721b32015-04-28 18:41:46 +00002939 // Import the body, if any.
2940 if (Stmt *FromBody = D->getBody()) {
2941 if (Stmt *ToBody = Importer.Import(FromBody)) {
2942 ToFunction->setBody(ToBody);
2943 }
2944 }
2945
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002946 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002947
2948 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00002949 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00002950
Douglas Gregor43f54792010-02-17 02:12:47 +00002951 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002952}
2953
Douglas Gregor00eace12010-02-21 18:29:16 +00002954Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
2955 return VisitFunctionDecl(D);
2956}
2957
2958Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
2959 return VisitCXXMethodDecl(D);
2960}
2961
2962Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
2963 return VisitCXXMethodDecl(D);
2964}
2965
2966Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
2967 return VisitCXXMethodDecl(D);
2968}
2969
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002970static unsigned getFieldIndex(Decl *F) {
2971 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
2972 if (!Owner)
2973 return 0;
2974
2975 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00002976 for (const auto *D : Owner->noload_decls()) {
2977 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002978 return Index;
2979
2980 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
2981 ++Index;
2982 }
2983
2984 return Index;
2985}
2986
Douglas Gregor5c73e912010-02-11 00:48:18 +00002987Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
2988 // Import the major distinguishing characteristics of a variable.
2989 DeclContext *DC, *LexicalDC;
2990 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002991 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002992 NamedDecl *ToD;
2993 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002994 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002995 if (ToD)
2996 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002997
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002998 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002999 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003000 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003001 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3002 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003003 // For anonymous fields, match up by index.
3004 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3005 continue;
3006
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003007 if (Importer.IsStructurallyEquivalent(D->getType(),
3008 FoundField->getType())) {
3009 Importer.Imported(D, FoundField);
3010 return FoundField;
3011 }
3012
3013 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3014 << Name << D->getType() << FoundField->getType();
3015 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3016 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003017 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003018 }
3019 }
3020
Douglas Gregorb4964f72010-02-15 23:54:17 +00003021 // Import the type.
3022 QualType T = Importer.Import(D->getType());
3023 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003024 return nullptr;
3025
Douglas Gregor5c73e912010-02-11 00:48:18 +00003026 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3027 Expr *BitWidth = Importer.Import(D->getBitWidth());
3028 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003029 return nullptr;
3030
Abramo Bagnaradff19302011-03-08 08:55:46 +00003031 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3032 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003033 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003034 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003035 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003036 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003037 ToField->setLexicalDeclContext(LexicalDC);
Richard Smith938f40b2011-06-11 17:19:42 +00003038 if (ToField->hasInClassInitializer())
3039 ToField->setInClassInitializer(D->getInClassInitializer());
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003040 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003041 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003042 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003043 return ToField;
3044}
3045
Francois Pichet783dd6e2010-11-21 06:08:52 +00003046Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3047 // Import the major distinguishing characteristics of a variable.
3048 DeclContext *DC, *LexicalDC;
3049 DeclarationName Name;
3050 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003051 NamedDecl *ToD;
3052 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003053 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003054 if (ToD)
3055 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003056
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003057 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003058 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003059 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003060 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003061 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003062 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003063 // For anonymous indirect fields, match up by index.
3064 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3065 continue;
3066
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003067 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003068 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003069 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003070 Importer.Imported(D, FoundField);
3071 return FoundField;
3072 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003073
3074 // If there are more anonymous fields to check, continue.
3075 if (!Name && I < N-1)
3076 continue;
3077
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003078 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3079 << Name << D->getType() << FoundField->getType();
3080 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3081 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003082 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003083 }
3084 }
3085
Francois Pichet783dd6e2010-11-21 06:08:52 +00003086 // Import the type.
3087 QualType T = Importer.Import(D->getType());
3088 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003089 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003090
3091 NamedDecl **NamedChain =
3092 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3093
3094 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003095 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003096 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003097 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003098 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003099 NamedChain[i++] = cast<NamedDecl>(D);
3100 }
3101
3102 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003103 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3104 NamedChain, D->getChainingSize());
3105
3106 for (const auto *Attr : D->attrs())
3107 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3108
Francois Pichet783dd6e2010-11-21 06:08:52 +00003109 ToIndirectField->setAccess(D->getAccess());
3110 ToIndirectField->setLexicalDeclContext(LexicalDC);
3111 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003112 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003113 return ToIndirectField;
3114}
3115
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003116Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3117 // Import the major distinguishing characteristics of an ivar.
3118 DeclContext *DC, *LexicalDC;
3119 DeclarationName Name;
3120 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003121 NamedDecl *ToD;
3122 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003123 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003124 if (ToD)
3125 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003126
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003127 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003128 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003129 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003130 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3131 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003132 if (Importer.IsStructurallyEquivalent(D->getType(),
3133 FoundIvar->getType())) {
3134 Importer.Imported(D, FoundIvar);
3135 return FoundIvar;
3136 }
3137
3138 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3139 << Name << D->getType() << FoundIvar->getType();
3140 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3141 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003142 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003143 }
3144 }
3145
3146 // Import the type.
3147 QualType T = Importer.Import(D->getType());
3148 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003149 return nullptr;
3150
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003151 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3152 Expr *BitWidth = Importer.Import(D->getBitWidth());
3153 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003154 return nullptr;
3155
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003156 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3157 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003158 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003159 Loc, Name.getAsIdentifierInfo(),
3160 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003161 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003162 ToIvar->setLexicalDeclContext(LexicalDC);
3163 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003164 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003165 return ToIvar;
3166
3167}
3168
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003169Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3170 // Import the major distinguishing characteristics of a variable.
3171 DeclContext *DC, *LexicalDC;
3172 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003173 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003174 NamedDecl *ToD;
3175 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003176 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003177 if (ToD)
3178 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003179
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003180 // Try to find a variable in our own ("to") context with the same name and
3181 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003182 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003183 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003184 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003185 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003186 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003187 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003188 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3189 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003190 continue;
3191
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003192 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003193 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003194 if (FoundVar->hasExternalFormalLinkage() &&
3195 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003196 if (Importer.IsStructurallyEquivalent(D->getType(),
3197 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003198 MergeWithVar = FoundVar;
3199 break;
3200 }
3201
Douglas Gregor56521c52010-02-12 17:23:39 +00003202 const ArrayType *FoundArray
3203 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3204 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003205 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003206 if (FoundArray && TArray) {
3207 if (isa<IncompleteArrayType>(FoundArray) &&
3208 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003209 // Import the type.
3210 QualType T = Importer.Import(D->getType());
3211 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003212 return nullptr;
3213
Douglas Gregor56521c52010-02-12 17:23:39 +00003214 FoundVar->setType(T);
3215 MergeWithVar = FoundVar;
3216 break;
3217 } else if (isa<IncompleteArrayType>(TArray) &&
3218 isa<ConstantArrayType>(FoundArray)) {
3219 MergeWithVar = FoundVar;
3220 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003221 }
3222 }
3223
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003224 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003225 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003226 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3227 << FoundVar->getType();
3228 }
3229 }
3230
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003231 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003232 }
3233
3234 if (MergeWithVar) {
3235 // An equivalent variable with external linkage has been found. Link
3236 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003237 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003238
3239 if (VarDecl *DDef = D->getDefinition()) {
3240 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3241 Importer.ToDiag(ExistingDef->getLocation(),
3242 diag::err_odr_variable_multiple_def)
3243 << Name;
3244 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3245 } else {
3246 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003247 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003248 if (DDef->isInitKnownICE()) {
3249 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3250 Eval->CheckedICE = true;
3251 Eval->IsICE = DDef->isInitICE();
3252 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003253 }
3254 }
3255
3256 return MergeWithVar;
3257 }
3258
3259 if (!ConflictingDecls.empty()) {
3260 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3261 ConflictingDecls.data(),
3262 ConflictingDecls.size());
3263 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003264 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003265 }
3266 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003267
Douglas Gregorb4964f72010-02-15 23:54:17 +00003268 // Import the type.
3269 QualType T = Importer.Import(D->getType());
3270 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003271 return nullptr;
3272
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003273 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003274 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003275 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3276 Importer.Import(D->getInnerLocStart()),
3277 Loc, Name.getAsIdentifierInfo(),
3278 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003279 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003280 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003281 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003282 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003283 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003284 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003285
Sean Callanan59721b32015-04-28 18:41:46 +00003286 if (!D->isFileVarDecl() &&
3287 D->isUsed())
3288 ToVar->setIsUsed();
3289
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003290 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003291 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003292 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003293
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003294 return ToVar;
3295}
3296
Douglas Gregor8b228d72010-02-17 21:22:52 +00003297Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3298 // Parameters are created in the translation unit's context, then moved
3299 // into the function declaration's context afterward.
3300 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3301
3302 // Import the name of this declaration.
3303 DeclarationName Name = Importer.Import(D->getDeclName());
3304 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003305 return nullptr;
3306
Douglas Gregor8b228d72010-02-17 21:22:52 +00003307 // Import the location of this declaration.
3308 SourceLocation Loc = Importer.Import(D->getLocation());
3309
3310 // Import the parameter's type.
3311 QualType T = Importer.Import(D->getType());
3312 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003313 return nullptr;
3314
Douglas Gregor8b228d72010-02-17 21:22:52 +00003315 // Create the imported parameter.
3316 ImplicitParamDecl *ToParm
3317 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3318 Loc, Name.getAsIdentifierInfo(),
3319 T);
3320 return Importer.Imported(D, ToParm);
3321}
3322
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003323Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3324 // Parameters are created in the translation unit's context, then moved
3325 // into the function declaration's context afterward.
3326 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3327
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003328 // Import the name of this declaration.
3329 DeclarationName Name = Importer.Import(D->getDeclName());
3330 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003331 return nullptr;
3332
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003333 // Import the location of this declaration.
3334 SourceLocation Loc = Importer.Import(D->getLocation());
3335
3336 // Import the parameter's type.
3337 QualType T = Importer.Import(D->getType());
3338 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003339 return nullptr;
3340
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003341 // Create the imported parameter.
3342 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3343 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003344 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003345 Loc, Name.getAsIdentifierInfo(),
3346 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003347 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003348 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003349
3350 if (D->isUsed())
3351 ToParm->setIsUsed();
3352
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003353 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003354}
3355
Douglas Gregor43f54792010-02-17 02:12:47 +00003356Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3357 // Import the major distinguishing characteristics of a method.
3358 DeclContext *DC, *LexicalDC;
3359 DeclarationName Name;
3360 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003361 NamedDecl *ToD;
3362 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003363 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003364 if (ToD)
3365 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003366
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003367 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003368 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003369 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3370 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003371 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3372 continue;
3373
3374 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003375 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3376 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003377 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003378 << D->isInstanceMethod() << Name << D->getReturnType()
3379 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003380 Importer.ToDiag(FoundMethod->getLocation(),
3381 diag::note_odr_objc_method_here)
3382 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003383 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003384 }
3385
3386 // Check the number of parameters.
3387 if (D->param_size() != FoundMethod->param_size()) {
3388 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3389 << D->isInstanceMethod() << Name
3390 << D->param_size() << FoundMethod->param_size();
3391 Importer.ToDiag(FoundMethod->getLocation(),
3392 diag::note_odr_objc_method_here)
3393 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003394 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003395 }
3396
3397 // Check parameter types.
3398 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3399 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3400 P != PEnd; ++P, ++FoundP) {
3401 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3402 (*FoundP)->getType())) {
3403 Importer.FromDiag((*P)->getLocation(),
3404 diag::err_odr_objc_method_param_type_inconsistent)
3405 << D->isInstanceMethod() << Name
3406 << (*P)->getType() << (*FoundP)->getType();
3407 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3408 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003409 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003410 }
3411 }
3412
3413 // Check variadic/non-variadic.
3414 // Check the number of parameters.
3415 if (D->isVariadic() != FoundMethod->isVariadic()) {
3416 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3417 << D->isInstanceMethod() << Name;
3418 Importer.ToDiag(FoundMethod->getLocation(),
3419 diag::note_odr_objc_method_here)
3420 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003421 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003422 }
3423
3424 // FIXME: Any other bits we need to merge?
3425 return Importer.Imported(D, FoundMethod);
3426 }
3427 }
3428
3429 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003430 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003431 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003432 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003433
Alp Toker314cc812014-01-25 16:55:45 +00003434 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003435
Alp Toker314cc812014-01-25 16:55:45 +00003436 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3437 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3438 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3439 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3440 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003441
3442 // FIXME: When we decide to merge method definitions, we'll need to
3443 // deal with implicit parameters.
3444
3445 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003446 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003447 for (auto *FromP : D->params()) {
3448 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003449 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003450 return nullptr;
3451
Douglas Gregor43f54792010-02-17 02:12:47 +00003452 ToParams.push_back(ToP);
3453 }
3454
3455 // Set the parameters.
3456 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3457 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003458 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003459 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003460 SmallVector<SourceLocation, 12> SelLocs;
3461 D->getSelectorLocs(SelLocs);
3462 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003463
3464 ToMethod->setLexicalDeclContext(LexicalDC);
3465 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003466 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003467 return ToMethod;
3468}
3469
Douglas Gregor85f3f952015-07-07 03:57:15 +00003470Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3471 // Import the major distinguishing characteristics of a category.
3472 DeclContext *DC, *LexicalDC;
3473 DeclarationName Name;
3474 SourceLocation Loc;
3475 NamedDecl *ToD;
3476 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3477 return nullptr;
3478 if (ToD)
3479 return ToD;
3480
3481 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3482 if (!BoundInfo)
3483 return nullptr;
3484
3485 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3486 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003487 D->getVariance(),
3488 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003489 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003490 Importer.Import(D->getLocation()),
3491 Name.getAsIdentifierInfo(),
3492 Importer.Import(D->getColonLoc()),
3493 BoundInfo);
3494 Importer.Imported(D, Result);
3495 Result->setLexicalDeclContext(LexicalDC);
3496 return Result;
3497}
3498
Douglas Gregor84c51c32010-02-18 01:47:50 +00003499Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3500 // Import the major distinguishing characteristics of a category.
3501 DeclContext *DC, *LexicalDC;
3502 DeclarationName Name;
3503 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003504 NamedDecl *ToD;
3505 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003506 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003507 if (ToD)
3508 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003509
Douglas Gregor84c51c32010-02-18 01:47:50 +00003510 ObjCInterfaceDecl *ToInterface
3511 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3512 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003513 return nullptr;
3514
Douglas Gregor84c51c32010-02-18 01:47:50 +00003515 // Determine if we've already encountered this category.
3516 ObjCCategoryDecl *MergeWithCategory
3517 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3518 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3519 if (!ToCategory) {
3520 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003521 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003522 Loc,
3523 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003524 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003525 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003526 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003527 Importer.Import(D->getIvarLBraceLoc()),
3528 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003529 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003530 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003531 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003532 // Import the type parameter list after calling Imported, to avoid
3533 // loops when bringing in their DeclContext.
3534 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3535 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003536
Douglas Gregor84c51c32010-02-18 01:47:50 +00003537 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003538 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3539 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003540 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3541 = D->protocol_loc_begin();
3542 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3543 FromProtoEnd = D->protocol_end();
3544 FromProto != FromProtoEnd;
3545 ++FromProto, ++FromProtoLoc) {
3546 ObjCProtocolDecl *ToProto
3547 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3548 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003549 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003550 Protocols.push_back(ToProto);
3551 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3552 }
3553
3554 // FIXME: If we're merging, make sure that the protocol list is the same.
3555 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3556 ProtocolLocs.data(), Importer.getToContext());
3557
3558 } else {
3559 Importer.Imported(D, ToCategory);
3560 }
3561
3562 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003563 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003564
3565 // If we have an implementation, import it as well.
3566 if (D->getImplementation()) {
3567 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003568 = cast_or_null<ObjCCategoryImplDecl>(
3569 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003570 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003571 return nullptr;
3572
Douglas Gregor84c51c32010-02-18 01:47:50 +00003573 ToCategory->setImplementation(Impl);
3574 }
3575
3576 return ToCategory;
3577}
3578
Douglas Gregor2aa53772012-01-24 17:42:07 +00003579bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3580 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003581 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003582 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003583 if (shouldForceImportDeclContext(Kind))
3584 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003585 return false;
3586 }
3587
3588 // Start the protocol definition
3589 To->startDefinition();
3590
3591 // Import protocols
3592 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3593 SmallVector<SourceLocation, 4> ProtocolLocs;
3594 ObjCProtocolDecl::protocol_loc_iterator
3595 FromProtoLoc = From->protocol_loc_begin();
3596 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3597 FromProtoEnd = From->protocol_end();
3598 FromProto != FromProtoEnd;
3599 ++FromProto, ++FromProtoLoc) {
3600 ObjCProtocolDecl *ToProto
3601 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3602 if (!ToProto)
3603 return true;
3604 Protocols.push_back(ToProto);
3605 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3606 }
3607
3608 // FIXME: If we're merging, make sure that the protocol list is the same.
3609 To->setProtocolList(Protocols.data(), Protocols.size(),
3610 ProtocolLocs.data(), Importer.getToContext());
3611
Douglas Gregor2e15c842012-02-01 21:00:38 +00003612 if (shouldForceImportDeclContext(Kind)) {
3613 // Import all of the members of this protocol.
3614 ImportDeclContext(From, /*ForceImport=*/true);
3615 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003616 return false;
3617}
3618
Douglas Gregor98d156a2010-02-17 16:12:00 +00003619Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003620 // If this protocol has a definition in the translation unit we're coming
3621 // from, but this particular declaration is not that definition, import the
3622 // definition and map to that.
3623 ObjCProtocolDecl *Definition = D->getDefinition();
3624 if (Definition && Definition != D) {
3625 Decl *ImportedDef = Importer.Import(Definition);
3626 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003627 return nullptr;
3628
Douglas Gregor2aa53772012-01-24 17:42:07 +00003629 return Importer.Imported(D, ImportedDef);
3630 }
3631
Douglas Gregor84c51c32010-02-18 01:47:50 +00003632 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003633 DeclContext *DC, *LexicalDC;
3634 DeclarationName Name;
3635 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003636 NamedDecl *ToD;
3637 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003638 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003639 if (ToD)
3640 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003641
Craig Topper36250ad2014-05-12 05:36:57 +00003642 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003643 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003644 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003645 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3646 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003647 continue;
3648
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003649 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003650 break;
3651 }
3652
3653 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003654 if (!ToProto) {
3655 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3656 Name.getAsIdentifierInfo(), Loc,
3657 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003658 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003659 ToProto->setLexicalDeclContext(LexicalDC);
3660 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003661 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003662
3663 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003664
Douglas Gregor2aa53772012-01-24 17:42:07 +00003665 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003666 return nullptr;
3667
Douglas Gregor98d156a2010-02-17 16:12:00 +00003668 return ToProto;
3669}
3670
Sean Callanan0aae0412014-12-10 00:00:37 +00003671Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3672 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3673 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3674
3675 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3676 SourceLocation LangLoc = Importer.Import(D->getLocation());
3677
3678 bool HasBraces = D->hasBraces();
3679
Sean Callananb12a8552014-12-10 21:22:20 +00003680 LinkageSpecDecl *ToLinkageSpec =
3681 LinkageSpecDecl::Create(Importer.getToContext(),
3682 DC,
3683 ExternLoc,
3684 LangLoc,
3685 D->getLanguage(),
3686 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003687
3688 if (HasBraces) {
3689 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3690 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3691 }
3692
3693 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3694 LexicalDC->addDeclInternal(ToLinkageSpec);
3695
3696 Importer.Imported(D, ToLinkageSpec);
3697
3698 return ToLinkageSpec;
3699}
3700
Douglas Gregor2aa53772012-01-24 17:42:07 +00003701bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3702 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003703 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003704 if (To->getDefinition()) {
3705 // Check consistency of superclass.
3706 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3707 if (FromSuper) {
3708 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3709 if (!FromSuper)
3710 return true;
3711 }
3712
3713 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3714 if ((bool)FromSuper != (bool)ToSuper ||
3715 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3716 Importer.ToDiag(To->getLocation(),
3717 diag::err_odr_objc_superclass_inconsistent)
3718 << To->getDeclName();
3719 if (ToSuper)
3720 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3721 << To->getSuperClass()->getDeclName();
3722 else
3723 Importer.ToDiag(To->getLocation(),
3724 diag::note_odr_objc_missing_superclass);
3725 if (From->getSuperClass())
3726 Importer.FromDiag(From->getSuperClassLoc(),
3727 diag::note_odr_objc_superclass)
3728 << From->getSuperClass()->getDeclName();
3729 else
3730 Importer.FromDiag(From->getLocation(),
3731 diag::note_odr_objc_missing_superclass);
3732 }
3733
Douglas Gregor2e15c842012-02-01 21:00:38 +00003734 if (shouldForceImportDeclContext(Kind))
3735 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003736 return false;
3737 }
3738
3739 // Start the definition.
3740 To->startDefinition();
3741
3742 // If this class has a superclass, import it.
3743 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003744 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3745 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003746 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003747
3748 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003749 }
3750
3751 // Import protocols
3752 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3753 SmallVector<SourceLocation, 4> ProtocolLocs;
3754 ObjCInterfaceDecl::protocol_loc_iterator
3755 FromProtoLoc = From->protocol_loc_begin();
3756
3757 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3758 FromProtoEnd = From->protocol_end();
3759 FromProto != FromProtoEnd;
3760 ++FromProto, ++FromProtoLoc) {
3761 ObjCProtocolDecl *ToProto
3762 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3763 if (!ToProto)
3764 return true;
3765 Protocols.push_back(ToProto);
3766 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3767 }
3768
3769 // FIXME: If we're merging, make sure that the protocol list is the same.
3770 To->setProtocolList(Protocols.data(), Protocols.size(),
3771 ProtocolLocs.data(), Importer.getToContext());
3772
3773 // Import categories. When the categories themselves are imported, they'll
3774 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003775 for (auto *Cat : From->known_categories())
3776 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003777
Douglas Gregor2aa53772012-01-24 17:42:07 +00003778 // If we have an @implementation, import it as well.
3779 if (From->getImplementation()) {
3780 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3781 Importer.Import(From->getImplementation()));
3782 if (!Impl)
3783 return true;
3784
3785 To->setImplementation(Impl);
3786 }
3787
Douglas Gregor2e15c842012-02-01 21:00:38 +00003788 if (shouldForceImportDeclContext(Kind)) {
3789 // Import all of the members of this class.
3790 ImportDeclContext(From, /*ForceImport=*/true);
3791 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003792 return false;
3793}
3794
Douglas Gregor85f3f952015-07-07 03:57:15 +00003795ObjCTypeParamList *
3796ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3797 if (!list)
3798 return nullptr;
3799
3800 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3801 for (auto fromTypeParam : *list) {
3802 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3803 Importer.Import(fromTypeParam));
3804 if (!toTypeParam)
3805 return nullptr;
3806
3807 toTypeParams.push_back(toTypeParam);
3808 }
3809
3810 return ObjCTypeParamList::create(Importer.getToContext(),
3811 Importer.Import(list->getLAngleLoc()),
3812 toTypeParams,
3813 Importer.Import(list->getRAngleLoc()));
3814}
3815
Douglas Gregor45635322010-02-16 01:20:57 +00003816Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003817 // If this class has a definition in the translation unit we're coming from,
3818 // but this particular declaration is not that definition, import the
3819 // definition and map to that.
3820 ObjCInterfaceDecl *Definition = D->getDefinition();
3821 if (Definition && Definition != D) {
3822 Decl *ImportedDef = Importer.Import(Definition);
3823 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003824 return nullptr;
3825
Douglas Gregor2aa53772012-01-24 17:42:07 +00003826 return Importer.Imported(D, ImportedDef);
3827 }
3828
Douglas Gregor45635322010-02-16 01:20:57 +00003829 // Import the major distinguishing characteristics of an @interface.
3830 DeclContext *DC, *LexicalDC;
3831 DeclarationName Name;
3832 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003833 NamedDecl *ToD;
3834 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003835 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003836 if (ToD)
3837 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003838
Douglas Gregor2aa53772012-01-24 17:42:07 +00003839 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003840 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003841 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003842 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003843 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3844 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003845 continue;
3846
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003847 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003848 break;
3849 }
3850
Douglas Gregor2aa53772012-01-24 17:42:07 +00003851 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003852 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003853 if (!ToIface) {
3854 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
3855 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003856 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003857 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00003858 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00003859 D->isImplicitInterfaceDecl());
3860 ToIface->setLexicalDeclContext(LexicalDC);
3861 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00003862 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003863 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003864 // Import the type parameter list after calling Imported, to avoid
3865 // loops when bringing in their DeclContext.
3866 ToIface->setTypeParamList(ImportObjCTypeParamList(
3867 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00003868
Douglas Gregor2aa53772012-01-24 17:42:07 +00003869 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00003870 return nullptr;
3871
Douglas Gregor98d156a2010-02-17 16:12:00 +00003872 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00003873}
3874
Douglas Gregor4da9d682010-12-07 15:32:12 +00003875Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
3876 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
3877 Importer.Import(D->getCategoryDecl()));
3878 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00003879 return nullptr;
3880
Douglas Gregor4da9d682010-12-07 15:32:12 +00003881 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
3882 if (!ToImpl) {
3883 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3884 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00003885 return nullptr;
3886
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003887 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00003888 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00003889 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003890 Category->getClassInterface(),
3891 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00003892 Importer.Import(D->getAtStartLoc()),
3893 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003894
3895 DeclContext *LexicalDC = DC;
3896 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3897 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3898 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003899 return nullptr;
3900
Douglas Gregor4da9d682010-12-07 15:32:12 +00003901 ToImpl->setLexicalDeclContext(LexicalDC);
3902 }
3903
Sean Callanan95e74be2011-10-21 02:57:43 +00003904 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003905 Category->setImplementation(ToImpl);
3906 }
3907
3908 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003909 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00003910 return ToImpl;
3911}
3912
Douglas Gregorda8025c2010-12-07 01:26:03 +00003913Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
3914 // Find the corresponding interface.
3915 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
3916 Importer.Import(D->getClassInterface()));
3917 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00003918 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003919
3920 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00003921 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003922 if (D->getSuperClass()) {
3923 Super = cast_or_null<ObjCInterfaceDecl>(
3924 Importer.Import(D->getSuperClass()));
3925 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00003926 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003927 }
3928
3929 ObjCImplementationDecl *Impl = Iface->getImplementation();
3930 if (!Impl) {
3931 // We haven't imported an implementation yet. Create a new @implementation
3932 // now.
3933 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
3934 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003935 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00003936 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003937 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00003938 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003939 Importer.Import(D->getIvarLBraceLoc()),
3940 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00003941
3942 if (D->getDeclContext() != D->getLexicalDeclContext()) {
3943 DeclContext *LexicalDC
3944 = Importer.ImportContext(D->getLexicalDeclContext());
3945 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00003946 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003947 Impl->setLexicalDeclContext(LexicalDC);
3948 }
3949
3950 // Associate the implementation with the class it implements.
3951 Iface->setImplementation(Impl);
3952 Importer.Imported(D, Iface->getImplementation());
3953 } else {
3954 Importer.Imported(D, Iface->getImplementation());
3955
3956 // Verify that the existing @implementation has the same superclass.
3957 if ((Super && !Impl->getSuperClass()) ||
3958 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00003959 (Super && Impl->getSuperClass() &&
3960 !declaresSameEntity(Super->getCanonicalDecl(),
3961 Impl->getSuperClass()))) {
3962 Importer.ToDiag(Impl->getLocation(),
3963 diag::err_odr_objc_superclass_inconsistent)
3964 << Iface->getDeclName();
3965 // FIXME: It would be nice to have the location of the superclass
3966 // below.
3967 if (Impl->getSuperClass())
3968 Importer.ToDiag(Impl->getLocation(),
3969 diag::note_odr_objc_superclass)
3970 << Impl->getSuperClass()->getDeclName();
3971 else
3972 Importer.ToDiag(Impl->getLocation(),
3973 diag::note_odr_objc_missing_superclass);
3974 if (D->getSuperClass())
3975 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003976 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00003977 << D->getSuperClass()->getDeclName();
3978 else
3979 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00003980 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00003981 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00003982 }
3983 }
3984
3985 // Import all of the members of this @implementation.
3986 ImportDeclContext(D);
3987
3988 return Impl;
3989}
3990
Douglas Gregora11c4582010-02-17 18:02:10 +00003991Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
3992 // Import the major distinguishing characteristics of an @property.
3993 DeclContext *DC, *LexicalDC;
3994 DeclarationName Name;
3995 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003996 NamedDecl *ToD;
3997 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003998 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003999 if (ToD)
4000 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004001
4002 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004003 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004004 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004005 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004006 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004007 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004008 // Check property types.
4009 if (!Importer.IsStructurallyEquivalent(D->getType(),
4010 FoundProp->getType())) {
4011 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4012 << Name << D->getType() << FoundProp->getType();
4013 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4014 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004015 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004016 }
4017
4018 // FIXME: Check property attributes, getters, setters, etc.?
4019
4020 // Consider these properties to be equivalent.
4021 Importer.Imported(D, FoundProp);
4022 return FoundProp;
4023 }
4024 }
4025
4026 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004027 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4028 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004029 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004030
4031 // Create the new property.
4032 ObjCPropertyDecl *ToProperty
4033 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4034 Name.getAsIdentifierInfo(),
4035 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004036 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004037 Importer.Import(D->getType()),
4038 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004039 D->getPropertyImplementation());
4040 Importer.Imported(D, ToProperty);
4041 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004042 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004043
4044 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004045 ToProperty->setPropertyAttributesAsWritten(
4046 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004047 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4048 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4049 ToProperty->setGetterMethodDecl(
4050 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4051 ToProperty->setSetterMethodDecl(
4052 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4053 ToProperty->setPropertyIvarDecl(
4054 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4055 return ToProperty;
4056}
4057
Douglas Gregor14a49e22010-12-07 18:32:03 +00004058Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4059 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4060 Importer.Import(D->getPropertyDecl()));
4061 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004062 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004063
4064 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4065 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004066 return nullptr;
4067
Douglas Gregor14a49e22010-12-07 18:32:03 +00004068 // Import the lexical declaration context.
4069 DeclContext *LexicalDC = DC;
4070 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4071 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4072 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004073 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004074 }
4075
4076 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4077 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004078 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004079
4080 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004081 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004082 if (D->getPropertyIvarDecl()) {
4083 Ivar = cast_or_null<ObjCIvarDecl>(
4084 Importer.Import(D->getPropertyIvarDecl()));
4085 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004086 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004087 }
4088
4089 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004090 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4091 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004092 if (!ToImpl) {
4093 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4094 Importer.Import(D->getLocStart()),
4095 Importer.Import(D->getLocation()),
4096 Property,
4097 D->getPropertyImplementation(),
4098 Ivar,
4099 Importer.Import(D->getPropertyIvarDeclLoc()));
4100 ToImpl->setLexicalDeclContext(LexicalDC);
4101 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004102 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004103 } else {
4104 // Check that we have the same kind of property implementation (@synthesize
4105 // vs. @dynamic).
4106 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4107 Importer.ToDiag(ToImpl->getLocation(),
4108 diag::err_odr_objc_property_impl_kind_inconsistent)
4109 << Property->getDeclName()
4110 << (ToImpl->getPropertyImplementation()
4111 == ObjCPropertyImplDecl::Dynamic);
4112 Importer.FromDiag(D->getLocation(),
4113 diag::note_odr_objc_property_impl_kind)
4114 << D->getPropertyDecl()->getDeclName()
4115 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004116 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004117 }
4118
4119 // For @synthesize, check that we have the same
4120 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4121 Ivar != ToImpl->getPropertyIvarDecl()) {
4122 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4123 diag::err_odr_objc_synthesize_ivar_inconsistent)
4124 << Property->getDeclName()
4125 << ToImpl->getPropertyIvarDecl()->getDeclName()
4126 << Ivar->getDeclName();
4127 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4128 diag::note_odr_objc_synthesize_ivar_here)
4129 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004130 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004131 }
4132
4133 // Merge the existing implementation with the new implementation.
4134 Importer.Imported(D, ToImpl);
4135 }
4136
4137 return ToImpl;
4138}
4139
Douglas Gregora082a492010-11-30 19:14:50 +00004140Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4141 // For template arguments, we adopt the translation unit as our declaration
4142 // context. This context will be fixed when the actual template declaration
4143 // is created.
4144
4145 // FIXME: Import default argument.
4146 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4147 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004148 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004149 Importer.Import(D->getLocation()),
4150 D->getDepth(),
4151 D->getIndex(),
4152 Importer.Import(D->getIdentifier()),
4153 D->wasDeclaredWithTypename(),
4154 D->isParameterPack());
4155}
4156
4157Decl *
4158ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4159 // Import the name of this declaration.
4160 DeclarationName Name = Importer.Import(D->getDeclName());
4161 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004162 return nullptr;
4163
Douglas Gregora082a492010-11-30 19:14:50 +00004164 // Import the location of this declaration.
4165 SourceLocation Loc = Importer.Import(D->getLocation());
4166
4167 // Import the type of this declaration.
4168 QualType T = Importer.Import(D->getType());
4169 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004170 return nullptr;
4171
Douglas Gregora082a492010-11-30 19:14:50 +00004172 // Import type-source information.
4173 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4174 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004175 return nullptr;
4176
Douglas Gregora082a492010-11-30 19:14:50 +00004177 // FIXME: Import default argument.
4178
4179 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4180 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004181 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004182 Loc, D->getDepth(), D->getPosition(),
4183 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004184 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004185}
4186
4187Decl *
4188ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4189 // Import the name of this declaration.
4190 DeclarationName Name = Importer.Import(D->getDeclName());
4191 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004192 return nullptr;
4193
Douglas Gregora082a492010-11-30 19:14:50 +00004194 // Import the location of this declaration.
4195 SourceLocation Loc = Importer.Import(D->getLocation());
4196
4197 // Import template parameters.
4198 TemplateParameterList *TemplateParams
4199 = ImportTemplateParameterList(D->getTemplateParameters());
4200 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004201 return nullptr;
4202
Douglas Gregora082a492010-11-30 19:14:50 +00004203 // FIXME: Import default argument.
4204
4205 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4206 Importer.getToContext().getTranslationUnitDecl(),
4207 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004208 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004209 Name.getAsIdentifierInfo(),
4210 TemplateParams);
4211}
4212
4213Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4214 // If this record has a definition in the translation unit we're coming from,
4215 // but this particular declaration is not that definition, import the
4216 // definition and map to that.
4217 CXXRecordDecl *Definition
4218 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4219 if (Definition && Definition != D->getTemplatedDecl()) {
4220 Decl *ImportedDef
4221 = Importer.Import(Definition->getDescribedClassTemplate());
4222 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004223 return nullptr;
4224
Douglas Gregora082a492010-11-30 19:14:50 +00004225 return Importer.Imported(D, ImportedDef);
4226 }
4227
4228 // Import the major distinguishing characteristics of this class template.
4229 DeclContext *DC, *LexicalDC;
4230 DeclarationName Name;
4231 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004232 NamedDecl *ToD;
4233 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004234 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004235 if (ToD)
4236 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004237
Douglas Gregora082a492010-11-30 19:14:50 +00004238 // We may already have a template of the same name; try to find and match it.
4239 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004240 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004241 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004242 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004243 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4244 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004245 continue;
4246
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004247 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004248 if (ClassTemplateDecl *FoundTemplate
4249 = dyn_cast<ClassTemplateDecl>(Found)) {
4250 if (IsStructuralMatch(D, FoundTemplate)) {
4251 // The class templates structurally match; call it the same template.
4252 // FIXME: We may be filling in a forward declaration here. Handle
4253 // this case!
4254 Importer.Imported(D->getTemplatedDecl(),
4255 FoundTemplate->getTemplatedDecl());
4256 return Importer.Imported(D, FoundTemplate);
4257 }
4258 }
4259
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004260 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004261 }
4262
4263 if (!ConflictingDecls.empty()) {
4264 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4265 ConflictingDecls.data(),
4266 ConflictingDecls.size());
4267 }
4268
4269 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004270 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004271 }
4272
4273 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4274
4275 // Create the declaration that is being templated.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004276 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4277 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
Douglas Gregora082a492010-11-30 19:14:50 +00004278 CXXRecordDecl *D2Templated = CXXRecordDecl::Create(Importer.getToContext(),
4279 DTemplated->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004280 DC, StartLoc, IdLoc,
4281 Name.getAsIdentifierInfo());
Douglas Gregora082a492010-11-30 19:14:50 +00004282 D2Templated->setAccess(DTemplated->getAccess());
Douglas Gregor14454802011-02-25 02:25:35 +00004283 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
Douglas Gregora082a492010-11-30 19:14:50 +00004284 D2Templated->setLexicalDeclContext(LexicalDC);
4285
4286 // Create the class template declaration itself.
4287 TemplateParameterList *TemplateParams
4288 = ImportTemplateParameterList(D->getTemplateParameters());
4289 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004290 return nullptr;
4291
Douglas Gregora082a492010-11-30 19:14:50 +00004292 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4293 Loc, Name, TemplateParams,
4294 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004295 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004296 D2Templated->setDescribedClassTemplate(D2);
4297
4298 D2->setAccess(D->getAccess());
4299 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004300 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004301
4302 // Note the relationship between the class templates.
4303 Importer.Imported(D, D2);
4304 Importer.Imported(DTemplated, D2Templated);
4305
John McCallf937c022011-10-07 06:10:15 +00004306 if (DTemplated->isCompleteDefinition() &&
4307 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004308 // FIXME: Import definition!
4309 }
4310
4311 return D2;
4312}
4313
Douglas Gregore2e50d332010-12-01 01:36:18 +00004314Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4315 ClassTemplateSpecializationDecl *D) {
4316 // If this record has a definition in the translation unit we're coming from,
4317 // but this particular declaration is not that definition, import the
4318 // definition and map to that.
4319 TagDecl *Definition = D->getDefinition();
4320 if (Definition && Definition != D) {
4321 Decl *ImportedDef = Importer.Import(Definition);
4322 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004323 return nullptr;
4324
Douglas Gregore2e50d332010-12-01 01:36:18 +00004325 return Importer.Imported(D, ImportedDef);
4326 }
4327
4328 ClassTemplateDecl *ClassTemplate
4329 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4330 D->getSpecializedTemplate()));
4331 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004332 return nullptr;
4333
Douglas Gregore2e50d332010-12-01 01:36:18 +00004334 // Import the context of this declaration.
4335 DeclContext *DC = ClassTemplate->getDeclContext();
4336 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004337 return nullptr;
4338
Douglas Gregore2e50d332010-12-01 01:36:18 +00004339 DeclContext *LexicalDC = DC;
4340 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4341 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4342 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004343 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004344 }
4345
4346 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004347 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4348 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004349
4350 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004351 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004352 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4353 D->getTemplateArgs().size(),
4354 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004355 return nullptr;
4356
Douglas Gregore2e50d332010-12-01 01:36:18 +00004357 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004358 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004359 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004360 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004361 if (D2) {
4362 // We already have a class template specialization with these template
4363 // arguments.
4364
4365 // FIXME: Check for specialization vs. instantiation errors.
4366
4367 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004368 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004369 // The record types structurally match, or the "from" translation
4370 // unit only had a forward declaration anyway; call it the same
4371 // function.
4372 return Importer.Imported(D, FoundDef);
4373 }
4374 }
4375 } else {
4376 // Create a new specialization.
4377 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4378 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004379 StartLoc, IdLoc,
4380 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004381 TemplateArgs.data(),
4382 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004383 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004384 D2->setSpecializationKind(D->getSpecializationKind());
4385
4386 // Add this specialization to the class template.
4387 ClassTemplate->AddSpecialization(D2, InsertPos);
4388
4389 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004390 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004391
4392 // Add the specialization to this context.
4393 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004394 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004395 }
4396 Importer.Imported(D, D2);
4397
John McCallf937c022011-10-07 06:10:15 +00004398 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004399 return nullptr;
4400
Douglas Gregore2e50d332010-12-01 01:36:18 +00004401 return D2;
4402}
4403
Larisse Voufo39a1e502013-08-06 01:03:05 +00004404Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4405 // If this variable has a definition in the translation unit we're coming
4406 // from,
4407 // but this particular declaration is not that definition, import the
4408 // definition and map to that.
4409 VarDecl *Definition =
4410 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4411 if (Definition && Definition != D->getTemplatedDecl()) {
4412 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4413 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004414 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004415
4416 return Importer.Imported(D, ImportedDef);
4417 }
4418
4419 // Import the major distinguishing characteristics of this variable template.
4420 DeclContext *DC, *LexicalDC;
4421 DeclarationName Name;
4422 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004423 NamedDecl *ToD;
4424 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004425 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004426 if (ToD)
4427 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004428
4429 // We may already have a template of the same name; try to find and match it.
4430 assert(!DC->isFunctionOrMethod() &&
4431 "Variable templates cannot be declared at function scope");
4432 SmallVector<NamedDecl *, 4> ConflictingDecls;
4433 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004434 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004435 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4436 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4437 continue;
4438
4439 Decl *Found = FoundDecls[I];
4440 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4441 if (IsStructuralMatch(D, FoundTemplate)) {
4442 // The variable templates structurally match; call it the same template.
4443 Importer.Imported(D->getTemplatedDecl(),
4444 FoundTemplate->getTemplatedDecl());
4445 return Importer.Imported(D, FoundTemplate);
4446 }
4447 }
4448
4449 ConflictingDecls.push_back(FoundDecls[I]);
4450 }
4451
4452 if (!ConflictingDecls.empty()) {
4453 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4454 ConflictingDecls.data(),
4455 ConflictingDecls.size());
4456 }
4457
4458 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004459 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004460
4461 VarDecl *DTemplated = D->getTemplatedDecl();
4462
4463 // Import the type.
4464 QualType T = Importer.Import(DTemplated->getType());
4465 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004466 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004467
4468 // Create the declaration that is being templated.
4469 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4470 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4471 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4472 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4473 IdLoc, Name.getAsIdentifierInfo(), T,
4474 TInfo, DTemplated->getStorageClass());
4475 D2Templated->setAccess(DTemplated->getAccess());
4476 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4477 D2Templated->setLexicalDeclContext(LexicalDC);
4478
4479 // Importer.Imported(DTemplated, D2Templated);
4480 // LexicalDC->addDeclInternal(D2Templated);
4481
4482 // Merge the initializer.
4483 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004484 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004485
4486 // Create the variable template declaration itself.
4487 TemplateParameterList *TemplateParams =
4488 ImportTemplateParameterList(D->getTemplateParameters());
4489 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004490 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004491
4492 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004493 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004494 D2Templated->setDescribedVarTemplate(D2);
4495
4496 D2->setAccess(D->getAccess());
4497 D2->setLexicalDeclContext(LexicalDC);
4498 LexicalDC->addDeclInternal(D2);
4499
4500 // Note the relationship between the variable templates.
4501 Importer.Imported(D, D2);
4502 Importer.Imported(DTemplated, D2Templated);
4503
4504 if (DTemplated->isThisDeclarationADefinition() &&
4505 !D2Templated->isThisDeclarationADefinition()) {
4506 // FIXME: Import definition!
4507 }
4508
4509 return D2;
4510}
4511
4512Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4513 VarTemplateSpecializationDecl *D) {
4514 // If this record has a definition in the translation unit we're coming from,
4515 // but this particular declaration is not that definition, import the
4516 // definition and map to that.
4517 VarDecl *Definition = D->getDefinition();
4518 if (Definition && Definition != D) {
4519 Decl *ImportedDef = Importer.Import(Definition);
4520 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004521 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004522
4523 return Importer.Imported(D, ImportedDef);
4524 }
4525
4526 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4527 Importer.Import(D->getSpecializedTemplate()));
4528 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004529 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004530
4531 // Import the context of this declaration.
4532 DeclContext *DC = VarTemplate->getDeclContext();
4533 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004534 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004535
4536 DeclContext *LexicalDC = DC;
4537 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4538 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4539 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004540 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004541 }
4542
4543 // Import the location of this declaration.
4544 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4545 SourceLocation IdLoc = Importer.Import(D->getLocation());
4546
4547 // Import template arguments.
4548 SmallVector<TemplateArgument, 2> TemplateArgs;
4549 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4550 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004551 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004552
4553 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004554 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004555 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004556 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004557 if (D2) {
4558 // We already have a variable template specialization with these template
4559 // arguments.
4560
4561 // FIXME: Check for specialization vs. instantiation errors.
4562
4563 if (VarDecl *FoundDef = D2->getDefinition()) {
4564 if (!D->isThisDeclarationADefinition() ||
4565 IsStructuralMatch(D, FoundDef)) {
4566 // The record types structurally match, or the "from" translation
4567 // unit only had a forward declaration anyway; call it the same
4568 // variable.
4569 return Importer.Imported(D, FoundDef);
4570 }
4571 }
4572 } else {
4573
4574 // Import the type.
4575 QualType T = Importer.Import(D->getType());
4576 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004577 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004578 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4579
4580 // Create a new specialization.
4581 D2 = VarTemplateSpecializationDecl::Create(
4582 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4583 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4584 D2->setSpecializationKind(D->getSpecializationKind());
4585 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4586
4587 // Add this specialization to the class template.
4588 VarTemplate->AddSpecialization(D2, InsertPos);
4589
4590 // Import the qualifier, if any.
4591 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4592
4593 // Add the specialization to this context.
4594 D2->setLexicalDeclContext(LexicalDC);
4595 LexicalDC->addDeclInternal(D2);
4596 }
4597 Importer.Imported(D, D2);
4598
4599 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004600 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004601
4602 return D2;
4603}
4604
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004605//----------------------------------------------------------------------------
4606// Import Statements
4607//----------------------------------------------------------------------------
4608
Sean Callanan59721b32015-04-28 18:41:46 +00004609DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4610 if (DG.isNull())
4611 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4612 size_t NumDecls = DG.end() - DG.begin();
4613 SmallVector<Decl *, 1> ToDecls(NumDecls);
4614 auto &_Importer = this->Importer;
4615 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4616 [&_Importer](Decl *D) -> Decl * {
4617 return _Importer.Import(D);
4618 });
4619 return DeclGroupRef::Create(Importer.getToContext(),
4620 ToDecls.begin(),
4621 NumDecls);
4622}
4623
4624 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4625 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4626 << S->getStmtClassName();
4627 return nullptr;
4628 }
4629
4630Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4631 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4632 for (Decl *ToD : ToDG) {
4633 if (!ToD)
4634 return nullptr;
4635 }
4636 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4637 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4638 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4639}
4640
4641Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4642 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4643 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4644 S->hasLeadingEmptyMacro());
4645}
4646
4647Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
4648 SmallVector<Stmt *, 4> ToStmts(S->size());
4649 auto &_Importer = this->Importer;
4650 std::transform(S->body_begin(), S->body_end(), ToStmts.begin(),
4651 [&_Importer](Stmt *CS) -> Stmt * {
4652 return _Importer.Import(CS);
4653 });
4654 for (Stmt *ToS : ToStmts) {
4655 if (!ToS)
4656 return nullptr;
4657 }
4658 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4659 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4660 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4661 ToStmts,
4662 ToLBraceLoc, ToRBraceLoc);
4663}
4664
4665Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4666 Expr *ToLHS = Importer.Import(S->getLHS());
4667 if (!ToLHS)
4668 return nullptr;
4669 Expr *ToRHS = Importer.Import(S->getRHS());
4670 if (!ToRHS && S->getRHS())
4671 return nullptr;
4672 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4673 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4674 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4675 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4676 ToCaseLoc, ToEllipsisLoc,
4677 ToColonLoc);
4678}
4679
4680Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4681 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4682 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4683 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4684 if (!ToSubStmt && S->getSubStmt())
4685 return nullptr;
4686 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4687 ToSubStmt);
4688}
4689
4690Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4691 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4692 LabelDecl *ToLabelDecl =
4693 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4694 if (!ToLabelDecl && S->getDecl())
4695 return nullptr;
4696 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4697 if (!ToSubStmt && S->getSubStmt())
4698 return nullptr;
4699 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4700 ToSubStmt);
4701}
4702
4703Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4704 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4705 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4706 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4707 ASTContext &_ToContext = Importer.getToContext();
4708 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4709 [&_ToContext](const Attr *A) -> const Attr * {
4710 return A->clone(_ToContext);
4711 });
4712 for (const Attr *ToA : ToAttrs) {
4713 if (!ToA)
4714 return nullptr;
4715 }
4716 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4717 if (!ToSubStmt && S->getSubStmt())
4718 return nullptr;
4719 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4720 ToAttrs, ToSubStmt);
4721}
4722
4723Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4724 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4725 VarDecl *ToConditionVariable = nullptr;
4726 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4727 ToConditionVariable =
4728 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4729 if (!ToConditionVariable)
4730 return nullptr;
4731 }
4732 Expr *ToCondition = Importer.Import(S->getCond());
4733 if (!ToCondition && S->getCond())
4734 return nullptr;
4735 Stmt *ToThenStmt = Importer.Import(S->getThen());
4736 if (!ToThenStmt && S->getThen())
4737 return nullptr;
4738 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4739 Stmt *ToElseStmt = Importer.Import(S->getElse());
4740 if (!ToElseStmt && S->getElse())
4741 return nullptr;
4742 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4743 ToIfLoc, ToConditionVariable,
4744 ToCondition, ToThenStmt,
4745 ToElseLoc, ToElseStmt);
4746}
4747
4748Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4749 VarDecl *ToConditionVariable = nullptr;
4750 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4751 ToConditionVariable =
4752 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4753 if (!ToConditionVariable)
4754 return nullptr;
4755 }
4756 Expr *ToCondition = Importer.Import(S->getCond());
4757 if (!ToCondition && S->getCond())
4758 return nullptr;
4759 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4760 Importer.getToContext(), ToConditionVariable,
4761 ToCondition);
4762 Stmt *ToBody = Importer.Import(S->getBody());
4763 if (!ToBody && S->getBody())
4764 return nullptr;
4765 ToStmt->setBody(ToBody);
4766 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4767 // Now we have to re-chain the cases.
4768 SwitchCase *LastChainedSwitchCase = nullptr;
4769 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4770 SC = SC->getNextSwitchCase()) {
4771 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4772 if (!ToSC)
4773 return nullptr;
4774 if (LastChainedSwitchCase)
4775 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4776 else
4777 ToStmt->setSwitchCaseList(ToSC);
4778 LastChainedSwitchCase = ToSC;
4779 }
4780 return ToStmt;
4781}
4782
4783Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4784 VarDecl *ToConditionVariable = nullptr;
4785 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4786 ToConditionVariable =
4787 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4788 if (!ToConditionVariable)
4789 return nullptr;
4790 }
4791 Expr *ToCondition = Importer.Import(S->getCond());
4792 if (!ToCondition && S->getCond())
4793 return nullptr;
4794 Stmt *ToBody = Importer.Import(S->getBody());
4795 if (!ToBody && S->getBody())
4796 return nullptr;
4797 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4798 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
4799 ToConditionVariable,
4800 ToCondition, ToBody,
4801 ToWhileLoc);
4802}
4803
4804Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
4805 Stmt *ToBody = Importer.Import(S->getBody());
4806 if (!ToBody && S->getBody())
4807 return nullptr;
4808 Expr *ToCondition = Importer.Import(S->getCond());
4809 if (!ToCondition && S->getCond())
4810 return nullptr;
4811 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
4812 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
4813 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4814 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
4815 ToDoLoc, ToWhileLoc,
4816 ToRParenLoc);
4817}
4818
4819Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
4820 Stmt *ToInit = Importer.Import(S->getInit());
4821 if (!ToInit && S->getInit())
4822 return nullptr;
4823 Expr *ToCondition = Importer.Import(S->getCond());
4824 if (!ToCondition && S->getCond())
4825 return nullptr;
4826 VarDecl *ToConditionVariable = nullptr;
4827 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4828 ToConditionVariable =
4829 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4830 if (!ToConditionVariable)
4831 return nullptr;
4832 }
4833 Expr *ToInc = Importer.Import(S->getInc());
4834 if (!ToInc && S->getInc())
4835 return nullptr;
4836 Stmt *ToBody = Importer.Import(S->getBody());
4837 if (!ToBody && S->getBody())
4838 return nullptr;
4839 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4840 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
4841 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4842 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
4843 ToInit, ToCondition,
4844 ToConditionVariable,
4845 ToInc, ToBody,
4846 ToForLoc, ToLParenLoc,
4847 ToRParenLoc);
4848}
4849
4850Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
4851 LabelDecl *ToLabel = nullptr;
4852 if (LabelDecl *FromLabel = S->getLabel()) {
4853 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
4854 if (!ToLabel)
4855 return nullptr;
4856 }
4857 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4858 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
4859 return new (Importer.getToContext()) GotoStmt(ToLabel,
4860 ToGotoLoc, ToLabelLoc);
4861}
4862
4863Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
4864 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
4865 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
4866 Expr *ToTarget = Importer.Import(S->getTarget());
4867 if (!ToTarget && S->getTarget())
4868 return nullptr;
4869 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
4870 ToTarget);
4871}
4872
4873Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
4874 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
4875 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
4876}
4877
4878Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
4879 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
4880 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
4881}
4882
4883Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
4884 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
4885 Expr *ToRetExpr = Importer.Import(S->getRetValue());
4886 if (!ToRetExpr && S->getRetValue())
4887 return nullptr;
4888 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
4889 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
4890 if (!ToNRVOCandidate && NRVOCandidate)
4891 return nullptr;
4892 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
4893 ToNRVOCandidate);
4894}
4895
4896Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
4897 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
4898 VarDecl *ToExceptionDecl = nullptr;
4899 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
4900 ToExceptionDecl =
4901 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4902 if (!ToExceptionDecl)
4903 return nullptr;
4904 }
4905 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
4906 if (!ToHandlerBlock && S->getHandlerBlock())
4907 return nullptr;
4908 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
4909 ToExceptionDecl,
4910 ToHandlerBlock);
4911}
4912
4913Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
4914 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
4915 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
4916 if (!ToTryBlock && S->getTryBlock())
4917 return nullptr;
4918 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
4919 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
4920 CXXCatchStmt *FromHandler = S->getHandler(HI);
4921 if (Stmt *ToHandler = Importer.Import(FromHandler))
4922 ToHandlers[HI] = ToHandler;
4923 else
4924 return nullptr;
4925 }
4926 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
4927 ToHandlers);
4928}
4929
4930Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
4931 DeclStmt *ToRange =
4932 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
4933 if (!ToRange && S->getRangeStmt())
4934 return nullptr;
4935 DeclStmt *ToBeginEnd =
4936 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginEndStmt()));
4937 if (!ToBeginEnd && S->getBeginEndStmt())
4938 return nullptr;
4939 Expr *ToCond = Importer.Import(S->getCond());
4940 if (!ToCond && S->getCond())
4941 return nullptr;
4942 Expr *ToInc = Importer.Import(S->getInc());
4943 if (!ToInc && S->getInc())
4944 return nullptr;
4945 DeclStmt *ToLoopVar =
4946 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
4947 if (!ToLoopVar && S->getLoopVarStmt())
4948 return nullptr;
4949 Stmt *ToBody = Importer.Import(S->getBody());
4950 if (!ToBody && S->getBody())
4951 return nullptr;
4952 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00004953 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00004954 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4955 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4956 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBeginEnd,
4957 ToCond, ToInc,
4958 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00004959 ToForLoc, ToCoawaitLoc,
4960 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00004961}
4962
4963Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
4964 Stmt *ToElem = Importer.Import(S->getElement());
4965 if (!ToElem && S->getElement())
4966 return nullptr;
4967 Expr *ToCollect = Importer.Import(S->getCollection());
4968 if (!ToCollect && S->getCollection())
4969 return nullptr;
4970 Stmt *ToBody = Importer.Import(S->getBody());
4971 if (!ToBody && S->getBody())
4972 return nullptr;
4973 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
4974 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4975 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
4976 ToCollect,
4977 ToBody, ToForLoc,
4978 ToRParenLoc);
4979}
4980
4981Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
4982 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
4983 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
4984 VarDecl *ToExceptionDecl = nullptr;
4985 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
4986 ToExceptionDecl =
4987 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
4988 if (!ToExceptionDecl)
4989 return nullptr;
4990 }
4991 Stmt *ToBody = Importer.Import(S->getCatchBody());
4992 if (!ToBody && S->getCatchBody())
4993 return nullptr;
4994 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
4995 ToRParenLoc,
4996 ToExceptionDecl,
4997 ToBody);
4998}
4999
5000Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5001 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5002 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5003 if (!ToAtFinallyStmt && S->getFinallyBody())
5004 return nullptr;
5005 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5006 ToAtFinallyStmt);
5007}
5008
5009Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5010 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5011 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5012 if (!ToAtTryStmt && S->getTryBody())
5013 return nullptr;
5014 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5015 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5016 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5017 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5018 ToCatchStmts[CI] = ToCatchStmt;
5019 else
5020 return nullptr;
5021 }
5022 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5023 if (!ToAtFinallyStmt && S->getFinallyStmt())
5024 return nullptr;
5025 return ObjCAtTryStmt::Create(Importer.getToContext(),
5026 ToAtTryLoc, ToAtTryStmt,
5027 ToCatchStmts.begin(), ToCatchStmts.size(),
5028 ToAtFinallyStmt);
5029}
5030
5031Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5032 (ObjCAtSynchronizedStmt *S) {
5033 SourceLocation ToAtSynchronizedLoc =
5034 Importer.Import(S->getAtSynchronizedLoc());
5035 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5036 if (!ToSynchExpr && S->getSynchExpr())
5037 return nullptr;
5038 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5039 if (!ToSynchBody && S->getSynchBody())
5040 return nullptr;
5041 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5042 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5043}
5044
5045Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5046 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5047 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5048 if (!ToThrow && S->getThrowExpr())
5049 return nullptr;
5050 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5051}
5052
5053Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5054 (ObjCAutoreleasePoolStmt *S) {
5055 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5056 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5057 if (!ToSubStmt && S->getSubStmt())
5058 return nullptr;
5059 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5060 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005061}
5062
5063//----------------------------------------------------------------------------
5064// Import Expressions
5065//----------------------------------------------------------------------------
5066Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5067 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5068 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005069 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005070}
5071
Douglas Gregor52f820e2010-02-19 01:17:02 +00005072Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005073 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5074 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005075 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005076
Craig Topper36250ad2014-05-12 05:36:57 +00005077 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005078 if (E->getDecl() != E->getFoundDecl()) {
5079 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5080 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005081 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005082 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005083
5084 QualType T = Importer.Import(E->getType());
5085 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005086 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005087
5088 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5089 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005090 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005091 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005092 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005093 Importer.Import(E->getLocation()),
5094 T, E->getValueKind(),
5095 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005096 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005097 if (E->hadMultipleCandidates())
5098 DRE->setHadMultipleCandidates(true);
5099 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005100}
5101
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005102Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5103 QualType T = Importer.Import(E->getType());
5104 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005105 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005106
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005107 return IntegerLiteral::Create(Importer.getToContext(),
5108 E->getValue(), T,
5109 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005110}
5111
Douglas Gregor623421d2010-02-18 02:21:22 +00005112Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5113 QualType T = Importer.Import(E->getType());
5114 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005115 return nullptr;
5116
Douglas Gregorfb65e592011-07-27 05:40:30 +00005117 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5118 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005119 Importer.Import(E->getLocation()));
5120}
5121
Douglas Gregorc74247e2010-02-19 01:07:06 +00005122Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5123 Expr *SubExpr = Importer.Import(E->getSubExpr());
5124 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005125 return nullptr;
5126
Douglas Gregorc74247e2010-02-19 01:07:06 +00005127 return new (Importer.getToContext())
5128 ParenExpr(Importer.Import(E->getLParen()),
5129 Importer.Import(E->getRParen()),
5130 SubExpr);
5131}
5132
5133Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5134 QualType T = Importer.Import(E->getType());
5135 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005136 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005137
5138 Expr *SubExpr = Importer.Import(E->getSubExpr());
5139 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005140 return nullptr;
5141
Douglas Gregorc74247e2010-02-19 01:07:06 +00005142 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005143 T, E->getValueKind(),
5144 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005145 Importer.Import(E->getOperatorLoc()));
5146}
5147
Peter Collingbournee190dee2011-03-11 19:24:49 +00005148Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5149 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005150 QualType ResultType = Importer.Import(E->getType());
5151
5152 if (E->isArgumentType()) {
5153 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5154 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005155 return nullptr;
5156
Peter Collingbournee190dee2011-03-11 19:24:49 +00005157 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5158 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005159 Importer.Import(E->getOperatorLoc()),
5160 Importer.Import(E->getRParenLoc()));
5161 }
5162
5163 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5164 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005165 return nullptr;
5166
Peter Collingbournee190dee2011-03-11 19:24:49 +00005167 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5168 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005169 Importer.Import(E->getOperatorLoc()),
5170 Importer.Import(E->getRParenLoc()));
5171}
5172
Douglas Gregorc74247e2010-02-19 01:07:06 +00005173Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5174 QualType T = Importer.Import(E->getType());
5175 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005176 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005177
5178 Expr *LHS = Importer.Import(E->getLHS());
5179 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005180 return nullptr;
5181
Douglas Gregorc74247e2010-02-19 01:07:06 +00005182 Expr *RHS = Importer.Import(E->getRHS());
5183 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005184 return nullptr;
5185
Douglas Gregorc74247e2010-02-19 01:07:06 +00005186 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005187 T, E->getValueKind(),
5188 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005189 Importer.Import(E->getOperatorLoc()),
5190 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005191}
5192
5193Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5194 QualType T = Importer.Import(E->getType());
5195 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005196 return nullptr;
5197
Douglas Gregorc74247e2010-02-19 01:07:06 +00005198 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5199 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005200 return nullptr;
5201
Douglas Gregorc74247e2010-02-19 01:07:06 +00005202 QualType CompResultType = Importer.Import(E->getComputationResultType());
5203 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005204 return nullptr;
5205
Douglas Gregorc74247e2010-02-19 01:07:06 +00005206 Expr *LHS = Importer.Import(E->getLHS());
5207 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005208 return nullptr;
5209
Douglas Gregorc74247e2010-02-19 01:07:06 +00005210 Expr *RHS = Importer.Import(E->getRHS());
5211 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005212 return nullptr;
5213
Douglas Gregorc74247e2010-02-19 01:07:06 +00005214 return new (Importer.getToContext())
5215 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005216 T, E->getValueKind(),
5217 E->getObjectKind(),
5218 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005219 Importer.Import(E->getOperatorLoc()),
5220 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005221}
5222
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005223static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005224 if (E->path_empty()) return false;
5225
5226 // TODO: import cast paths
5227 return true;
5228}
5229
Douglas Gregor98c10182010-02-12 22:17:39 +00005230Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5231 QualType T = Importer.Import(E->getType());
5232 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005233 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005234
5235 Expr *SubExpr = Importer.Import(E->getSubExpr());
5236 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005237 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005238
5239 CXXCastPath BasePath;
5240 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005241 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005242
5243 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005244 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005245}
5246
Douglas Gregor5481d322010-02-19 01:32:14 +00005247Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5248 QualType T = Importer.Import(E->getType());
5249 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005250 return nullptr;
5251
Douglas Gregor5481d322010-02-19 01:32:14 +00005252 Expr *SubExpr = Importer.Import(E->getSubExpr());
5253 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005254 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005255
5256 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5257 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005258 return nullptr;
5259
John McCallcf142162010-08-07 06:22:56 +00005260 CXXCastPath BasePath;
5261 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005262 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005263
John McCall7decc9e2010-11-18 06:31:45 +00005264 return CStyleCastExpr::Create(Importer.getToContext(), T,
5265 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005266 SubExpr, &BasePath, TInfo,
5267 Importer.Import(E->getLParenLoc()),
5268 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005269}
5270
Sean Callanan59721b32015-04-28 18:41:46 +00005271Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5272 QualType T = Importer.Import(E->getType());
5273 if (T.isNull())
5274 return nullptr;
5275
5276 CXXConstructorDecl *ToCCD =
5277 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
5278 if (!ToCCD && E->getConstructor())
5279 return nullptr;
5280
5281 size_t NumArgs = E->getNumArgs();
5282 SmallVector<Expr *, 1> ToArgs(NumArgs);
5283 ASTImporter &_Importer = Importer;
5284 std::transform(E->arg_begin(), E->arg_end(), ToArgs.begin(),
5285 [&_Importer](Expr *AE) -> Expr * {
5286 return _Importer.Import(AE);
5287 });
5288 for (Expr *ToA : ToArgs) {
5289 if (!ToA)
5290 return nullptr;
5291 }
5292
5293 return CXXConstructExpr::Create(Importer.getToContext(), T,
5294 Importer.Import(E->getLocation()),
5295 ToCCD, E->isElidable(),
5296 ToArgs, E->hadMultipleCandidates(),
5297 E->isListInitialization(),
5298 E->isStdInitListInitialization(),
5299 E->requiresZeroInitialization(),
5300 E->getConstructionKind(),
5301 Importer.Import(E->getParenOrBraceRange()));
5302}
5303
5304Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5305 QualType T = Importer.Import(E->getType());
5306 if (T.isNull())
5307 return nullptr;
5308
5309 Expr *ToBase = Importer.Import(E->getBase());
5310 if (!ToBase && E->getBase())
5311 return nullptr;
5312
5313 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5314 if (!ToMember && E->getMemberDecl())
5315 return nullptr;
5316
5317 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5318 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5319 E->getFoundDecl().getAccess());
5320
5321 DeclarationNameInfo ToMemberNameInfo(
5322 Importer.Import(E->getMemberNameInfo().getName()),
5323 Importer.Import(E->getMemberNameInfo().getLoc()));
5324
5325 if (E->hasExplicitTemplateArgs()) {
5326 return nullptr; // FIXME: handle template arguments
5327 }
5328
5329 return MemberExpr::Create(Importer.getToContext(), ToBase,
5330 E->isArrow(),
5331 Importer.Import(E->getOperatorLoc()),
5332 Importer.Import(E->getQualifierLoc()),
5333 Importer.Import(E->getTemplateKeywordLoc()),
5334 ToMember, ToFoundDecl, ToMemberNameInfo,
5335 nullptr, T, E->getValueKind(),
5336 E->getObjectKind());
5337}
5338
5339Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5340 QualType T = Importer.Import(E->getType());
5341 if (T.isNull())
5342 return nullptr;
5343
5344 Expr *ToCallee = Importer.Import(E->getCallee());
5345 if (!ToCallee && E->getCallee())
5346 return nullptr;
5347
5348 unsigned NumArgs = E->getNumArgs();
5349
5350 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5351
5352 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5353 Expr *FromArg = E->getArg(ai);
5354 Expr *ToArg = Importer.Import(FromArg);
5355 if (!ToArg)
5356 return nullptr;
5357 ToArgs[ai] = ToArg;
5358 }
5359
5360 Expr **ToArgs_Copied = new (Importer.getToContext())
5361 Expr*[NumArgs];
5362
5363 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5364 ToArgs_Copied[ai] = ToArgs[ai];
5365
5366 return new (Importer.getToContext())
5367 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005368 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005369 Importer.Import(E->getRParenLoc()));
5370}
5371
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005372ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005373 ASTContext &FromContext, FileManager &FromFileManager,
5374 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005375 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005376 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005377 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005378{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005379 ImportedDecls[FromContext.getTranslationUnitDecl()]
5380 = ToContext.getTranslationUnitDecl();
5381}
5382
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005383ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005384
5385QualType ASTImporter::Import(QualType FromT) {
5386 if (FromT.isNull())
5387 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005388
5389 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005390
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005391 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005392 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5393 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005394 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005395 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005396
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005397 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005398 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005399 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005400 if (ToT.isNull())
5401 return ToT;
5402
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005403 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005404 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005405
John McCall424cec92011-01-19 06:33:43 +00005406 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005407}
5408
Douglas Gregor62d311f2010-02-09 19:21:46 +00005409TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005410 if (!FromTSI)
5411 return FromTSI;
5412
5413 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005414 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005415 QualType T = Import(FromTSI->getType());
5416 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005417 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005418
5419 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005420 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005421}
5422
Sean Callanan59721b32015-04-28 18:41:46 +00005423Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5424 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
5425 if (Pos != ImportedDecls.end()) {
5426 Decl *ToD = Pos->second;
5427 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
5428 return ToD;
5429 } else {
5430 return nullptr;
5431 }
5432}
5433
Douglas Gregor62d311f2010-02-09 19:21:46 +00005434Decl *ASTImporter::Import(Decl *FromD) {
5435 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00005436 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005437
Douglas Gregord451ea92011-07-29 23:31:30 +00005438 ASTNodeImporter Importer(*this);
5439
Douglas Gregor62d311f2010-02-09 19:21:46 +00005440 // Check whether we've already imported this declaration.
5441 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00005442 if (Pos != ImportedDecls.end()) {
5443 Decl *ToD = Pos->second;
5444 Importer.ImportDefinitionIfNeeded(FromD, ToD);
5445 return ToD;
5446 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00005447
5448 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00005449 Decl *ToD = Importer.Visit(FromD);
5450 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005451 return nullptr;
5452
Douglas Gregor62d311f2010-02-09 19:21:46 +00005453 // Record the imported declaration.
5454 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00005455
5456 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
5457 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00005458 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00005459 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00005460 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005461 // When we've finished transforming a typedef, see whether it was the
5462 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00005463 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00005464 FromTag = AnonTagsWithPendingTypedefs.begin(),
5465 FromTagEnd = AnonTagsWithPendingTypedefs.end();
5466 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00005467 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00005468 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
5469 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00005470 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00005471 AnonTagsWithPendingTypedefs.erase(FromTag);
5472 break;
5473 }
5474 }
5475 }
5476 }
5477
Douglas Gregor62d311f2010-02-09 19:21:46 +00005478 return ToD;
5479}
5480
5481DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
5482 if (!FromDC)
5483 return FromDC;
5484
Douglas Gregor95d82832012-01-24 18:36:04 +00005485 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00005486 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00005487 return nullptr;
5488
Douglas Gregor2e15c842012-02-01 21:00:38 +00005489 // When we're using a record/enum/Objective-C class/protocol as a context, we
5490 // need it to have a definition.
5491 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00005492 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00005493 if (ToRecord->isCompleteDefinition()) {
5494 // Do nothing.
5495 } else if (FromRecord->isCompleteDefinition()) {
5496 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
5497 ASTNodeImporter::IDK_Basic);
5498 } else {
5499 CompleteDecl(ToRecord);
5500 }
5501 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
5502 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
5503 if (ToEnum->isCompleteDefinition()) {
5504 // Do nothing.
5505 } else if (FromEnum->isCompleteDefinition()) {
5506 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
5507 ASTNodeImporter::IDK_Basic);
5508 } else {
5509 CompleteDecl(ToEnum);
5510 }
5511 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
5512 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
5513 if (ToClass->getDefinition()) {
5514 // Do nothing.
5515 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
5516 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
5517 ASTNodeImporter::IDK_Basic);
5518 } else {
5519 CompleteDecl(ToClass);
5520 }
5521 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
5522 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
5523 if (ToProto->getDefinition()) {
5524 // Do nothing.
5525 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
5526 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
5527 ASTNodeImporter::IDK_Basic);
5528 } else {
5529 CompleteDecl(ToProto);
5530 }
Douglas Gregor95d82832012-01-24 18:36:04 +00005531 }
5532
5533 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005534}
5535
5536Expr *ASTImporter::Import(Expr *FromE) {
5537 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00005538 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005539
5540 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
5541}
5542
5543Stmt *ASTImporter::Import(Stmt *FromS) {
5544 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00005545 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005546
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005547 // Check whether we've already imported this declaration.
5548 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
5549 if (Pos != ImportedStmts.end())
5550 return Pos->second;
5551
5552 // Import the type
5553 ASTNodeImporter Importer(*this);
5554 Stmt *ToS = Importer.Visit(FromS);
5555 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00005556 return nullptr;
5557
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005558 // Record the imported declaration.
5559 ImportedStmts[FromS] = ToS;
5560 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005561}
5562
5563NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
5564 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00005565 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005566
Douglas Gregor90ebf252011-04-27 16:48:40 +00005567 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
5568
5569 switch (FromNNS->getKind()) {
5570 case NestedNameSpecifier::Identifier:
5571 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
5572 return NestedNameSpecifier::Create(ToContext, prefix, II);
5573 }
Craig Topper36250ad2014-05-12 05:36:57 +00005574 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005575
5576 case NestedNameSpecifier::Namespace:
5577 if (NamespaceDecl *NS =
5578 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
5579 return NestedNameSpecifier::Create(ToContext, prefix, NS);
5580 }
Craig Topper36250ad2014-05-12 05:36:57 +00005581 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005582
5583 case NestedNameSpecifier::NamespaceAlias:
5584 if (NamespaceAliasDecl *NSAD =
5585 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
5586 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
5587 }
Craig Topper36250ad2014-05-12 05:36:57 +00005588 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005589
5590 case NestedNameSpecifier::Global:
5591 return NestedNameSpecifier::GlobalSpecifier(ToContext);
5592
Nikola Smiljanic67860242014-09-26 00:28:20 +00005593 case NestedNameSpecifier::Super:
5594 if (CXXRecordDecl *RD =
5595 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
5596 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
5597 }
5598 return nullptr;
5599
Douglas Gregor90ebf252011-04-27 16:48:40 +00005600 case NestedNameSpecifier::TypeSpec:
5601 case NestedNameSpecifier::TypeSpecWithTemplate: {
5602 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
5603 if (!T.isNull()) {
5604 bool bTemplate = FromNNS->getKind() ==
5605 NestedNameSpecifier::TypeSpecWithTemplate;
5606 return NestedNameSpecifier::Create(ToContext, prefix,
5607 bTemplate, T.getTypePtr());
5608 }
5609 }
Craig Topper36250ad2014-05-12 05:36:57 +00005610 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00005611 }
5612
5613 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00005614}
5615
Douglas Gregor14454802011-02-25 02:25:35 +00005616NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
5617 // FIXME: Implement!
5618 return NestedNameSpecifierLoc();
5619}
5620
Douglas Gregore2e50d332010-12-01 01:36:18 +00005621TemplateName ASTImporter::Import(TemplateName From) {
5622 switch (From.getKind()) {
5623 case TemplateName::Template:
5624 if (TemplateDecl *ToTemplate
5625 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5626 return TemplateName(ToTemplate);
5627
5628 return TemplateName();
5629
5630 case TemplateName::OverloadedTemplate: {
5631 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
5632 UnresolvedSet<2> ToTemplates;
5633 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
5634 E = FromStorage->end();
5635 I != E; ++I) {
5636 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
5637 ToTemplates.addDecl(To);
5638 else
5639 return TemplateName();
5640 }
5641 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
5642 ToTemplates.end());
5643 }
5644
5645 case TemplateName::QualifiedTemplate: {
5646 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
5647 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
5648 if (!Qualifier)
5649 return TemplateName();
5650
5651 if (TemplateDecl *ToTemplate
5652 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
5653 return ToContext.getQualifiedTemplateName(Qualifier,
5654 QTN->hasTemplateKeyword(),
5655 ToTemplate);
5656
5657 return TemplateName();
5658 }
5659
5660 case TemplateName::DependentTemplate: {
5661 DependentTemplateName *DTN = From.getAsDependentTemplateName();
5662 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
5663 if (!Qualifier)
5664 return TemplateName();
5665
5666 if (DTN->isIdentifier()) {
5667 return ToContext.getDependentTemplateName(Qualifier,
5668 Import(DTN->getIdentifier()));
5669 }
5670
5671 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
5672 }
John McCalld9dfe3a2011-06-30 08:33:18 +00005673
5674 case TemplateName::SubstTemplateTemplateParm: {
5675 SubstTemplateTemplateParmStorage *subst
5676 = From.getAsSubstTemplateTemplateParm();
5677 TemplateTemplateParmDecl *param
5678 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
5679 if (!param)
5680 return TemplateName();
5681
5682 TemplateName replacement = Import(subst->getReplacement());
5683 if (replacement.isNull()) return TemplateName();
5684
5685 return ToContext.getSubstTemplateTemplateParm(param, replacement);
5686 }
Douglas Gregor5590be02011-01-15 06:45:20 +00005687
5688 case TemplateName::SubstTemplateTemplateParmPack: {
5689 SubstTemplateTemplateParmPackStorage *SubstPack
5690 = From.getAsSubstTemplateTemplateParmPack();
5691 TemplateTemplateParmDecl *Param
5692 = cast_or_null<TemplateTemplateParmDecl>(
5693 Import(SubstPack->getParameterPack()));
5694 if (!Param)
5695 return TemplateName();
5696
5697 ASTNodeImporter Importer(*this);
5698 TemplateArgument ArgPack
5699 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
5700 if (ArgPack.isNull())
5701 return TemplateName();
5702
5703 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
5704 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00005705 }
5706
5707 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00005708}
5709
Douglas Gregor62d311f2010-02-09 19:21:46 +00005710SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
5711 if (FromLoc.isInvalid())
5712 return SourceLocation();
5713
Douglas Gregor811663e2010-02-10 00:15:17 +00005714 SourceManager &FromSM = FromContext.getSourceManager();
5715
5716 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00005717 // don't have to import macro expansions.
5718 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00005719 FromLoc = FromSM.getSpellingLoc(FromLoc);
5720 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
5721 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00005722 FileID ToFileID = Import(Decomposed.first);
5723 if (ToFileID.isInvalid())
5724 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00005725 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
5726 .getLocWithOffset(Decomposed.second);
5727 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00005728}
5729
5730SourceRange ASTImporter::Import(SourceRange FromRange) {
5731 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
5732}
5733
Douglas Gregor811663e2010-02-10 00:15:17 +00005734FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00005735 llvm::DenseMap<FileID, FileID>::iterator Pos
5736 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00005737 if (Pos != ImportedFileIDs.end())
5738 return Pos->second;
5739
5740 SourceManager &FromSM = FromContext.getSourceManager();
5741 SourceManager &ToSM = ToContext.getSourceManager();
5742 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00005743 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00005744
5745 // Include location of this file.
5746 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
5747
5748 // Map the FileID for to the "to" source manager.
5749 FileID ToID;
5750 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00005751 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00005752 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
5753 // disk again
5754 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
5755 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00005756 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00005757 if (!Entry)
5758 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00005759 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
5760 FromSLoc.getFile().getFileCharacteristic());
5761 } else {
5762 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005763 const llvm::MemoryBuffer *
5764 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005765 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00005766 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00005767 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00005768 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00005769 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00005770 }
5771
5772
Sebastian Redl99219f12010-09-30 01:03:06 +00005773 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00005774 return ToID;
5775}
5776
Douglas Gregor0a791672011-01-18 03:11:38 +00005777void ASTImporter::ImportDefinition(Decl *From) {
5778 Decl *To = Import(From);
5779 if (!To)
5780 return;
5781
5782 if (DeclContext *FromDC = cast<DeclContext>(From)) {
5783 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005784
5785 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
5786 if (!ToRecord->getDefinition()) {
5787 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00005788 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00005789 return;
5790 }
5791 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005792
5793 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
5794 if (!ToEnum->getDefinition()) {
5795 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005796 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00005797 return;
5798 }
5799 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00005800
5801 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
5802 if (!ToIFace->getDefinition()) {
5803 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005804 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005805 return;
5806 }
5807 }
Douglas Gregord451ea92011-07-29 23:31:30 +00005808
Douglas Gregor2aa53772012-01-24 17:42:07 +00005809 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
5810 if (!ToProto->getDefinition()) {
5811 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00005812 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00005813 return;
5814 }
5815 }
5816
Douglas Gregor0a791672011-01-18 03:11:38 +00005817 Importer.ImportDeclContext(FromDC, true);
5818 }
5819}
5820
Douglas Gregor96e578d2010-02-05 17:54:41 +00005821DeclarationName ASTImporter::Import(DeclarationName FromName) {
5822 if (!FromName)
5823 return DeclarationName();
5824
5825 switch (FromName.getNameKind()) {
5826 case DeclarationName::Identifier:
5827 return Import(FromName.getAsIdentifierInfo());
5828
5829 case DeclarationName::ObjCZeroArgSelector:
5830 case DeclarationName::ObjCOneArgSelector:
5831 case DeclarationName::ObjCMultiArgSelector:
5832 return Import(FromName.getObjCSelector());
5833
5834 case DeclarationName::CXXConstructorName: {
5835 QualType T = Import(FromName.getCXXNameType());
5836 if (T.isNull())
5837 return DeclarationName();
5838
5839 return ToContext.DeclarationNames.getCXXConstructorName(
5840 ToContext.getCanonicalType(T));
5841 }
5842
5843 case DeclarationName::CXXDestructorName: {
5844 QualType T = Import(FromName.getCXXNameType());
5845 if (T.isNull())
5846 return DeclarationName();
5847
5848 return ToContext.DeclarationNames.getCXXDestructorName(
5849 ToContext.getCanonicalType(T));
5850 }
5851
5852 case DeclarationName::CXXConversionFunctionName: {
5853 QualType T = Import(FromName.getCXXNameType());
5854 if (T.isNull())
5855 return DeclarationName();
5856
5857 return ToContext.DeclarationNames.getCXXConversionFunctionName(
5858 ToContext.getCanonicalType(T));
5859 }
5860
5861 case DeclarationName::CXXOperatorName:
5862 return ToContext.DeclarationNames.getCXXOperatorName(
5863 FromName.getCXXOverloadedOperator());
5864
5865 case DeclarationName::CXXLiteralOperatorName:
5866 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
5867 Import(FromName.getCXXLiteralIdentifier()));
5868
5869 case DeclarationName::CXXUsingDirective:
5870 // FIXME: STATICS!
5871 return DeclarationName::getUsingDirectiveName();
5872 }
5873
David Blaikiee4d798f2012-01-20 21:50:17 +00005874 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00005875}
5876
Douglas Gregore2e50d332010-12-01 01:36:18 +00005877IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00005878 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00005879 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00005880
5881 return &ToContext.Idents.get(FromId->getName());
5882}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005883
Douglas Gregor43f54792010-02-17 02:12:47 +00005884Selector ASTImporter::Import(Selector FromSel) {
5885 if (FromSel.isNull())
5886 return Selector();
5887
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005888 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00005889 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
5890 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
5891 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
5892 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
5893}
5894
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005895DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
5896 DeclContext *DC,
5897 unsigned IDNS,
5898 NamedDecl **Decls,
5899 unsigned NumDecls) {
5900 return Name;
5901}
5902
5903DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005904 if (LastDiagFromFrom)
5905 ToContext.getDiagnostics().notePriorDiagnosticFrom(
5906 FromContext.getDiagnostics());
5907 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005908 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005909}
5910
5911DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005912 if (!LastDiagFromFrom)
5913 FromContext.getDiagnostics().notePriorDiagnosticFrom(
5914 ToContext.getDiagnostics());
5915 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005916 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00005917}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005918
Douglas Gregor2e15c842012-02-01 21:00:38 +00005919void ASTImporter::CompleteDecl (Decl *D) {
5920 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
5921 if (!ID->getDefinition())
5922 ID->startDefinition();
5923 }
5924 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
5925 if (!PD->getDefinition())
5926 PD->startDefinition();
5927 }
5928 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
5929 if (!TD->getDefinition() && !TD->isBeingDefined()) {
5930 TD->startDefinition();
5931 TD->setCompleteDefinition(true);
5932 }
5933 }
5934 else {
5935 assert (0 && "CompleteDecl called on a Decl that can't be completed");
5936 }
5937}
5938
Douglas Gregor8cdbe642010-02-12 23:44:20 +00005939Decl *ASTImporter::Imported(Decl *From, Decl *To) {
5940 ImportedDecls[From] = To;
5941 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00005942}
Douglas Gregorb4964f72010-02-15 23:54:17 +00005943
Douglas Gregordd6006f2012-07-17 21:16:27 +00005944bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
5945 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00005946 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00005947 = ImportedTypes.find(From.getTypePtr());
5948 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
5949 return true;
5950
Douglas Gregordd6006f2012-07-17 21:16:27 +00005951 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
5952 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00005953 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00005954}