blob: 8665d809ae4de09c6fa14642d5e6cd2581259102 [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;
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000032
Douglas Gregor96e578d2010-02-05 17:54:41 +000033 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);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000067 QualType VisitInjectedClassNameType(const InjectedClassNameType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000068 // FIXME: DependentDecltypeType
John McCall424cec92011-01-19 06:33:43 +000069 QualType VisitRecordType(const RecordType *T);
70 QualType VisitEnumType(const EnumType *T);
Sean Callanan72fe0852015-04-02 23:50:08 +000071 QualType VisitAttributedType(const AttributedType *T);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000072 QualType VisitTemplateTypeParmType(const TemplateTypeParmType *T);
Douglas Gregor96e578d2010-02-05 17:54:41 +000073 // FIXME: SubstTemplateTypeParmType
John McCall424cec92011-01-19 06:33:43 +000074 QualType VisitTemplateSpecializationType(const TemplateSpecializationType *T);
75 QualType VisitElaboratedType(const ElaboratedType *T);
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +000076 // FIXME: DependentNameType
John McCallc392f372010-06-11 00:33:02 +000077 // FIXME: DependentTemplateSpecializationType
John McCall424cec92011-01-19 06:33:43 +000078 QualType VisitObjCInterfaceType(const ObjCInterfaceType *T);
79 QualType VisitObjCObjectType(const ObjCObjectType *T);
80 QualType VisitObjCObjectPointerType(const ObjCObjectPointerType *T);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +000081
Douglas Gregor95d82832012-01-24 18:36:04 +000082 // Importing declarations
Douglas Gregorbb7930c2010-02-10 19:54:31 +000083 bool ImportDeclParts(NamedDecl *D, DeclContext *&DC,
84 DeclContext *&LexicalDC, DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +000085 NamedDecl *&ToD, SourceLocation &Loc);
Craig Topper36250ad2014-05-12 05:36:57 +000086 void ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD = nullptr);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +000087 void ImportDeclarationNameLoc(const DeclarationNameInfo &From,
88 DeclarationNameInfo& To);
Douglas Gregor0a791672011-01-18 03:11:38 +000089 void ImportDeclContext(DeclContext *FromDC, bool ForceImport = false);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +000090
91 typedef DesignatedInitExpr::Designator Designator;
92 Designator ImportDesignator(const Designator &D);
93
Douglas Gregor2e15c842012-02-01 21:00:38 +000094
Douglas Gregor95d82832012-01-24 18:36:04 +000095 /// \brief What we should import from the definition.
96 enum ImportDefinitionKind {
97 /// \brief Import the default subset of the definition, which might be
98 /// nothing (if minimal import is set) or might be everything (if minimal
99 /// import is not set).
100 IDK_Default,
101 /// \brief Import everything.
102 IDK_Everything,
103 /// \brief Import only the bare bones needed to establish a valid
104 /// DeclContext.
105 IDK_Basic
106 };
107
Douglas Gregor2e15c842012-02-01 21:00:38 +0000108 bool shouldForceImportDeclContext(ImportDefinitionKind IDK) {
109 return IDK == IDK_Everything ||
110 (IDK == IDK_Default && !Importer.isMinimalImport());
111 }
112
Douglas Gregord451ea92011-07-29 23:31:30 +0000113 bool ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +0000114 ImportDefinitionKind Kind = IDK_Default);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000115 bool ImportDefinition(VarDecl *From, VarDecl *To,
116 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregord451ea92011-07-29 23:31:30 +0000117 bool ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000118 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000119 bool ImportDefinition(ObjCInterfaceDecl *From, ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000120 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregor2aa53772012-01-24 17:42:07 +0000121 bool ImportDefinition(ObjCProtocolDecl *From, ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +0000122 ImportDefinitionKind Kind = IDK_Default);
Douglas Gregora082a492010-11-30 19:14:50 +0000123 TemplateParameterList *ImportTemplateParameterList(
124 TemplateParameterList *Params);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000125 TemplateArgument ImportTemplateArgument(const TemplateArgument &From);
126 bool ImportTemplateArguments(const TemplateArgument *FromArgs,
127 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000128 SmallVectorImpl<TemplateArgument> &ToArgs);
Douglas Gregordd6006f2012-07-17 21:16:27 +0000129 bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord,
130 bool Complain = true);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000131 bool IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
132 bool Complain = true);
Douglas Gregor3996e242010-02-15 22:01:00 +0000133 bool IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToRecord);
Douglas Gregor91155082012-11-14 22:29:20 +0000134 bool IsStructuralMatch(EnumConstantDecl *FromEC, EnumConstantDecl *ToEC);
Douglas Gregora082a492010-11-30 19:14:50 +0000135 bool IsStructuralMatch(ClassTemplateDecl *From, ClassTemplateDecl *To);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000136 bool IsStructuralMatch(VarTemplateDecl *From, VarTemplateDecl *To);
Douglas Gregore4c83e42010-02-09 22:48:33 +0000137 Decl *VisitDecl(Decl *D);
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +0000138 Decl *VisitAccessSpecDecl(AccessSpecDecl *D);
Sean Callanan65198272011-11-17 23:20:56 +0000139 Decl *VisitTranslationUnitDecl(TranslationUnitDecl *D);
Douglas Gregorf18a2c72010-02-21 18:26:36 +0000140 Decl *VisitNamespaceDecl(NamespaceDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000141 Decl *VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias);
Douglas Gregor5fa74c32010-02-10 21:10:29 +0000142 Decl *VisitTypedefDecl(TypedefDecl *D);
Richard Smithdda56e42011-04-15 14:24:37 +0000143 Decl *VisitTypeAliasDecl(TypeAliasDecl *D);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000144 Decl *VisitLabelDecl(LabelDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000145 Decl *VisitEnumDecl(EnumDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000146 Decl *VisitRecordDecl(RecordDecl *D);
Douglas Gregor98c10182010-02-12 22:17:39 +0000147 Decl *VisitEnumConstantDecl(EnumConstantDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000148 Decl *VisitFunctionDecl(FunctionDecl *D);
Douglas Gregor00eace12010-02-21 18:29:16 +0000149 Decl *VisitCXXMethodDecl(CXXMethodDecl *D);
150 Decl *VisitCXXConstructorDecl(CXXConstructorDecl *D);
151 Decl *VisitCXXDestructorDecl(CXXDestructorDecl *D);
152 Decl *VisitCXXConversionDecl(CXXConversionDecl *D);
Douglas Gregor5c73e912010-02-11 00:48:18 +0000153 Decl *VisitFieldDecl(FieldDecl *D);
Francois Pichet783dd6e2010-11-21 06:08:52 +0000154 Decl *VisitIndirectFieldDecl(IndirectFieldDecl *D);
Douglas Gregor7244b0b2010-02-17 00:34:30 +0000155 Decl *VisitObjCIvarDecl(ObjCIvarDecl *D);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +0000156 Decl *VisitVarDecl(VarDecl *D);
Douglas Gregor8b228d72010-02-17 21:22:52 +0000157 Decl *VisitImplicitParamDecl(ImplicitParamDecl *D);
Douglas Gregorbb7930c2010-02-10 19:54:31 +0000158 Decl *VisitParmVarDecl(ParmVarDecl *D);
Douglas Gregor43f54792010-02-17 02:12:47 +0000159 Decl *VisitObjCMethodDecl(ObjCMethodDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000160 Decl *VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
Douglas Gregor84c51c32010-02-18 01:47:50 +0000161 Decl *VisitObjCCategoryDecl(ObjCCategoryDecl *D);
Douglas Gregor98d156a2010-02-17 16:12:00 +0000162 Decl *VisitObjCProtocolDecl(ObjCProtocolDecl *D);
Sean Callanan0aae0412014-12-10 00:00:37 +0000163 Decl *VisitLinkageSpecDecl(LinkageSpecDecl *D);
Douglas Gregor85f3f952015-07-07 03:57:15 +0000164
165 ObjCTypeParamList *ImportObjCTypeParamList(ObjCTypeParamList *list);
Douglas Gregor45635322010-02-16 01:20:57 +0000166 Decl *VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
Douglas Gregor4da9d682010-12-07 15:32:12 +0000167 Decl *VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
Douglas Gregorda8025c2010-12-07 01:26:03 +0000168 Decl *VisitObjCImplementationDecl(ObjCImplementationDecl *D);
Douglas Gregora11c4582010-02-17 18:02:10 +0000169 Decl *VisitObjCPropertyDecl(ObjCPropertyDecl *D);
Douglas Gregor14a49e22010-12-07 18:32:03 +0000170 Decl *VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
Douglas Gregora082a492010-11-30 19:14:50 +0000171 Decl *VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
172 Decl *VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
173 Decl *VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
174 Decl *VisitClassTemplateDecl(ClassTemplateDecl *D);
Douglas Gregore2e50d332010-12-01 01:36:18 +0000175 Decl *VisitClassTemplateSpecializationDecl(
176 ClassTemplateSpecializationDecl *D);
Larisse Voufo39a1e502013-08-06 01:03:05 +0000177 Decl *VisitVarTemplateDecl(VarTemplateDecl *D);
178 Decl *VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
179
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000180 // Importing statements
Sean Callanan59721b32015-04-28 18:41:46 +0000181 DeclGroupRef ImportDeclGroup(DeclGroupRef DG);
182
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000183 Stmt *VisitStmt(Stmt *S);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000184 Stmt *VisitGCCAsmStmt(GCCAsmStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000185 Stmt *VisitDeclStmt(DeclStmt *S);
186 Stmt *VisitNullStmt(NullStmt *S);
187 Stmt *VisitCompoundStmt(CompoundStmt *S);
188 Stmt *VisitCaseStmt(CaseStmt *S);
189 Stmt *VisitDefaultStmt(DefaultStmt *S);
190 Stmt *VisitLabelStmt(LabelStmt *S);
191 Stmt *VisitAttributedStmt(AttributedStmt *S);
192 Stmt *VisitIfStmt(IfStmt *S);
193 Stmt *VisitSwitchStmt(SwitchStmt *S);
194 Stmt *VisitWhileStmt(WhileStmt *S);
195 Stmt *VisitDoStmt(DoStmt *S);
196 Stmt *VisitForStmt(ForStmt *S);
197 Stmt *VisitGotoStmt(GotoStmt *S);
198 Stmt *VisitIndirectGotoStmt(IndirectGotoStmt *S);
199 Stmt *VisitContinueStmt(ContinueStmt *S);
200 Stmt *VisitBreakStmt(BreakStmt *S);
201 Stmt *VisitReturnStmt(ReturnStmt *S);
Sean Callanan59721b32015-04-28 18:41:46 +0000202 // FIXME: MSAsmStmt
203 // FIXME: SEHExceptStmt
204 // FIXME: SEHFinallyStmt
205 // FIXME: SEHTryStmt
206 // FIXME: SEHLeaveStmt
207 // FIXME: CapturedStmt
208 Stmt *VisitCXXCatchStmt(CXXCatchStmt *S);
209 Stmt *VisitCXXTryStmt(CXXTryStmt *S);
210 Stmt *VisitCXXForRangeStmt(CXXForRangeStmt *S);
211 // FIXME: MSDependentExistsStmt
212 Stmt *VisitObjCForCollectionStmt(ObjCForCollectionStmt *S);
213 Stmt *VisitObjCAtCatchStmt(ObjCAtCatchStmt *S);
214 Stmt *VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S);
215 Stmt *VisitObjCAtTryStmt(ObjCAtTryStmt *S);
216 Stmt *VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S);
217 Stmt *VisitObjCAtThrowStmt(ObjCAtThrowStmt *S);
218 Stmt *VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000219
220 // Importing expressions
221 Expr *VisitExpr(Expr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000222 Expr *VisitVAArgExpr(VAArgExpr *E);
223 Expr *VisitGNUNullExpr(GNUNullExpr *E);
224 Expr *VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregor52f820e2010-02-19 01:17:02 +0000225 Expr *VisitDeclRefExpr(DeclRefExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000226 Expr *VisitImplicitValueInitExpr(ImplicitValueInitExpr *ILE);
227 Expr *VisitDesignatedInitExpr(DesignatedInitExpr *E);
228 Expr *VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
Douglas Gregor7eeb5972010-02-11 19:21:55 +0000229 Expr *VisitIntegerLiteral(IntegerLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000230 Expr *VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor623421d2010-02-18 02:21:22 +0000231 Expr *VisitCharacterLiteral(CharacterLiteral *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000232 Expr *VisitStringLiteral(StringLiteral *E);
233 Expr *VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
234 Expr *VisitAtomicExpr(AtomicExpr *E);
235 Expr *VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000236 Expr *VisitParenExpr(ParenExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000237 Expr *VisitParenListExpr(ParenListExpr *E);
238 Expr *VisitStmtExpr(StmtExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000239 Expr *VisitUnaryOperator(UnaryOperator *E);
Peter Collingbournee190dee2011-03-11 19:24:49 +0000240 Expr *VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000241 Expr *VisitBinaryOperator(BinaryOperator *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000242 Expr *VisitConditionalOperator(ConditionalOperator *E);
243 Expr *VisitBinaryConditionalOperator(BinaryConditionalOperator *E);
244 Expr *VisitOpaqueValueExpr(OpaqueValueExpr *E);
Douglas Gregorc74247e2010-02-19 01:07:06 +0000245 Expr *VisitCompoundAssignOperator(CompoundAssignOperator *E);
Douglas Gregor98c10182010-02-12 22:17:39 +0000246 Expr *VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregor5481d322010-02-19 01:32:14 +0000247 Expr *VisitCStyleCastExpr(CStyleCastExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000248 Expr *VisitCXXConstructExpr(CXXConstructExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000249 Expr *VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
250 Expr *VisitCXXThisExpr(CXXThisExpr *E);
251 Expr *VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
Sean Callanan59721b32015-04-28 18:41:46 +0000252 Expr *VisitMemberExpr(MemberExpr *E);
253 Expr *VisitCallExpr(CallExpr *E);
Sean Callanan8bca9962016-03-28 21:43:01 +0000254 Expr *VisitInitListExpr(InitListExpr *E);
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000255
256 template<typename IIter, typename OIter>
257 void ImportArray(IIter Ibegin, IIter Iend, OIter Obegin) {
258 typedef typename std::remove_reference<decltype(*Obegin)>::type ItemT;
259 ASTImporter &ImporterRef = Importer;
260 std::transform(Ibegin, Iend, Obegin,
261 [&ImporterRef](ItemT From) -> ItemT {
262 return ImporterRef.Import(From);
Sean Callanan8bca9962016-03-28 21:43:01 +0000263 });
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000264 }
265
266 template<typename IIter, typename OIter>
267 bool ImportArrayChecked(IIter Ibegin, IIter Iend, OIter Obegin) {
268 typedef typename std::remove_reference<decltype(**Obegin)>::type ItemT;
269 ASTImporter &ImporterRef = Importer;
270 bool Failed = false;
271 std::transform(Ibegin, Iend, Obegin,
272 [&ImporterRef, &Failed](ItemT *From) -> ItemT * {
273 ItemT *To = ImporterRef.Import(From);
274 if (!To && From)
275 Failed = true;
276 return To;
277 });
278 return Failed;
Sean Callanan8bca9962016-03-28 21:43:01 +0000279 }
Douglas Gregor96e578d2010-02-05 17:54:41 +0000280 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000281}
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +0000282
Douglas Gregor3c2404b2011-11-03 18:07:07 +0000283using namespace clang;
Douglas Gregor96e578d2010-02-05 17:54:41 +0000284
285//----------------------------------------------------------------------------
Douglas Gregor3996e242010-02-15 22:01:00 +0000286// Structural Equivalence
287//----------------------------------------------------------------------------
288
289namespace {
290 struct StructuralEquivalenceContext {
291 /// \brief AST contexts for which we are checking structural equivalence.
292 ASTContext &C1, &C2;
293
Douglas Gregor3996e242010-02-15 22:01:00 +0000294 /// \brief The set of "tentative" equivalences between two canonical
295 /// declarations, mapping from a declaration in the first context to the
296 /// declaration in the second context that we believe to be equivalent.
297 llvm::DenseMap<Decl *, Decl *> TentativeEquivalences;
298
299 /// \brief Queue of declarations in the first context whose equivalence
300 /// with a declaration in the second context still needs to be verified.
301 std::deque<Decl *> DeclsToCheck;
302
Douglas Gregorb4964f72010-02-15 23:54:17 +0000303 /// \brief Declaration (from, to) pairs that are known not to be equivalent
304 /// (which we have already complained about).
305 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls;
306
Douglas Gregor3996e242010-02-15 22:01:00 +0000307 /// \brief Whether we're being strict about the spelling of types when
308 /// unifying two types.
309 bool StrictTypeSpelling;
Douglas Gregordd6006f2012-07-17 21:16:27 +0000310
311 /// \brief Whether to complain about failures.
312 bool Complain;
313
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000314 /// \brief \c true if the last diagnostic came from C2.
315 bool LastDiagFromC2;
316
Douglas Gregor3996e242010-02-15 22:01:00 +0000317 StructuralEquivalenceContext(ASTContext &C1, ASTContext &C2,
Douglas Gregorb4964f72010-02-15 23:54:17 +0000318 llvm::DenseSet<std::pair<Decl *, Decl *> > &NonEquivalentDecls,
Douglas Gregordd6006f2012-07-17 21:16:27 +0000319 bool StrictTypeSpelling = false,
320 bool Complain = true)
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000321 : C1(C1), C2(C2), NonEquivalentDecls(NonEquivalentDecls),
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000322 StrictTypeSpelling(StrictTypeSpelling), Complain(Complain),
323 LastDiagFromC2(false) {}
Douglas Gregor3996e242010-02-15 22:01:00 +0000324
325 /// \brief Determine whether the two declarations are structurally
326 /// equivalent.
327 bool IsStructurallyEquivalent(Decl *D1, Decl *D2);
328
329 /// \brief Determine whether the two types are structurally equivalent.
330 bool IsStructurallyEquivalent(QualType T1, QualType T2);
331
332 private:
333 /// \brief Finish checking all of the structural equivalences.
334 ///
335 /// \returns true if an error occurred, false otherwise.
336 bool Finish();
337
338 public:
339 DiagnosticBuilder Diag1(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000340 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000341 if (LastDiagFromC2)
342 C1.getDiagnostics().notePriorDiagnosticFrom(C2.getDiagnostics());
343 LastDiagFromC2 = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000344 return C1.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000345 }
346
347 DiagnosticBuilder Diag2(SourceLocation Loc, unsigned DiagID) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000348 assert(Complain && "Not allowed to complain");
Richard Smith5bb4cdf2012-12-20 02:22:15 +0000349 if (!LastDiagFromC2)
350 C2.getDiagnostics().notePriorDiagnosticFrom(C1.getDiagnostics());
351 LastDiagFromC2 = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +0000352 return C2.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3996e242010-02-15 22:01:00 +0000353 }
354 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000355}
Douglas Gregor3996e242010-02-15 22:01:00 +0000356
357static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
358 QualType T1, QualType T2);
359static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
360 Decl *D1, Decl *D2);
361
Douglas Gregor3996e242010-02-15 22:01:00 +0000362/// \brief Determine structural equivalence of two expressions.
363static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
364 Expr *E1, Expr *E2) {
365 if (!E1 || !E2)
366 return E1 == E2;
367
368 // FIXME: Actually perform a structural comparison!
369 return true;
370}
371
372/// \brief Determine whether two identifiers are equivalent.
373static bool IsStructurallyEquivalent(const IdentifierInfo *Name1,
374 const IdentifierInfo *Name2) {
375 if (!Name1 || !Name2)
376 return Name1 == Name2;
377
378 return Name1->getName() == Name2->getName();
379}
380
381/// \brief Determine whether two nested-name-specifiers are equivalent.
382static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
383 NestedNameSpecifier *NNS1,
384 NestedNameSpecifier *NNS2) {
385 // FIXME: Implement!
386 return true;
387}
388
389/// \brief Determine whether two template arguments are equivalent.
390static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
391 const TemplateArgument &Arg1,
392 const TemplateArgument &Arg2) {
Douglas Gregore2e50d332010-12-01 01:36:18 +0000393 if (Arg1.getKind() != Arg2.getKind())
394 return false;
395
396 switch (Arg1.getKind()) {
397 case TemplateArgument::Null:
398 return true;
399
400 case TemplateArgument::Type:
401 return Context.IsStructurallyEquivalent(Arg1.getAsType(), Arg2.getAsType());
Eli Friedmanb826a002012-09-26 02:36:12 +0000402
Douglas Gregore2e50d332010-12-01 01:36:18 +0000403 case TemplateArgument::Integral:
404 if (!Context.IsStructurallyEquivalent(Arg1.getIntegralType(),
405 Arg2.getIntegralType()))
406 return false;
407
Eric Christopher6dcc3762012-07-15 00:23:57 +0000408 return llvm::APSInt::isSameValue(Arg1.getAsIntegral(), Arg2.getAsIntegral());
Douglas Gregore2e50d332010-12-01 01:36:18 +0000409
410 case TemplateArgument::Declaration:
411 return Context.IsStructurallyEquivalent(Arg1.getAsDecl(), Arg2.getAsDecl());
Eli Friedmanb826a002012-09-26 02:36:12 +0000412
413 case TemplateArgument::NullPtr:
414 return true; // FIXME: Is this correct?
415
Douglas Gregore2e50d332010-12-01 01:36:18 +0000416 case TemplateArgument::Template:
417 return IsStructurallyEquivalent(Context,
418 Arg1.getAsTemplate(),
419 Arg2.getAsTemplate());
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000420
421 case TemplateArgument::TemplateExpansion:
422 return IsStructurallyEquivalent(Context,
423 Arg1.getAsTemplateOrTemplatePattern(),
424 Arg2.getAsTemplateOrTemplatePattern());
425
Douglas Gregore2e50d332010-12-01 01:36:18 +0000426 case TemplateArgument::Expression:
427 return IsStructurallyEquivalent(Context,
428 Arg1.getAsExpr(), Arg2.getAsExpr());
429
430 case TemplateArgument::Pack:
431 if (Arg1.pack_size() != Arg2.pack_size())
432 return false;
433
434 for (unsigned I = 0, N = Arg1.pack_size(); I != N; ++I)
435 if (!IsStructurallyEquivalent(Context,
436 Arg1.pack_begin()[I],
437 Arg2.pack_begin()[I]))
438 return false;
439
440 return true;
441 }
442
443 llvm_unreachable("Invalid template argument kind");
Douglas Gregor3996e242010-02-15 22:01:00 +0000444}
445
446/// \brief Determine structural equivalence for the common part of array
447/// types.
448static bool IsArrayStructurallyEquivalent(StructuralEquivalenceContext &Context,
449 const ArrayType *Array1,
450 const ArrayType *Array2) {
451 if (!IsStructurallyEquivalent(Context,
452 Array1->getElementType(),
453 Array2->getElementType()))
454 return false;
455 if (Array1->getSizeModifier() != Array2->getSizeModifier())
456 return false;
457 if (Array1->getIndexTypeQualifiers() != Array2->getIndexTypeQualifiers())
458 return false;
459
460 return true;
461}
462
463/// \brief Determine structural equivalence of two types.
464static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
465 QualType T1, QualType T2) {
466 if (T1.isNull() || T2.isNull())
467 return T1.isNull() && T2.isNull();
468
469 if (!Context.StrictTypeSpelling) {
470 // We aren't being strict about token-to-token equivalence of types,
471 // so map down to the canonical type.
472 T1 = Context.C1.getCanonicalType(T1);
473 T2 = Context.C2.getCanonicalType(T2);
474 }
475
476 if (T1.getQualifiers() != T2.getQualifiers())
477 return false;
478
Douglas Gregorb4964f72010-02-15 23:54:17 +0000479 Type::TypeClass TC = T1->getTypeClass();
Douglas Gregor3996e242010-02-15 22:01:00 +0000480
Douglas Gregorb4964f72010-02-15 23:54:17 +0000481 if (T1->getTypeClass() != T2->getTypeClass()) {
482 // Compare function types with prototypes vs. without prototypes as if
483 // both did not have prototypes.
484 if (T1->getTypeClass() == Type::FunctionProto &&
485 T2->getTypeClass() == Type::FunctionNoProto)
486 TC = Type::FunctionNoProto;
487 else if (T1->getTypeClass() == Type::FunctionNoProto &&
488 T2->getTypeClass() == Type::FunctionProto)
489 TC = Type::FunctionNoProto;
490 else
491 return false;
492 }
493
494 switch (TC) {
495 case Type::Builtin:
Douglas Gregor3996e242010-02-15 22:01:00 +0000496 // FIXME: Deal with Char_S/Char_U.
497 if (cast<BuiltinType>(T1)->getKind() != cast<BuiltinType>(T2)->getKind())
498 return false;
499 break;
500
501 case Type::Complex:
502 if (!IsStructurallyEquivalent(Context,
503 cast<ComplexType>(T1)->getElementType(),
504 cast<ComplexType>(T2)->getElementType()))
505 return false;
506 break;
507
Reid Kleckner0503a872013-12-05 01:23:43 +0000508 case Type::Adjusted:
Reid Kleckner8a365022013-06-24 17:51:48 +0000509 case Type::Decayed:
510 if (!IsStructurallyEquivalent(Context,
Reid Kleckner0503a872013-12-05 01:23:43 +0000511 cast<AdjustedType>(T1)->getOriginalType(),
512 cast<AdjustedType>(T2)->getOriginalType()))
Reid Kleckner8a365022013-06-24 17:51:48 +0000513 return false;
514 break;
515
Douglas Gregor3996e242010-02-15 22:01:00 +0000516 case Type::Pointer:
517 if (!IsStructurallyEquivalent(Context,
518 cast<PointerType>(T1)->getPointeeType(),
519 cast<PointerType>(T2)->getPointeeType()))
520 return false;
521 break;
522
523 case Type::BlockPointer:
524 if (!IsStructurallyEquivalent(Context,
525 cast<BlockPointerType>(T1)->getPointeeType(),
526 cast<BlockPointerType>(T2)->getPointeeType()))
527 return false;
528 break;
529
530 case Type::LValueReference:
531 case Type::RValueReference: {
532 const ReferenceType *Ref1 = cast<ReferenceType>(T1);
533 const ReferenceType *Ref2 = cast<ReferenceType>(T2);
534 if (Ref1->isSpelledAsLValue() != Ref2->isSpelledAsLValue())
535 return false;
536 if (Ref1->isInnerRef() != Ref2->isInnerRef())
537 return false;
538 if (!IsStructurallyEquivalent(Context,
539 Ref1->getPointeeTypeAsWritten(),
540 Ref2->getPointeeTypeAsWritten()))
541 return false;
542 break;
543 }
544
545 case Type::MemberPointer: {
546 const MemberPointerType *MemPtr1 = cast<MemberPointerType>(T1);
547 const MemberPointerType *MemPtr2 = cast<MemberPointerType>(T2);
548 if (!IsStructurallyEquivalent(Context,
549 MemPtr1->getPointeeType(),
550 MemPtr2->getPointeeType()))
551 return false;
552 if (!IsStructurallyEquivalent(Context,
553 QualType(MemPtr1->getClass(), 0),
554 QualType(MemPtr2->getClass(), 0)))
555 return false;
556 break;
557 }
558
559 case Type::ConstantArray: {
560 const ConstantArrayType *Array1 = cast<ConstantArrayType>(T1);
561 const ConstantArrayType *Array2 = cast<ConstantArrayType>(T2);
Eric Christopher6dcc3762012-07-15 00:23:57 +0000562 if (!llvm::APInt::isSameValue(Array1->getSize(), Array2->getSize()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000563 return false;
564
565 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
566 return false;
567 break;
568 }
569
570 case Type::IncompleteArray:
571 if (!IsArrayStructurallyEquivalent(Context,
572 cast<ArrayType>(T1),
573 cast<ArrayType>(T2)))
574 return false;
575 break;
576
577 case Type::VariableArray: {
578 const VariableArrayType *Array1 = cast<VariableArrayType>(T1);
579 const VariableArrayType *Array2 = cast<VariableArrayType>(T2);
580 if (!IsStructurallyEquivalent(Context,
581 Array1->getSizeExpr(), Array2->getSizeExpr()))
582 return false;
583
584 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
585 return false;
586
587 break;
588 }
589
590 case Type::DependentSizedArray: {
591 const DependentSizedArrayType *Array1 = cast<DependentSizedArrayType>(T1);
592 const DependentSizedArrayType *Array2 = cast<DependentSizedArrayType>(T2);
593 if (!IsStructurallyEquivalent(Context,
594 Array1->getSizeExpr(), Array2->getSizeExpr()))
595 return false;
596
597 if (!IsArrayStructurallyEquivalent(Context, Array1, Array2))
598 return false;
599
600 break;
601 }
602
603 case Type::DependentSizedExtVector: {
604 const DependentSizedExtVectorType *Vec1
605 = cast<DependentSizedExtVectorType>(T1);
606 const DependentSizedExtVectorType *Vec2
607 = cast<DependentSizedExtVectorType>(T2);
608 if (!IsStructurallyEquivalent(Context,
609 Vec1->getSizeExpr(), Vec2->getSizeExpr()))
610 return false;
611 if (!IsStructurallyEquivalent(Context,
612 Vec1->getElementType(),
613 Vec2->getElementType()))
614 return false;
615 break;
616 }
617
618 case Type::Vector:
619 case Type::ExtVector: {
620 const VectorType *Vec1 = cast<VectorType>(T1);
621 const VectorType *Vec2 = cast<VectorType>(T2);
622 if (!IsStructurallyEquivalent(Context,
623 Vec1->getElementType(),
624 Vec2->getElementType()))
625 return false;
626 if (Vec1->getNumElements() != Vec2->getNumElements())
627 return false;
Bob Wilsonaeb56442010-11-10 21:56:12 +0000628 if (Vec1->getVectorKind() != Vec2->getVectorKind())
Douglas Gregor3996e242010-02-15 22:01:00 +0000629 return false;
Douglas Gregor01cc4372010-02-19 01:36:36 +0000630 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000631 }
632
633 case Type::FunctionProto: {
634 const FunctionProtoType *Proto1 = cast<FunctionProtoType>(T1);
635 const FunctionProtoType *Proto2 = cast<FunctionProtoType>(T2);
Alp Toker9cacbab2014-01-20 20:26:09 +0000636 if (Proto1->getNumParams() != Proto2->getNumParams())
Douglas Gregor3996e242010-02-15 22:01:00 +0000637 return false;
Alp Toker9cacbab2014-01-20 20:26:09 +0000638 for (unsigned I = 0, N = Proto1->getNumParams(); I != N; ++I) {
639 if (!IsStructurallyEquivalent(Context, Proto1->getParamType(I),
640 Proto2->getParamType(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000641 return false;
642 }
643 if (Proto1->isVariadic() != Proto2->isVariadic())
644 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000645 if (Proto1->getExceptionSpecType() != Proto2->getExceptionSpecType())
Douglas Gregor3996e242010-02-15 22:01:00 +0000646 return false;
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000647 if (Proto1->getExceptionSpecType() == EST_Dynamic) {
648 if (Proto1->getNumExceptions() != Proto2->getNumExceptions())
649 return false;
650 for (unsigned I = 0, N = Proto1->getNumExceptions(); I != N; ++I) {
651 if (!IsStructurallyEquivalent(Context,
652 Proto1->getExceptionType(I),
653 Proto2->getExceptionType(I)))
654 return false;
655 }
656 } else if (Proto1->getExceptionSpecType() == EST_ComputedNoexcept) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000657 if (!IsStructurallyEquivalent(Context,
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000658 Proto1->getNoexceptExpr(),
659 Proto2->getNoexceptExpr()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000660 return false;
661 }
662 if (Proto1->getTypeQuals() != Proto2->getTypeQuals())
663 return false;
664
665 // Fall through to check the bits common with FunctionNoProtoType.
666 }
667
668 case Type::FunctionNoProto: {
669 const FunctionType *Function1 = cast<FunctionType>(T1);
670 const FunctionType *Function2 = cast<FunctionType>(T2);
Alp Toker314cc812014-01-25 16:55:45 +0000671 if (!IsStructurallyEquivalent(Context, Function1->getReturnType(),
672 Function2->getReturnType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000673 return false;
Justin Bogner62c04de2016-03-20 16:58:03 +0000674 if (Function1->getExtInfo() != Function2->getExtInfo())
675 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000676 break;
677 }
678
679 case Type::UnresolvedUsing:
680 if (!IsStructurallyEquivalent(Context,
681 cast<UnresolvedUsingType>(T1)->getDecl(),
682 cast<UnresolvedUsingType>(T2)->getDecl()))
683 return false;
684
685 break;
John McCall81904512011-01-06 01:58:22 +0000686
687 case Type::Attributed:
688 if (!IsStructurallyEquivalent(Context,
689 cast<AttributedType>(T1)->getModifiedType(),
690 cast<AttributedType>(T2)->getModifiedType()))
691 return false;
692 if (!IsStructurallyEquivalent(Context,
693 cast<AttributedType>(T1)->getEquivalentType(),
694 cast<AttributedType>(T2)->getEquivalentType()))
695 return false;
696 break;
Douglas Gregor3996e242010-02-15 22:01:00 +0000697
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000698 case Type::Paren:
699 if (!IsStructurallyEquivalent(Context,
700 cast<ParenType>(T1)->getInnerType(),
701 cast<ParenType>(T2)->getInnerType()))
702 return false;
703 break;
704
Douglas Gregor3996e242010-02-15 22:01:00 +0000705 case Type::Typedef:
706 if (!IsStructurallyEquivalent(Context,
707 cast<TypedefType>(T1)->getDecl(),
708 cast<TypedefType>(T2)->getDecl()))
709 return false;
710 break;
711
712 case Type::TypeOfExpr:
713 if (!IsStructurallyEquivalent(Context,
714 cast<TypeOfExprType>(T1)->getUnderlyingExpr(),
715 cast<TypeOfExprType>(T2)->getUnderlyingExpr()))
716 return false;
717 break;
718
719 case Type::TypeOf:
720 if (!IsStructurallyEquivalent(Context,
721 cast<TypeOfType>(T1)->getUnderlyingType(),
722 cast<TypeOfType>(T2)->getUnderlyingType()))
723 return false;
724 break;
Alexis Hunte852b102011-05-24 22:41:36 +0000725
726 case Type::UnaryTransform:
727 if (!IsStructurallyEquivalent(Context,
728 cast<UnaryTransformType>(T1)->getUnderlyingType(),
729 cast<UnaryTransformType>(T1)->getUnderlyingType()))
730 return false;
731 break;
732
Douglas Gregor3996e242010-02-15 22:01:00 +0000733 case Type::Decltype:
734 if (!IsStructurallyEquivalent(Context,
735 cast<DecltypeType>(T1)->getUnderlyingExpr(),
736 cast<DecltypeType>(T2)->getUnderlyingExpr()))
737 return false;
738 break;
739
Richard Smith30482bc2011-02-20 03:19:35 +0000740 case Type::Auto:
741 if (!IsStructurallyEquivalent(Context,
742 cast<AutoType>(T1)->getDeducedType(),
743 cast<AutoType>(T2)->getDeducedType()))
744 return false;
745 break;
746
Douglas Gregor3996e242010-02-15 22:01:00 +0000747 case Type::Record:
748 case Type::Enum:
749 if (!IsStructurallyEquivalent(Context,
750 cast<TagType>(T1)->getDecl(),
751 cast<TagType>(T2)->getDecl()))
752 return false;
753 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +0000754
Douglas Gregor3996e242010-02-15 22:01:00 +0000755 case Type::TemplateTypeParm: {
756 const TemplateTypeParmType *Parm1 = cast<TemplateTypeParmType>(T1);
757 const TemplateTypeParmType *Parm2 = cast<TemplateTypeParmType>(T2);
758 if (Parm1->getDepth() != Parm2->getDepth())
759 return false;
760 if (Parm1->getIndex() != Parm2->getIndex())
761 return false;
762 if (Parm1->isParameterPack() != Parm2->isParameterPack())
763 return false;
764
765 // Names of template type parameters are never significant.
766 break;
767 }
768
769 case Type::SubstTemplateTypeParm: {
770 const SubstTemplateTypeParmType *Subst1
771 = cast<SubstTemplateTypeParmType>(T1);
772 const SubstTemplateTypeParmType *Subst2
773 = cast<SubstTemplateTypeParmType>(T2);
774 if (!IsStructurallyEquivalent(Context,
775 QualType(Subst1->getReplacedParameter(), 0),
776 QualType(Subst2->getReplacedParameter(), 0)))
777 return false;
778 if (!IsStructurallyEquivalent(Context,
779 Subst1->getReplacementType(),
780 Subst2->getReplacementType()))
781 return false;
782 break;
783 }
784
Douglas Gregorfb322d82011-01-14 05:11:40 +0000785 case Type::SubstTemplateTypeParmPack: {
786 const SubstTemplateTypeParmPackType *Subst1
787 = cast<SubstTemplateTypeParmPackType>(T1);
788 const SubstTemplateTypeParmPackType *Subst2
789 = cast<SubstTemplateTypeParmPackType>(T2);
790 if (!IsStructurallyEquivalent(Context,
791 QualType(Subst1->getReplacedParameter(), 0),
792 QualType(Subst2->getReplacedParameter(), 0)))
793 return false;
794 if (!IsStructurallyEquivalent(Context,
795 Subst1->getArgumentPack(),
796 Subst2->getArgumentPack()))
797 return false;
798 break;
799 }
Douglas Gregor3996e242010-02-15 22:01:00 +0000800 case Type::TemplateSpecialization: {
801 const TemplateSpecializationType *Spec1
802 = cast<TemplateSpecializationType>(T1);
803 const TemplateSpecializationType *Spec2
804 = cast<TemplateSpecializationType>(T2);
805 if (!IsStructurallyEquivalent(Context,
806 Spec1->getTemplateName(),
807 Spec2->getTemplateName()))
808 return false;
809 if (Spec1->getNumArgs() != Spec2->getNumArgs())
810 return false;
811 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
812 if (!IsStructurallyEquivalent(Context,
813 Spec1->getArg(I), Spec2->getArg(I)))
814 return false;
815 }
816 break;
817 }
818
Abramo Bagnara6150c882010-05-11 21:36:43 +0000819 case Type::Elaborated: {
820 const ElaboratedType *Elab1 = cast<ElaboratedType>(T1);
821 const ElaboratedType *Elab2 = cast<ElaboratedType>(T2);
822 // CHECKME: what if a keyword is ETK_None or ETK_typename ?
823 if (Elab1->getKeyword() != Elab2->getKeyword())
824 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000825 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000826 Elab1->getQualifier(),
827 Elab2->getQualifier()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000828 return false;
829 if (!IsStructurallyEquivalent(Context,
Abramo Bagnara6150c882010-05-11 21:36:43 +0000830 Elab1->getNamedType(),
831 Elab2->getNamedType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000832 return false;
833 break;
834 }
835
John McCalle78aac42010-03-10 03:28:59 +0000836 case Type::InjectedClassName: {
837 const InjectedClassNameType *Inj1 = cast<InjectedClassNameType>(T1);
838 const InjectedClassNameType *Inj2 = cast<InjectedClassNameType>(T2);
839 if (!IsStructurallyEquivalent(Context,
John McCall2408e322010-04-27 00:57:59 +0000840 Inj1->getInjectedSpecializationType(),
841 Inj2->getInjectedSpecializationType()))
John McCalle78aac42010-03-10 03:28:59 +0000842 return false;
843 break;
844 }
845
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000846 case Type::DependentName: {
847 const DependentNameType *Typename1 = cast<DependentNameType>(T1);
848 const DependentNameType *Typename2 = cast<DependentNameType>(T2);
Douglas Gregor3996e242010-02-15 22:01:00 +0000849 if (!IsStructurallyEquivalent(Context,
850 Typename1->getQualifier(),
851 Typename2->getQualifier()))
852 return false;
853 if (!IsStructurallyEquivalent(Typename1->getIdentifier(),
854 Typename2->getIdentifier()))
855 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000856
857 break;
858 }
859
John McCallc392f372010-06-11 00:33:02 +0000860 case Type::DependentTemplateSpecialization: {
861 const DependentTemplateSpecializationType *Spec1 =
862 cast<DependentTemplateSpecializationType>(T1);
863 const DependentTemplateSpecializationType *Spec2 =
864 cast<DependentTemplateSpecializationType>(T2);
865 if (!IsStructurallyEquivalent(Context,
866 Spec1->getQualifier(),
867 Spec2->getQualifier()))
868 return false;
869 if (!IsStructurallyEquivalent(Spec1->getIdentifier(),
870 Spec2->getIdentifier()))
871 return false;
872 if (Spec1->getNumArgs() != Spec2->getNumArgs())
873 return false;
874 for (unsigned I = 0, N = Spec1->getNumArgs(); I != N; ++I) {
875 if (!IsStructurallyEquivalent(Context,
876 Spec1->getArg(I), Spec2->getArg(I)))
877 return false;
878 }
879 break;
880 }
Douglas Gregord2fa7662010-12-20 02:24:11 +0000881
882 case Type::PackExpansion:
883 if (!IsStructurallyEquivalent(Context,
884 cast<PackExpansionType>(T1)->getPattern(),
885 cast<PackExpansionType>(T2)->getPattern()))
886 return false;
887 break;
888
Douglas Gregor3996e242010-02-15 22:01:00 +0000889 case Type::ObjCInterface: {
890 const ObjCInterfaceType *Iface1 = cast<ObjCInterfaceType>(T1);
891 const ObjCInterfaceType *Iface2 = cast<ObjCInterfaceType>(T2);
892 if (!IsStructurallyEquivalent(Context,
893 Iface1->getDecl(), Iface2->getDecl()))
894 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000895 break;
896 }
897
898 case Type::ObjCObject: {
899 const ObjCObjectType *Obj1 = cast<ObjCObjectType>(T1);
900 const ObjCObjectType *Obj2 = cast<ObjCObjectType>(T2);
901 if (!IsStructurallyEquivalent(Context,
902 Obj1->getBaseType(),
903 Obj2->getBaseType()))
Douglas Gregor3996e242010-02-15 22:01:00 +0000904 return false;
John McCall8b07ec22010-05-15 11:32:37 +0000905 if (Obj1->getNumProtocols() != Obj2->getNumProtocols())
906 return false;
907 for (unsigned I = 0, N = Obj1->getNumProtocols(); I != N; ++I) {
Douglas Gregor3996e242010-02-15 22:01:00 +0000908 if (!IsStructurallyEquivalent(Context,
John McCall8b07ec22010-05-15 11:32:37 +0000909 Obj1->getProtocol(I),
910 Obj2->getProtocol(I)))
Douglas Gregor3996e242010-02-15 22:01:00 +0000911 return false;
912 }
913 break;
914 }
915
916 case Type::ObjCObjectPointer: {
917 const ObjCObjectPointerType *Ptr1 = cast<ObjCObjectPointerType>(T1);
918 const ObjCObjectPointerType *Ptr2 = cast<ObjCObjectPointerType>(T2);
919 if (!IsStructurallyEquivalent(Context,
920 Ptr1->getPointeeType(),
921 Ptr2->getPointeeType()))
922 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +0000923 break;
924 }
Eli Friedman0dfb8892011-10-06 23:00:33 +0000925
926 case Type::Atomic: {
927 if (!IsStructurallyEquivalent(Context,
928 cast<AtomicType>(T1)->getValueType(),
929 cast<AtomicType>(T2)->getValueType()))
930 return false;
931 break;
932 }
933
Xiuli Pan9c14e282016-01-09 12:53:17 +0000934 case Type::Pipe: {
935 if (!IsStructurallyEquivalent(Context,
936 cast<PipeType>(T1)->getElementType(),
937 cast<PipeType>(T2)->getElementType()))
938 return false;
939 break;
940 }
941
Douglas Gregor3996e242010-02-15 22:01:00 +0000942 } // end switch
943
944 return true;
945}
946
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000947/// \brief Determine structural equivalence of two fields.
948static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
949 FieldDecl *Field1, FieldDecl *Field2) {
950 RecordDecl *Owner2 = cast<RecordDecl>(Field2->getDeclContext());
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000951
952 // For anonymous structs/unions, match up the anonymous struct/union type
953 // declarations directly, so that we don't go off searching for anonymous
954 // types
955 if (Field1->isAnonymousStructOrUnion() &&
956 Field2->isAnonymousStructOrUnion()) {
957 RecordDecl *D1 = Field1->getType()->castAs<RecordType>()->getDecl();
958 RecordDecl *D2 = Field2->getType()->castAs<RecordType>()->getDecl();
959 return IsStructurallyEquivalent(Context, D1, D2);
960 }
Sean Callanan969c5bd2013-04-26 22:49:25 +0000961
962 // Check for equivalent field names.
963 IdentifierInfo *Name1 = Field1->getIdentifier();
964 IdentifierInfo *Name2 = Field2->getIdentifier();
965 if (!::IsStructurallyEquivalent(Name1, Name2))
966 return false;
Douglas Gregorceb32bf2012-10-26 16:45:11 +0000967
968 if (!IsStructurallyEquivalent(Context,
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000969 Field1->getType(), Field2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000970 if (Context.Complain) {
971 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
972 << Context.C2.getTypeDeclType(Owner2);
973 Context.Diag2(Field2->getLocation(), diag::note_odr_field)
974 << Field2->getDeclName() << Field2->getType();
975 Context.Diag1(Field1->getLocation(), diag::note_odr_field)
976 << Field1->getDeclName() << Field1->getType();
977 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000978 return false;
979 }
980
981 if (Field1->isBitField() != Field2->isBitField()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +0000982 if (Context.Complain) {
983 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
984 << Context.C2.getTypeDeclType(Owner2);
985 if (Field1->isBitField()) {
986 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
987 << Field1->getDeclName() << Field1->getType()
988 << Field1->getBitWidthValue(Context.C1);
989 Context.Diag2(Field2->getLocation(), diag::note_odr_not_bit_field)
990 << Field2->getDeclName();
991 } else {
992 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
993 << Field2->getDeclName() << Field2->getType()
994 << Field2->getBitWidthValue(Context.C2);
995 Context.Diag1(Field1->getLocation(), diag::note_odr_not_bit_field)
996 << Field1->getDeclName();
997 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +0000998 }
999 return false;
1000 }
1001
1002 if (Field1->isBitField()) {
1003 // Make sure that the bit-fields are the same length.
1004 unsigned Bits1 = Field1->getBitWidthValue(Context.C1);
1005 unsigned Bits2 = Field2->getBitWidthValue(Context.C2);
1006
1007 if (Bits1 != Bits2) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001008 if (Context.Complain) {
1009 Context.Diag2(Owner2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1010 << Context.C2.getTypeDeclType(Owner2);
1011 Context.Diag2(Field2->getLocation(), diag::note_odr_bit_field)
1012 << Field2->getDeclName() << Field2->getType() << Bits2;
1013 Context.Diag1(Field1->getLocation(), diag::note_odr_bit_field)
1014 << Field1->getDeclName() << Field1->getType() << Bits1;
1015 }
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001016 return false;
1017 }
1018 }
1019
1020 return true;
1021}
1022
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001023/// \brief Find the index of the given anonymous struct/union within its
1024/// context.
1025///
1026/// \returns Returns the index of this anonymous struct/union in its context,
1027/// including the next assigned index (if none of them match). Returns an
1028/// empty option if the context is not a record, i.e.. if the anonymous
1029/// struct/union is at namespace or block scope.
David Blaikie05785d12013-02-20 22:23:23 +00001030static Optional<unsigned> findAnonymousStructOrUnionIndex(RecordDecl *Anon) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001031 ASTContext &Context = Anon->getASTContext();
1032 QualType AnonTy = Context.getRecordType(Anon);
1033
1034 RecordDecl *Owner = dyn_cast<RecordDecl>(Anon->getDeclContext());
1035 if (!Owner)
David Blaikie7a30dc52013-02-21 01:47:18 +00001036 return None;
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001037
1038 unsigned Index = 0;
Aaron Ballman629afae2014-03-07 19:56:05 +00001039 for (const auto *D : Owner->noload_decls()) {
1040 const auto *F = dyn_cast<FieldDecl>(D);
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001041 if (!F || !F->isAnonymousStructOrUnion())
1042 continue;
1043
1044 if (Context.hasSameType(F->getType(), AnonTy))
1045 break;
1046
1047 ++Index;
1048 }
1049
1050 return Index;
1051}
1052
Douglas Gregor3996e242010-02-15 22:01:00 +00001053/// \brief Determine structural equivalence of two records.
1054static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1055 RecordDecl *D1, RecordDecl *D2) {
1056 if (D1->isUnion() != D2->isUnion()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001057 if (Context.Complain) {
1058 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1059 << Context.C2.getTypeDeclType(D2);
1060 Context.Diag1(D1->getLocation(), diag::note_odr_tag_kind_here)
1061 << D1->getDeclName() << (unsigned)D1->getTagKind();
1062 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001063 return false;
1064 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001065
1066 if (D1->isAnonymousStructOrUnion() && D2->isAnonymousStructOrUnion()) {
1067 // If both anonymous structs/unions are in a record context, make sure
1068 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00001069 if (Optional<unsigned> Index1 = findAnonymousStructOrUnionIndex(D1)) {
1070 if (Optional<unsigned> Index2 = findAnonymousStructOrUnionIndex(D2)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00001071 if (*Index1 != *Index2)
1072 return false;
1073 }
1074 }
1075 }
1076
Douglas Gregore2e50d332010-12-01 01:36:18 +00001077 // If both declarations are class template specializations, we know
1078 // the ODR applies, so check the template and template arguments.
1079 ClassTemplateSpecializationDecl *Spec1
1080 = dyn_cast<ClassTemplateSpecializationDecl>(D1);
1081 ClassTemplateSpecializationDecl *Spec2
1082 = dyn_cast<ClassTemplateSpecializationDecl>(D2);
1083 if (Spec1 && Spec2) {
1084 // Check that the specialized templates are the same.
1085 if (!IsStructurallyEquivalent(Context, Spec1->getSpecializedTemplate(),
1086 Spec2->getSpecializedTemplate()))
1087 return false;
1088
1089 // Check that the template arguments are the same.
1090 if (Spec1->getTemplateArgs().size() != Spec2->getTemplateArgs().size())
1091 return false;
1092
1093 for (unsigned I = 0, N = Spec1->getTemplateArgs().size(); I != N; ++I)
1094 if (!IsStructurallyEquivalent(Context,
1095 Spec1->getTemplateArgs().get(I),
1096 Spec2->getTemplateArgs().get(I)))
1097 return false;
1098 }
1099 // If one is a class template specialization and the other is not, these
Chris Lattner57540c52011-04-15 05:22:18 +00001100 // structures are different.
Douglas Gregore2e50d332010-12-01 01:36:18 +00001101 else if (Spec1 || Spec2)
1102 return false;
1103
Douglas Gregorb4964f72010-02-15 23:54:17 +00001104 // Compare the definitions of these two records. If either or both are
1105 // incomplete, we assume that they are equivalent.
1106 D1 = D1->getDefinition();
1107 D2 = D2->getDefinition();
1108 if (!D1 || !D2)
1109 return true;
1110
Douglas Gregor3996e242010-02-15 22:01:00 +00001111 if (CXXRecordDecl *D1CXX = dyn_cast<CXXRecordDecl>(D1)) {
1112 if (CXXRecordDecl *D2CXX = dyn_cast<CXXRecordDecl>(D2)) {
1113 if (D1CXX->getNumBases() != D2CXX->getNumBases()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001114 if (Context.Complain) {
1115 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1116 << Context.C2.getTypeDeclType(D2);
1117 Context.Diag2(D2->getLocation(), diag::note_odr_number_of_bases)
1118 << D2CXX->getNumBases();
1119 Context.Diag1(D1->getLocation(), diag::note_odr_number_of_bases)
1120 << D1CXX->getNumBases();
1121 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001122 return false;
1123 }
1124
1125 // Check the base classes.
1126 for (CXXRecordDecl::base_class_iterator Base1 = D1CXX->bases_begin(),
1127 BaseEnd1 = D1CXX->bases_end(),
1128 Base2 = D2CXX->bases_begin();
1129 Base1 != BaseEnd1;
1130 ++Base1, ++Base2) {
1131 if (!IsStructurallyEquivalent(Context,
1132 Base1->getType(), Base2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001133 if (Context.Complain) {
1134 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1135 << Context.C2.getTypeDeclType(D2);
1136 Context.Diag2(Base2->getLocStart(), diag::note_odr_base)
1137 << Base2->getType()
1138 << Base2->getSourceRange();
1139 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1140 << Base1->getType()
1141 << Base1->getSourceRange();
1142 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001143 return false;
1144 }
1145
1146 // Check virtual vs. non-virtual inheritance mismatch.
1147 if (Base1->isVirtual() != Base2->isVirtual()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001148 if (Context.Complain) {
1149 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1150 << Context.C2.getTypeDeclType(D2);
1151 Context.Diag2(Base2->getLocStart(),
1152 diag::note_odr_virtual_base)
1153 << Base2->isVirtual() << Base2->getSourceRange();
1154 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1155 << Base1->isVirtual()
1156 << Base1->getSourceRange();
1157 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001158 return false;
1159 }
1160 }
1161 } else if (D1CXX->getNumBases() > 0) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001162 if (Context.Complain) {
1163 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1164 << Context.C2.getTypeDeclType(D2);
1165 const CXXBaseSpecifier *Base1 = D1CXX->bases_begin();
1166 Context.Diag1(Base1->getLocStart(), diag::note_odr_base)
1167 << Base1->getType()
1168 << Base1->getSourceRange();
1169 Context.Diag2(D2->getLocation(), diag::note_odr_missing_base);
1170 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001171 return false;
1172 }
1173 }
1174
1175 // Check the fields for consistency.
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001176 RecordDecl::field_iterator Field2 = D2->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001177 Field2End = D2->field_end();
Dmitri Gribenko898cff02012-05-19 17:17:26 +00001178 for (RecordDecl::field_iterator Field1 = D1->field_begin(),
Douglas Gregor3996e242010-02-15 22:01:00 +00001179 Field1End = D1->field_end();
1180 Field1 != Field1End;
1181 ++Field1, ++Field2) {
1182 if (Field2 == Field2End) {
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.Diag1(Field1->getLocation(), diag::note_odr_field)
1187 << Field1->getDeclName() << Field1->getType();
1188 Context.Diag2(D2->getLocation(), diag::note_odr_missing_field);
1189 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001190 return false;
1191 }
1192
David Blaikie40ed2972012-06-06 20:45:41 +00001193 if (!IsStructurallyEquivalent(Context, *Field1, *Field2))
Douglas Gregor03d1ed32011-10-14 21:54:42 +00001194 return false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001195 }
1196
1197 if (Field2 != Field2End) {
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(Field2->getLocation(), diag::note_odr_field)
1202 << Field2->getDeclName() << Field2->getType();
1203 Context.Diag1(D1->getLocation(), diag::note_odr_missing_field);
1204 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001205 return false;
1206 }
1207
1208 return true;
1209}
1210
1211/// \brief Determine structural equivalence of two enums.
1212static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1213 EnumDecl *D1, EnumDecl *D2) {
1214 EnumDecl::enumerator_iterator EC2 = D2->enumerator_begin(),
1215 EC2End = D2->enumerator_end();
1216 for (EnumDecl::enumerator_iterator EC1 = D1->enumerator_begin(),
1217 EC1End = D1->enumerator_end();
1218 EC1 != EC1End; ++EC1, ++EC2) {
1219 if (EC2 == EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001220 if (Context.Complain) {
1221 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1222 << Context.C2.getTypeDeclType(D2);
1223 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1224 << EC1->getDeclName()
1225 << EC1->getInitVal().toString(10);
1226 Context.Diag2(D2->getLocation(), diag::note_odr_missing_enumerator);
1227 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001228 return false;
1229 }
1230
1231 llvm::APSInt Val1 = EC1->getInitVal();
1232 llvm::APSInt Val2 = EC2->getInitVal();
Eric Christopher6dcc3762012-07-15 00:23:57 +00001233 if (!llvm::APSInt::isSameValue(Val1, Val2) ||
Douglas Gregor3996e242010-02-15 22:01:00 +00001234 !IsStructurallyEquivalent(EC1->getIdentifier(), EC2->getIdentifier())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001235 if (Context.Complain) {
1236 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1237 << Context.C2.getTypeDeclType(D2);
1238 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1239 << EC2->getDeclName()
1240 << EC2->getInitVal().toString(10);
1241 Context.Diag1(EC1->getLocation(), diag::note_odr_enumerator)
1242 << EC1->getDeclName()
1243 << EC1->getInitVal().toString(10);
1244 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001245 return false;
1246 }
1247 }
1248
1249 if (EC2 != EC2End) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001250 if (Context.Complain) {
1251 Context.Diag2(D2->getLocation(), diag::warn_odr_tag_type_inconsistent)
1252 << Context.C2.getTypeDeclType(D2);
1253 Context.Diag2(EC2->getLocation(), diag::note_odr_enumerator)
1254 << EC2->getDeclName()
1255 << EC2->getInitVal().toString(10);
1256 Context.Diag1(D1->getLocation(), diag::note_odr_missing_enumerator);
1257 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001258 return false;
1259 }
1260
1261 return true;
1262}
Douglas Gregora082a492010-11-30 19:14:50 +00001263
1264static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1265 TemplateParameterList *Params1,
1266 TemplateParameterList *Params2) {
1267 if (Params1->size() != Params2->size()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001268 if (Context.Complain) {
1269 Context.Diag2(Params2->getTemplateLoc(),
1270 diag::err_odr_different_num_template_parameters)
1271 << Params1->size() << Params2->size();
1272 Context.Diag1(Params1->getTemplateLoc(),
1273 diag::note_odr_template_parameter_list);
1274 }
Douglas Gregora082a492010-11-30 19:14:50 +00001275 return false;
1276 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001277
Douglas Gregora082a492010-11-30 19:14:50 +00001278 for (unsigned I = 0, N = Params1->size(); I != N; ++I) {
1279 if (Params1->getParam(I)->getKind() != Params2->getParam(I)->getKind()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001280 if (Context.Complain) {
1281 Context.Diag2(Params2->getParam(I)->getLocation(),
1282 diag::err_odr_different_template_parameter_kind);
1283 Context.Diag1(Params1->getParam(I)->getLocation(),
1284 diag::note_odr_template_parameter_here);
1285 }
Douglas Gregora082a492010-11-30 19:14:50 +00001286 return false;
1287 }
1288
1289 if (!Context.IsStructurallyEquivalent(Params1->getParam(I),
1290 Params2->getParam(I))) {
1291
1292 return false;
1293 }
1294 }
1295
1296 return true;
1297}
1298
1299static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1300 TemplateTypeParmDecl *D1,
1301 TemplateTypeParmDecl *D2) {
1302 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001303 if (Context.Complain) {
1304 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1305 << D2->isParameterPack();
1306 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1307 << D1->isParameterPack();
1308 }
Douglas Gregora082a492010-11-30 19:14:50 +00001309 return false;
1310 }
1311
1312 return true;
1313}
1314
1315static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1316 NonTypeTemplateParmDecl *D1,
1317 NonTypeTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001318 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001319 if (Context.Complain) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001320 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1321 << D2->isParameterPack();
1322 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1323 << D1->isParameterPack();
1324 }
Douglas Gregora082a492010-11-30 19:14:50 +00001325 return false;
1326 }
Douglas Gregora082a492010-11-30 19:14:50 +00001327
1328 // Check types.
1329 if (!Context.IsStructurallyEquivalent(D1->getType(), D2->getType())) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001330 if (Context.Complain) {
1331 Context.Diag2(D2->getLocation(),
1332 diag::err_odr_non_type_parameter_type_inconsistent)
1333 << D2->getType() << D1->getType();
1334 Context.Diag1(D1->getLocation(), diag::note_odr_value_here)
1335 << D1->getType();
1336 }
Douglas Gregora082a492010-11-30 19:14:50 +00001337 return false;
1338 }
1339
1340 return true;
1341}
1342
1343static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1344 TemplateTemplateParmDecl *D1,
1345 TemplateTemplateParmDecl *D2) {
Douglas Gregora082a492010-11-30 19:14:50 +00001346 if (D1->isParameterPack() != D2->isParameterPack()) {
Douglas Gregor069bbaf2012-10-26 15:34:11 +00001347 if (Context.Complain) {
1348 Context.Diag2(D2->getLocation(), diag::err_odr_parameter_pack_non_pack)
1349 << D2->isParameterPack();
1350 Context.Diag1(D1->getLocation(), diag::note_odr_parameter_pack_non_pack)
1351 << D1->isParameterPack();
1352 }
Douglas Gregora082a492010-11-30 19:14:50 +00001353 return false;
1354 }
Douglas Gregor3c7380b2012-10-26 15:36:15 +00001355
Douglas Gregora082a492010-11-30 19:14:50 +00001356 // Check template parameter lists.
1357 return IsStructurallyEquivalent(Context, D1->getTemplateParameters(),
1358 D2->getTemplateParameters());
1359}
1360
1361static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1362 ClassTemplateDecl *D1,
1363 ClassTemplateDecl *D2) {
1364 // Check template parameters.
1365 if (!IsStructurallyEquivalent(Context,
1366 D1->getTemplateParameters(),
1367 D2->getTemplateParameters()))
1368 return false;
1369
1370 // Check the templated declaration.
1371 return Context.IsStructurallyEquivalent(D1->getTemplatedDecl(),
1372 D2->getTemplatedDecl());
1373}
1374
Douglas Gregor3996e242010-02-15 22:01:00 +00001375/// \brief Determine structural equivalence of two declarations.
1376static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
1377 Decl *D1, Decl *D2) {
1378 // FIXME: Check for known structural equivalences via a callback of some sort.
1379
Douglas Gregorb4964f72010-02-15 23:54:17 +00001380 // Check whether we already know that these two declarations are not
1381 // structurally equivalent.
1382 if (Context.NonEquivalentDecls.count(std::make_pair(D1->getCanonicalDecl(),
1383 D2->getCanonicalDecl())))
1384 return false;
1385
Douglas Gregor3996e242010-02-15 22:01:00 +00001386 // Determine whether we've already produced a tentative equivalence for D1.
1387 Decl *&EquivToD1 = Context.TentativeEquivalences[D1->getCanonicalDecl()];
1388 if (EquivToD1)
1389 return EquivToD1 == D2->getCanonicalDecl();
1390
1391 // Produce a tentative equivalence D1 <-> D2, which will be checked later.
1392 EquivToD1 = D2->getCanonicalDecl();
1393 Context.DeclsToCheck.push_back(D1->getCanonicalDecl());
1394 return true;
1395}
1396
1397bool StructuralEquivalenceContext::IsStructurallyEquivalent(Decl *D1,
1398 Decl *D2) {
1399 if (!::IsStructurallyEquivalent(*this, D1, D2))
1400 return false;
1401
1402 return !Finish();
1403}
1404
1405bool StructuralEquivalenceContext::IsStructurallyEquivalent(QualType T1,
1406 QualType T2) {
1407 if (!::IsStructurallyEquivalent(*this, T1, T2))
1408 return false;
1409
1410 return !Finish();
1411}
1412
1413bool StructuralEquivalenceContext::Finish() {
1414 while (!DeclsToCheck.empty()) {
1415 // Check the next declaration.
1416 Decl *D1 = DeclsToCheck.front();
1417 DeclsToCheck.pop_front();
1418
1419 Decl *D2 = TentativeEquivalences[D1];
1420 assert(D2 && "Unrecorded tentative equivalence?");
1421
Douglas Gregorb4964f72010-02-15 23:54:17 +00001422 bool Equivalent = true;
1423
Douglas Gregor3996e242010-02-15 22:01:00 +00001424 // FIXME: Switch on all declaration kinds. For now, we're just going to
1425 // check the obvious ones.
1426 if (RecordDecl *Record1 = dyn_cast<RecordDecl>(D1)) {
1427 if (RecordDecl *Record2 = dyn_cast<RecordDecl>(D2)) {
1428 // Check for equivalent structure names.
1429 IdentifierInfo *Name1 = Record1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001430 if (!Name1 && Record1->getTypedefNameForAnonDecl())
1431 Name1 = Record1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001432 IdentifierInfo *Name2 = Record2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001433 if (!Name2 && Record2->getTypedefNameForAnonDecl())
1434 Name2 = Record2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001435 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1436 !::IsStructurallyEquivalent(*this, Record1, Record2))
1437 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001438 } else {
1439 // Record/non-record mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001440 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001441 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00001442 } else if (EnumDecl *Enum1 = dyn_cast<EnumDecl>(D1)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001443 if (EnumDecl *Enum2 = dyn_cast<EnumDecl>(D2)) {
1444 // Check for equivalent enum names.
1445 IdentifierInfo *Name1 = Enum1->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001446 if (!Name1 && Enum1->getTypedefNameForAnonDecl())
1447 Name1 = Enum1->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregor3996e242010-02-15 22:01:00 +00001448 IdentifierInfo *Name2 = Enum2->getIdentifier();
Richard Smithdda56e42011-04-15 14:24:37 +00001449 if (!Name2 && Enum2->getTypedefNameForAnonDecl())
1450 Name2 = Enum2->getTypedefNameForAnonDecl()->getIdentifier();
Douglas Gregorb4964f72010-02-15 23:54:17 +00001451 if (!::IsStructurallyEquivalent(Name1, Name2) ||
1452 !::IsStructurallyEquivalent(*this, Enum1, Enum2))
1453 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001454 } else {
1455 // Enum/non-enum mismatch
Douglas Gregorb4964f72010-02-15 23:54:17 +00001456 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001457 }
Richard Smithdda56e42011-04-15 14:24:37 +00001458 } else if (TypedefNameDecl *Typedef1 = dyn_cast<TypedefNameDecl>(D1)) {
1459 if (TypedefNameDecl *Typedef2 = dyn_cast<TypedefNameDecl>(D2)) {
Douglas Gregor3996e242010-02-15 22:01:00 +00001460 if (!::IsStructurallyEquivalent(Typedef1->getIdentifier(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00001461 Typedef2->getIdentifier()) ||
1462 !::IsStructurallyEquivalent(*this,
Douglas Gregor3996e242010-02-15 22:01:00 +00001463 Typedef1->getUnderlyingType(),
1464 Typedef2->getUnderlyingType()))
Douglas Gregorb4964f72010-02-15 23:54:17 +00001465 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001466 } else {
1467 // Typedef/non-typedef mismatch.
Douglas Gregorb4964f72010-02-15 23:54:17 +00001468 Equivalent = false;
Douglas Gregor3996e242010-02-15 22:01:00 +00001469 }
Douglas Gregora082a492010-11-30 19:14:50 +00001470 } else if (ClassTemplateDecl *ClassTemplate1
1471 = dyn_cast<ClassTemplateDecl>(D1)) {
1472 if (ClassTemplateDecl *ClassTemplate2 = dyn_cast<ClassTemplateDecl>(D2)) {
1473 if (!::IsStructurallyEquivalent(ClassTemplate1->getIdentifier(),
1474 ClassTemplate2->getIdentifier()) ||
1475 !::IsStructurallyEquivalent(*this, ClassTemplate1, ClassTemplate2))
1476 Equivalent = false;
1477 } else {
1478 // Class template/non-class-template mismatch.
1479 Equivalent = false;
1480 }
1481 } else if (TemplateTypeParmDecl *TTP1= dyn_cast<TemplateTypeParmDecl>(D1)) {
1482 if (TemplateTypeParmDecl *TTP2 = dyn_cast<TemplateTypeParmDecl>(D2)) {
1483 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1484 Equivalent = false;
1485 } else {
1486 // Kind mismatch.
1487 Equivalent = false;
1488 }
1489 } else if (NonTypeTemplateParmDecl *NTTP1
1490 = dyn_cast<NonTypeTemplateParmDecl>(D1)) {
1491 if (NonTypeTemplateParmDecl *NTTP2
1492 = dyn_cast<NonTypeTemplateParmDecl>(D2)) {
1493 if (!::IsStructurallyEquivalent(*this, NTTP1, NTTP2))
1494 Equivalent = false;
1495 } else {
1496 // Kind mismatch.
1497 Equivalent = false;
1498 }
1499 } else if (TemplateTemplateParmDecl *TTP1
1500 = dyn_cast<TemplateTemplateParmDecl>(D1)) {
1501 if (TemplateTemplateParmDecl *TTP2
1502 = dyn_cast<TemplateTemplateParmDecl>(D2)) {
1503 if (!::IsStructurallyEquivalent(*this, TTP1, TTP2))
1504 Equivalent = false;
1505 } else {
1506 // Kind mismatch.
1507 Equivalent = false;
1508 }
1509 }
1510
Douglas Gregorb4964f72010-02-15 23:54:17 +00001511 if (!Equivalent) {
1512 // Note that these two declarations are not equivalent (and we already
1513 // know about it).
1514 NonEquivalentDecls.insert(std::make_pair(D1->getCanonicalDecl(),
1515 D2->getCanonicalDecl()));
1516 return true;
1517 }
Douglas Gregor3996e242010-02-15 22:01:00 +00001518 // FIXME: Check other declaration kinds!
1519 }
1520
1521 return false;
1522}
1523
1524//----------------------------------------------------------------------------
Douglas Gregor96e578d2010-02-05 17:54:41 +00001525// Import Types
1526//----------------------------------------------------------------------------
1527
John McCall424cec92011-01-19 06:33:43 +00001528QualType ASTNodeImporter::VisitType(const Type *T) {
Douglas Gregore4c83e42010-02-09 22:48:33 +00001529 Importer.FromDiag(SourceLocation(), diag::err_unsupported_ast_node)
1530 << T->getTypeClassName();
1531 return QualType();
1532}
1533
John McCall424cec92011-01-19 06:33:43 +00001534QualType ASTNodeImporter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001535 switch (T->getKind()) {
Alexey Bader954ba212016-04-08 13:40:33 +00001536#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
1537 case BuiltinType::Id: \
1538 return Importer.getToContext().SingletonId;
Alexey Baderb62f1442016-04-13 08:33:41 +00001539#include "clang/Basic/OpenCLImageTypes.def"
John McCalle314e272011-10-18 21:02:43 +00001540#define SHARED_SINGLETON_TYPE(Expansion)
1541#define BUILTIN_TYPE(Id, SingletonId) \
1542 case BuiltinType::Id: return Importer.getToContext().SingletonId;
1543#include "clang/AST/BuiltinTypes.def"
1544
1545 // FIXME: for Char16, Char32, and NullPtr, make sure that the "to"
1546 // context supports C++.
1547
1548 // FIXME: for ObjCId, ObjCClass, and ObjCSel, make sure that the "to"
1549 // context supports ObjC.
1550
Douglas Gregor96e578d2010-02-05 17:54:41 +00001551 case BuiltinType::Char_U:
1552 // The context we're importing from has an unsigned 'char'. If we're
1553 // importing into a context with a signed 'char', translate to
1554 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001555 if (Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001556 return Importer.getToContext().UnsignedCharTy;
1557
1558 return Importer.getToContext().CharTy;
1559
Douglas Gregor96e578d2010-02-05 17:54:41 +00001560 case BuiltinType::Char_S:
1561 // The context we're importing from has an unsigned 'char'. If we're
1562 // importing into a context with a signed 'char', translate to
1563 // 'unsigned char' instead.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001564 if (!Importer.getToContext().getLangOpts().CharIsSigned)
Douglas Gregor96e578d2010-02-05 17:54:41 +00001565 return Importer.getToContext().SignedCharTy;
1566
1567 return Importer.getToContext().CharTy;
1568
Chris Lattnerad3467e2010-12-25 23:25:43 +00001569 case BuiltinType::WChar_S:
1570 case BuiltinType::WChar_U:
Douglas Gregor96e578d2010-02-05 17:54:41 +00001571 // FIXME: If not in C++, shall we translate to the C equivalent of
1572 // wchar_t?
1573 return Importer.getToContext().WCharTy;
Douglas Gregor96e578d2010-02-05 17:54:41 +00001574 }
David Blaikiee4d798f2012-01-20 21:50:17 +00001575
1576 llvm_unreachable("Invalid BuiltinType Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00001577}
1578
John McCall424cec92011-01-19 06:33:43 +00001579QualType ASTNodeImporter::VisitComplexType(const ComplexType *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().getComplexType(ToElementType);
1585}
1586
John McCall424cec92011-01-19 06:33:43 +00001587QualType ASTNodeImporter::VisitPointerType(const PointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001588 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1589 if (ToPointeeType.isNull())
1590 return QualType();
1591
1592 return Importer.getToContext().getPointerType(ToPointeeType);
1593}
1594
John McCall424cec92011-01-19 06:33:43 +00001595QualType ASTNodeImporter::VisitBlockPointerType(const BlockPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001596 // FIXME: Check for blocks support in "to" context.
1597 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1598 if (ToPointeeType.isNull())
1599 return QualType();
1600
1601 return Importer.getToContext().getBlockPointerType(ToPointeeType);
1602}
1603
John McCall424cec92011-01-19 06:33:43 +00001604QualType
1605ASTNodeImporter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001606 // FIXME: Check for C++ support in "to" context.
1607 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1608 if (ToPointeeType.isNull())
1609 return QualType();
1610
1611 return Importer.getToContext().getLValueReferenceType(ToPointeeType);
1612}
1613
John McCall424cec92011-01-19 06:33:43 +00001614QualType
1615ASTNodeImporter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001616 // FIXME: Check for C++0x support in "to" context.
1617 QualType ToPointeeType = Importer.Import(T->getPointeeTypeAsWritten());
1618 if (ToPointeeType.isNull())
1619 return QualType();
1620
1621 return Importer.getToContext().getRValueReferenceType(ToPointeeType);
1622}
1623
John McCall424cec92011-01-19 06:33:43 +00001624QualType ASTNodeImporter::VisitMemberPointerType(const MemberPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001625 // FIXME: Check for C++ support in "to" context.
1626 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1627 if (ToPointeeType.isNull())
1628 return QualType();
1629
1630 QualType ClassType = Importer.Import(QualType(T->getClass(), 0));
1631 return Importer.getToContext().getMemberPointerType(ToPointeeType,
1632 ClassType.getTypePtr());
1633}
1634
John McCall424cec92011-01-19 06:33:43 +00001635QualType ASTNodeImporter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001636 QualType ToElementType = Importer.Import(T->getElementType());
1637 if (ToElementType.isNull())
1638 return QualType();
1639
1640 return Importer.getToContext().getConstantArrayType(ToElementType,
1641 T->getSize(),
1642 T->getSizeModifier(),
1643 T->getIndexTypeCVRQualifiers());
1644}
1645
John McCall424cec92011-01-19 06:33:43 +00001646QualType
1647ASTNodeImporter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001648 QualType ToElementType = Importer.Import(T->getElementType());
1649 if (ToElementType.isNull())
1650 return QualType();
1651
1652 return Importer.getToContext().getIncompleteArrayType(ToElementType,
1653 T->getSizeModifier(),
1654 T->getIndexTypeCVRQualifiers());
1655}
1656
John McCall424cec92011-01-19 06:33:43 +00001657QualType ASTNodeImporter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001658 QualType ToElementType = Importer.Import(T->getElementType());
1659 if (ToElementType.isNull())
1660 return QualType();
1661
1662 Expr *Size = Importer.Import(T->getSizeExpr());
1663 if (!Size)
1664 return QualType();
1665
1666 SourceRange Brackets = Importer.Import(T->getBracketsRange());
1667 return Importer.getToContext().getVariableArrayType(ToElementType, Size,
1668 T->getSizeModifier(),
1669 T->getIndexTypeCVRQualifiers(),
1670 Brackets);
1671}
1672
John McCall424cec92011-01-19 06:33:43 +00001673QualType ASTNodeImporter::VisitVectorType(const VectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001674 QualType ToElementType = Importer.Import(T->getElementType());
1675 if (ToElementType.isNull())
1676 return QualType();
1677
1678 return Importer.getToContext().getVectorType(ToElementType,
1679 T->getNumElements(),
Bob Wilsonaeb56442010-11-10 21:56:12 +00001680 T->getVectorKind());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001681}
1682
John McCall424cec92011-01-19 06:33:43 +00001683QualType ASTNodeImporter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001684 QualType ToElementType = Importer.Import(T->getElementType());
1685 if (ToElementType.isNull())
1686 return QualType();
1687
1688 return Importer.getToContext().getExtVectorType(ToElementType,
1689 T->getNumElements());
1690}
1691
John McCall424cec92011-01-19 06:33:43 +00001692QualType
1693ASTNodeImporter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001694 // FIXME: What happens if we're importing a function without a prototype
1695 // into C++? Should we make it variadic?
Alp Toker314cc812014-01-25 16:55:45 +00001696 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001697 if (ToResultType.isNull())
1698 return QualType();
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001699
Douglas Gregor96e578d2010-02-05 17:54:41 +00001700 return Importer.getToContext().getFunctionNoProtoType(ToResultType,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001701 T->getExtInfo());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001702}
1703
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00001704QualType ASTNodeImporter::VisitFunctionProtoType(const FunctionProtoType *T) {
Alp Toker314cc812014-01-25 16:55:45 +00001705 QualType ToResultType = Importer.Import(T->getReturnType());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001706 if (ToResultType.isNull())
1707 return QualType();
1708
1709 // Import argument types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001710 SmallVector<QualType, 4> ArgTypes;
Aaron Ballman40bd0aa2014-03-17 15:23:01 +00001711 for (const auto &A : T->param_types()) {
1712 QualType ArgType = Importer.Import(A);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001713 if (ArgType.isNull())
1714 return QualType();
1715 ArgTypes.push_back(ArgType);
1716 }
1717
1718 // Import exception types
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001719 SmallVector<QualType, 4> ExceptionTypes;
Aaron Ballmanb088fbe2014-03-17 15:38:09 +00001720 for (const auto &E : T->exceptions()) {
1721 QualType ExceptionType = Importer.Import(E);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001722 if (ExceptionType.isNull())
1723 return QualType();
1724 ExceptionTypes.push_back(ExceptionType);
1725 }
John McCalldb40c7f2010-12-14 08:05:40 +00001726
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001727 FunctionProtoType::ExtProtoInfo FromEPI = T->getExtProtoInfo();
1728 FunctionProtoType::ExtProtoInfo ToEPI;
1729
1730 ToEPI.ExtInfo = FromEPI.ExtInfo;
1731 ToEPI.Variadic = FromEPI.Variadic;
1732 ToEPI.HasTrailingReturn = FromEPI.HasTrailingReturn;
1733 ToEPI.TypeQuals = FromEPI.TypeQuals;
1734 ToEPI.RefQualifier = FromEPI.RefQualifier;
Richard Smith8acb4282014-07-31 21:57:55 +00001735 ToEPI.ExceptionSpec.Type = FromEPI.ExceptionSpec.Type;
1736 ToEPI.ExceptionSpec.Exceptions = ExceptionTypes;
1737 ToEPI.ExceptionSpec.NoexceptExpr =
1738 Importer.Import(FromEPI.ExceptionSpec.NoexceptExpr);
1739 ToEPI.ExceptionSpec.SourceDecl = cast_or_null<FunctionDecl>(
1740 Importer.Import(FromEPI.ExceptionSpec.SourceDecl));
1741 ToEPI.ExceptionSpec.SourceTemplate = cast_or_null<FunctionDecl>(
1742 Importer.Import(FromEPI.ExceptionSpec.SourceTemplate));
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001743
Jordan Rose5c382722013-03-08 21:51:21 +00001744 return Importer.getToContext().getFunctionType(ToResultType, ArgTypes, ToEPI);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00001745}
1746
Sean Callananda6df8a2011-08-11 16:56:07 +00001747QualType ASTNodeImporter::VisitParenType(const ParenType *T) {
1748 QualType ToInnerType = Importer.Import(T->getInnerType());
1749 if (ToInnerType.isNull())
1750 return QualType();
1751
1752 return Importer.getToContext().getParenType(ToInnerType);
1753}
1754
John McCall424cec92011-01-19 06:33:43 +00001755QualType ASTNodeImporter::VisitTypedefType(const TypedefType *T) {
Richard Smithdda56e42011-04-15 14:24:37 +00001756 TypedefNameDecl *ToDecl
1757 = dyn_cast_or_null<TypedefNameDecl>(Importer.Import(T->getDecl()));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001758 if (!ToDecl)
1759 return QualType();
1760
1761 return Importer.getToContext().getTypeDeclType(ToDecl);
1762}
1763
John McCall424cec92011-01-19 06:33:43 +00001764QualType ASTNodeImporter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001765 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1766 if (!ToExpr)
1767 return QualType();
1768
1769 return Importer.getToContext().getTypeOfExprType(ToExpr);
1770}
1771
John McCall424cec92011-01-19 06:33:43 +00001772QualType ASTNodeImporter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001773 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1774 if (ToUnderlyingType.isNull())
1775 return QualType();
1776
1777 return Importer.getToContext().getTypeOfType(ToUnderlyingType);
1778}
1779
John McCall424cec92011-01-19 06:33:43 +00001780QualType ASTNodeImporter::VisitDecltypeType(const DecltypeType *T) {
Richard Smith30482bc2011-02-20 03:19:35 +00001781 // FIXME: Make sure that the "to" context supports C++0x!
Douglas Gregor96e578d2010-02-05 17:54:41 +00001782 Expr *ToExpr = Importer.Import(T->getUnderlyingExpr());
1783 if (!ToExpr)
1784 return QualType();
1785
Douglas Gregor81495f32012-02-12 18:42:33 +00001786 QualType UnderlyingType = Importer.Import(T->getUnderlyingType());
1787 if (UnderlyingType.isNull())
1788 return QualType();
1789
1790 return Importer.getToContext().getDecltypeType(ToExpr, UnderlyingType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001791}
1792
Alexis Hunte852b102011-05-24 22:41:36 +00001793QualType ASTNodeImporter::VisitUnaryTransformType(const UnaryTransformType *T) {
1794 QualType ToBaseType = Importer.Import(T->getBaseType());
1795 QualType ToUnderlyingType = Importer.Import(T->getUnderlyingType());
1796 if (ToBaseType.isNull() || ToUnderlyingType.isNull())
1797 return QualType();
1798
1799 return Importer.getToContext().getUnaryTransformType(ToBaseType,
1800 ToUnderlyingType,
1801 T->getUTTKind());
1802}
1803
Richard Smith30482bc2011-02-20 03:19:35 +00001804QualType ASTNodeImporter::VisitAutoType(const AutoType *T) {
Richard Smith74aeef52013-04-26 16:15:35 +00001805 // FIXME: Make sure that the "to" context supports C++11!
Richard Smith30482bc2011-02-20 03:19:35 +00001806 QualType FromDeduced = T->getDeducedType();
1807 QualType ToDeduced;
1808 if (!FromDeduced.isNull()) {
1809 ToDeduced = Importer.Import(FromDeduced);
1810 if (ToDeduced.isNull())
1811 return QualType();
1812 }
1813
Richard Smithe301ba22015-11-11 02:02:15 +00001814 return Importer.getToContext().getAutoType(ToDeduced, T->getKeyword(),
Faisal Vali2b391ab2013-09-26 19:54:12 +00001815 /*IsDependent*/false);
Richard Smith30482bc2011-02-20 03:19:35 +00001816}
1817
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001818QualType ASTNodeImporter::VisitInjectedClassNameType(
1819 const InjectedClassNameType *T) {
1820 CXXRecordDecl *D = cast_or_null<CXXRecordDecl>(Importer.Import(T->getDecl()));
1821 if (!D)
1822 return QualType();
1823
1824 QualType InjType = Importer.Import(T->getInjectedSpecializationType());
1825 if (InjType.isNull())
1826 return QualType();
1827
1828 // FIXME: ASTContext::getInjectedClassNameType is not suitable for AST reading
1829 // See comments in InjectedClassNameType definition for details
1830 // return Importer.getToContext().getInjectedClassNameType(D, InjType);
1831 enum {
1832 TypeAlignmentInBits = 4,
1833 TypeAlignment = 1 << TypeAlignmentInBits
1834 };
1835
1836 return QualType(new (Importer.getToContext(), TypeAlignment)
1837 InjectedClassNameType(D, InjType), 0);
1838}
1839
John McCall424cec92011-01-19 06:33:43 +00001840QualType ASTNodeImporter::VisitRecordType(const RecordType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001841 RecordDecl *ToDecl
1842 = dyn_cast_or_null<RecordDecl>(Importer.Import(T->getDecl()));
1843 if (!ToDecl)
1844 return QualType();
1845
1846 return Importer.getToContext().getTagDeclType(ToDecl);
1847}
1848
John McCall424cec92011-01-19 06:33:43 +00001849QualType ASTNodeImporter::VisitEnumType(const EnumType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001850 EnumDecl *ToDecl
1851 = dyn_cast_or_null<EnumDecl>(Importer.Import(T->getDecl()));
1852 if (!ToDecl)
1853 return QualType();
1854
1855 return Importer.getToContext().getTagDeclType(ToDecl);
1856}
1857
Sean Callanan72fe0852015-04-02 23:50:08 +00001858QualType ASTNodeImporter::VisitAttributedType(const AttributedType *T) {
1859 QualType FromModifiedType = T->getModifiedType();
1860 QualType FromEquivalentType = T->getEquivalentType();
1861 QualType ToModifiedType;
1862 QualType ToEquivalentType;
1863
1864 if (!FromModifiedType.isNull()) {
1865 ToModifiedType = Importer.Import(FromModifiedType);
1866 if (ToModifiedType.isNull())
1867 return QualType();
1868 }
1869 if (!FromEquivalentType.isNull()) {
1870 ToEquivalentType = Importer.Import(FromEquivalentType);
1871 if (ToEquivalentType.isNull())
1872 return QualType();
1873 }
1874
1875 return Importer.getToContext().getAttributedType(T->getAttrKind(),
1876 ToModifiedType, ToEquivalentType);
1877}
1878
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00001879
1880QualType ASTNodeImporter::VisitTemplateTypeParmType(
1881 const TemplateTypeParmType *T) {
1882 TemplateTypeParmDecl *ParmDecl =
1883 cast_or_null<TemplateTypeParmDecl>(Importer.Import(T->getDecl()));
1884 if (!ParmDecl && T->getDecl())
1885 return QualType();
1886
1887 return Importer.getToContext().getTemplateTypeParmType(
1888 T->getDepth(), T->getIndex(), T->isParameterPack(), ParmDecl);
1889}
1890
Douglas Gregore2e50d332010-12-01 01:36:18 +00001891QualType ASTNodeImporter::VisitTemplateSpecializationType(
John McCall424cec92011-01-19 06:33:43 +00001892 const TemplateSpecializationType *T) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00001893 TemplateName ToTemplate = Importer.Import(T->getTemplateName());
1894 if (ToTemplate.isNull())
1895 return QualType();
1896
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001897 SmallVector<TemplateArgument, 2> ToTemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00001898 if (ImportTemplateArguments(T->getArgs(), T->getNumArgs(), ToTemplateArgs))
1899 return QualType();
1900
1901 QualType ToCanonType;
1902 if (!QualType(T, 0).isCanonical()) {
1903 QualType FromCanonType
1904 = Importer.getFromContext().getCanonicalType(QualType(T, 0));
1905 ToCanonType =Importer.Import(FromCanonType);
1906 if (ToCanonType.isNull())
1907 return QualType();
1908 }
1909 return Importer.getToContext().getTemplateSpecializationType(ToTemplate,
1910 ToTemplateArgs.data(),
1911 ToTemplateArgs.size(),
1912 ToCanonType);
1913}
1914
John McCall424cec92011-01-19 06:33:43 +00001915QualType ASTNodeImporter::VisitElaboratedType(const ElaboratedType *T) {
Craig Topper36250ad2014-05-12 05:36:57 +00001916 NestedNameSpecifier *ToQualifier = nullptr;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001917 // Note: the qualifier in an ElaboratedType is optional.
1918 if (T->getQualifier()) {
1919 ToQualifier = Importer.Import(T->getQualifier());
1920 if (!ToQualifier)
1921 return QualType();
1922 }
Douglas Gregor96e578d2010-02-05 17:54:41 +00001923
1924 QualType ToNamedType = Importer.Import(T->getNamedType());
1925 if (ToNamedType.isNull())
1926 return QualType();
1927
Abramo Bagnara6150c882010-05-11 21:36:43 +00001928 return Importer.getToContext().getElaboratedType(T->getKeyword(),
1929 ToQualifier, ToNamedType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001930}
1931
John McCall424cec92011-01-19 06:33:43 +00001932QualType ASTNodeImporter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001933 ObjCInterfaceDecl *Class
1934 = dyn_cast_or_null<ObjCInterfaceDecl>(Importer.Import(T->getDecl()));
1935 if (!Class)
1936 return QualType();
1937
John McCall8b07ec22010-05-15 11:32:37 +00001938 return Importer.getToContext().getObjCInterfaceType(Class);
1939}
1940
John McCall424cec92011-01-19 06:33:43 +00001941QualType ASTNodeImporter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +00001942 QualType ToBaseType = Importer.Import(T->getBaseType());
1943 if (ToBaseType.isNull())
1944 return QualType();
1945
Douglas Gregore9d95f12015-07-07 03:57:35 +00001946 SmallVector<QualType, 4> TypeArgs;
Douglas Gregore83b9562015-07-07 03:57:53 +00001947 for (auto TypeArg : T->getTypeArgsAsWritten()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00001948 QualType ImportedTypeArg = Importer.Import(TypeArg);
1949 if (ImportedTypeArg.isNull())
1950 return QualType();
1951
1952 TypeArgs.push_back(ImportedTypeArg);
1953 }
1954
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001955 SmallVector<ObjCProtocolDecl *, 4> Protocols;
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001956 for (auto *P : T->quals()) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001957 ObjCProtocolDecl *Protocol
Aaron Ballman1683f7b2014-03-17 15:55:30 +00001958 = dyn_cast_or_null<ObjCProtocolDecl>(Importer.Import(P));
Douglas Gregor96e578d2010-02-05 17:54:41 +00001959 if (!Protocol)
1960 return QualType();
1961 Protocols.push_back(Protocol);
1962 }
1963
Douglas Gregore9d95f12015-07-07 03:57:35 +00001964 return Importer.getToContext().getObjCObjectType(ToBaseType, TypeArgs,
Douglas Gregorab209d82015-07-07 03:58:42 +00001965 Protocols,
1966 T->isKindOfTypeAsWritten());
Douglas Gregor96e578d2010-02-05 17:54:41 +00001967}
1968
John McCall424cec92011-01-19 06:33:43 +00001969QualType
1970ASTNodeImporter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00001971 QualType ToPointeeType = Importer.Import(T->getPointeeType());
1972 if (ToPointeeType.isNull())
1973 return QualType();
1974
John McCall8b07ec22010-05-15 11:32:37 +00001975 return Importer.getToContext().getObjCObjectPointerType(ToPointeeType);
Douglas Gregor96e578d2010-02-05 17:54:41 +00001976}
1977
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00001978//----------------------------------------------------------------------------
1979// Import Declarations
1980//----------------------------------------------------------------------------
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001981bool ASTNodeImporter::ImportDeclParts(NamedDecl *D, DeclContext *&DC,
1982 DeclContext *&LexicalDC,
1983 DeclarationName &Name,
Sean Callanan59721b32015-04-28 18:41:46 +00001984 NamedDecl *&ToD,
Douglas Gregorbb7930c2010-02-10 19:54:31 +00001985 SourceLocation &Loc) {
1986 // Import the context of this declaration.
1987 DC = Importer.ImportContext(D->getDeclContext());
1988 if (!DC)
1989 return true;
1990
1991 LexicalDC = DC;
1992 if (D->getDeclContext() != D->getLexicalDeclContext()) {
1993 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
1994 if (!LexicalDC)
1995 return true;
1996 }
1997
1998 // Import the name of this declaration.
1999 Name = Importer.Import(D->getDeclName());
2000 if (D->getDeclName() && !Name)
2001 return true;
2002
2003 // Import the location of this declaration.
2004 Loc = Importer.Import(D->getLocation());
Sean Callanan59721b32015-04-28 18:41:46 +00002005 ToD = cast_or_null<NamedDecl>(Importer.GetAlreadyImportedOrNull(D));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002006 return false;
2007}
2008
Douglas Gregord451ea92011-07-29 23:31:30 +00002009void ASTNodeImporter::ImportDefinitionIfNeeded(Decl *FromD, Decl *ToD) {
2010 if (!FromD)
2011 return;
2012
2013 if (!ToD) {
2014 ToD = Importer.Import(FromD);
2015 if (!ToD)
2016 return;
2017 }
2018
2019 if (RecordDecl *FromRecord = dyn_cast<RecordDecl>(FromD)) {
2020 if (RecordDecl *ToRecord = cast_or_null<RecordDecl>(ToD)) {
Sean Callanan19dfc932013-01-11 23:17:47 +00002021 if (FromRecord->getDefinition() && FromRecord->isCompleteDefinition() && !ToRecord->getDefinition()) {
Douglas Gregord451ea92011-07-29 23:31:30 +00002022 ImportDefinition(FromRecord, ToRecord);
2023 }
2024 }
2025 return;
2026 }
2027
2028 if (EnumDecl *FromEnum = dyn_cast<EnumDecl>(FromD)) {
2029 if (EnumDecl *ToEnum = cast_or_null<EnumDecl>(ToD)) {
2030 if (FromEnum->getDefinition() && !ToEnum->getDefinition()) {
2031 ImportDefinition(FromEnum, ToEnum);
2032 }
2033 }
2034 return;
2035 }
2036}
2037
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002038void
2039ASTNodeImporter::ImportDeclarationNameLoc(const DeclarationNameInfo &From,
2040 DeclarationNameInfo& To) {
2041 // NOTE: To.Name and To.Loc are already imported.
2042 // We only have to import To.LocInfo.
2043 switch (To.getName().getNameKind()) {
2044 case DeclarationName::Identifier:
2045 case DeclarationName::ObjCZeroArgSelector:
2046 case DeclarationName::ObjCOneArgSelector:
2047 case DeclarationName::ObjCMultiArgSelector:
2048 case DeclarationName::CXXUsingDirective:
2049 return;
2050
2051 case DeclarationName::CXXOperatorName: {
2052 SourceRange Range = From.getCXXOperatorNameRange();
2053 To.setCXXOperatorNameRange(Importer.Import(Range));
2054 return;
2055 }
2056 case DeclarationName::CXXLiteralOperatorName: {
2057 SourceLocation Loc = From.getCXXLiteralOperatorNameLoc();
2058 To.setCXXLiteralOperatorNameLoc(Importer.Import(Loc));
2059 return;
2060 }
2061 case DeclarationName::CXXConstructorName:
2062 case DeclarationName::CXXDestructorName:
2063 case DeclarationName::CXXConversionFunctionName: {
2064 TypeSourceInfo *FromTInfo = From.getNamedTypeInfo();
2065 To.setNamedTypeInfo(Importer.Import(FromTInfo));
2066 return;
2067 }
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002068 }
Douglas Gregor07216d12011-11-02 20:52:01 +00002069 llvm_unreachable("Unknown name kind.");
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002070}
2071
Douglas Gregor2e15c842012-02-01 21:00:38 +00002072void ASTNodeImporter::ImportDeclContext(DeclContext *FromDC, bool ForceImport) {
Douglas Gregor0a791672011-01-18 03:11:38 +00002073 if (Importer.isMinimalImport() && !ForceImport) {
Sean Callanan81d577c2011-07-22 23:46:03 +00002074 Importer.ImportContext(FromDC);
Douglas Gregor0a791672011-01-18 03:11:38 +00002075 return;
2076 }
2077
Aaron Ballman629afae2014-03-07 19:56:05 +00002078 for (auto *From : FromDC->decls())
2079 Importer.Import(From);
Douglas Gregor968d6332010-02-21 18:24:45 +00002080}
2081
Douglas Gregord451ea92011-07-29 23:31:30 +00002082bool ASTNodeImporter::ImportDefinition(RecordDecl *From, RecordDecl *To,
Douglas Gregor95d82832012-01-24 18:36:04 +00002083 ImportDefinitionKind Kind) {
2084 if (To->getDefinition() || To->isBeingDefined()) {
2085 if (Kind == IDK_Everything)
2086 ImportDeclContext(From, /*ForceImport=*/true);
2087
Douglas Gregore2e50d332010-12-01 01:36:18 +00002088 return false;
Douglas Gregor95d82832012-01-24 18:36:04 +00002089 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00002090
2091 To->startDefinition();
2092
2093 // Add base classes.
2094 if (CXXRecordDecl *ToCXX = dyn_cast<CXXRecordDecl>(To)) {
2095 CXXRecordDecl *FromCXX = cast<CXXRecordDecl>(From);
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002096
2097 struct CXXRecordDecl::DefinitionData &ToData = ToCXX->data();
2098 struct CXXRecordDecl::DefinitionData &FromData = FromCXX->data();
2099 ToData.UserDeclaredConstructor = FromData.UserDeclaredConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002100 ToData.UserDeclaredSpecialMembers = FromData.UserDeclaredSpecialMembers;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002101 ToData.Aggregate = FromData.Aggregate;
2102 ToData.PlainOldData = FromData.PlainOldData;
2103 ToData.Empty = FromData.Empty;
2104 ToData.Polymorphic = FromData.Polymorphic;
2105 ToData.Abstract = FromData.Abstract;
2106 ToData.IsStandardLayout = FromData.IsStandardLayout;
2107 ToData.HasNoNonEmptyBases = FromData.HasNoNonEmptyBases;
2108 ToData.HasPrivateFields = FromData.HasPrivateFields;
2109 ToData.HasProtectedFields = FromData.HasProtectedFields;
2110 ToData.HasPublicFields = FromData.HasPublicFields;
2111 ToData.HasMutableFields = FromData.HasMutableFields;
Richard Smithab44d5b2013-12-10 08:25:00 +00002112 ToData.HasVariantMembers = FromData.HasVariantMembers;
Richard Smith561fb152012-02-25 07:33:38 +00002113 ToData.HasOnlyCMembers = FromData.HasOnlyCMembers;
Richard Smithe2648ba2012-05-07 01:07:30 +00002114 ToData.HasInClassInitializer = FromData.HasInClassInitializer;
Richard Smith593f9932012-12-08 02:01:17 +00002115 ToData.HasUninitializedReferenceMember
2116 = FromData.HasUninitializedReferenceMember;
Nico Weber6a6376b2016-02-19 01:52:46 +00002117 ToData.HasUninitializedFields = FromData.HasUninitializedFields;
Richard Smith6b02d462012-12-08 08:32:28 +00002118 ToData.NeedOverloadResolutionForMoveConstructor
2119 = FromData.NeedOverloadResolutionForMoveConstructor;
2120 ToData.NeedOverloadResolutionForMoveAssignment
2121 = FromData.NeedOverloadResolutionForMoveAssignment;
2122 ToData.NeedOverloadResolutionForDestructor
2123 = FromData.NeedOverloadResolutionForDestructor;
2124 ToData.DefaultedMoveConstructorIsDeleted
2125 = FromData.DefaultedMoveConstructorIsDeleted;
2126 ToData.DefaultedMoveAssignmentIsDeleted
2127 = FromData.DefaultedMoveAssignmentIsDeleted;
2128 ToData.DefaultedDestructorIsDeleted = FromData.DefaultedDestructorIsDeleted;
Richard Smith328aae52012-11-30 05:11:39 +00002129 ToData.HasTrivialSpecialMembers = FromData.HasTrivialSpecialMembers;
2130 ToData.HasIrrelevantDestructor = FromData.HasIrrelevantDestructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002131 ToData.HasConstexprNonCopyMoveConstructor
2132 = FromData.HasConstexprNonCopyMoveConstructor;
Nico Weber72c57f42016-02-24 20:58:14 +00002133 ToData.HasDefaultedDefaultConstructor
2134 = FromData.HasDefaultedDefaultConstructor;
Richard Smith561fb152012-02-25 07:33:38 +00002135 ToData.DefaultedDefaultConstructorIsConstexpr
2136 = FromData.DefaultedDefaultConstructorIsConstexpr;
Richard Smith561fb152012-02-25 07:33:38 +00002137 ToData.HasConstexprDefaultConstructor
2138 = FromData.HasConstexprDefaultConstructor;
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002139 ToData.HasNonLiteralTypeFieldsOrBases
2140 = FromData.HasNonLiteralTypeFieldsOrBases;
Richard Smith561fb152012-02-25 07:33:38 +00002141 // ComputedVisibleConversions not imported.
Douglas Gregor3c2404b2011-11-03 18:07:07 +00002142 ToData.UserProvidedDefaultConstructor
2143 = FromData.UserProvidedDefaultConstructor;
Richard Smith328aae52012-11-30 05:11:39 +00002144 ToData.DeclaredSpecialMembers = FromData.DeclaredSpecialMembers;
Richard Smith1c33fe82012-11-28 06:23:12 +00002145 ToData.ImplicitCopyConstructorHasConstParam
2146 = FromData.ImplicitCopyConstructorHasConstParam;
2147 ToData.ImplicitCopyAssignmentHasConstParam
2148 = FromData.ImplicitCopyAssignmentHasConstParam;
2149 ToData.HasDeclaredCopyConstructorWithConstParam
2150 = FromData.HasDeclaredCopyConstructorWithConstParam;
2151 ToData.HasDeclaredCopyAssignmentWithConstParam
2152 = FromData.HasDeclaredCopyAssignmentWithConstParam;
Richard Smith561fb152012-02-25 07:33:38 +00002153 ToData.IsLambda = FromData.IsLambda;
2154
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002155 SmallVector<CXXBaseSpecifier *, 4> Bases;
Aaron Ballman574705e2014-03-13 15:41:46 +00002156 for (const auto &Base1 : FromCXX->bases()) {
2157 QualType T = Importer.Import(Base1.getType());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002158 if (T.isNull())
Douglas Gregor96303ea2010-12-02 19:33:37 +00002159 return true;
Douglas Gregor752a5952011-01-03 22:36:02 +00002160
2161 SourceLocation EllipsisLoc;
Aaron Ballman574705e2014-03-13 15:41:46 +00002162 if (Base1.isPackExpansion())
2163 EllipsisLoc = Importer.Import(Base1.getEllipsisLoc());
Douglas Gregord451ea92011-07-29 23:31:30 +00002164
2165 // Ensure that we have a definition for the base.
Aaron Ballman574705e2014-03-13 15:41:46 +00002166 ImportDefinitionIfNeeded(Base1.getType()->getAsCXXRecordDecl());
Douglas Gregord451ea92011-07-29 23:31:30 +00002167
Douglas Gregore2e50d332010-12-01 01:36:18 +00002168 Bases.push_back(
2169 new (Importer.getToContext())
Aaron Ballman574705e2014-03-13 15:41:46 +00002170 CXXBaseSpecifier(Importer.Import(Base1.getSourceRange()),
2171 Base1.isVirtual(),
2172 Base1.isBaseOfClass(),
2173 Base1.getAccessSpecifierAsWritten(),
2174 Importer.Import(Base1.getTypeSourceInfo()),
Douglas Gregor752a5952011-01-03 22:36:02 +00002175 EllipsisLoc));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002176 }
2177 if (!Bases.empty())
Craig Toppere6337e12015-12-25 00:36:02 +00002178 ToCXX->setBases(Bases.data(), Bases.size());
Douglas Gregore2e50d332010-12-01 01:36:18 +00002179 }
2180
Douglas Gregor2e15c842012-02-01 21:00:38 +00002181 if (shouldForceImportDeclContext(Kind))
Douglas Gregor95d82832012-01-24 18:36:04 +00002182 ImportDeclContext(From, /*ForceImport=*/true);
2183
Douglas Gregore2e50d332010-12-01 01:36:18 +00002184 To->completeDefinition();
Douglas Gregor96303ea2010-12-02 19:33:37 +00002185 return false;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002186}
2187
Larisse Voufo39a1e502013-08-06 01:03:05 +00002188bool ASTNodeImporter::ImportDefinition(VarDecl *From, VarDecl *To,
2189 ImportDefinitionKind Kind) {
Sean Callanan59721b32015-04-28 18:41:46 +00002190 if (To->getAnyInitializer())
Larisse Voufo39a1e502013-08-06 01:03:05 +00002191 return false;
2192
2193 // FIXME: Can we really import any initializer? Alternatively, we could force
2194 // ourselves to import every declaration of a variable and then only use
2195 // getInit() here.
2196 To->setInit(Importer.Import(const_cast<Expr *>(From->getAnyInitializer())));
2197
2198 // FIXME: Other bits to merge?
2199
2200 return false;
2201}
2202
Douglas Gregord451ea92011-07-29 23:31:30 +00002203bool ASTNodeImporter::ImportDefinition(EnumDecl *From, EnumDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00002204 ImportDefinitionKind Kind) {
2205 if (To->getDefinition() || To->isBeingDefined()) {
2206 if (Kind == IDK_Everything)
2207 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002208 return false;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002209 }
Douglas Gregord451ea92011-07-29 23:31:30 +00002210
2211 To->startDefinition();
2212
2213 QualType T = Importer.Import(Importer.getFromContext().getTypeDeclType(From));
2214 if (T.isNull())
2215 return true;
2216
2217 QualType ToPromotionType = Importer.Import(From->getPromotionType());
2218 if (ToPromotionType.isNull())
2219 return true;
Douglas Gregor2e15c842012-02-01 21:00:38 +00002220
2221 if (shouldForceImportDeclContext(Kind))
2222 ImportDeclContext(From, /*ForceImport=*/true);
Douglas Gregord451ea92011-07-29 23:31:30 +00002223
2224 // FIXME: we might need to merge the number of positive or negative bits
2225 // if the enumerator lists don't match.
2226 To->completeDefinition(T, ToPromotionType,
2227 From->getNumPositiveBits(),
2228 From->getNumNegativeBits());
2229 return false;
2230}
2231
Douglas Gregora082a492010-11-30 19:14:50 +00002232TemplateParameterList *ASTNodeImporter::ImportTemplateParameterList(
2233 TemplateParameterList *Params) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002234 SmallVector<NamedDecl *, 4> ToParams;
Douglas Gregora082a492010-11-30 19:14:50 +00002235 ToParams.reserve(Params->size());
2236 for (TemplateParameterList::iterator P = Params->begin(),
2237 PEnd = Params->end();
2238 P != PEnd; ++P) {
2239 Decl *To = Importer.Import(*P);
2240 if (!To)
Craig Topper36250ad2014-05-12 05:36:57 +00002241 return nullptr;
2242
Douglas Gregora082a492010-11-30 19:14:50 +00002243 ToParams.push_back(cast<NamedDecl>(To));
2244 }
2245
2246 return TemplateParameterList::Create(Importer.getToContext(),
2247 Importer.Import(Params->getTemplateLoc()),
2248 Importer.Import(Params->getLAngleLoc()),
David Majnemer902f8c62015-12-27 07:16:27 +00002249 ToParams,
Douglas Gregora082a492010-11-30 19:14:50 +00002250 Importer.Import(Params->getRAngleLoc()));
2251}
2252
Douglas Gregore2e50d332010-12-01 01:36:18 +00002253TemplateArgument
2254ASTNodeImporter::ImportTemplateArgument(const TemplateArgument &From) {
2255 switch (From.getKind()) {
2256 case TemplateArgument::Null:
2257 return TemplateArgument();
2258
2259 case TemplateArgument::Type: {
2260 QualType ToType = Importer.Import(From.getAsType());
2261 if (ToType.isNull())
2262 return TemplateArgument();
2263 return TemplateArgument(ToType);
2264 }
2265
2266 case TemplateArgument::Integral: {
2267 QualType ToType = Importer.Import(From.getIntegralType());
2268 if (ToType.isNull())
2269 return TemplateArgument();
Benjamin Kramer6003ad52012-06-07 15:09:51 +00002270 return TemplateArgument(From, ToType);
Douglas Gregore2e50d332010-12-01 01:36:18 +00002271 }
2272
Eli Friedmanb826a002012-09-26 02:36:12 +00002273 case TemplateArgument::Declaration: {
David Blaikie3c7dd6b2014-10-22 19:54:16 +00002274 ValueDecl *To = cast_or_null<ValueDecl>(Importer.Import(From.getAsDecl()));
2275 QualType ToType = Importer.Import(From.getParamTypeForDecl());
2276 if (!To || ToType.isNull())
2277 return TemplateArgument();
2278 return TemplateArgument(To, ToType);
Eli Friedmanb826a002012-09-26 02:36:12 +00002279 }
2280
2281 case TemplateArgument::NullPtr: {
2282 QualType ToType = Importer.Import(From.getNullPtrType());
2283 if (ToType.isNull())
2284 return TemplateArgument();
2285 return TemplateArgument(ToType, /*isNullPtr*/true);
2286 }
2287
Douglas Gregore2e50d332010-12-01 01:36:18 +00002288 case TemplateArgument::Template: {
2289 TemplateName ToTemplate = Importer.Import(From.getAsTemplate());
2290 if (ToTemplate.isNull())
2291 return TemplateArgument();
2292
2293 return TemplateArgument(ToTemplate);
2294 }
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002295
2296 case TemplateArgument::TemplateExpansion: {
2297 TemplateName ToTemplate
2298 = Importer.Import(From.getAsTemplateOrTemplatePattern());
2299 if (ToTemplate.isNull())
2300 return TemplateArgument();
2301
Douglas Gregore1d60df2011-01-14 23:41:42 +00002302 return TemplateArgument(ToTemplate, From.getNumTemplateExpansions());
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002303 }
2304
Douglas Gregore2e50d332010-12-01 01:36:18 +00002305 case TemplateArgument::Expression:
2306 if (Expr *ToExpr = Importer.Import(From.getAsExpr()))
2307 return TemplateArgument(ToExpr);
2308 return TemplateArgument();
2309
2310 case TemplateArgument::Pack: {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002311 SmallVector<TemplateArgument, 2> ToPack;
Douglas Gregore2e50d332010-12-01 01:36:18 +00002312 ToPack.reserve(From.pack_size());
2313 if (ImportTemplateArguments(From.pack_begin(), From.pack_size(), ToPack))
2314 return TemplateArgument();
Benjamin Kramercce63472015-08-05 09:40:22 +00002315
2316 return TemplateArgument(
2317 llvm::makeArrayRef(ToPack).copy(Importer.getToContext()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00002318 }
2319 }
2320
2321 llvm_unreachable("Invalid template argument kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00002322}
2323
2324bool ASTNodeImporter::ImportTemplateArguments(const TemplateArgument *FromArgs,
2325 unsigned NumFromArgs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002326 SmallVectorImpl<TemplateArgument> &ToArgs) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00002327 for (unsigned I = 0; I != NumFromArgs; ++I) {
2328 TemplateArgument To = ImportTemplateArgument(FromArgs[I]);
2329 if (To.isNull() && !FromArgs[I].isNull())
2330 return true;
2331
2332 ToArgs.push_back(To);
2333 }
2334
2335 return false;
2336}
2337
Douglas Gregor5c73e912010-02-11 00:48:18 +00002338bool ASTNodeImporter::IsStructuralMatch(RecordDecl *FromRecord,
Douglas Gregordd6006f2012-07-17 21:16:27 +00002339 RecordDecl *ToRecord, bool Complain) {
Sean Callananc665c9e2013-10-09 21:45:11 +00002340 // Eliminate a potential failure point where we attempt to re-import
2341 // something we're trying to import while completing ToRecord.
2342 Decl *ToOrigin = Importer.GetOriginalDecl(ToRecord);
2343 if (ToOrigin) {
2344 RecordDecl *ToOriginRecord = dyn_cast<RecordDecl>(ToOrigin);
2345 if (ToOriginRecord)
2346 ToRecord = ToOriginRecord;
2347 }
2348
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002349 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Sean Callananc665c9e2013-10-09 21:45:11 +00002350 ToRecord->getASTContext(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00002351 Importer.getNonEquivalentDecls(),
2352 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002353 return Ctx.IsStructurallyEquivalent(FromRecord, ToRecord);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002354}
2355
Larisse Voufo39a1e502013-08-06 01:03:05 +00002356bool ASTNodeImporter::IsStructuralMatch(VarDecl *FromVar, VarDecl *ToVar,
2357 bool Complain) {
2358 StructuralEquivalenceContext Ctx(
2359 Importer.getFromContext(), Importer.getToContext(),
2360 Importer.getNonEquivalentDecls(), false, Complain);
2361 return Ctx.IsStructurallyEquivalent(FromVar, ToVar);
2362}
2363
Douglas Gregor98c10182010-02-12 22:17:39 +00002364bool ASTNodeImporter::IsStructuralMatch(EnumDecl *FromEnum, EnumDecl *ToEnum) {
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002365 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
Douglas Gregor3996e242010-02-15 22:01:00 +00002366 Importer.getToContext(),
Douglas Gregorb4964f72010-02-15 23:54:17 +00002367 Importer.getNonEquivalentDecls());
Benjamin Kramer26d19c52010-02-18 13:02:13 +00002368 return Ctx.IsStructurallyEquivalent(FromEnum, ToEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002369}
2370
Douglas Gregor91155082012-11-14 22:29:20 +00002371bool ASTNodeImporter::IsStructuralMatch(EnumConstantDecl *FromEC,
2372 EnumConstantDecl *ToEC)
2373{
2374 const llvm::APSInt &FromVal = FromEC->getInitVal();
2375 const llvm::APSInt &ToVal = ToEC->getInitVal();
2376
2377 return FromVal.isSigned() == ToVal.isSigned() &&
2378 FromVal.getBitWidth() == ToVal.getBitWidth() &&
2379 FromVal == ToVal;
2380}
2381
2382bool ASTNodeImporter::IsStructuralMatch(ClassTemplateDecl *From,
Douglas Gregora082a492010-11-30 19:14:50 +00002383 ClassTemplateDecl *To) {
2384 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2385 Importer.getToContext(),
2386 Importer.getNonEquivalentDecls());
2387 return Ctx.IsStructurallyEquivalent(From, To);
2388}
2389
Larisse Voufo39a1e502013-08-06 01:03:05 +00002390bool ASTNodeImporter::IsStructuralMatch(VarTemplateDecl *From,
2391 VarTemplateDecl *To) {
2392 StructuralEquivalenceContext Ctx(Importer.getFromContext(),
2393 Importer.getToContext(),
2394 Importer.getNonEquivalentDecls());
2395 return Ctx.IsStructurallyEquivalent(From, To);
2396}
2397
Douglas Gregore4c83e42010-02-09 22:48:33 +00002398Decl *ASTNodeImporter::VisitDecl(Decl *D) {
Douglas Gregor811663e2010-02-10 00:15:17 +00002399 Importer.FromDiag(D->getLocation(), diag::err_unsupported_ast_node)
Douglas Gregore4c83e42010-02-09 22:48:33 +00002400 << D->getDeclKindName();
Craig Topper36250ad2014-05-12 05:36:57 +00002401 return nullptr;
Douglas Gregore4c83e42010-02-09 22:48:33 +00002402}
2403
Sean Callanan65198272011-11-17 23:20:56 +00002404Decl *ASTNodeImporter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
2405 TranslationUnitDecl *ToD =
2406 Importer.getToContext().getTranslationUnitDecl();
2407
2408 Importer.Imported(D, ToD);
2409
2410 return ToD;
2411}
2412
Argyrios Kyrtzidis544ea712016-02-18 23:08:36 +00002413Decl *ASTNodeImporter::VisitAccessSpecDecl(AccessSpecDecl *D) {
2414
2415 SourceLocation Loc = Importer.Import(D->getLocation());
2416 SourceLocation ColonLoc = Importer.Import(D->getColonLoc());
2417
2418 // Import the context of this declaration.
2419 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
2420 if (!DC)
2421 return nullptr;
2422
2423 AccessSpecDecl *accessSpecDecl
2424 = AccessSpecDecl::Create(Importer.getToContext(), D->getAccess(),
2425 DC, Loc, ColonLoc);
2426
2427 if (!accessSpecDecl)
2428 return nullptr;
2429
2430 // Lexical DeclContext and Semantic DeclContext
2431 // is always the same for the accessSpec.
2432 accessSpecDecl->setLexicalDeclContext(DC);
2433 DC->addDeclInternal(accessSpecDecl);
2434
2435 return accessSpecDecl;
2436}
2437
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002438Decl *ASTNodeImporter::VisitNamespaceDecl(NamespaceDecl *D) {
2439 // Import the major distinguishing characteristics of this namespace.
2440 DeclContext *DC, *LexicalDC;
2441 DeclarationName Name;
2442 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002443 NamedDecl *ToD;
2444 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002445 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002446 if (ToD)
2447 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002448
2449 NamespaceDecl *MergeWithNamespace = nullptr;
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002450 if (!Name) {
2451 // This is an anonymous namespace. Adopt an existing anonymous
2452 // namespace if we can.
2453 // FIXME: Not testable.
2454 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2455 MergeWithNamespace = TU->getAnonymousNamespace();
2456 else
2457 MergeWithNamespace = cast<NamespaceDecl>(DC)->getAnonymousNamespace();
2458 } else {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002459 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002460 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002461 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002462 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2463 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Namespace))
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002464 continue;
2465
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002466 if (NamespaceDecl *FoundNS = dyn_cast<NamespaceDecl>(FoundDecls[I])) {
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002467 MergeWithNamespace = FoundNS;
2468 ConflictingDecls.clear();
2469 break;
2470 }
2471
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002472 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002473 }
2474
2475 if (!ConflictingDecls.empty()) {
John McCalle87beb22010-04-23 18:46:30 +00002476 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Namespace,
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002477 ConflictingDecls.data(),
2478 ConflictingDecls.size());
2479 }
2480 }
2481
2482 // Create the "to" namespace, if needed.
2483 NamespaceDecl *ToNamespace = MergeWithNamespace;
2484 if (!ToNamespace) {
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002485 ToNamespace = NamespaceDecl::Create(Importer.getToContext(), DC,
Douglas Gregore57e7522012-01-07 09:11:48 +00002486 D->isInline(),
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002487 Importer.Import(D->getLocStart()),
Douglas Gregore57e7522012-01-07 09:11:48 +00002488 Loc, Name.getAsIdentifierInfo(),
Craig Topper36250ad2014-05-12 05:36:57 +00002489 /*PrevDecl=*/nullptr);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002490 ToNamespace->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002491 LexicalDC->addDeclInternal(ToNamespace);
Douglas Gregorf18a2c72010-02-21 18:26:36 +00002492
2493 // If this is an anonymous namespace, register it as the anonymous
2494 // namespace within its context.
2495 if (!Name) {
2496 if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(DC))
2497 TU->setAnonymousNamespace(ToNamespace);
2498 else
2499 cast<NamespaceDecl>(DC)->setAnonymousNamespace(ToNamespace);
2500 }
2501 }
2502 Importer.Imported(D, ToNamespace);
2503
2504 ImportDeclContext(D);
2505
2506 return ToNamespace;
2507}
2508
Richard Smithdda56e42011-04-15 14:24:37 +00002509Decl *ASTNodeImporter::VisitTypedefNameDecl(TypedefNameDecl *D, bool IsAlias) {
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002510 // Import the major distinguishing characteristics of this typedef.
2511 DeclContext *DC, *LexicalDC;
2512 DeclarationName Name;
2513 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002514 NamedDecl *ToD;
2515 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002516 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002517 if (ToD)
2518 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002519
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002520 // If this typedef is not in block scope, determine whether we've
2521 // seen a typedef with the same name (that we can merge with) or any
2522 // other entity by that name (which name lookup could conflict with).
2523 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002524 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002525 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002526 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002527 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002528 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2529 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002530 continue;
Richard Smithdda56e42011-04-15 14:24:37 +00002531 if (TypedefNameDecl *FoundTypedef =
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002532 dyn_cast<TypedefNameDecl>(FoundDecls[I])) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002533 if (Importer.IsStructurallyEquivalent(D->getUnderlyingType(),
2534 FoundTypedef->getUnderlyingType()))
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002535 return Importer.Imported(D, FoundTypedef);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002536 }
2537
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002538 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002539 }
2540
2541 if (!ConflictingDecls.empty()) {
2542 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2543 ConflictingDecls.data(),
2544 ConflictingDecls.size());
2545 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002546 return nullptr;
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002547 }
2548 }
2549
Douglas Gregorb4964f72010-02-15 23:54:17 +00002550 // Import the underlying type of this typedef;
2551 QualType T = Importer.Import(D->getUnderlyingType());
2552 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002553 return nullptr;
2554
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002555 // Create the new typedef node.
2556 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002557 SourceLocation StartL = Importer.Import(D->getLocStart());
Richard Smithdda56e42011-04-15 14:24:37 +00002558 TypedefNameDecl *ToTypedef;
2559 if (IsAlias)
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002560 ToTypedef = TypeAliasDecl::Create(Importer.getToContext(), DC,
2561 StartL, Loc,
2562 Name.getAsIdentifierInfo(),
2563 TInfo);
2564 else
Richard Smithdda56e42011-04-15 14:24:37 +00002565 ToTypedef = TypedefDecl::Create(Importer.getToContext(), DC,
2566 StartL, Loc,
2567 Name.getAsIdentifierInfo(),
2568 TInfo);
Douglas Gregor03d1ed32011-10-14 21:54:42 +00002569
Douglas Gregordd483172010-02-22 17:42:47 +00002570 ToTypedef->setAccess(D->getAccess());
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002571 ToTypedef->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002572 Importer.Imported(D, ToTypedef);
Sean Callanan95e74be2011-10-21 02:57:43 +00002573 LexicalDC->addDeclInternal(ToTypedef);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002574
Douglas Gregor5fa74c32010-02-10 21:10:29 +00002575 return ToTypedef;
2576}
2577
Richard Smithdda56e42011-04-15 14:24:37 +00002578Decl *ASTNodeImporter::VisitTypedefDecl(TypedefDecl *D) {
2579 return VisitTypedefNameDecl(D, /*IsAlias=*/false);
2580}
2581
2582Decl *ASTNodeImporter::VisitTypeAliasDecl(TypeAliasDecl *D) {
2583 return VisitTypedefNameDecl(D, /*IsAlias=*/true);
2584}
2585
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00002586Decl *ASTNodeImporter::VisitLabelDecl(LabelDecl *D) {
2587 // Import the major distinguishing characteristics of this label.
2588 DeclContext *DC, *LexicalDC;
2589 DeclarationName Name;
2590 SourceLocation Loc;
2591 NamedDecl *ToD;
2592 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
2593 return nullptr;
2594 if (ToD)
2595 return ToD;
2596
2597 assert(LexicalDC->isFunctionOrMethod());
2598
2599 LabelDecl *ToLabel = D->isGnuLocal()
2600 ? LabelDecl::Create(Importer.getToContext(),
2601 DC, Importer.Import(D->getLocation()),
2602 Name.getAsIdentifierInfo(),
2603 Importer.Import(D->getLocStart()))
2604 : LabelDecl::Create(Importer.getToContext(),
2605 DC, Importer.Import(D->getLocation()),
2606 Name.getAsIdentifierInfo());
2607 Importer.Imported(D, ToLabel);
2608
2609 LabelStmt *Label = cast_or_null<LabelStmt>(Importer.Import(D->getStmt()));
2610 if (!Label)
2611 return nullptr;
2612
2613 ToLabel->setStmt(Label);
2614 ToLabel->setLexicalDeclContext(LexicalDC);
2615 LexicalDC->addDeclInternal(ToLabel);
2616 return ToLabel;
2617}
2618
Douglas Gregor98c10182010-02-12 22:17:39 +00002619Decl *ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
2620 // Import the major distinguishing characteristics of this enum.
2621 DeclContext *DC, *LexicalDC;
2622 DeclarationName Name;
2623 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002624 NamedDecl *ToD;
2625 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002626 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002627 if (ToD)
2628 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002629
Douglas Gregor98c10182010-02-12 22:17:39 +00002630 // Figure out what enum name we're looking for.
2631 unsigned IDNS = Decl::IDNS_Tag;
2632 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002633 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2634 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor98c10182010-02-12 22:17:39 +00002635 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002636 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor98c10182010-02-12 22:17:39 +00002637 IDNS |= Decl::IDNS_Ordinary;
2638
2639 // We may already have an enum of the same name; try to find and match it.
2640 if (!DC->isFunctionOrMethod() && SearchName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002641 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002642 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002643 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002644 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2645 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002646 continue;
2647
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002648 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002649 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor98c10182010-02-12 22:17:39 +00002650 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2651 Found = Tag->getDecl();
2652 }
2653
2654 if (EnumDecl *FoundEnum = dyn_cast<EnumDecl>(Found)) {
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002655 if (IsStructuralMatch(D, FoundEnum))
2656 return Importer.Imported(D, FoundEnum);
Douglas Gregor98c10182010-02-12 22:17:39 +00002657 }
2658
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002659 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002660 }
2661
2662 if (!ConflictingDecls.empty()) {
2663 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2664 ConflictingDecls.data(),
2665 ConflictingDecls.size());
2666 }
2667 }
2668
2669 // Create the enum declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002670 EnumDecl *D2 = EnumDecl::Create(Importer.getToContext(), DC,
2671 Importer.Import(D->getLocStart()),
Craig Topper36250ad2014-05-12 05:36:57 +00002672 Loc, Name.getAsIdentifierInfo(), nullptr,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002673 D->isScoped(), D->isScopedUsingClassTag(),
2674 D->isFixed());
John McCall3e11ebe2010-03-15 10:12:16 +00002675 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00002676 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00002677 D2->setAccess(D->getAccess());
Douglas Gregor3996e242010-02-15 22:01:00 +00002678 D2->setLexicalDeclContext(LexicalDC);
2679 Importer.Imported(D, D2);
Sean Callanan95e74be2011-10-21 02:57:43 +00002680 LexicalDC->addDeclInternal(D2);
Douglas Gregor98c10182010-02-12 22:17:39 +00002681
2682 // Import the integer type.
2683 QualType ToIntegerType = Importer.Import(D->getIntegerType());
2684 if (ToIntegerType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002685 return nullptr;
Douglas Gregor3996e242010-02-15 22:01:00 +00002686 D2->setIntegerType(ToIntegerType);
Douglas Gregor98c10182010-02-12 22:17:39 +00002687
2688 // Import the definition
John McCallf937c022011-10-07 06:10:15 +00002689 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00002690 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002691
Douglas Gregor3996e242010-02-15 22:01:00 +00002692 return D2;
Douglas Gregor98c10182010-02-12 22:17:39 +00002693}
2694
Douglas Gregor5c73e912010-02-11 00:48:18 +00002695Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) {
2696 // If this record has a definition in the translation unit we're coming from,
2697 // but this particular declaration is not that definition, import the
2698 // definition and map to that.
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002699 TagDecl *Definition = D->getDefinition();
Douglas Gregor5c73e912010-02-11 00:48:18 +00002700 if (Definition && Definition != D) {
2701 Decl *ImportedDef = Importer.Import(Definition);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002702 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00002703 return nullptr;
2704
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002705 return Importer.Imported(D, ImportedDef);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002706 }
2707
2708 // Import the major distinguishing characteristics of this record.
2709 DeclContext *DC, *LexicalDC;
2710 DeclarationName Name;
2711 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002712 NamedDecl *ToD;
2713 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002714 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002715 if (ToD)
2716 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00002717
Douglas Gregor5c73e912010-02-11 00:48:18 +00002718 // Figure out what structure name we're looking for.
2719 unsigned IDNS = Decl::IDNS_Tag;
2720 DeclarationName SearchName = Name;
Richard Smithdda56e42011-04-15 14:24:37 +00002721 if (!SearchName && D->getTypedefNameForAnonDecl()) {
2722 SearchName = Importer.Import(D->getTypedefNameForAnonDecl()->getDeclName());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002723 IDNS = Decl::IDNS_Ordinary;
David Blaikiebbafb8a2012-03-11 07:00:24 +00002724 } else if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregor5c73e912010-02-11 00:48:18 +00002725 IDNS |= Decl::IDNS_Ordinary;
2726
2727 // We may already have a record of the same name; try to find and match it.
Craig Topper36250ad2014-05-12 05:36:57 +00002728 RecordDecl *AdoptDecl = nullptr;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002729 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002730 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002731 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002732 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002733 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2734 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor5c73e912010-02-11 00:48:18 +00002735 continue;
2736
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002737 Decl *Found = FoundDecls[I];
Richard Smithdda56e42011-04-15 14:24:37 +00002738 if (TypedefNameDecl *Typedef = dyn_cast<TypedefNameDecl>(Found)) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002739 if (const TagType *Tag = Typedef->getUnderlyingType()->getAs<TagType>())
2740 Found = Tag->getDecl();
2741 }
2742
2743 if (RecordDecl *FoundRecord = dyn_cast<RecordDecl>(Found)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002744 if (D->isAnonymousStructOrUnion() &&
2745 FoundRecord->isAnonymousStructOrUnion()) {
2746 // If both anonymous structs/unions are in a record context, make sure
2747 // they occur in the same location in the context records.
David Blaikie05785d12013-02-20 22:23:23 +00002748 if (Optional<unsigned> Index1
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002749 = findAnonymousStructOrUnionIndex(D)) {
David Blaikie05785d12013-02-20 22:23:23 +00002750 if (Optional<unsigned> Index2 =
2751 findAnonymousStructOrUnionIndex(FoundRecord)) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00002752 if (*Index1 != *Index2)
2753 continue;
2754 }
2755 }
2756 }
2757
Douglas Gregor25791052010-02-12 00:09:27 +00002758 if (RecordDecl *FoundDef = FoundRecord->getDefinition()) {
Douglas Gregordd6006f2012-07-17 21:16:27 +00002759 if ((SearchName && !D->isCompleteDefinition())
2760 || (D->isCompleteDefinition() &&
2761 D->isAnonymousStructOrUnion()
2762 == FoundDef->isAnonymousStructOrUnion() &&
2763 IsStructuralMatch(D, FoundDef))) {
Douglas Gregor25791052010-02-12 00:09:27 +00002764 // The record types structurally match, or the "from" translation
2765 // unit only had a forward declaration anyway; call it the same
2766 // function.
2767 // FIXME: For C++, we should also merge methods here.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002768 return Importer.Imported(D, FoundDef);
Douglas Gregor25791052010-02-12 00:09:27 +00002769 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00002770 } else if (!D->isCompleteDefinition()) {
Douglas Gregor25791052010-02-12 00:09:27 +00002771 // We have a forward declaration of this type, so adopt that forward
2772 // declaration rather than building a new one.
Sean Callananc94711c2014-03-04 18:11:50 +00002773
2774 // If one or both can be completed from external storage then try one
2775 // last time to complete and compare them before doing this.
2776
2777 if (FoundRecord->hasExternalLexicalStorage() &&
2778 !FoundRecord->isCompleteDefinition())
2779 FoundRecord->getASTContext().getExternalSource()->CompleteType(FoundRecord);
2780 if (D->hasExternalLexicalStorage())
2781 D->getASTContext().getExternalSource()->CompleteType(D);
2782
2783 if (FoundRecord->isCompleteDefinition() &&
2784 D->isCompleteDefinition() &&
2785 !IsStructuralMatch(D, FoundRecord))
2786 continue;
2787
Douglas Gregor25791052010-02-12 00:09:27 +00002788 AdoptDecl = FoundRecord;
2789 continue;
Douglas Gregordd6006f2012-07-17 21:16:27 +00002790 } else if (!SearchName) {
2791 continue;
2792 }
Douglas Gregor5c73e912010-02-11 00:48:18 +00002793 }
2794
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002795 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002796 }
2797
Douglas Gregordd6006f2012-07-17 21:16:27 +00002798 if (!ConflictingDecls.empty() && SearchName) {
Douglas Gregor5c73e912010-02-11 00:48:18 +00002799 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2800 ConflictingDecls.data(),
2801 ConflictingDecls.size());
2802 }
2803 }
2804
2805 // Create the record declaration.
Douglas Gregor3996e242010-02-15 22:01:00 +00002806 RecordDecl *D2 = AdoptDecl;
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002807 SourceLocation StartLoc = Importer.Import(D->getLocStart());
Douglas Gregor3996e242010-02-15 22:01:00 +00002808 if (!D2) {
Sean Callanan8bca9962016-03-28 21:43:01 +00002809 CXXRecordDecl *D2CXX = nullptr;
2810 if (CXXRecordDecl *DCXX = llvm::dyn_cast<CXXRecordDecl>(D)) {
2811 if (DCXX->isLambda()) {
2812 TypeSourceInfo *TInfo = Importer.Import(DCXX->getLambdaTypeInfo());
2813 D2CXX = CXXRecordDecl::CreateLambda(Importer.getToContext(),
2814 DC, TInfo, Loc,
2815 DCXX->isDependentLambda(),
2816 DCXX->isGenericLambda(),
2817 DCXX->getLambdaCaptureDefault());
2818 Decl *CDecl = Importer.Import(DCXX->getLambdaContextDecl());
2819 if (DCXX->getLambdaContextDecl() && !CDecl)
2820 return nullptr;
2821 D2CXX->setLambdaMangling(DCXX->getLambdaManglingNumber(),
2822 CDecl);
2823 } else {
2824 D2CXX = CXXRecordDecl::Create(Importer.getToContext(),
2825 D->getTagKind(),
2826 DC, StartLoc, Loc,
2827 Name.getAsIdentifierInfo());
2828 }
Douglas Gregor3996e242010-02-15 22:01:00 +00002829 D2 = D2CXX;
Douglas Gregordd483172010-02-22 17:42:47 +00002830 D2->setAccess(D->getAccess());
Douglas Gregor25791052010-02-12 00:09:27 +00002831 } else {
Douglas Gregor3996e242010-02-15 22:01:00 +00002832 D2 = RecordDecl::Create(Importer.getToContext(), D->getTagKind(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002833 DC, StartLoc, Loc, Name.getAsIdentifierInfo());
Douglas Gregor5c73e912010-02-11 00:48:18 +00002834 }
Douglas Gregor14454802011-02-25 02:25:35 +00002835
2836 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregor3996e242010-02-15 22:01:00 +00002837 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00002838 LexicalDC->addDeclInternal(D2);
Douglas Gregordd6006f2012-07-17 21:16:27 +00002839 if (D->isAnonymousStructOrUnion())
2840 D2->setAnonymousStructOrUnion(true);
Douglas Gregor5c73e912010-02-11 00:48:18 +00002841 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002842
Douglas Gregor3996e242010-02-15 22:01:00 +00002843 Importer.Imported(D, D2);
Douglas Gregor25791052010-02-12 00:09:27 +00002844
Douglas Gregor95d82832012-01-24 18:36:04 +00002845 if (D->isCompleteDefinition() && ImportDefinition(D, D2, IDK_Default))
Craig Topper36250ad2014-05-12 05:36:57 +00002846 return nullptr;
2847
Douglas Gregor3996e242010-02-15 22:01:00 +00002848 return D2;
Douglas Gregor5c73e912010-02-11 00:48:18 +00002849}
2850
Douglas Gregor98c10182010-02-12 22:17:39 +00002851Decl *ASTNodeImporter::VisitEnumConstantDecl(EnumConstantDecl *D) {
2852 // Import the major distinguishing characteristics of this enumerator.
2853 DeclContext *DC, *LexicalDC;
2854 DeclarationName Name;
2855 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002856 NamedDecl *ToD;
2857 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002858 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002859 if (ToD)
2860 return ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002861
2862 QualType T = Importer.Import(D->getType());
2863 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002864 return nullptr;
Douglas Gregorb4964f72010-02-15 23:54:17 +00002865
Douglas Gregor98c10182010-02-12 22:17:39 +00002866 // Determine whether there are any other declarations with the same name and
2867 // in the same context.
2868 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002869 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor98c10182010-02-12 22:17:39 +00002870 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002871 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002872 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002873 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2874 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor98c10182010-02-12 22:17:39 +00002875 continue;
Douglas Gregor91155082012-11-14 22:29:20 +00002876
2877 if (EnumConstantDecl *FoundEnumConstant
2878 = dyn_cast<EnumConstantDecl>(FoundDecls[I])) {
2879 if (IsStructuralMatch(D, FoundEnumConstant))
2880 return Importer.Imported(D, FoundEnumConstant);
2881 }
2882
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002883 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor98c10182010-02-12 22:17:39 +00002884 }
2885
2886 if (!ConflictingDecls.empty()) {
2887 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2888 ConflictingDecls.data(),
2889 ConflictingDecls.size());
2890 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002891 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00002892 }
2893 }
2894
2895 Expr *Init = Importer.Import(D->getInitExpr());
2896 if (D->getInitExpr() && !Init)
Craig Topper36250ad2014-05-12 05:36:57 +00002897 return nullptr;
2898
Douglas Gregor98c10182010-02-12 22:17:39 +00002899 EnumConstantDecl *ToEnumerator
2900 = EnumConstantDecl::Create(Importer.getToContext(), cast<EnumDecl>(DC), Loc,
2901 Name.getAsIdentifierInfo(), T,
2902 Init, D->getInitVal());
Douglas Gregordd483172010-02-22 17:42:47 +00002903 ToEnumerator->setAccess(D->getAccess());
Douglas Gregor98c10182010-02-12 22:17:39 +00002904 ToEnumerator->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002905 Importer.Imported(D, ToEnumerator);
Sean Callanan95e74be2011-10-21 02:57:43 +00002906 LexicalDC->addDeclInternal(ToEnumerator);
Douglas Gregor98c10182010-02-12 22:17:39 +00002907 return ToEnumerator;
2908}
Douglas Gregor5c73e912010-02-11 00:48:18 +00002909
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002910Decl *ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) {
2911 // Import the major distinguishing characteristics of this function.
2912 DeclContext *DC, *LexicalDC;
2913 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002914 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00002915 NamedDecl *ToD;
2916 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00002917 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00002918 if (ToD)
2919 return ToD;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002920
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002921 // Try to find a function in our own ("to") context with the same name, same
2922 // type, and in the same context as the function we're importing.
2923 if (!LexicalDC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002924 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002925 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002926 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00002927 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002928 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
2929 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002930 continue;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00002931
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002932 if (FunctionDecl *FoundFunction = dyn_cast<FunctionDecl>(FoundDecls[I])) {
Rafael Espindola3ae00052013-05-13 00:12:11 +00002933 if (FoundFunction->hasExternalFormalLinkage() &&
2934 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00002935 if (Importer.IsStructurallyEquivalent(D->getType(),
2936 FoundFunction->getType())) {
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002937 // FIXME: Actually try to merge the body and other attributes.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00002938 return Importer.Imported(D, FoundFunction);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002939 }
2940
2941 // FIXME: Check for overloading more carefully, e.g., by boosting
2942 // Sema::IsOverload out to the AST library.
2943
2944 // Function overloading is okay in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002945 if (Importer.getToContext().getLangOpts().CPlusPlus)
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002946 continue;
2947
2948 // Complain about inconsistent function types.
2949 Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00002950 << Name << D->getType() << FoundFunction->getType();
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002951 Importer.ToDiag(FoundFunction->getLocation(),
2952 diag::note_odr_value_here)
2953 << FoundFunction->getType();
2954 }
2955 }
2956
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00002957 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002958 }
2959
2960 if (!ConflictingDecls.empty()) {
2961 Name = Importer.HandleNameConflict(Name, DC, IDNS,
2962 ConflictingDecls.data(),
2963 ConflictingDecls.size());
2964 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00002965 return nullptr;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002966 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00002967 }
Douglas Gregorb4964f72010-02-15 23:54:17 +00002968
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002969 DeclarationNameInfo NameInfo(Name, Loc);
2970 // Import additional name location/type info.
2971 ImportDeclarationNameLoc(D->getNameInfo(), NameInfo);
2972
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002973 QualType FromTy = D->getType();
2974 bool usedDifferentExceptionSpec = false;
2975
2976 if (const FunctionProtoType *
2977 FromFPT = D->getType()->getAs<FunctionProtoType>()) {
2978 FunctionProtoType::ExtProtoInfo FromEPI = FromFPT->getExtProtoInfo();
2979 // FunctionProtoType::ExtProtoInfo's ExceptionSpecDecl can point to the
2980 // FunctionDecl that we are importing the FunctionProtoType for.
2981 // To avoid an infinite recursion when importing, create the FunctionDecl
2982 // with a simplified function type and update it afterwards.
Richard Smith8acb4282014-07-31 21:57:55 +00002983 if (FromEPI.ExceptionSpec.SourceDecl ||
2984 FromEPI.ExceptionSpec.SourceTemplate ||
2985 FromEPI.ExceptionSpec.NoexceptExpr) {
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002986 FunctionProtoType::ExtProtoInfo DefaultEPI;
2987 FromTy = Importer.getFromContext().getFunctionType(
Alp Toker314cc812014-01-25 16:55:45 +00002988 FromFPT->getReturnType(), FromFPT->getParamTypes(), DefaultEPI);
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002989 usedDifferentExceptionSpec = true;
2990 }
2991 }
2992
Douglas Gregorb4964f72010-02-15 23:54:17 +00002993 // Import the type.
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00002994 QualType T = Importer.Import(FromTy);
Douglas Gregorb4964f72010-02-15 23:54:17 +00002995 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00002996 return nullptr;
2997
Douglas Gregorbb7930c2010-02-10 19:54:31 +00002998 // Import the function parameters.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002999 SmallVector<ParmVarDecl *, 8> Parameters;
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00003000 for (auto P : D->params()) {
3001 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(P));
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003002 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003003 return nullptr;
3004
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003005 Parameters.push_back(ToP);
3006 }
3007
3008 // Create the imported function.
3009 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Craig Topper36250ad2014-05-12 05:36:57 +00003010 FunctionDecl *ToFunction = nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003011 SourceLocation InnerLocStart = Importer.Import(D->getInnerLocStart());
Douglas Gregor00eace12010-02-21 18:29:16 +00003012 if (CXXConstructorDecl *FromConstructor = dyn_cast<CXXConstructorDecl>(D)) {
3013 ToFunction = CXXConstructorDecl::Create(Importer.getToContext(),
3014 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003015 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003016 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003017 FromConstructor->isExplicit(),
3018 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003019 D->isImplicit(),
3020 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003021 } else if (isa<CXXDestructorDecl>(D)) {
3022 ToFunction = CXXDestructorDecl::Create(Importer.getToContext(),
3023 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003024 InnerLocStart,
Craig Silversteinaf8808d2010-10-21 00:44:50 +00003025 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003026 D->isInlineSpecified(),
3027 D->isImplicit());
3028 } else if (CXXConversionDecl *FromConversion
3029 = dyn_cast<CXXConversionDecl>(D)) {
3030 ToFunction = CXXConversionDecl::Create(Importer.getToContext(),
3031 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003032 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003033 NameInfo, T, TInfo,
Douglas Gregor00eace12010-02-21 18:29:16 +00003034 D->isInlineSpecified(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003035 FromConversion->isExplicit(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003036 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003037 Importer.Import(D->getLocEnd()));
Douglas Gregora50ad132010-11-29 16:04:58 +00003038 } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) {
3039 ToFunction = CXXMethodDecl::Create(Importer.getToContext(),
3040 cast<CXXRecordDecl>(DC),
Sean Callanan59721b32015-04-28 18:41:46 +00003041 InnerLocStart,
Douglas Gregora50ad132010-11-29 16:04:58 +00003042 NameInfo, T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003043 Method->getStorageClass(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003044 Method->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003045 D->isConstexpr(),
Douglas Gregorf2f08062011-03-08 17:10:18 +00003046 Importer.Import(D->getLocEnd()));
Douglas Gregor00eace12010-02-21 18:29:16 +00003047 } else {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003048 ToFunction = FunctionDecl::Create(Importer.getToContext(), DC,
Sean Callanan59721b32015-04-28 18:41:46 +00003049 InnerLocStart,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00003050 NameInfo, T, TInfo, D->getStorageClass(),
Douglas Gregor00eace12010-02-21 18:29:16 +00003051 D->isInlineSpecified(),
Richard Smitha77a0a62011-08-15 21:04:07 +00003052 D->hasWrittenPrototype(),
3053 D->isConstexpr());
Douglas Gregor00eace12010-02-21 18:29:16 +00003054 }
John McCall3e11ebe2010-03-15 10:12:16 +00003055
3056 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00003057 ToFunction->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003058 ToFunction->setAccess(D->getAccess());
Douglas Gregor43f54792010-02-17 02:12:47 +00003059 ToFunction->setLexicalDeclContext(LexicalDC);
John McCall08432c82011-01-27 02:37:01 +00003060 ToFunction->setVirtualAsWritten(D->isVirtualAsWritten());
3061 ToFunction->setTrivial(D->isTrivial());
3062 ToFunction->setPure(D->isPure());
Douglas Gregor43f54792010-02-17 02:12:47 +00003063 Importer.Imported(D, ToFunction);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003064
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003065 // Set the parameters.
3066 for (unsigned I = 0, N = Parameters.size(); I != N; ++I) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003067 Parameters[I]->setOwningFunction(ToFunction);
Sean Callanan95e74be2011-10-21 02:57:43 +00003068 ToFunction->addDeclInternal(Parameters[I]);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003069 }
David Blaikie9c70e042011-09-21 18:16:56 +00003070 ToFunction->setParams(Parameters);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003071
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003072 if (usedDifferentExceptionSpec) {
3073 // Update FunctionProtoType::ExtProtoInfo.
3074 QualType T = Importer.Import(D->getType());
3075 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003076 return nullptr;
Argyrios Kyrtzidis2f458532012-09-25 19:26:39 +00003077 ToFunction->setType(T);
Argyrios Kyrtzidisb41791d2012-09-22 01:58:06 +00003078 }
3079
Sean Callanan59721b32015-04-28 18:41:46 +00003080 // Import the body, if any.
3081 if (Stmt *FromBody = D->getBody()) {
3082 if (Stmt *ToBody = Importer.Import(FromBody)) {
3083 ToFunction->setBody(ToBody);
3084 }
3085 }
3086
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003087 // FIXME: Other bits to merge?
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003088
3089 // Add this function to the lexical context.
Sean Callanan95e74be2011-10-21 02:57:43 +00003090 LexicalDC->addDeclInternal(ToFunction);
Douglas Gregor0eaa2bf2010-10-01 23:55:07 +00003091
Douglas Gregor43f54792010-02-17 02:12:47 +00003092 return ToFunction;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003093}
3094
Douglas Gregor00eace12010-02-21 18:29:16 +00003095Decl *ASTNodeImporter::VisitCXXMethodDecl(CXXMethodDecl *D) {
3096 return VisitFunctionDecl(D);
3097}
3098
3099Decl *ASTNodeImporter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
3100 return VisitCXXMethodDecl(D);
3101}
3102
3103Decl *ASTNodeImporter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
3104 return VisitCXXMethodDecl(D);
3105}
3106
3107Decl *ASTNodeImporter::VisitCXXConversionDecl(CXXConversionDecl *D) {
3108 return VisitCXXMethodDecl(D);
3109}
3110
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003111static unsigned getFieldIndex(Decl *F) {
3112 RecordDecl *Owner = dyn_cast<RecordDecl>(F->getDeclContext());
3113 if (!Owner)
3114 return 0;
3115
3116 unsigned Index = 1;
Aaron Ballman629afae2014-03-07 19:56:05 +00003117 for (const auto *D : Owner->noload_decls()) {
3118 if (D == F)
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003119 return Index;
3120
3121 if (isa<FieldDecl>(*D) || isa<IndirectFieldDecl>(*D))
3122 ++Index;
3123 }
3124
3125 return Index;
3126}
3127
Douglas Gregor5c73e912010-02-11 00:48:18 +00003128Decl *ASTNodeImporter::VisitFieldDecl(FieldDecl *D) {
3129 // Import the major distinguishing characteristics of a variable.
3130 DeclContext *DC, *LexicalDC;
3131 DeclarationName Name;
Douglas Gregor5c73e912010-02-11 00:48:18 +00003132 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003133 NamedDecl *ToD;
3134 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003135 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003136 if (ToD)
3137 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003138
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003139 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003140 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003141 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003142 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3143 if (FieldDecl *FoundField = dyn_cast<FieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003144 // For anonymous fields, match up by index.
3145 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3146 continue;
3147
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003148 if (Importer.IsStructurallyEquivalent(D->getType(),
3149 FoundField->getType())) {
3150 Importer.Imported(D, FoundField);
3151 return FoundField;
3152 }
3153
3154 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3155 << Name << D->getType() << FoundField->getType();
3156 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3157 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003158 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003159 }
3160 }
3161
Douglas Gregorb4964f72010-02-15 23:54:17 +00003162 // Import the type.
3163 QualType T = Importer.Import(D->getType());
3164 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003165 return nullptr;
3166
Douglas Gregor5c73e912010-02-11 00:48:18 +00003167 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3168 Expr *BitWidth = Importer.Import(D->getBitWidth());
3169 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003170 return nullptr;
3171
Abramo Bagnaradff19302011-03-08 08:55:46 +00003172 FieldDecl *ToField = FieldDecl::Create(Importer.getToContext(), DC,
3173 Importer.Import(D->getInnerLocStart()),
Douglas Gregor5c73e912010-02-11 00:48:18 +00003174 Loc, Name.getAsIdentifierInfo(),
Richard Smith938f40b2011-06-11 17:19:42 +00003175 T, TInfo, BitWidth, D->isMutable(),
Richard Smith2b013182012-06-10 03:12:00 +00003176 D->getInClassInitStyle());
Douglas Gregordd483172010-02-22 17:42:47 +00003177 ToField->setAccess(D->getAccess());
Douglas Gregor5c73e912010-02-11 00:48:18 +00003178 ToField->setLexicalDeclContext(LexicalDC);
Sean Callanan3a83ea72016-03-03 02:22:05 +00003179 if (Expr *FromInitializer = D->getInClassInitializer()) {
Sean Callananbb33f582016-03-03 01:21:28 +00003180 Expr *ToInitializer = Importer.Import(FromInitializer);
3181 if (ToInitializer)
3182 ToField->setInClassInitializer(ToInitializer);
3183 else
3184 return nullptr;
3185 }
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003186 ToField->setImplicit(D->isImplicit());
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003187 Importer.Imported(D, ToField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003188 LexicalDC->addDeclInternal(ToField);
Douglas Gregor5c73e912010-02-11 00:48:18 +00003189 return ToField;
3190}
3191
Francois Pichet783dd6e2010-11-21 06:08:52 +00003192Decl *ASTNodeImporter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
3193 // Import the major distinguishing characteristics of a variable.
3194 DeclContext *DC, *LexicalDC;
3195 DeclarationName Name;
3196 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003197 NamedDecl *ToD;
3198 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003199 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003200 if (ToD)
3201 return ToD;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003202
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003203 // Determine whether we've already imported this field.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003204 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003205 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003206 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003207 if (IndirectFieldDecl *FoundField
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003208 = dyn_cast<IndirectFieldDecl>(FoundDecls[I])) {
Douglas Gregorceb32bf2012-10-26 16:45:11 +00003209 // For anonymous indirect fields, match up by index.
3210 if (!Name && getFieldIndex(D) != getFieldIndex(FoundField))
3211 continue;
3212
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003213 if (Importer.IsStructurallyEquivalent(D->getType(),
Douglas Gregordd6006f2012-07-17 21:16:27 +00003214 FoundField->getType(),
David Blaikie7d170102013-05-15 07:37:26 +00003215 !Name.isEmpty())) {
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003216 Importer.Imported(D, FoundField);
3217 return FoundField;
3218 }
Douglas Gregordd6006f2012-07-17 21:16:27 +00003219
3220 // If there are more anonymous fields to check, continue.
3221 if (!Name && I < N-1)
3222 continue;
3223
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003224 Importer.ToDiag(Loc, diag::err_odr_field_type_inconsistent)
3225 << Name << D->getType() << FoundField->getType();
3226 Importer.ToDiag(FoundField->getLocation(), diag::note_odr_value_here)
3227 << FoundField->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003228 return nullptr;
Douglas Gregor03d1ed32011-10-14 21:54:42 +00003229 }
3230 }
3231
Francois Pichet783dd6e2010-11-21 06:08:52 +00003232 // Import the type.
3233 QualType T = Importer.Import(D->getType());
3234 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003235 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003236
3237 NamedDecl **NamedChain =
3238 new (Importer.getToContext())NamedDecl*[D->getChainingSize()];
3239
3240 unsigned i = 0;
Aaron Ballman29c94602014-03-07 18:36:15 +00003241 for (auto *PI : D->chain()) {
Aaron Ballman13916082014-03-07 18:11:58 +00003242 Decl *D = Importer.Import(PI);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003243 if (!D)
Craig Topper36250ad2014-05-12 05:36:57 +00003244 return nullptr;
Francois Pichet783dd6e2010-11-21 06:08:52 +00003245 NamedChain[i++] = cast<NamedDecl>(D);
3246 }
3247
3248 IndirectFieldDecl *ToIndirectField = IndirectFieldDecl::Create(
Aaron Ballman260995b2014-10-15 16:58:18 +00003249 Importer.getToContext(), DC, Loc, Name.getAsIdentifierInfo(), T,
3250 NamedChain, D->getChainingSize());
3251
3252 for (const auto *Attr : D->attrs())
3253 ToIndirectField->addAttr(Attr->clone(Importer.getToContext()));
3254
Francois Pichet783dd6e2010-11-21 06:08:52 +00003255 ToIndirectField->setAccess(D->getAccess());
3256 ToIndirectField->setLexicalDeclContext(LexicalDC);
3257 Importer.Imported(D, ToIndirectField);
Sean Callanan95e74be2011-10-21 02:57:43 +00003258 LexicalDC->addDeclInternal(ToIndirectField);
Francois Pichet783dd6e2010-11-21 06:08:52 +00003259 return ToIndirectField;
3260}
3261
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003262Decl *ASTNodeImporter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
3263 // Import the major distinguishing characteristics of an ivar.
3264 DeclContext *DC, *LexicalDC;
3265 DeclarationName Name;
3266 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003267 NamedDecl *ToD;
3268 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003269 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003270 if (ToD)
3271 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003272
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003273 // Determine whether we've already imported this ivar
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003274 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003275 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003276 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3277 if (ObjCIvarDecl *FoundIvar = dyn_cast<ObjCIvarDecl>(FoundDecls[I])) {
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003278 if (Importer.IsStructurallyEquivalent(D->getType(),
3279 FoundIvar->getType())) {
3280 Importer.Imported(D, FoundIvar);
3281 return FoundIvar;
3282 }
3283
3284 Importer.ToDiag(Loc, diag::err_odr_ivar_type_inconsistent)
3285 << Name << D->getType() << FoundIvar->getType();
3286 Importer.ToDiag(FoundIvar->getLocation(), diag::note_odr_value_here)
3287 << FoundIvar->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003288 return nullptr;
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003289 }
3290 }
3291
3292 // Import the type.
3293 QualType T = Importer.Import(D->getType());
3294 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003295 return nullptr;
3296
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003297 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3298 Expr *BitWidth = Importer.Import(D->getBitWidth());
3299 if (!BitWidth && D->getBitWidth())
Craig Topper36250ad2014-05-12 05:36:57 +00003300 return nullptr;
3301
Daniel Dunbarfe3ead72010-04-02 20:10:03 +00003302 ObjCIvarDecl *ToIvar = ObjCIvarDecl::Create(Importer.getToContext(),
3303 cast<ObjCContainerDecl>(DC),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003304 Importer.Import(D->getInnerLocStart()),
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003305 Loc, Name.getAsIdentifierInfo(),
3306 T, TInfo, D->getAccessControl(),
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003307 BitWidth, D->getSynthesize());
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003308 ToIvar->setLexicalDeclContext(LexicalDC);
3309 Importer.Imported(D, ToIvar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003310 LexicalDC->addDeclInternal(ToIvar);
Douglas Gregor7244b0b2010-02-17 00:34:30 +00003311 return ToIvar;
3312
3313}
3314
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003315Decl *ASTNodeImporter::VisitVarDecl(VarDecl *D) {
3316 // Import the major distinguishing characteristics of a variable.
3317 DeclContext *DC, *LexicalDC;
3318 DeclarationName Name;
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003319 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003320 NamedDecl *ToD;
3321 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003322 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003323 if (ToD)
3324 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003325
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003326 // Try to find a variable in our own ("to") context with the same name and
3327 // in the same context as the variable we're importing.
Douglas Gregor62d311f2010-02-09 19:21:46 +00003328 if (D->isFileVarDecl()) {
Craig Topper36250ad2014-05-12 05:36:57 +00003329 VarDecl *MergeWithVar = nullptr;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003330 SmallVector<NamedDecl *, 4> ConflictingDecls;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003331 unsigned IDNS = Decl::IDNS_Ordinary;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003332 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003333 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003334 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3335 if (!FoundDecls[I]->isInIdentifierNamespace(IDNS))
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003336 continue;
3337
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003338 if (VarDecl *FoundVar = dyn_cast<VarDecl>(FoundDecls[I])) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003339 // We have found a variable that we may need to merge with. Check it.
Rafael Espindola3ae00052013-05-13 00:12:11 +00003340 if (FoundVar->hasExternalFormalLinkage() &&
3341 D->hasExternalFormalLinkage()) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003342 if (Importer.IsStructurallyEquivalent(D->getType(),
3343 FoundVar->getType())) {
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003344 MergeWithVar = FoundVar;
3345 break;
3346 }
3347
Douglas Gregor56521c52010-02-12 17:23:39 +00003348 const ArrayType *FoundArray
3349 = Importer.getToContext().getAsArrayType(FoundVar->getType());
3350 const ArrayType *TArray
Douglas Gregorb4964f72010-02-15 23:54:17 +00003351 = Importer.getToContext().getAsArrayType(D->getType());
Douglas Gregor56521c52010-02-12 17:23:39 +00003352 if (FoundArray && TArray) {
3353 if (isa<IncompleteArrayType>(FoundArray) &&
3354 isa<ConstantArrayType>(TArray)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00003355 // Import the type.
3356 QualType T = Importer.Import(D->getType());
3357 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003358 return nullptr;
3359
Douglas Gregor56521c52010-02-12 17:23:39 +00003360 FoundVar->setType(T);
3361 MergeWithVar = FoundVar;
3362 break;
3363 } else if (isa<IncompleteArrayType>(TArray) &&
3364 isa<ConstantArrayType>(FoundArray)) {
3365 MergeWithVar = FoundVar;
3366 break;
Douglas Gregor2fbe5582010-02-10 17:16:49 +00003367 }
3368 }
3369
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003370 Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent)
Douglas Gregorb4964f72010-02-15 23:54:17 +00003371 << Name << D->getType() << FoundVar->getType();
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003372 Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here)
3373 << FoundVar->getType();
3374 }
3375 }
3376
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003377 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003378 }
3379
3380 if (MergeWithVar) {
3381 // An equivalent variable with external linkage has been found. Link
3382 // the two declarations, then merge them.
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003383 Importer.Imported(D, MergeWithVar);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003384
3385 if (VarDecl *DDef = D->getDefinition()) {
3386 if (VarDecl *ExistingDef = MergeWithVar->getDefinition()) {
3387 Importer.ToDiag(ExistingDef->getLocation(),
3388 diag::err_odr_variable_multiple_def)
3389 << Name;
3390 Importer.FromDiag(DDef->getLocation(), diag::note_odr_defined_here);
3391 } else {
3392 Expr *Init = Importer.Import(DDef->getInit());
Douglas Gregord5058122010-02-11 01:19:42 +00003393 MergeWithVar->setInit(Init);
Richard Smithd0b4dd62011-12-19 06:19:21 +00003394 if (DDef->isInitKnownICE()) {
3395 EvaluatedStmt *Eval = MergeWithVar->ensureEvaluatedStmt();
3396 Eval->CheckedICE = true;
3397 Eval->IsICE = DDef->isInitICE();
3398 }
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003399 }
3400 }
3401
3402 return MergeWithVar;
3403 }
3404
3405 if (!ConflictingDecls.empty()) {
3406 Name = Importer.HandleNameConflict(Name, DC, IDNS,
3407 ConflictingDecls.data(),
3408 ConflictingDecls.size());
3409 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003410 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003411 }
3412 }
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003413
Douglas Gregorb4964f72010-02-15 23:54:17 +00003414 // Import the type.
3415 QualType T = Importer.Import(D->getType());
3416 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003417 return nullptr;
3418
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003419 // Create the imported variable.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003420 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
Abramo Bagnaradff19302011-03-08 08:55:46 +00003421 VarDecl *ToVar = VarDecl::Create(Importer.getToContext(), DC,
3422 Importer.Import(D->getInnerLocStart()),
3423 Loc, Name.getAsIdentifierInfo(),
3424 T, TInfo,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003425 D->getStorageClass());
Douglas Gregor14454802011-02-25 02:25:35 +00003426 ToVar->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregordd483172010-02-22 17:42:47 +00003427 ToVar->setAccess(D->getAccess());
Douglas Gregor62d311f2010-02-09 19:21:46 +00003428 ToVar->setLexicalDeclContext(LexicalDC);
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003429 Importer.Imported(D, ToVar);
Sean Callanan95e74be2011-10-21 02:57:43 +00003430 LexicalDC->addDeclInternal(ToVar);
Douglas Gregor62d311f2010-02-09 19:21:46 +00003431
Sean Callanan59721b32015-04-28 18:41:46 +00003432 if (!D->isFileVarDecl() &&
3433 D->isUsed())
3434 ToVar->setIsUsed();
3435
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003436 // Merge the initializer.
Larisse Voufo39a1e502013-08-06 01:03:05 +00003437 if (ImportDefinition(D, ToVar))
Craig Topper36250ad2014-05-12 05:36:57 +00003438 return nullptr;
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003439
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00003440 return ToVar;
3441}
3442
Douglas Gregor8b228d72010-02-17 21:22:52 +00003443Decl *ASTNodeImporter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
3444 // Parameters are created in the translation unit's context, then moved
3445 // into the function declaration's context afterward.
3446 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3447
3448 // Import the name of this declaration.
3449 DeclarationName Name = Importer.Import(D->getDeclName());
3450 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003451 return nullptr;
3452
Douglas Gregor8b228d72010-02-17 21:22:52 +00003453 // Import the location of this declaration.
3454 SourceLocation Loc = Importer.Import(D->getLocation());
3455
3456 // Import the parameter's type.
3457 QualType T = Importer.Import(D->getType());
3458 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003459 return nullptr;
3460
Douglas Gregor8b228d72010-02-17 21:22:52 +00003461 // Create the imported parameter.
3462 ImplicitParamDecl *ToParm
3463 = ImplicitParamDecl::Create(Importer.getToContext(), DC,
3464 Loc, Name.getAsIdentifierInfo(),
3465 T);
3466 return Importer.Imported(D, ToParm);
3467}
3468
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003469Decl *ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) {
3470 // Parameters are created in the translation unit's context, then moved
3471 // into the function declaration's context afterward.
3472 DeclContext *DC = Importer.getToContext().getTranslationUnitDecl();
3473
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00003474 // Import the name of this declaration.
3475 DeclarationName Name = Importer.Import(D->getDeclName());
3476 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00003477 return nullptr;
3478
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003479 // Import the location of this declaration.
3480 SourceLocation Loc = Importer.Import(D->getLocation());
3481
3482 // Import the parameter's type.
3483 QualType T = Importer.Import(D->getType());
3484 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003485 return nullptr;
3486
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003487 // Create the imported parameter.
3488 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
3489 ParmVarDecl *ToParm = ParmVarDecl::Create(Importer.getToContext(), DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00003490 Importer.Import(D->getInnerLocStart()),
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003491 Loc, Name.getAsIdentifierInfo(),
3492 T, TInfo, D->getStorageClass(),
Craig Topper36250ad2014-05-12 05:36:57 +00003493 /*FIXME: Default argument*/nullptr);
John McCallf3cd6652010-03-12 18:31:32 +00003494 ToParm->setHasInheritedDefaultArg(D->hasInheritedDefaultArg());
Sean Callanan59721b32015-04-28 18:41:46 +00003495
3496 if (D->isUsed())
3497 ToParm->setIsUsed();
3498
Douglas Gregor8cdbe642010-02-12 23:44:20 +00003499 return Importer.Imported(D, ToParm);
Douglas Gregorbb7930c2010-02-10 19:54:31 +00003500}
3501
Douglas Gregor43f54792010-02-17 02:12:47 +00003502Decl *ASTNodeImporter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
3503 // Import the major distinguishing characteristics of a method.
3504 DeclContext *DC, *LexicalDC;
3505 DeclarationName Name;
3506 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003507 NamedDecl *ToD;
3508 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003509 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003510 if (ToD)
3511 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003512
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003513 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003514 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003515 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3516 if (ObjCMethodDecl *FoundMethod = dyn_cast<ObjCMethodDecl>(FoundDecls[I])) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003517 if (FoundMethod->isInstanceMethod() != D->isInstanceMethod())
3518 continue;
3519
3520 // Check return types.
Alp Toker314cc812014-01-25 16:55:45 +00003521 if (!Importer.IsStructurallyEquivalent(D->getReturnType(),
3522 FoundMethod->getReturnType())) {
Douglas Gregor43f54792010-02-17 02:12:47 +00003523 Importer.ToDiag(Loc, diag::err_odr_objc_method_result_type_inconsistent)
Alp Toker314cc812014-01-25 16:55:45 +00003524 << D->isInstanceMethod() << Name << D->getReturnType()
3525 << FoundMethod->getReturnType();
Douglas Gregor43f54792010-02-17 02:12:47 +00003526 Importer.ToDiag(FoundMethod->getLocation(),
3527 diag::note_odr_objc_method_here)
3528 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003529 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003530 }
3531
3532 // Check the number of parameters.
3533 if (D->param_size() != FoundMethod->param_size()) {
3534 Importer.ToDiag(Loc, diag::err_odr_objc_method_num_params_inconsistent)
3535 << D->isInstanceMethod() << Name
3536 << D->param_size() << FoundMethod->param_size();
3537 Importer.ToDiag(FoundMethod->getLocation(),
3538 diag::note_odr_objc_method_here)
3539 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003540 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003541 }
3542
3543 // Check parameter types.
3544 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
3545 PEnd = D->param_end(), FoundP = FoundMethod->param_begin();
3546 P != PEnd; ++P, ++FoundP) {
3547 if (!Importer.IsStructurallyEquivalent((*P)->getType(),
3548 (*FoundP)->getType())) {
3549 Importer.FromDiag((*P)->getLocation(),
3550 diag::err_odr_objc_method_param_type_inconsistent)
3551 << D->isInstanceMethod() << Name
3552 << (*P)->getType() << (*FoundP)->getType();
3553 Importer.ToDiag((*FoundP)->getLocation(), diag::note_odr_value_here)
3554 << (*FoundP)->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00003555 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003556 }
3557 }
3558
3559 // Check variadic/non-variadic.
3560 // Check the number of parameters.
3561 if (D->isVariadic() != FoundMethod->isVariadic()) {
3562 Importer.ToDiag(Loc, diag::err_odr_objc_method_variadic_inconsistent)
3563 << D->isInstanceMethod() << Name;
3564 Importer.ToDiag(FoundMethod->getLocation(),
3565 diag::note_odr_objc_method_here)
3566 << D->isInstanceMethod() << Name;
Craig Topper36250ad2014-05-12 05:36:57 +00003567 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003568 }
3569
3570 // FIXME: Any other bits we need to merge?
3571 return Importer.Imported(D, FoundMethod);
3572 }
3573 }
3574
3575 // Import the result type.
Alp Toker314cc812014-01-25 16:55:45 +00003576 QualType ResultTy = Importer.Import(D->getReturnType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003577 if (ResultTy.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00003578 return nullptr;
Douglas Gregor43f54792010-02-17 02:12:47 +00003579
Alp Toker314cc812014-01-25 16:55:45 +00003580 TypeSourceInfo *ReturnTInfo = Importer.Import(D->getReturnTypeSourceInfo());
Douglas Gregor12852d92010-03-08 14:59:44 +00003581
Alp Toker314cc812014-01-25 16:55:45 +00003582 ObjCMethodDecl *ToMethod = ObjCMethodDecl::Create(
3583 Importer.getToContext(), Loc, Importer.Import(D->getLocEnd()),
3584 Name.getObjCSelector(), ResultTy, ReturnTInfo, DC, D->isInstanceMethod(),
3585 D->isVariadic(), D->isPropertyAccessor(), D->isImplicit(), D->isDefined(),
3586 D->getImplementationControl(), D->hasRelatedResultType());
Douglas Gregor43f54792010-02-17 02:12:47 +00003587
3588 // FIXME: When we decide to merge method definitions, we'll need to
3589 // deal with implicit parameters.
3590
3591 // Import the parameters
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003592 SmallVector<ParmVarDecl *, 5> ToParams;
Aaron Ballman43b68be2014-03-07 17:50:17 +00003593 for (auto *FromP : D->params()) {
3594 ParmVarDecl *ToP = cast_or_null<ParmVarDecl>(Importer.Import(FromP));
Douglas Gregor43f54792010-02-17 02:12:47 +00003595 if (!ToP)
Craig Topper36250ad2014-05-12 05:36:57 +00003596 return nullptr;
3597
Douglas Gregor43f54792010-02-17 02:12:47 +00003598 ToParams.push_back(ToP);
3599 }
3600
3601 // Set the parameters.
3602 for (unsigned I = 0, N = ToParams.size(); I != N; ++I) {
3603 ToParams[I]->setOwningFunction(ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003604 ToMethod->addDeclInternal(ToParams[I]);
Douglas Gregor43f54792010-02-17 02:12:47 +00003605 }
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003606 SmallVector<SourceLocation, 12> SelLocs;
3607 D->getSelectorLocs(SelLocs);
3608 ToMethod->setMethodParams(Importer.getToContext(), ToParams, SelLocs);
Douglas Gregor43f54792010-02-17 02:12:47 +00003609
3610 ToMethod->setLexicalDeclContext(LexicalDC);
3611 Importer.Imported(D, ToMethod);
Sean Callanan95e74be2011-10-21 02:57:43 +00003612 LexicalDC->addDeclInternal(ToMethod);
Douglas Gregor43f54792010-02-17 02:12:47 +00003613 return ToMethod;
3614}
3615
Douglas Gregor85f3f952015-07-07 03:57:15 +00003616Decl *ASTNodeImporter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
3617 // Import the major distinguishing characteristics of a category.
3618 DeclContext *DC, *LexicalDC;
3619 DeclarationName Name;
3620 SourceLocation Loc;
3621 NamedDecl *ToD;
3622 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
3623 return nullptr;
3624 if (ToD)
3625 return ToD;
3626
3627 TypeSourceInfo *BoundInfo = Importer.Import(D->getTypeSourceInfo());
3628 if (!BoundInfo)
3629 return nullptr;
3630
3631 ObjCTypeParamDecl *Result = ObjCTypeParamDecl::Create(
3632 Importer.getToContext(), DC,
Douglas Gregor1ac1b632015-07-07 03:58:54 +00003633 D->getVariance(),
3634 Importer.Import(D->getVarianceLoc()),
Douglas Gregore83b9562015-07-07 03:57:53 +00003635 D->getIndex(),
Douglas Gregor85f3f952015-07-07 03:57:15 +00003636 Importer.Import(D->getLocation()),
3637 Name.getAsIdentifierInfo(),
3638 Importer.Import(D->getColonLoc()),
3639 BoundInfo);
3640 Importer.Imported(D, Result);
3641 Result->setLexicalDeclContext(LexicalDC);
3642 return Result;
3643}
3644
Douglas Gregor84c51c32010-02-18 01:47:50 +00003645Decl *ASTNodeImporter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
3646 // Import the major distinguishing characteristics of a category.
3647 DeclContext *DC, *LexicalDC;
3648 DeclarationName Name;
3649 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003650 NamedDecl *ToD;
3651 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003652 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003653 if (ToD)
3654 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00003655
Douglas Gregor84c51c32010-02-18 01:47:50 +00003656 ObjCInterfaceDecl *ToInterface
3657 = cast_or_null<ObjCInterfaceDecl>(Importer.Import(D->getClassInterface()));
3658 if (!ToInterface)
Craig Topper36250ad2014-05-12 05:36:57 +00003659 return nullptr;
3660
Douglas Gregor84c51c32010-02-18 01:47:50 +00003661 // Determine if we've already encountered this category.
3662 ObjCCategoryDecl *MergeWithCategory
3663 = ToInterface->FindCategoryDeclaration(Name.getAsIdentifierInfo());
3664 ObjCCategoryDecl *ToCategory = MergeWithCategory;
3665 if (!ToCategory) {
3666 ToCategory = ObjCCategoryDecl::Create(Importer.getToContext(), DC,
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00003667 Importer.Import(D->getAtStartLoc()),
Douglas Gregor84c51c32010-02-18 01:47:50 +00003668 Loc,
3669 Importer.Import(D->getCategoryNameLoc()),
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +00003670 Name.getAsIdentifierInfo(),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003671 ToInterface,
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003672 /*TypeParamList=*/nullptr,
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00003673 Importer.Import(D->getIvarLBraceLoc()),
3674 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003675 ToCategory->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00003676 LexicalDC->addDeclInternal(ToCategory);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003677 Importer.Imported(D, ToCategory);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00003678 // Import the type parameter list after calling Imported, to avoid
3679 // loops when bringing in their DeclContext.
3680 ToCategory->setTypeParamList(ImportObjCTypeParamList(
3681 D->getTypeParamList()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003682
Douglas Gregor84c51c32010-02-18 01:47:50 +00003683 // Import protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003684 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3685 SmallVector<SourceLocation, 4> ProtocolLocs;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003686 ObjCCategoryDecl::protocol_loc_iterator FromProtoLoc
3687 = D->protocol_loc_begin();
3688 for (ObjCCategoryDecl::protocol_iterator FromProto = D->protocol_begin(),
3689 FromProtoEnd = D->protocol_end();
3690 FromProto != FromProtoEnd;
3691 ++FromProto, ++FromProtoLoc) {
3692 ObjCProtocolDecl *ToProto
3693 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3694 if (!ToProto)
Craig Topper36250ad2014-05-12 05:36:57 +00003695 return nullptr;
Douglas Gregor84c51c32010-02-18 01:47:50 +00003696 Protocols.push_back(ToProto);
3697 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3698 }
3699
3700 // FIXME: If we're merging, make sure that the protocol list is the same.
3701 ToCategory->setProtocolList(Protocols.data(), Protocols.size(),
3702 ProtocolLocs.data(), Importer.getToContext());
3703
3704 } else {
3705 Importer.Imported(D, ToCategory);
3706 }
3707
3708 // Import all of the members of this category.
Douglas Gregor968d6332010-02-21 18:24:45 +00003709 ImportDeclContext(D);
Douglas Gregor84c51c32010-02-18 01:47:50 +00003710
3711 // If we have an implementation, import it as well.
3712 if (D->getImplementation()) {
3713 ObjCCategoryImplDecl *Impl
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00003714 = cast_or_null<ObjCCategoryImplDecl>(
3715 Importer.Import(D->getImplementation()));
Douglas Gregor84c51c32010-02-18 01:47:50 +00003716 if (!Impl)
Craig Topper36250ad2014-05-12 05:36:57 +00003717 return nullptr;
3718
Douglas Gregor84c51c32010-02-18 01:47:50 +00003719 ToCategory->setImplementation(Impl);
3720 }
3721
3722 return ToCategory;
3723}
3724
Douglas Gregor2aa53772012-01-24 17:42:07 +00003725bool ASTNodeImporter::ImportDefinition(ObjCProtocolDecl *From,
3726 ObjCProtocolDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003727 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003728 if (To->getDefinition()) {
Douglas Gregor2e15c842012-02-01 21:00:38 +00003729 if (shouldForceImportDeclContext(Kind))
3730 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003731 return false;
3732 }
3733
3734 // Start the protocol definition
3735 To->startDefinition();
3736
3737 // Import protocols
3738 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3739 SmallVector<SourceLocation, 4> ProtocolLocs;
3740 ObjCProtocolDecl::protocol_loc_iterator
3741 FromProtoLoc = From->protocol_loc_begin();
3742 for (ObjCProtocolDecl::protocol_iterator FromProto = From->protocol_begin(),
3743 FromProtoEnd = From->protocol_end();
3744 FromProto != FromProtoEnd;
3745 ++FromProto, ++FromProtoLoc) {
3746 ObjCProtocolDecl *ToProto
3747 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3748 if (!ToProto)
3749 return true;
3750 Protocols.push_back(ToProto);
3751 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3752 }
3753
3754 // FIXME: If we're merging, make sure that the protocol list is the same.
3755 To->setProtocolList(Protocols.data(), Protocols.size(),
3756 ProtocolLocs.data(), Importer.getToContext());
3757
Douglas Gregor2e15c842012-02-01 21:00:38 +00003758 if (shouldForceImportDeclContext(Kind)) {
3759 // Import all of the members of this protocol.
3760 ImportDeclContext(From, /*ForceImport=*/true);
3761 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003762 return false;
3763}
3764
Douglas Gregor98d156a2010-02-17 16:12:00 +00003765Decl *ASTNodeImporter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003766 // If this protocol has a definition in the translation unit we're coming
3767 // from, but this particular declaration is not that definition, import the
3768 // definition and map to that.
3769 ObjCProtocolDecl *Definition = D->getDefinition();
3770 if (Definition && Definition != D) {
3771 Decl *ImportedDef = Importer.Import(Definition);
3772 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003773 return nullptr;
3774
Douglas Gregor2aa53772012-01-24 17:42:07 +00003775 return Importer.Imported(D, ImportedDef);
3776 }
3777
Douglas Gregor84c51c32010-02-18 01:47:50 +00003778 // Import the major distinguishing characteristics of a protocol.
Douglas Gregor98d156a2010-02-17 16:12:00 +00003779 DeclContext *DC, *LexicalDC;
3780 DeclarationName Name;
3781 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003782 NamedDecl *ToD;
3783 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003784 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003785 if (ToD)
3786 return ToD;
Douglas Gregor98d156a2010-02-17 16:12:00 +00003787
Craig Topper36250ad2014-05-12 05:36:57 +00003788 ObjCProtocolDecl *MergeWithProtocol = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003789 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003790 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003791 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3792 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_ObjCProtocol))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003793 continue;
3794
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003795 if ((MergeWithProtocol = dyn_cast<ObjCProtocolDecl>(FoundDecls[I])))
Douglas Gregor98d156a2010-02-17 16:12:00 +00003796 break;
3797 }
3798
3799 ObjCProtocolDecl *ToProto = MergeWithProtocol;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003800 if (!ToProto) {
3801 ToProto = ObjCProtocolDecl::Create(Importer.getToContext(), DC,
3802 Name.getAsIdentifierInfo(), Loc,
3803 Importer.Import(D->getAtStartLoc()),
Craig Topper36250ad2014-05-12 05:36:57 +00003804 /*PrevDecl=*/nullptr);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003805 ToProto->setLexicalDeclContext(LexicalDC);
3806 LexicalDC->addDeclInternal(ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003807 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003808
3809 Importer.Imported(D, ToProto);
Douglas Gregor98d156a2010-02-17 16:12:00 +00003810
Douglas Gregor2aa53772012-01-24 17:42:07 +00003811 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToProto))
Craig Topper36250ad2014-05-12 05:36:57 +00003812 return nullptr;
3813
Douglas Gregor98d156a2010-02-17 16:12:00 +00003814 return ToProto;
3815}
3816
Sean Callanan0aae0412014-12-10 00:00:37 +00003817Decl *ASTNodeImporter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
3818 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
3819 DeclContext *LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
3820
3821 SourceLocation ExternLoc = Importer.Import(D->getExternLoc());
3822 SourceLocation LangLoc = Importer.Import(D->getLocation());
3823
3824 bool HasBraces = D->hasBraces();
3825
Sean Callananb12a8552014-12-10 21:22:20 +00003826 LinkageSpecDecl *ToLinkageSpec =
3827 LinkageSpecDecl::Create(Importer.getToContext(),
3828 DC,
3829 ExternLoc,
3830 LangLoc,
3831 D->getLanguage(),
3832 HasBraces);
Sean Callanan0aae0412014-12-10 00:00:37 +00003833
3834 if (HasBraces) {
3835 SourceLocation RBraceLoc = Importer.Import(D->getRBraceLoc());
3836 ToLinkageSpec->setRBraceLoc(RBraceLoc);
3837 }
3838
3839 ToLinkageSpec->setLexicalDeclContext(LexicalDC);
3840 LexicalDC->addDeclInternal(ToLinkageSpec);
3841
3842 Importer.Imported(D, ToLinkageSpec);
3843
3844 return ToLinkageSpec;
3845}
3846
Douglas Gregor2aa53772012-01-24 17:42:07 +00003847bool ASTNodeImporter::ImportDefinition(ObjCInterfaceDecl *From,
3848 ObjCInterfaceDecl *To,
Douglas Gregor2e15c842012-02-01 21:00:38 +00003849 ImportDefinitionKind Kind) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003850 if (To->getDefinition()) {
3851 // Check consistency of superclass.
3852 ObjCInterfaceDecl *FromSuper = From->getSuperClass();
3853 if (FromSuper) {
3854 FromSuper = cast_or_null<ObjCInterfaceDecl>(Importer.Import(FromSuper));
3855 if (!FromSuper)
3856 return true;
3857 }
3858
3859 ObjCInterfaceDecl *ToSuper = To->getSuperClass();
3860 if ((bool)FromSuper != (bool)ToSuper ||
3861 (FromSuper && !declaresSameEntity(FromSuper, ToSuper))) {
3862 Importer.ToDiag(To->getLocation(),
3863 diag::err_odr_objc_superclass_inconsistent)
3864 << To->getDeclName();
3865 if (ToSuper)
3866 Importer.ToDiag(To->getSuperClassLoc(), diag::note_odr_objc_superclass)
3867 << To->getSuperClass()->getDeclName();
3868 else
3869 Importer.ToDiag(To->getLocation(),
3870 diag::note_odr_objc_missing_superclass);
3871 if (From->getSuperClass())
3872 Importer.FromDiag(From->getSuperClassLoc(),
3873 diag::note_odr_objc_superclass)
3874 << From->getSuperClass()->getDeclName();
3875 else
3876 Importer.FromDiag(From->getLocation(),
3877 diag::note_odr_objc_missing_superclass);
3878 }
3879
Douglas Gregor2e15c842012-02-01 21:00:38 +00003880 if (shouldForceImportDeclContext(Kind))
3881 ImportDeclContext(From);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003882 return false;
3883 }
3884
3885 // Start the definition.
3886 To->startDefinition();
3887
3888 // If this class has a superclass, import it.
3889 if (From->getSuperClass()) {
Douglas Gregore9d95f12015-07-07 03:57:35 +00003890 TypeSourceInfo *SuperTInfo = Importer.Import(From->getSuperClassTInfo());
3891 if (!SuperTInfo)
Douglas Gregor2aa53772012-01-24 17:42:07 +00003892 return true;
Douglas Gregore9d95f12015-07-07 03:57:35 +00003893
3894 To->setSuperClass(SuperTInfo);
Douglas Gregor2aa53772012-01-24 17:42:07 +00003895 }
3896
3897 // Import protocols
3898 SmallVector<ObjCProtocolDecl *, 4> Protocols;
3899 SmallVector<SourceLocation, 4> ProtocolLocs;
3900 ObjCInterfaceDecl::protocol_loc_iterator
3901 FromProtoLoc = From->protocol_loc_begin();
3902
3903 for (ObjCInterfaceDecl::protocol_iterator FromProto = From->protocol_begin(),
3904 FromProtoEnd = From->protocol_end();
3905 FromProto != FromProtoEnd;
3906 ++FromProto, ++FromProtoLoc) {
3907 ObjCProtocolDecl *ToProto
3908 = cast_or_null<ObjCProtocolDecl>(Importer.Import(*FromProto));
3909 if (!ToProto)
3910 return true;
3911 Protocols.push_back(ToProto);
3912 ProtocolLocs.push_back(Importer.Import(*FromProtoLoc));
3913 }
3914
3915 // FIXME: If we're merging, make sure that the protocol list is the same.
3916 To->setProtocolList(Protocols.data(), Protocols.size(),
3917 ProtocolLocs.data(), Importer.getToContext());
3918
3919 // Import categories. When the categories themselves are imported, they'll
3920 // hook themselves into this interface.
Aaron Ballman15063e12014-03-13 21:35:02 +00003921 for (auto *Cat : From->known_categories())
3922 Importer.Import(Cat);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00003923
Douglas Gregor2aa53772012-01-24 17:42:07 +00003924 // If we have an @implementation, import it as well.
3925 if (From->getImplementation()) {
3926 ObjCImplementationDecl *Impl = cast_or_null<ObjCImplementationDecl>(
3927 Importer.Import(From->getImplementation()));
3928 if (!Impl)
3929 return true;
3930
3931 To->setImplementation(Impl);
3932 }
3933
Douglas Gregor2e15c842012-02-01 21:00:38 +00003934 if (shouldForceImportDeclContext(Kind)) {
3935 // Import all of the members of this class.
3936 ImportDeclContext(From, /*ForceImport=*/true);
3937 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00003938 return false;
3939}
3940
Douglas Gregor85f3f952015-07-07 03:57:15 +00003941ObjCTypeParamList *
3942ASTNodeImporter::ImportObjCTypeParamList(ObjCTypeParamList *list) {
3943 if (!list)
3944 return nullptr;
3945
3946 SmallVector<ObjCTypeParamDecl *, 4> toTypeParams;
3947 for (auto fromTypeParam : *list) {
3948 auto toTypeParam = cast_or_null<ObjCTypeParamDecl>(
3949 Importer.Import(fromTypeParam));
3950 if (!toTypeParam)
3951 return nullptr;
3952
3953 toTypeParams.push_back(toTypeParam);
3954 }
3955
3956 return ObjCTypeParamList::create(Importer.getToContext(),
3957 Importer.Import(list->getLAngleLoc()),
3958 toTypeParams,
3959 Importer.Import(list->getRAngleLoc()));
3960}
3961
Douglas Gregor45635322010-02-16 01:20:57 +00003962Decl *ASTNodeImporter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
Douglas Gregor2aa53772012-01-24 17:42:07 +00003963 // If this class has a definition in the translation unit we're coming from,
3964 // but this particular declaration is not that definition, import the
3965 // definition and map to that.
3966 ObjCInterfaceDecl *Definition = D->getDefinition();
3967 if (Definition && Definition != D) {
3968 Decl *ImportedDef = Importer.Import(Definition);
3969 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00003970 return nullptr;
3971
Douglas Gregor2aa53772012-01-24 17:42:07 +00003972 return Importer.Imported(D, ImportedDef);
3973 }
3974
Douglas Gregor45635322010-02-16 01:20:57 +00003975 // Import the major distinguishing characteristics of an @interface.
3976 DeclContext *DC, *LexicalDC;
3977 DeclarationName Name;
3978 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00003979 NamedDecl *ToD;
3980 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00003981 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00003982 if (ToD)
3983 return ToD;
Douglas Gregor45635322010-02-16 01:20:57 +00003984
Douglas Gregor2aa53772012-01-24 17:42:07 +00003985 // Look for an existing interface with the same name.
Craig Topper36250ad2014-05-12 05:36:57 +00003986 ObjCInterfaceDecl *MergeWithIface = nullptr;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00003987 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00003988 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003989 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
3990 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregor45635322010-02-16 01:20:57 +00003991 continue;
3992
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00003993 if ((MergeWithIface = dyn_cast<ObjCInterfaceDecl>(FoundDecls[I])))
Douglas Gregor45635322010-02-16 01:20:57 +00003994 break;
3995 }
3996
Douglas Gregor2aa53772012-01-24 17:42:07 +00003997 // Create an interface declaration, if one does not already exist.
Douglas Gregor45635322010-02-16 01:20:57 +00003998 ObjCInterfaceDecl *ToIface = MergeWithIface;
Douglas Gregor2aa53772012-01-24 17:42:07 +00003999 if (!ToIface) {
4000 ToIface = ObjCInterfaceDecl::Create(Importer.getToContext(), DC,
4001 Importer.Import(D->getAtStartLoc()),
Douglas Gregor85f3f952015-07-07 03:57:15 +00004002 Name.getAsIdentifierInfo(),
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004003 /*TypeParamList=*/nullptr,
Craig Topper36250ad2014-05-12 05:36:57 +00004004 /*PrevDecl=*/nullptr, Loc,
Douglas Gregor2aa53772012-01-24 17:42:07 +00004005 D->isImplicitInterfaceDecl());
4006 ToIface->setLexicalDeclContext(LexicalDC);
4007 LexicalDC->addDeclInternal(ToIface);
Douglas Gregor45635322010-02-16 01:20:57 +00004008 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00004009 Importer.Imported(D, ToIface);
Douglas Gregorab7f0b32015-07-07 06:20:12 +00004010 // Import the type parameter list after calling Imported, to avoid
4011 // loops when bringing in their DeclContext.
4012 ToIface->setTypeParamList(ImportObjCTypeParamList(
4013 D->getTypeParamListAsWritten()));
Douglas Gregor45635322010-02-16 01:20:57 +00004014
Douglas Gregor2aa53772012-01-24 17:42:07 +00004015 if (D->isThisDeclarationADefinition() && ImportDefinition(D, ToIface))
Craig Topper36250ad2014-05-12 05:36:57 +00004016 return nullptr;
4017
Douglas Gregor98d156a2010-02-17 16:12:00 +00004018 return ToIface;
Douglas Gregor45635322010-02-16 01:20:57 +00004019}
4020
Douglas Gregor4da9d682010-12-07 15:32:12 +00004021Decl *ASTNodeImporter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
4022 ObjCCategoryDecl *Category = cast_or_null<ObjCCategoryDecl>(
4023 Importer.Import(D->getCategoryDecl()));
4024 if (!Category)
Craig Topper36250ad2014-05-12 05:36:57 +00004025 return nullptr;
4026
Douglas Gregor4da9d682010-12-07 15:32:12 +00004027 ObjCCategoryImplDecl *ToImpl = Category->getImplementation();
4028 if (!ToImpl) {
4029 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4030 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004031 return nullptr;
4032
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004033 SourceLocation CategoryNameLoc = Importer.Import(D->getCategoryNameLoc());
Douglas Gregor4da9d682010-12-07 15:32:12 +00004034 ToImpl = ObjCCategoryImplDecl::Create(Importer.getToContext(), DC,
Douglas Gregor4da9d682010-12-07 15:32:12 +00004035 Importer.Import(D->getIdentifier()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004036 Category->getClassInterface(),
4037 Importer.Import(D->getLocation()),
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +00004038 Importer.Import(D->getAtStartLoc()),
4039 CategoryNameLoc);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004040
4041 DeclContext *LexicalDC = DC;
4042 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4043 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4044 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004045 return nullptr;
4046
Douglas Gregor4da9d682010-12-07 15:32:12 +00004047 ToImpl->setLexicalDeclContext(LexicalDC);
4048 }
4049
Sean Callanan95e74be2011-10-21 02:57:43 +00004050 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004051 Category->setImplementation(ToImpl);
4052 }
4053
4054 Importer.Imported(D, ToImpl);
Douglas Gregor35fd7bc2010-12-08 16:41:55 +00004055 ImportDeclContext(D);
Douglas Gregor4da9d682010-12-07 15:32:12 +00004056 return ToImpl;
4057}
4058
Douglas Gregorda8025c2010-12-07 01:26:03 +00004059Decl *ASTNodeImporter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
4060 // Find the corresponding interface.
4061 ObjCInterfaceDecl *Iface = cast_or_null<ObjCInterfaceDecl>(
4062 Importer.Import(D->getClassInterface()));
4063 if (!Iface)
Craig Topper36250ad2014-05-12 05:36:57 +00004064 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004065
4066 // Import the superclass, if any.
Craig Topper36250ad2014-05-12 05:36:57 +00004067 ObjCInterfaceDecl *Super = nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004068 if (D->getSuperClass()) {
4069 Super = cast_or_null<ObjCInterfaceDecl>(
4070 Importer.Import(D->getSuperClass()));
4071 if (!Super)
Craig Topper36250ad2014-05-12 05:36:57 +00004072 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004073 }
4074
4075 ObjCImplementationDecl *Impl = Iface->getImplementation();
4076 if (!Impl) {
4077 // We haven't imported an implementation yet. Create a new @implementation
4078 // now.
4079 Impl = ObjCImplementationDecl::Create(Importer.getToContext(),
4080 Importer.ImportContext(D->getDeclContext()),
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00004081 Iface, Super,
Douglas Gregorda8025c2010-12-07 01:26:03 +00004082 Importer.Import(D->getLocation()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004083 Importer.Import(D->getAtStartLoc()),
Argyrios Kyrtzidis5d2ce842013-05-03 22:31:26 +00004084 Importer.Import(D->getSuperClassLoc()),
Fariborz Jahaniana7765fe2012-02-20 20:09:20 +00004085 Importer.Import(D->getIvarLBraceLoc()),
4086 Importer.Import(D->getIvarRBraceLoc()));
Douglas Gregorda8025c2010-12-07 01:26:03 +00004087
4088 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4089 DeclContext *LexicalDC
4090 = Importer.ImportContext(D->getLexicalDeclContext());
4091 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004092 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004093 Impl->setLexicalDeclContext(LexicalDC);
4094 }
4095
4096 // Associate the implementation with the class it implements.
4097 Iface->setImplementation(Impl);
4098 Importer.Imported(D, Iface->getImplementation());
4099 } else {
4100 Importer.Imported(D, Iface->getImplementation());
4101
4102 // Verify that the existing @implementation has the same superclass.
4103 if ((Super && !Impl->getSuperClass()) ||
4104 (!Super && Impl->getSuperClass()) ||
Craig Topperdcfc60f2014-05-07 06:57:44 +00004105 (Super && Impl->getSuperClass() &&
4106 !declaresSameEntity(Super->getCanonicalDecl(),
4107 Impl->getSuperClass()))) {
4108 Importer.ToDiag(Impl->getLocation(),
4109 diag::err_odr_objc_superclass_inconsistent)
4110 << Iface->getDeclName();
4111 // FIXME: It would be nice to have the location of the superclass
4112 // below.
4113 if (Impl->getSuperClass())
4114 Importer.ToDiag(Impl->getLocation(),
4115 diag::note_odr_objc_superclass)
4116 << Impl->getSuperClass()->getDeclName();
4117 else
4118 Importer.ToDiag(Impl->getLocation(),
4119 diag::note_odr_objc_missing_superclass);
4120 if (D->getSuperClass())
4121 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004122 diag::note_odr_objc_superclass)
Craig Topperdcfc60f2014-05-07 06:57:44 +00004123 << D->getSuperClass()->getDeclName();
4124 else
4125 Importer.FromDiag(D->getLocation(),
Douglas Gregorda8025c2010-12-07 01:26:03 +00004126 diag::note_odr_objc_missing_superclass);
Craig Topper36250ad2014-05-12 05:36:57 +00004127 return nullptr;
Douglas Gregorda8025c2010-12-07 01:26:03 +00004128 }
4129 }
4130
4131 // Import all of the members of this @implementation.
4132 ImportDeclContext(D);
4133
4134 return Impl;
4135}
4136
Douglas Gregora11c4582010-02-17 18:02:10 +00004137Decl *ASTNodeImporter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
4138 // Import the major distinguishing characteristics of an @property.
4139 DeclContext *DC, *LexicalDC;
4140 DeclarationName Name;
4141 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004142 NamedDecl *ToD;
4143 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004144 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004145 if (ToD)
4146 return ToD;
Douglas Gregora11c4582010-02-17 18:02:10 +00004147
4148 // Check whether we have already imported this property.
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004149 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004150 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004151 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004152 if (ObjCPropertyDecl *FoundProp
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004153 = dyn_cast<ObjCPropertyDecl>(FoundDecls[I])) {
Douglas Gregora11c4582010-02-17 18:02:10 +00004154 // Check property types.
4155 if (!Importer.IsStructurallyEquivalent(D->getType(),
4156 FoundProp->getType())) {
4157 Importer.ToDiag(Loc, diag::err_odr_objc_property_type_inconsistent)
4158 << Name << D->getType() << FoundProp->getType();
4159 Importer.ToDiag(FoundProp->getLocation(), diag::note_odr_value_here)
4160 << FoundProp->getType();
Craig Topper36250ad2014-05-12 05:36:57 +00004161 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004162 }
4163
4164 // FIXME: Check property attributes, getters, setters, etc.?
4165
4166 // Consider these properties to be equivalent.
4167 Importer.Imported(D, FoundProp);
4168 return FoundProp;
4169 }
4170 }
4171
4172 // Import the type.
Douglas Gregor813a0662015-06-19 18:14:38 +00004173 TypeSourceInfo *TSI = Importer.Import(D->getTypeSourceInfo());
4174 if (!TSI)
Craig Topper36250ad2014-05-12 05:36:57 +00004175 return nullptr;
Douglas Gregora11c4582010-02-17 18:02:10 +00004176
4177 // Create the new property.
4178 ObjCPropertyDecl *ToProperty
4179 = ObjCPropertyDecl::Create(Importer.getToContext(), DC, Loc,
4180 Name.getAsIdentifierInfo(),
4181 Importer.Import(D->getAtLoc()),
Fariborz Jahanian86c2f5c2012-02-29 22:18:55 +00004182 Importer.Import(D->getLParenLoc()),
Douglas Gregor813a0662015-06-19 18:14:38 +00004183 Importer.Import(D->getType()),
4184 TSI,
Douglas Gregora11c4582010-02-17 18:02:10 +00004185 D->getPropertyImplementation());
4186 Importer.Imported(D, ToProperty);
4187 ToProperty->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004188 LexicalDC->addDeclInternal(ToProperty);
Douglas Gregora11c4582010-02-17 18:02:10 +00004189
4190 ToProperty->setPropertyAttributes(D->getPropertyAttributes());
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +00004191 ToProperty->setPropertyAttributesAsWritten(
4192 D->getPropertyAttributesAsWritten());
Douglas Gregora11c4582010-02-17 18:02:10 +00004193 ToProperty->setGetterName(Importer.Import(D->getGetterName()));
4194 ToProperty->setSetterName(Importer.Import(D->getSetterName()));
4195 ToProperty->setGetterMethodDecl(
4196 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getGetterMethodDecl())));
4197 ToProperty->setSetterMethodDecl(
4198 cast_or_null<ObjCMethodDecl>(Importer.Import(D->getSetterMethodDecl())));
4199 ToProperty->setPropertyIvarDecl(
4200 cast_or_null<ObjCIvarDecl>(Importer.Import(D->getPropertyIvarDecl())));
4201 return ToProperty;
4202}
4203
Douglas Gregor14a49e22010-12-07 18:32:03 +00004204Decl *ASTNodeImporter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
4205 ObjCPropertyDecl *Property = cast_or_null<ObjCPropertyDecl>(
4206 Importer.Import(D->getPropertyDecl()));
4207 if (!Property)
Craig Topper36250ad2014-05-12 05:36:57 +00004208 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004209
4210 DeclContext *DC = Importer.ImportContext(D->getDeclContext());
4211 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004212 return nullptr;
4213
Douglas Gregor14a49e22010-12-07 18:32:03 +00004214 // Import the lexical declaration context.
4215 DeclContext *LexicalDC = DC;
4216 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4217 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4218 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004219 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004220 }
4221
4222 ObjCImplDecl *InImpl = dyn_cast<ObjCImplDecl>(LexicalDC);
4223 if (!InImpl)
Craig Topper36250ad2014-05-12 05:36:57 +00004224 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004225
4226 // Import the ivar (for an @synthesize).
Craig Topper36250ad2014-05-12 05:36:57 +00004227 ObjCIvarDecl *Ivar = nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004228 if (D->getPropertyIvarDecl()) {
4229 Ivar = cast_or_null<ObjCIvarDecl>(
4230 Importer.Import(D->getPropertyIvarDecl()));
4231 if (!Ivar)
Craig Topper36250ad2014-05-12 05:36:57 +00004232 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004233 }
4234
4235 ObjCPropertyImplDecl *ToImpl
Manman Ren5b786402016-01-28 18:49:28 +00004236 = InImpl->FindPropertyImplDecl(Property->getIdentifier(),
4237 Property->getQueryKind());
Douglas Gregor14a49e22010-12-07 18:32:03 +00004238 if (!ToImpl) {
4239 ToImpl = ObjCPropertyImplDecl::Create(Importer.getToContext(), DC,
4240 Importer.Import(D->getLocStart()),
4241 Importer.Import(D->getLocation()),
4242 Property,
4243 D->getPropertyImplementation(),
4244 Ivar,
4245 Importer.Import(D->getPropertyIvarDeclLoc()));
4246 ToImpl->setLexicalDeclContext(LexicalDC);
4247 Importer.Imported(D, ToImpl);
Sean Callanan95e74be2011-10-21 02:57:43 +00004248 LexicalDC->addDeclInternal(ToImpl);
Douglas Gregor14a49e22010-12-07 18:32:03 +00004249 } else {
4250 // Check that we have the same kind of property implementation (@synthesize
4251 // vs. @dynamic).
4252 if (D->getPropertyImplementation() != ToImpl->getPropertyImplementation()) {
4253 Importer.ToDiag(ToImpl->getLocation(),
4254 diag::err_odr_objc_property_impl_kind_inconsistent)
4255 << Property->getDeclName()
4256 << (ToImpl->getPropertyImplementation()
4257 == ObjCPropertyImplDecl::Dynamic);
4258 Importer.FromDiag(D->getLocation(),
4259 diag::note_odr_objc_property_impl_kind)
4260 << D->getPropertyDecl()->getDeclName()
4261 << (D->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic);
Craig Topper36250ad2014-05-12 05:36:57 +00004262 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004263 }
4264
4265 // For @synthesize, check that we have the same
4266 if (D->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize &&
4267 Ivar != ToImpl->getPropertyIvarDecl()) {
4268 Importer.ToDiag(ToImpl->getPropertyIvarDeclLoc(),
4269 diag::err_odr_objc_synthesize_ivar_inconsistent)
4270 << Property->getDeclName()
4271 << ToImpl->getPropertyIvarDecl()->getDeclName()
4272 << Ivar->getDeclName();
4273 Importer.FromDiag(D->getPropertyIvarDeclLoc(),
4274 diag::note_odr_objc_synthesize_ivar_here)
4275 << D->getPropertyIvarDecl()->getDeclName();
Craig Topper36250ad2014-05-12 05:36:57 +00004276 return nullptr;
Douglas Gregor14a49e22010-12-07 18:32:03 +00004277 }
4278
4279 // Merge the existing implementation with the new implementation.
4280 Importer.Imported(D, ToImpl);
4281 }
4282
4283 return ToImpl;
4284}
4285
Douglas Gregora082a492010-11-30 19:14:50 +00004286Decl *ASTNodeImporter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
4287 // For template arguments, we adopt the translation unit as our declaration
4288 // context. This context will be fixed when the actual template declaration
4289 // is created.
4290
4291 // FIXME: Import default argument.
4292 return TemplateTypeParmDecl::Create(Importer.getToContext(),
4293 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnarab3185b02011-03-06 15:48:19 +00004294 Importer.Import(D->getLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004295 Importer.Import(D->getLocation()),
4296 D->getDepth(),
4297 D->getIndex(),
4298 Importer.Import(D->getIdentifier()),
4299 D->wasDeclaredWithTypename(),
4300 D->isParameterPack());
4301}
4302
4303Decl *
4304ASTNodeImporter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
4305 // Import the name of this declaration.
4306 DeclarationName Name = Importer.Import(D->getDeclName());
4307 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004308 return nullptr;
4309
Douglas Gregora082a492010-11-30 19:14:50 +00004310 // Import the location of this declaration.
4311 SourceLocation Loc = Importer.Import(D->getLocation());
4312
4313 // Import the type of this declaration.
4314 QualType T = Importer.Import(D->getType());
4315 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004316 return nullptr;
4317
Douglas Gregora082a492010-11-30 19:14:50 +00004318 // Import type-source information.
4319 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4320 if (D->getTypeSourceInfo() && !TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00004321 return nullptr;
4322
Douglas Gregora082a492010-11-30 19:14:50 +00004323 // FIXME: Import default argument.
4324
4325 return NonTypeTemplateParmDecl::Create(Importer.getToContext(),
4326 Importer.getToContext().getTranslationUnitDecl(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00004327 Importer.Import(D->getInnerLocStart()),
Douglas Gregora082a492010-11-30 19:14:50 +00004328 Loc, D->getDepth(), D->getPosition(),
4329 Name.getAsIdentifierInfo(),
Douglas Gregorda3cc0d2010-12-23 23:51:58 +00004330 T, D->isParameterPack(), TInfo);
Douglas Gregora082a492010-11-30 19:14:50 +00004331}
4332
4333Decl *
4334ASTNodeImporter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
4335 // Import the name of this declaration.
4336 DeclarationName Name = Importer.Import(D->getDeclName());
4337 if (D->getDeclName() && !Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004338 return nullptr;
4339
Douglas Gregora082a492010-11-30 19:14:50 +00004340 // Import the location of this declaration.
4341 SourceLocation Loc = Importer.Import(D->getLocation());
4342
4343 // Import template parameters.
4344 TemplateParameterList *TemplateParams
4345 = ImportTemplateParameterList(D->getTemplateParameters());
4346 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004347 return nullptr;
4348
Douglas Gregora082a492010-11-30 19:14:50 +00004349 // FIXME: Import default argument.
4350
4351 return TemplateTemplateParmDecl::Create(Importer.getToContext(),
4352 Importer.getToContext().getTranslationUnitDecl(),
4353 Loc, D->getDepth(), D->getPosition(),
Douglas Gregorf5500772011-01-05 15:48:55 +00004354 D->isParameterPack(),
Douglas Gregora082a492010-11-30 19:14:50 +00004355 Name.getAsIdentifierInfo(),
4356 TemplateParams);
4357}
4358
4359Decl *ASTNodeImporter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
4360 // If this record has a definition in the translation unit we're coming from,
4361 // but this particular declaration is not that definition, import the
4362 // definition and map to that.
4363 CXXRecordDecl *Definition
4364 = cast_or_null<CXXRecordDecl>(D->getTemplatedDecl()->getDefinition());
4365 if (Definition && Definition != D->getTemplatedDecl()) {
4366 Decl *ImportedDef
4367 = Importer.Import(Definition->getDescribedClassTemplate());
4368 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004369 return nullptr;
4370
Douglas Gregora082a492010-11-30 19:14:50 +00004371 return Importer.Imported(D, ImportedDef);
4372 }
4373
4374 // Import the major distinguishing characteristics of this class template.
4375 DeclContext *DC, *LexicalDC;
4376 DeclarationName Name;
4377 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004378 NamedDecl *ToD;
4379 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004380 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004381 if (ToD)
4382 return ToD;
Craig Topper36250ad2014-05-12 05:36:57 +00004383
Douglas Gregora082a492010-11-30 19:14:50 +00004384 // We may already have a template of the same name; try to find and match it.
4385 if (!DC->isFunctionOrMethod()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004386 SmallVector<NamedDecl *, 4> ConflictingDecls;
Dmitri Gribenkof8579502013-01-12 19:30:44 +00004387 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004388 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004389 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4390 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
Douglas Gregora082a492010-11-30 19:14:50 +00004391 continue;
4392
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004393 Decl *Found = FoundDecls[I];
Douglas Gregora082a492010-11-30 19:14:50 +00004394 if (ClassTemplateDecl *FoundTemplate
4395 = dyn_cast<ClassTemplateDecl>(Found)) {
4396 if (IsStructuralMatch(D, FoundTemplate)) {
4397 // The class templates structurally match; call it the same template.
4398 // FIXME: We may be filling in a forward declaration here. Handle
4399 // this case!
4400 Importer.Imported(D->getTemplatedDecl(),
4401 FoundTemplate->getTemplatedDecl());
4402 return Importer.Imported(D, FoundTemplate);
4403 }
4404 }
4405
Douglas Gregor9e0a5b32011-10-15 00:10:27 +00004406 ConflictingDecls.push_back(FoundDecls[I]);
Douglas Gregora082a492010-11-30 19:14:50 +00004407 }
4408
4409 if (!ConflictingDecls.empty()) {
4410 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4411 ConflictingDecls.data(),
4412 ConflictingDecls.size());
4413 }
4414
4415 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004416 return nullptr;
Douglas Gregora082a492010-11-30 19:14:50 +00004417 }
4418
4419 CXXRecordDecl *DTemplated = D->getTemplatedDecl();
4420
4421 // Create the declaration that is being templated.
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004422 // Create the declaration that is being templated.
4423 CXXRecordDecl *D2Templated = cast_or_null<CXXRecordDecl>(
4424 Importer.Import(DTemplated));
4425 if (!D2Templated)
4426 return nullptr;
4427
4428 // Resolve possible cyclic import.
4429 if (Decl *AlreadyImported = Importer.GetAlreadyImportedOrNull(D))
4430 return AlreadyImported;
4431
Douglas Gregora082a492010-11-30 19:14:50 +00004432 // Create the class template declaration itself.
4433 TemplateParameterList *TemplateParams
4434 = ImportTemplateParameterList(D->getTemplateParameters());
4435 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004436 return nullptr;
4437
Douglas Gregora082a492010-11-30 19:14:50 +00004438 ClassTemplateDecl *D2 = ClassTemplateDecl::Create(Importer.getToContext(), DC,
4439 Loc, Name, TemplateParams,
4440 D2Templated,
Craig Topper36250ad2014-05-12 05:36:57 +00004441 /*PrevDecl=*/nullptr);
Douglas Gregora082a492010-11-30 19:14:50 +00004442 D2Templated->setDescribedClassTemplate(D2);
4443
4444 D2->setAccess(D->getAccess());
4445 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004446 LexicalDC->addDeclInternal(D2);
Douglas Gregora082a492010-11-30 19:14:50 +00004447
4448 // Note the relationship between the class templates.
4449 Importer.Imported(D, D2);
4450 Importer.Imported(DTemplated, D2Templated);
4451
John McCallf937c022011-10-07 06:10:15 +00004452 if (DTemplated->isCompleteDefinition() &&
4453 !D2Templated->isCompleteDefinition()) {
Douglas Gregora082a492010-11-30 19:14:50 +00004454 // FIXME: Import definition!
4455 }
4456
4457 return D2;
4458}
4459
Douglas Gregore2e50d332010-12-01 01:36:18 +00004460Decl *ASTNodeImporter::VisitClassTemplateSpecializationDecl(
4461 ClassTemplateSpecializationDecl *D) {
4462 // If this record has a definition in the translation unit we're coming from,
4463 // but this particular declaration is not that definition, import the
4464 // definition and map to that.
4465 TagDecl *Definition = D->getDefinition();
4466 if (Definition && Definition != D) {
4467 Decl *ImportedDef = Importer.Import(Definition);
4468 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004469 return nullptr;
4470
Douglas Gregore2e50d332010-12-01 01:36:18 +00004471 return Importer.Imported(D, ImportedDef);
4472 }
4473
4474 ClassTemplateDecl *ClassTemplate
4475 = cast_or_null<ClassTemplateDecl>(Importer.Import(
4476 D->getSpecializedTemplate()));
4477 if (!ClassTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004478 return nullptr;
4479
Douglas Gregore2e50d332010-12-01 01:36:18 +00004480 // Import the context of this declaration.
4481 DeclContext *DC = ClassTemplate->getDeclContext();
4482 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004483 return nullptr;
4484
Douglas Gregore2e50d332010-12-01 01:36:18 +00004485 DeclContext *LexicalDC = DC;
4486 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4487 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4488 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004489 return nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004490 }
4491
4492 // Import the location of this declaration.
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004493 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4494 SourceLocation IdLoc = Importer.Import(D->getLocation());
Douglas Gregore2e50d332010-12-01 01:36:18 +00004495
4496 // Import template arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004497 SmallVector<TemplateArgument, 2> TemplateArgs;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004498 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4499 D->getTemplateArgs().size(),
4500 TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004501 return nullptr;
4502
Douglas Gregore2e50d332010-12-01 01:36:18 +00004503 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004504 void *InsertPos = nullptr;
Douglas Gregore2e50d332010-12-01 01:36:18 +00004505 ClassTemplateSpecializationDecl *D2
Craig Topper7e0daca2014-06-26 04:58:53 +00004506 = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004507 if (D2) {
4508 // We already have a class template specialization with these template
4509 // arguments.
4510
4511 // FIXME: Check for specialization vs. instantiation errors.
4512
4513 if (RecordDecl *FoundDef = D2->getDefinition()) {
John McCallf937c022011-10-07 06:10:15 +00004514 if (!D->isCompleteDefinition() || IsStructuralMatch(D, FoundDef)) {
Douglas Gregore2e50d332010-12-01 01:36:18 +00004515 // The record types structurally match, or the "from" translation
4516 // unit only had a forward declaration anyway; call it the same
4517 // function.
4518 return Importer.Imported(D, FoundDef);
4519 }
4520 }
4521 } else {
4522 // Create a new specialization.
4523 D2 = ClassTemplateSpecializationDecl::Create(Importer.getToContext(),
4524 D->getTagKind(), DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004525 StartLoc, IdLoc,
4526 ClassTemplate,
Douglas Gregore2e50d332010-12-01 01:36:18 +00004527 TemplateArgs.data(),
4528 TemplateArgs.size(),
Craig Topper36250ad2014-05-12 05:36:57 +00004529 /*PrevDecl=*/nullptr);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004530 D2->setSpecializationKind(D->getSpecializationKind());
4531
4532 // Add this specialization to the class template.
4533 ClassTemplate->AddSpecialization(D2, InsertPos);
4534
4535 // Import the qualifier, if any.
Douglas Gregor14454802011-02-25 02:25:35 +00004536 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
Douglas Gregore2e50d332010-12-01 01:36:18 +00004537
4538 // Add the specialization to this context.
4539 D2->setLexicalDeclContext(LexicalDC);
Sean Callanan95e74be2011-10-21 02:57:43 +00004540 LexicalDC->addDeclInternal(D2);
Douglas Gregore2e50d332010-12-01 01:36:18 +00004541 }
4542 Importer.Imported(D, D2);
4543
John McCallf937c022011-10-07 06:10:15 +00004544 if (D->isCompleteDefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004545 return nullptr;
4546
Douglas Gregore2e50d332010-12-01 01:36:18 +00004547 return D2;
4548}
4549
Larisse Voufo39a1e502013-08-06 01:03:05 +00004550Decl *ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
4551 // If this variable has a definition in the translation unit we're coming
4552 // from,
4553 // but this particular declaration is not that definition, import the
4554 // definition and map to that.
4555 VarDecl *Definition =
4556 cast_or_null<VarDecl>(D->getTemplatedDecl()->getDefinition());
4557 if (Definition && Definition != D->getTemplatedDecl()) {
4558 Decl *ImportedDef = Importer.Import(Definition->getDescribedVarTemplate());
4559 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004560 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004561
4562 return Importer.Imported(D, ImportedDef);
4563 }
4564
4565 // Import the major distinguishing characteristics of this variable template.
4566 DeclContext *DC, *LexicalDC;
4567 DeclarationName Name;
4568 SourceLocation Loc;
Sean Callanan59721b32015-04-28 18:41:46 +00004569 NamedDecl *ToD;
4570 if (ImportDeclParts(D, DC, LexicalDC, Name, ToD, Loc))
Craig Topper36250ad2014-05-12 05:36:57 +00004571 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00004572 if (ToD)
4573 return ToD;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004574
4575 // We may already have a template of the same name; try to find and match it.
4576 assert(!DC->isFunctionOrMethod() &&
4577 "Variable templates cannot be declared at function scope");
4578 SmallVector<NamedDecl *, 4> ConflictingDecls;
4579 SmallVector<NamedDecl *, 2> FoundDecls;
Sean Callanan49475322014-12-10 03:09:41 +00004580 DC->getRedeclContext()->localUncachedLookup(Name, FoundDecls);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004581 for (unsigned I = 0, N = FoundDecls.size(); I != N; ++I) {
4582 if (!FoundDecls[I]->isInIdentifierNamespace(Decl::IDNS_Ordinary))
4583 continue;
4584
4585 Decl *Found = FoundDecls[I];
4586 if (VarTemplateDecl *FoundTemplate = dyn_cast<VarTemplateDecl>(Found)) {
4587 if (IsStructuralMatch(D, FoundTemplate)) {
4588 // The variable templates structurally match; call it the same template.
4589 Importer.Imported(D->getTemplatedDecl(),
4590 FoundTemplate->getTemplatedDecl());
4591 return Importer.Imported(D, FoundTemplate);
4592 }
4593 }
4594
4595 ConflictingDecls.push_back(FoundDecls[I]);
4596 }
4597
4598 if (!ConflictingDecls.empty()) {
4599 Name = Importer.HandleNameConflict(Name, DC, Decl::IDNS_Ordinary,
4600 ConflictingDecls.data(),
4601 ConflictingDecls.size());
4602 }
4603
4604 if (!Name)
Craig Topper36250ad2014-05-12 05:36:57 +00004605 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004606
4607 VarDecl *DTemplated = D->getTemplatedDecl();
4608
4609 // Import the type.
4610 QualType T = Importer.Import(DTemplated->getType());
4611 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004612 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004613
4614 // Create the declaration that is being templated.
4615 SourceLocation StartLoc = Importer.Import(DTemplated->getLocStart());
4616 SourceLocation IdLoc = Importer.Import(DTemplated->getLocation());
4617 TypeSourceInfo *TInfo = Importer.Import(DTemplated->getTypeSourceInfo());
4618 VarDecl *D2Templated = VarDecl::Create(Importer.getToContext(), DC, StartLoc,
4619 IdLoc, Name.getAsIdentifierInfo(), T,
4620 TInfo, DTemplated->getStorageClass());
4621 D2Templated->setAccess(DTemplated->getAccess());
4622 D2Templated->setQualifierInfo(Importer.Import(DTemplated->getQualifierLoc()));
4623 D2Templated->setLexicalDeclContext(LexicalDC);
4624
4625 // Importer.Imported(DTemplated, D2Templated);
4626 // LexicalDC->addDeclInternal(D2Templated);
4627
4628 // Merge the initializer.
4629 if (ImportDefinition(DTemplated, D2Templated))
Craig Topper36250ad2014-05-12 05:36:57 +00004630 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004631
4632 // Create the variable template declaration itself.
4633 TemplateParameterList *TemplateParams =
4634 ImportTemplateParameterList(D->getTemplateParameters());
4635 if (!TemplateParams)
Craig Topper36250ad2014-05-12 05:36:57 +00004636 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004637
4638 VarTemplateDecl *D2 = VarTemplateDecl::Create(
Richard Smithbeef3452014-01-16 23:39:20 +00004639 Importer.getToContext(), DC, Loc, Name, TemplateParams, D2Templated);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004640 D2Templated->setDescribedVarTemplate(D2);
4641
4642 D2->setAccess(D->getAccess());
4643 D2->setLexicalDeclContext(LexicalDC);
4644 LexicalDC->addDeclInternal(D2);
4645
4646 // Note the relationship between the variable templates.
4647 Importer.Imported(D, D2);
4648 Importer.Imported(DTemplated, D2Templated);
4649
4650 if (DTemplated->isThisDeclarationADefinition() &&
4651 !D2Templated->isThisDeclarationADefinition()) {
4652 // FIXME: Import definition!
4653 }
4654
4655 return D2;
4656}
4657
4658Decl *ASTNodeImporter::VisitVarTemplateSpecializationDecl(
4659 VarTemplateSpecializationDecl *D) {
4660 // If this record has a definition in the translation unit we're coming from,
4661 // but this particular declaration is not that definition, import the
4662 // definition and map to that.
4663 VarDecl *Definition = D->getDefinition();
4664 if (Definition && Definition != D) {
4665 Decl *ImportedDef = Importer.Import(Definition);
4666 if (!ImportedDef)
Craig Topper36250ad2014-05-12 05:36:57 +00004667 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004668
4669 return Importer.Imported(D, ImportedDef);
4670 }
4671
4672 VarTemplateDecl *VarTemplate = cast_or_null<VarTemplateDecl>(
4673 Importer.Import(D->getSpecializedTemplate()));
4674 if (!VarTemplate)
Craig Topper36250ad2014-05-12 05:36:57 +00004675 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004676
4677 // Import the context of this declaration.
4678 DeclContext *DC = VarTemplate->getDeclContext();
4679 if (!DC)
Craig Topper36250ad2014-05-12 05:36:57 +00004680 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004681
4682 DeclContext *LexicalDC = DC;
4683 if (D->getDeclContext() != D->getLexicalDeclContext()) {
4684 LexicalDC = Importer.ImportContext(D->getLexicalDeclContext());
4685 if (!LexicalDC)
Craig Topper36250ad2014-05-12 05:36:57 +00004686 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004687 }
4688
4689 // Import the location of this declaration.
4690 SourceLocation StartLoc = Importer.Import(D->getLocStart());
4691 SourceLocation IdLoc = Importer.Import(D->getLocation());
4692
4693 // Import template arguments.
4694 SmallVector<TemplateArgument, 2> TemplateArgs;
4695 if (ImportTemplateArguments(D->getTemplateArgs().data(),
4696 D->getTemplateArgs().size(), TemplateArgs))
Craig Topper36250ad2014-05-12 05:36:57 +00004697 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004698
4699 // Try to find an existing specialization with these template arguments.
Craig Topper36250ad2014-05-12 05:36:57 +00004700 void *InsertPos = nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004701 VarTemplateSpecializationDecl *D2 = VarTemplate->findSpecialization(
Craig Topper7e0daca2014-06-26 04:58:53 +00004702 TemplateArgs, InsertPos);
Larisse Voufo39a1e502013-08-06 01:03:05 +00004703 if (D2) {
4704 // We already have a variable template specialization with these template
4705 // arguments.
4706
4707 // FIXME: Check for specialization vs. instantiation errors.
4708
4709 if (VarDecl *FoundDef = D2->getDefinition()) {
4710 if (!D->isThisDeclarationADefinition() ||
4711 IsStructuralMatch(D, FoundDef)) {
4712 // The record types structurally match, or the "from" translation
4713 // unit only had a forward declaration anyway; call it the same
4714 // variable.
4715 return Importer.Imported(D, FoundDef);
4716 }
4717 }
4718 } else {
4719
4720 // Import the type.
4721 QualType T = Importer.Import(D->getType());
4722 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00004723 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004724 TypeSourceInfo *TInfo = Importer.Import(D->getTypeSourceInfo());
4725
4726 // Create a new specialization.
4727 D2 = VarTemplateSpecializationDecl::Create(
4728 Importer.getToContext(), DC, StartLoc, IdLoc, VarTemplate, T, TInfo,
4729 D->getStorageClass(), TemplateArgs.data(), TemplateArgs.size());
4730 D2->setSpecializationKind(D->getSpecializationKind());
4731 D2->setTemplateArgsInfo(D->getTemplateArgsInfo());
4732
4733 // Add this specialization to the class template.
4734 VarTemplate->AddSpecialization(D2, InsertPos);
4735
4736 // Import the qualifier, if any.
4737 D2->setQualifierInfo(Importer.Import(D->getQualifierLoc()));
4738
4739 // Add the specialization to this context.
4740 D2->setLexicalDeclContext(LexicalDC);
4741 LexicalDC->addDeclInternal(D2);
4742 }
4743 Importer.Imported(D, D2);
4744
4745 if (D->isThisDeclarationADefinition() && ImportDefinition(D, D2))
Craig Topper36250ad2014-05-12 05:36:57 +00004746 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00004747
4748 return D2;
4749}
4750
Douglas Gregor7eeb5972010-02-11 19:21:55 +00004751//----------------------------------------------------------------------------
4752// Import Statements
4753//----------------------------------------------------------------------------
4754
Sean Callanan59721b32015-04-28 18:41:46 +00004755DeclGroupRef ASTNodeImporter::ImportDeclGroup(DeclGroupRef DG) {
4756 if (DG.isNull())
4757 return DeclGroupRef::Create(Importer.getToContext(), nullptr, 0);
4758 size_t NumDecls = DG.end() - DG.begin();
4759 SmallVector<Decl *, 1> ToDecls(NumDecls);
4760 auto &_Importer = this->Importer;
4761 std::transform(DG.begin(), DG.end(), ToDecls.begin(),
4762 [&_Importer](Decl *D) -> Decl * {
4763 return _Importer.Import(D);
4764 });
4765 return DeclGroupRef::Create(Importer.getToContext(),
4766 ToDecls.begin(),
4767 NumDecls);
4768}
4769
4770 Stmt *ASTNodeImporter::VisitStmt(Stmt *S) {
4771 Importer.FromDiag(S->getLocStart(), diag::err_unsupported_ast_node)
4772 << S->getStmtClassName();
4773 return nullptr;
4774 }
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004775
4776
4777Stmt *ASTNodeImporter::VisitGCCAsmStmt(GCCAsmStmt *S) {
4778 SmallVector<IdentifierInfo *, 4> Names;
4779 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4780 IdentifierInfo *ToII = Importer.Import(S->getOutputIdentifier(I));
4781 if (!ToII)
4782 return nullptr;
4783 Names.push_back(ToII);
4784 }
4785 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4786 IdentifierInfo *ToII = Importer.Import(S->getInputIdentifier(I));
4787 if (!ToII)
4788 return nullptr;
4789 Names.push_back(ToII);
4790 }
4791
4792 SmallVector<StringLiteral *, 4> Clobbers;
4793 for (unsigned I = 0, E = S->getNumClobbers(); I != E; I++) {
4794 StringLiteral *Clobber = cast_or_null<StringLiteral>(
4795 Importer.Import(S->getClobberStringLiteral(I)));
4796 if (!Clobber)
4797 return nullptr;
4798 Clobbers.push_back(Clobber);
4799 }
4800
4801 SmallVector<StringLiteral *, 4> Constraints;
4802 for (unsigned I = 0, E = S->getNumOutputs(); I != E; I++) {
4803 StringLiteral *Output = cast_or_null<StringLiteral>(
4804 Importer.Import(S->getOutputConstraintLiteral(I)));
4805 if (!Output)
4806 return nullptr;
4807 Constraints.push_back(Output);
4808 }
4809
4810 for (unsigned I = 0, E = S->getNumInputs(); I != E; I++) {
4811 StringLiteral *Input = cast_or_null<StringLiteral>(
4812 Importer.Import(S->getInputConstraintLiteral(I)));
4813 if (!Input)
4814 return nullptr;
4815 Constraints.push_back(Input);
4816 }
4817
4818 SmallVector<Expr *, 4> Exprs(S->getNumOutputs() + S->getNumInputs());
4819 if (ImportArrayChecked(S->begin_outputs(), S->end_outputs(), Exprs.begin()))
4820 return nullptr;
4821
4822 if (ImportArrayChecked(S->begin_inputs(), S->end_inputs(),
4823 Exprs.begin() + S->getNumOutputs()))
4824 return nullptr;
4825
4826 StringLiteral *AsmStr = cast_or_null<StringLiteral>(
4827 Importer.Import(S->getAsmString()));
4828 if (!AsmStr)
4829 return nullptr;
4830
4831 return new (Importer.getToContext()) GCCAsmStmt(
4832 Importer.getToContext(),
4833 Importer.Import(S->getAsmLoc()),
4834 S->isSimple(),
4835 S->isVolatile(),
4836 S->getNumOutputs(),
4837 S->getNumInputs(),
4838 Names.data(),
4839 Constraints.data(),
4840 Exprs.data(),
4841 AsmStr,
4842 S->getNumClobbers(),
4843 Clobbers.data(),
4844 Importer.Import(S->getRParenLoc()));
4845}
4846
Sean Callanan59721b32015-04-28 18:41:46 +00004847Stmt *ASTNodeImporter::VisitDeclStmt(DeclStmt *S) {
4848 DeclGroupRef ToDG = ImportDeclGroup(S->getDeclGroup());
4849 for (Decl *ToD : ToDG) {
4850 if (!ToD)
4851 return nullptr;
4852 }
4853 SourceLocation ToStartLoc = Importer.Import(S->getStartLoc());
4854 SourceLocation ToEndLoc = Importer.Import(S->getEndLoc());
4855 return new (Importer.getToContext()) DeclStmt(ToDG, ToStartLoc, ToEndLoc);
4856}
4857
4858Stmt *ASTNodeImporter::VisitNullStmt(NullStmt *S) {
4859 SourceLocation ToSemiLoc = Importer.Import(S->getSemiLoc());
4860 return new (Importer.getToContext()) NullStmt(ToSemiLoc,
4861 S->hasLeadingEmptyMacro());
4862}
4863
4864Stmt *ASTNodeImporter::VisitCompoundStmt(CompoundStmt *S) {
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004865 llvm::SmallVector<Stmt *, 8> ToStmts(S->size());
Sean Callanan8bca9962016-03-28 21:43:01 +00004866
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00004867 if (ImportArrayChecked(S->body_begin(), S->body_end(), ToStmts.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00004868 return nullptr;
4869
Sean Callanan59721b32015-04-28 18:41:46 +00004870 SourceLocation ToLBraceLoc = Importer.Import(S->getLBracLoc());
4871 SourceLocation ToRBraceLoc = Importer.Import(S->getRBracLoc());
4872 return new (Importer.getToContext()) CompoundStmt(Importer.getToContext(),
4873 ToStmts,
4874 ToLBraceLoc, ToRBraceLoc);
4875}
4876
4877Stmt *ASTNodeImporter::VisitCaseStmt(CaseStmt *S) {
4878 Expr *ToLHS = Importer.Import(S->getLHS());
4879 if (!ToLHS)
4880 return nullptr;
4881 Expr *ToRHS = Importer.Import(S->getRHS());
4882 if (!ToRHS && S->getRHS())
4883 return nullptr;
4884 SourceLocation ToCaseLoc = Importer.Import(S->getCaseLoc());
4885 SourceLocation ToEllipsisLoc = Importer.Import(S->getEllipsisLoc());
4886 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4887 return new (Importer.getToContext()) CaseStmt(ToLHS, ToRHS,
4888 ToCaseLoc, ToEllipsisLoc,
4889 ToColonLoc);
4890}
4891
4892Stmt *ASTNodeImporter::VisitDefaultStmt(DefaultStmt *S) {
4893 SourceLocation ToDefaultLoc = Importer.Import(S->getDefaultLoc());
4894 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
4895 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4896 if (!ToSubStmt && S->getSubStmt())
4897 return nullptr;
4898 return new (Importer.getToContext()) DefaultStmt(ToDefaultLoc, ToColonLoc,
4899 ToSubStmt);
4900}
4901
4902Stmt *ASTNodeImporter::VisitLabelStmt(LabelStmt *S) {
4903 SourceLocation ToIdentLoc = Importer.Import(S->getIdentLoc());
4904 LabelDecl *ToLabelDecl =
4905 cast_or_null<LabelDecl>(Importer.Import(S->getDecl()));
4906 if (!ToLabelDecl && S->getDecl())
4907 return nullptr;
4908 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4909 if (!ToSubStmt && S->getSubStmt())
4910 return nullptr;
4911 return new (Importer.getToContext()) LabelStmt(ToIdentLoc, ToLabelDecl,
4912 ToSubStmt);
4913}
4914
4915Stmt *ASTNodeImporter::VisitAttributedStmt(AttributedStmt *S) {
4916 SourceLocation ToAttrLoc = Importer.Import(S->getAttrLoc());
4917 ArrayRef<const Attr*> FromAttrs(S->getAttrs());
4918 SmallVector<const Attr *, 1> ToAttrs(FromAttrs.size());
4919 ASTContext &_ToContext = Importer.getToContext();
4920 std::transform(FromAttrs.begin(), FromAttrs.end(), ToAttrs.begin(),
4921 [&_ToContext](const Attr *A) -> const Attr * {
4922 return A->clone(_ToContext);
4923 });
4924 for (const Attr *ToA : ToAttrs) {
4925 if (!ToA)
4926 return nullptr;
4927 }
4928 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
4929 if (!ToSubStmt && S->getSubStmt())
4930 return nullptr;
4931 return AttributedStmt::Create(Importer.getToContext(), ToAttrLoc,
4932 ToAttrs, ToSubStmt);
4933}
4934
4935Stmt *ASTNodeImporter::VisitIfStmt(IfStmt *S) {
4936 SourceLocation ToIfLoc = Importer.Import(S->getIfLoc());
4937 VarDecl *ToConditionVariable = nullptr;
4938 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4939 ToConditionVariable =
4940 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4941 if (!ToConditionVariable)
4942 return nullptr;
4943 }
4944 Expr *ToCondition = Importer.Import(S->getCond());
4945 if (!ToCondition && S->getCond())
4946 return nullptr;
4947 Stmt *ToThenStmt = Importer.Import(S->getThen());
4948 if (!ToThenStmt && S->getThen())
4949 return nullptr;
4950 SourceLocation ToElseLoc = Importer.Import(S->getElseLoc());
4951 Stmt *ToElseStmt = Importer.Import(S->getElse());
4952 if (!ToElseStmt && S->getElse())
4953 return nullptr;
4954 return new (Importer.getToContext()) IfStmt(Importer.getToContext(),
4955 ToIfLoc, ToConditionVariable,
4956 ToCondition, ToThenStmt,
4957 ToElseLoc, ToElseStmt);
4958}
4959
4960Stmt *ASTNodeImporter::VisitSwitchStmt(SwitchStmt *S) {
4961 VarDecl *ToConditionVariable = nullptr;
4962 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4963 ToConditionVariable =
4964 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
4965 if (!ToConditionVariable)
4966 return nullptr;
4967 }
4968 Expr *ToCondition = Importer.Import(S->getCond());
4969 if (!ToCondition && S->getCond())
4970 return nullptr;
4971 SwitchStmt *ToStmt = new (Importer.getToContext()) SwitchStmt(
4972 Importer.getToContext(), ToConditionVariable,
4973 ToCondition);
4974 Stmt *ToBody = Importer.Import(S->getBody());
4975 if (!ToBody && S->getBody())
4976 return nullptr;
4977 ToStmt->setBody(ToBody);
4978 ToStmt->setSwitchLoc(Importer.Import(S->getSwitchLoc()));
4979 // Now we have to re-chain the cases.
4980 SwitchCase *LastChainedSwitchCase = nullptr;
4981 for (SwitchCase *SC = S->getSwitchCaseList(); SC != nullptr;
4982 SC = SC->getNextSwitchCase()) {
4983 SwitchCase *ToSC = dyn_cast_or_null<SwitchCase>(Importer.Import(SC));
4984 if (!ToSC)
4985 return nullptr;
4986 if (LastChainedSwitchCase)
4987 LastChainedSwitchCase->setNextSwitchCase(ToSC);
4988 else
4989 ToStmt->setSwitchCaseList(ToSC);
4990 LastChainedSwitchCase = ToSC;
4991 }
4992 return ToStmt;
4993}
4994
4995Stmt *ASTNodeImporter::VisitWhileStmt(WhileStmt *S) {
4996 VarDecl *ToConditionVariable = nullptr;
4997 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
4998 ToConditionVariable =
4999 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5000 if (!ToConditionVariable)
5001 return nullptr;
5002 }
5003 Expr *ToCondition = Importer.Import(S->getCond());
5004 if (!ToCondition && S->getCond())
5005 return nullptr;
5006 Stmt *ToBody = Importer.Import(S->getBody());
5007 if (!ToBody && S->getBody())
5008 return nullptr;
5009 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5010 return new (Importer.getToContext()) WhileStmt(Importer.getToContext(),
5011 ToConditionVariable,
5012 ToCondition, ToBody,
5013 ToWhileLoc);
5014}
5015
5016Stmt *ASTNodeImporter::VisitDoStmt(DoStmt *S) {
5017 Stmt *ToBody = Importer.Import(S->getBody());
5018 if (!ToBody && S->getBody())
5019 return nullptr;
5020 Expr *ToCondition = Importer.Import(S->getCond());
5021 if (!ToCondition && S->getCond())
5022 return nullptr;
5023 SourceLocation ToDoLoc = Importer.Import(S->getDoLoc());
5024 SourceLocation ToWhileLoc = Importer.Import(S->getWhileLoc());
5025 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5026 return new (Importer.getToContext()) DoStmt(ToBody, ToCondition,
5027 ToDoLoc, ToWhileLoc,
5028 ToRParenLoc);
5029}
5030
5031Stmt *ASTNodeImporter::VisitForStmt(ForStmt *S) {
5032 Stmt *ToInit = Importer.Import(S->getInit());
5033 if (!ToInit && S->getInit())
5034 return nullptr;
5035 Expr *ToCondition = Importer.Import(S->getCond());
5036 if (!ToCondition && S->getCond())
5037 return nullptr;
5038 VarDecl *ToConditionVariable = nullptr;
5039 if (VarDecl *FromConditionVariable = S->getConditionVariable()) {
5040 ToConditionVariable =
5041 dyn_cast_or_null<VarDecl>(Importer.Import(FromConditionVariable));
5042 if (!ToConditionVariable)
5043 return nullptr;
5044 }
5045 Expr *ToInc = Importer.Import(S->getInc());
5046 if (!ToInc && S->getInc())
5047 return nullptr;
5048 Stmt *ToBody = Importer.Import(S->getBody());
5049 if (!ToBody && S->getBody())
5050 return nullptr;
5051 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5052 SourceLocation ToLParenLoc = Importer.Import(S->getLParenLoc());
5053 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5054 return new (Importer.getToContext()) ForStmt(Importer.getToContext(),
5055 ToInit, ToCondition,
5056 ToConditionVariable,
5057 ToInc, ToBody,
5058 ToForLoc, ToLParenLoc,
5059 ToRParenLoc);
5060}
5061
5062Stmt *ASTNodeImporter::VisitGotoStmt(GotoStmt *S) {
5063 LabelDecl *ToLabel = nullptr;
5064 if (LabelDecl *FromLabel = S->getLabel()) {
5065 ToLabel = dyn_cast_or_null<LabelDecl>(Importer.Import(FromLabel));
5066 if (!ToLabel)
5067 return nullptr;
5068 }
5069 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5070 SourceLocation ToLabelLoc = Importer.Import(S->getLabelLoc());
5071 return new (Importer.getToContext()) GotoStmt(ToLabel,
5072 ToGotoLoc, ToLabelLoc);
5073}
5074
5075Stmt *ASTNodeImporter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
5076 SourceLocation ToGotoLoc = Importer.Import(S->getGotoLoc());
5077 SourceLocation ToStarLoc = Importer.Import(S->getStarLoc());
5078 Expr *ToTarget = Importer.Import(S->getTarget());
5079 if (!ToTarget && S->getTarget())
5080 return nullptr;
5081 return new (Importer.getToContext()) IndirectGotoStmt(ToGotoLoc, ToStarLoc,
5082 ToTarget);
5083}
5084
5085Stmt *ASTNodeImporter::VisitContinueStmt(ContinueStmt *S) {
5086 SourceLocation ToContinueLoc = Importer.Import(S->getContinueLoc());
5087 return new (Importer.getToContext()) ContinueStmt(ToContinueLoc);
5088}
5089
5090Stmt *ASTNodeImporter::VisitBreakStmt(BreakStmt *S) {
5091 SourceLocation ToBreakLoc = Importer.Import(S->getBreakLoc());
5092 return new (Importer.getToContext()) BreakStmt(ToBreakLoc);
5093}
5094
5095Stmt *ASTNodeImporter::VisitReturnStmt(ReturnStmt *S) {
5096 SourceLocation ToRetLoc = Importer.Import(S->getReturnLoc());
5097 Expr *ToRetExpr = Importer.Import(S->getRetValue());
5098 if (!ToRetExpr && S->getRetValue())
5099 return nullptr;
5100 VarDecl *NRVOCandidate = const_cast<VarDecl*>(S->getNRVOCandidate());
5101 VarDecl *ToNRVOCandidate = cast_or_null<VarDecl>(Importer.Import(NRVOCandidate));
5102 if (!ToNRVOCandidate && NRVOCandidate)
5103 return nullptr;
5104 return new (Importer.getToContext()) ReturnStmt(ToRetLoc, ToRetExpr,
5105 ToNRVOCandidate);
5106}
5107
5108Stmt *ASTNodeImporter::VisitCXXCatchStmt(CXXCatchStmt *S) {
5109 SourceLocation ToCatchLoc = Importer.Import(S->getCatchLoc());
5110 VarDecl *ToExceptionDecl = nullptr;
5111 if (VarDecl *FromExceptionDecl = S->getExceptionDecl()) {
5112 ToExceptionDecl =
5113 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5114 if (!ToExceptionDecl)
5115 return nullptr;
5116 }
5117 Stmt *ToHandlerBlock = Importer.Import(S->getHandlerBlock());
5118 if (!ToHandlerBlock && S->getHandlerBlock())
5119 return nullptr;
5120 return new (Importer.getToContext()) CXXCatchStmt(ToCatchLoc,
5121 ToExceptionDecl,
5122 ToHandlerBlock);
5123}
5124
5125Stmt *ASTNodeImporter::VisitCXXTryStmt(CXXTryStmt *S) {
5126 SourceLocation ToTryLoc = Importer.Import(S->getTryLoc());
5127 Stmt *ToTryBlock = Importer.Import(S->getTryBlock());
5128 if (!ToTryBlock && S->getTryBlock())
5129 return nullptr;
5130 SmallVector<Stmt *, 1> ToHandlers(S->getNumHandlers());
5131 for (unsigned HI = 0, HE = S->getNumHandlers(); HI != HE; ++HI) {
5132 CXXCatchStmt *FromHandler = S->getHandler(HI);
5133 if (Stmt *ToHandler = Importer.Import(FromHandler))
5134 ToHandlers[HI] = ToHandler;
5135 else
5136 return nullptr;
5137 }
5138 return CXXTryStmt::Create(Importer.getToContext(), ToTryLoc, ToTryBlock,
5139 ToHandlers);
5140}
5141
5142Stmt *ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
5143 DeclStmt *ToRange =
5144 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getRangeStmt()));
5145 if (!ToRange && S->getRangeStmt())
5146 return nullptr;
Richard Smith01694c32016-03-20 10:33:40 +00005147 DeclStmt *ToBegin =
5148 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getBeginStmt()));
5149 if (!ToBegin && S->getBeginStmt())
5150 return nullptr;
5151 DeclStmt *ToEnd =
5152 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getEndStmt()));
5153 if (!ToEnd && S->getEndStmt())
Sean Callanan59721b32015-04-28 18:41:46 +00005154 return nullptr;
5155 Expr *ToCond = Importer.Import(S->getCond());
5156 if (!ToCond && S->getCond())
5157 return nullptr;
5158 Expr *ToInc = Importer.Import(S->getInc());
5159 if (!ToInc && S->getInc())
5160 return nullptr;
5161 DeclStmt *ToLoopVar =
5162 dyn_cast_or_null<DeclStmt>(Importer.Import(S->getLoopVarStmt()));
5163 if (!ToLoopVar && S->getLoopVarStmt())
5164 return nullptr;
5165 Stmt *ToBody = Importer.Import(S->getBody());
5166 if (!ToBody && S->getBody())
5167 return nullptr;
5168 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
Richard Smith9f690bd2015-10-27 06:02:45 +00005169 SourceLocation ToCoawaitLoc = Importer.Import(S->getCoawaitLoc());
Sean Callanan59721b32015-04-28 18:41:46 +00005170 SourceLocation ToColonLoc = Importer.Import(S->getColonLoc());
5171 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
Richard Smith01694c32016-03-20 10:33:40 +00005172 return new (Importer.getToContext()) CXXForRangeStmt(ToRange, ToBegin, ToEnd,
Sean Callanan59721b32015-04-28 18:41:46 +00005173 ToCond, ToInc,
5174 ToLoopVar, ToBody,
Richard Smith9f690bd2015-10-27 06:02:45 +00005175 ToForLoc, ToCoawaitLoc,
5176 ToColonLoc, ToRParenLoc);
Sean Callanan59721b32015-04-28 18:41:46 +00005177}
5178
5179Stmt *ASTNodeImporter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
5180 Stmt *ToElem = Importer.Import(S->getElement());
5181 if (!ToElem && S->getElement())
5182 return nullptr;
5183 Expr *ToCollect = Importer.Import(S->getCollection());
5184 if (!ToCollect && S->getCollection())
5185 return nullptr;
5186 Stmt *ToBody = Importer.Import(S->getBody());
5187 if (!ToBody && S->getBody())
5188 return nullptr;
5189 SourceLocation ToForLoc = Importer.Import(S->getForLoc());
5190 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5191 return new (Importer.getToContext()) ObjCForCollectionStmt(ToElem,
5192 ToCollect,
5193 ToBody, ToForLoc,
5194 ToRParenLoc);
5195}
5196
5197Stmt *ASTNodeImporter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
5198 SourceLocation ToAtCatchLoc = Importer.Import(S->getAtCatchLoc());
5199 SourceLocation ToRParenLoc = Importer.Import(S->getRParenLoc());
5200 VarDecl *ToExceptionDecl = nullptr;
5201 if (VarDecl *FromExceptionDecl = S->getCatchParamDecl()) {
5202 ToExceptionDecl =
5203 dyn_cast_or_null<VarDecl>(Importer.Import(FromExceptionDecl));
5204 if (!ToExceptionDecl)
5205 return nullptr;
5206 }
5207 Stmt *ToBody = Importer.Import(S->getCatchBody());
5208 if (!ToBody && S->getCatchBody())
5209 return nullptr;
5210 return new (Importer.getToContext()) ObjCAtCatchStmt(ToAtCatchLoc,
5211 ToRParenLoc,
5212 ToExceptionDecl,
5213 ToBody);
5214}
5215
5216Stmt *ASTNodeImporter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
5217 SourceLocation ToAtFinallyLoc = Importer.Import(S->getAtFinallyLoc());
5218 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyBody());
5219 if (!ToAtFinallyStmt && S->getFinallyBody())
5220 return nullptr;
5221 return new (Importer.getToContext()) ObjCAtFinallyStmt(ToAtFinallyLoc,
5222 ToAtFinallyStmt);
5223}
5224
5225Stmt *ASTNodeImporter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
5226 SourceLocation ToAtTryLoc = Importer.Import(S->getAtTryLoc());
5227 Stmt *ToAtTryStmt = Importer.Import(S->getTryBody());
5228 if (!ToAtTryStmt && S->getTryBody())
5229 return nullptr;
5230 SmallVector<Stmt *, 1> ToCatchStmts(S->getNumCatchStmts());
5231 for (unsigned CI = 0, CE = S->getNumCatchStmts(); CI != CE; ++CI) {
5232 ObjCAtCatchStmt *FromCatchStmt = S->getCatchStmt(CI);
5233 if (Stmt *ToCatchStmt = Importer.Import(FromCatchStmt))
5234 ToCatchStmts[CI] = ToCatchStmt;
5235 else
5236 return nullptr;
5237 }
5238 Stmt *ToAtFinallyStmt = Importer.Import(S->getFinallyStmt());
5239 if (!ToAtFinallyStmt && S->getFinallyStmt())
5240 return nullptr;
5241 return ObjCAtTryStmt::Create(Importer.getToContext(),
5242 ToAtTryLoc, ToAtTryStmt,
5243 ToCatchStmts.begin(), ToCatchStmts.size(),
5244 ToAtFinallyStmt);
5245}
5246
5247Stmt *ASTNodeImporter::VisitObjCAtSynchronizedStmt
5248 (ObjCAtSynchronizedStmt *S) {
5249 SourceLocation ToAtSynchronizedLoc =
5250 Importer.Import(S->getAtSynchronizedLoc());
5251 Expr *ToSynchExpr = Importer.Import(S->getSynchExpr());
5252 if (!ToSynchExpr && S->getSynchExpr())
5253 return nullptr;
5254 Stmt *ToSynchBody = Importer.Import(S->getSynchBody());
5255 if (!ToSynchBody && S->getSynchBody())
5256 return nullptr;
5257 return new (Importer.getToContext()) ObjCAtSynchronizedStmt(
5258 ToAtSynchronizedLoc, ToSynchExpr, ToSynchBody);
5259}
5260
5261Stmt *ASTNodeImporter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
5262 SourceLocation ToAtThrowLoc = Importer.Import(S->getThrowLoc());
5263 Expr *ToThrow = Importer.Import(S->getThrowExpr());
5264 if (!ToThrow && S->getThrowExpr())
5265 return nullptr;
5266 return new (Importer.getToContext()) ObjCAtThrowStmt(ToAtThrowLoc, ToThrow);
5267}
5268
5269Stmt *ASTNodeImporter::VisitObjCAutoreleasePoolStmt
5270 (ObjCAutoreleasePoolStmt *S) {
5271 SourceLocation ToAtLoc = Importer.Import(S->getAtLoc());
5272 Stmt *ToSubStmt = Importer.Import(S->getSubStmt());
5273 if (!ToSubStmt && S->getSubStmt())
5274 return nullptr;
5275 return new (Importer.getToContext()) ObjCAutoreleasePoolStmt(ToAtLoc,
5276 ToSubStmt);
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005277}
5278
5279//----------------------------------------------------------------------------
5280// Import Expressions
5281//----------------------------------------------------------------------------
5282Expr *ASTNodeImporter::VisitExpr(Expr *E) {
5283 Importer.FromDiag(E->getLocStart(), diag::err_unsupported_ast_node)
5284 << E->getStmtClassName();
Craig Topper36250ad2014-05-12 05:36:57 +00005285 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005286}
5287
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005288Expr *ASTNodeImporter::VisitVAArgExpr(VAArgExpr *E) {
5289 QualType T = Importer.Import(E->getType());
5290 if (T.isNull())
5291 return nullptr;
5292
5293 Expr *SubExpr = Importer.Import(E->getSubExpr());
5294 if (!SubExpr && E->getSubExpr())
5295 return nullptr;
5296
5297 TypeSourceInfo *TInfo = Importer.Import(E->getWrittenTypeInfo());
5298 if (!TInfo)
5299 return nullptr;
5300
5301 return new (Importer.getToContext()) VAArgExpr(
5302 Importer.Import(E->getBuiltinLoc()), SubExpr, TInfo,
5303 Importer.Import(E->getRParenLoc()), T, E->isMicrosoftABI());
5304}
5305
5306
5307Expr *ASTNodeImporter::VisitGNUNullExpr(GNUNullExpr *E) {
5308 QualType T = Importer.Import(E->getType());
5309 if (T.isNull())
5310 return nullptr;
5311
5312 return new (Importer.getToContext()) GNUNullExpr(
5313 T, Importer.Import(E->getExprLoc()));
5314}
5315
5316Expr *ASTNodeImporter::VisitPredefinedExpr(PredefinedExpr *E) {
5317 QualType T = Importer.Import(E->getType());
5318 if (T.isNull())
5319 return nullptr;
5320
5321 StringLiteral *SL = cast_or_null<StringLiteral>(
5322 Importer.Import(E->getFunctionName()));
5323 if (!SL && E->getFunctionName())
5324 return nullptr;
5325
5326 return new (Importer.getToContext()) PredefinedExpr(
5327 Importer.Import(E->getExprLoc()), T, E->getIdentType(), SL);
5328}
5329
Douglas Gregor52f820e2010-02-19 01:17:02 +00005330Expr *ASTNodeImporter::VisitDeclRefExpr(DeclRefExpr *E) {
Douglas Gregor52f820e2010-02-19 01:17:02 +00005331 ValueDecl *ToD = cast_or_null<ValueDecl>(Importer.Import(E->getDecl()));
5332 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00005333 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005334
Craig Topper36250ad2014-05-12 05:36:57 +00005335 NamedDecl *FoundD = nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005336 if (E->getDecl() != E->getFoundDecl()) {
5337 FoundD = cast_or_null<NamedDecl>(Importer.Import(E->getFoundDecl()));
5338 if (!FoundD)
Craig Topper36250ad2014-05-12 05:36:57 +00005339 return nullptr;
Chandler Carruth8d26bb02011-05-01 23:48:14 +00005340 }
Douglas Gregor52f820e2010-02-19 01:17:02 +00005341
5342 QualType T = Importer.Import(E->getType());
5343 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005344 return nullptr;
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005345
5346 DeclRefExpr *DRE = DeclRefExpr::Create(Importer.getToContext(),
5347 Importer.Import(E->getQualifierLoc()),
Abramo Bagnara7945c982012-01-27 09:46:47 +00005348 Importer.Import(E->getTemplateKeywordLoc()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005349 ToD,
Alexey Bataev19acc3d2015-01-12 10:17:46 +00005350 E->refersToEnclosingVariableOrCapture(),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005351 Importer.Import(E->getLocation()),
5352 T, E->getValueKind(),
5353 FoundD,
Craig Topper36250ad2014-05-12 05:36:57 +00005354 /*FIXME:TemplateArgs=*/nullptr);
Abramo Bagnara635ed24e2011-10-05 07:56:41 +00005355 if (E->hadMultipleCandidates())
5356 DRE->setHadMultipleCandidates(true);
5357 return DRE;
Douglas Gregor52f820e2010-02-19 01:17:02 +00005358}
5359
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005360Expr *ASTNodeImporter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
5361 QualType T = Importer.Import(E->getType());
5362 if (T.isNull())
5363 return NULL;
5364
5365 return new (Importer.getToContext()) ImplicitValueInitExpr(T);
5366}
5367
5368ASTNodeImporter::Designator
5369ASTNodeImporter::ImportDesignator(const Designator &D) {
5370 if (D.isFieldDesignator()) {
5371 IdentifierInfo *ToFieldName = Importer.Import(D.getFieldName());
5372 // Caller checks for import error
5373 return Designator(ToFieldName, Importer.Import(D.getDotLoc()),
5374 Importer.Import(D.getFieldLoc()));
5375 }
5376 if (D.isArrayDesignator())
5377 return Designator(D.getFirstExprIndex(),
5378 Importer.Import(D.getLBracketLoc()),
5379 Importer.Import(D.getRBracketLoc()));
5380
5381 assert(D.isArrayRangeDesignator());
5382 return Designator(D.getFirstExprIndex(),
5383 Importer.Import(D.getLBracketLoc()),
5384 Importer.Import(D.getEllipsisLoc()),
5385 Importer.Import(D.getRBracketLoc()));
5386}
5387
5388
5389Expr *ASTNodeImporter::VisitDesignatedInitExpr(DesignatedInitExpr *DIE) {
5390 Expr *Init = cast_or_null<Expr>(Importer.Import(DIE->getInit()));
5391 if (!Init)
5392 return nullptr;
5393
5394 SmallVector<Expr *, 4> IndexExprs(DIE->getNumSubExprs() - 1);
5395 // List elements from the second, the first is Init itself
5396 for (unsigned I = 1, E = DIE->getNumSubExprs(); I < E; I++) {
5397 if (Expr *Arg = cast_or_null<Expr>(Importer.Import(DIE->getSubExpr(I))))
5398 IndexExprs[I - 1] = Arg;
5399 else
5400 return nullptr;
5401 }
5402
5403 SmallVector<Designator, 4> Designators(DIE->size());
5404 std::transform(DIE->designators_begin(), DIE->designators_end(),
5405 Designators.begin(),
5406 [this](const Designator &D) -> Designator {
5407 return ImportDesignator(D);
5408 });
5409
5410 for (auto I = DIE->designators_begin(), E = DIE->designators_end(); I != E;
5411 ++I)
5412 if (I->isFieldDesignator() && !I->getFieldName())
5413 return nullptr;
5414
5415 return DesignatedInitExpr::Create(
5416 Importer.getToContext(), Designators.data(), Designators.size(),
5417 IndexExprs, Importer.Import(DIE->getEqualOrColonLoc()),
5418 DIE->usesGNUSyntax(), Init);
5419}
5420
5421Expr *ASTNodeImporter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
5422 QualType T = Importer.Import(E->getType());
5423 if (T.isNull())
5424 return nullptr;
5425
5426 return new (Importer.getToContext())
5427 CXXNullPtrLiteralExpr(T, Importer.Import(E->getLocation()));
5428}
5429
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005430Expr *ASTNodeImporter::VisitIntegerLiteral(IntegerLiteral *E) {
5431 QualType T = Importer.Import(E->getType());
5432 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005433 return nullptr;
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005434
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +00005435 return IntegerLiteral::Create(Importer.getToContext(),
5436 E->getValue(), T,
5437 Importer.Import(E->getLocation()));
Douglas Gregor7eeb5972010-02-11 19:21:55 +00005438}
5439
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005440Expr *ASTNodeImporter::VisitFloatingLiteral(FloatingLiteral *E) {
5441 QualType T = Importer.Import(E->getType());
5442 if (T.isNull())
5443 return nullptr;
5444
5445 return FloatingLiteral::Create(Importer.getToContext(),
5446 E->getValue(), E->isExact(), T,
5447 Importer.Import(E->getLocation()));
5448}
5449
Douglas Gregor623421d2010-02-18 02:21:22 +00005450Expr *ASTNodeImporter::VisitCharacterLiteral(CharacterLiteral *E) {
5451 QualType T = Importer.Import(E->getType());
5452 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005453 return nullptr;
5454
Douglas Gregorfb65e592011-07-27 05:40:30 +00005455 return new (Importer.getToContext()) CharacterLiteral(E->getValue(),
5456 E->getKind(), T,
Douglas Gregor623421d2010-02-18 02:21:22 +00005457 Importer.Import(E->getLocation()));
5458}
5459
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005460Expr *ASTNodeImporter::VisitStringLiteral(StringLiteral *E) {
5461 QualType T = Importer.Import(E->getType());
5462 if (T.isNull())
5463 return nullptr;
5464
5465 SmallVector<SourceLocation, 4> Locations(E->getNumConcatenated());
5466 ImportArray(E->tokloc_begin(), E->tokloc_end(), Locations.begin());
5467
5468 return StringLiteral::Create(Importer.getToContext(), E->getBytes(),
5469 E->getKind(), E->isPascal(), T,
5470 Locations.data(), Locations.size());
5471}
5472
5473Expr *ASTNodeImporter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
5474 QualType T = Importer.Import(E->getType());
5475 if (T.isNull())
5476 return nullptr;
5477
5478 TypeSourceInfo *TInfo = Importer.Import(E->getTypeSourceInfo());
5479 if (!TInfo)
5480 return nullptr;
5481
5482 Expr *Init = Importer.Import(E->getInitializer());
5483 if (!Init)
5484 return nullptr;
5485
5486 return new (Importer.getToContext()) CompoundLiteralExpr(
5487 Importer.Import(E->getLParenLoc()), TInfo, T, E->getValueKind(),
5488 Init, E->isFileScope());
5489}
5490
5491Expr *ASTNodeImporter::VisitAtomicExpr(AtomicExpr *E) {
5492 QualType T = Importer.Import(E->getType());
5493 if (T.isNull())
5494 return nullptr;
5495
5496 SmallVector<Expr *, 6> Exprs(E->getNumSubExprs());
5497 if (ImportArrayChecked(
5498 E->getSubExprs(), E->getSubExprs() + E->getNumSubExprs(),
5499 Exprs.begin()))
5500 return nullptr;
5501
5502 return new (Importer.getToContext()) AtomicExpr(
5503 Importer.Import(E->getBuiltinLoc()), Exprs, T, E->getOp(),
5504 Importer.Import(E->getRParenLoc()));
5505}
5506
5507Expr *ASTNodeImporter::VisitAddrLabelExpr(AddrLabelExpr *E) {
5508 QualType T = Importer.Import(E->getType());
5509 if (T.isNull())
5510 return nullptr;
5511
5512 LabelDecl *ToLabel = cast_or_null<LabelDecl>(Importer.Import(E->getLabel()));
5513 if (!ToLabel)
5514 return nullptr;
5515
5516 return new (Importer.getToContext()) AddrLabelExpr(
5517 Importer.Import(E->getAmpAmpLoc()), Importer.Import(E->getLabelLoc()),
5518 ToLabel, T);
5519}
5520
Douglas Gregorc74247e2010-02-19 01:07:06 +00005521Expr *ASTNodeImporter::VisitParenExpr(ParenExpr *E) {
5522 Expr *SubExpr = Importer.Import(E->getSubExpr());
5523 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005524 return nullptr;
5525
Douglas Gregorc74247e2010-02-19 01:07:06 +00005526 return new (Importer.getToContext())
5527 ParenExpr(Importer.Import(E->getLParen()),
5528 Importer.Import(E->getRParen()),
5529 SubExpr);
5530}
5531
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005532Expr *ASTNodeImporter::VisitParenListExpr(ParenListExpr *E) {
5533 SmallVector<Expr *, 4> Exprs(E->getNumExprs());
5534 if (ImportArrayChecked(
5535 E->getExprs(), E->getExprs() + E->getNumExprs(), Exprs.begin()))
5536 return nullptr;
5537
5538 return new (Importer.getToContext()) ParenListExpr(
5539 Importer.getToContext(), Importer.Import(E->getLParenLoc()),
5540 Exprs, Importer.Import(E->getLParenLoc()));
5541}
5542
5543Expr *ASTNodeImporter::VisitStmtExpr(StmtExpr *E) {
5544 QualType T = Importer.Import(E->getType());
5545 if (T.isNull())
5546 return nullptr;
5547
5548 CompoundStmt *ToSubStmt = cast_or_null<CompoundStmt>(
5549 Importer.Import(E->getSubStmt()));
5550 if (!ToSubStmt && E->getSubStmt())
5551 return nullptr;
5552
5553 return new (Importer.getToContext()) StmtExpr(ToSubStmt, T,
5554 Importer.Import(E->getLParenLoc()), Importer.Import(E->getRParenLoc()));
5555}
5556
Douglas Gregorc74247e2010-02-19 01:07:06 +00005557Expr *ASTNodeImporter::VisitUnaryOperator(UnaryOperator *E) {
5558 QualType T = Importer.Import(E->getType());
5559 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005560 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005561
5562 Expr *SubExpr = Importer.Import(E->getSubExpr());
5563 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005564 return nullptr;
5565
Douglas Gregorc74247e2010-02-19 01:07:06 +00005566 return new (Importer.getToContext()) UnaryOperator(SubExpr, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005567 T, E->getValueKind(),
5568 E->getObjectKind(),
Douglas Gregorc74247e2010-02-19 01:07:06 +00005569 Importer.Import(E->getOperatorLoc()));
5570}
5571
Peter Collingbournee190dee2011-03-11 19:24:49 +00005572Expr *ASTNodeImporter::VisitUnaryExprOrTypeTraitExpr(
5573 UnaryExprOrTypeTraitExpr *E) {
Douglas Gregord8552cd2010-02-19 01:24:23 +00005574 QualType ResultType = Importer.Import(E->getType());
5575
5576 if (E->isArgumentType()) {
5577 TypeSourceInfo *TInfo = Importer.Import(E->getArgumentTypeInfo());
5578 if (!TInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00005579 return nullptr;
5580
Peter Collingbournee190dee2011-03-11 19:24:49 +00005581 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5582 TInfo, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005583 Importer.Import(E->getOperatorLoc()),
5584 Importer.Import(E->getRParenLoc()));
5585 }
5586
5587 Expr *SubExpr = Importer.Import(E->getArgumentExpr());
5588 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005589 return nullptr;
5590
Peter Collingbournee190dee2011-03-11 19:24:49 +00005591 return new (Importer.getToContext()) UnaryExprOrTypeTraitExpr(E->getKind(),
5592 SubExpr, ResultType,
Douglas Gregord8552cd2010-02-19 01:24:23 +00005593 Importer.Import(E->getOperatorLoc()),
5594 Importer.Import(E->getRParenLoc()));
5595}
5596
Douglas Gregorc74247e2010-02-19 01:07:06 +00005597Expr *ASTNodeImporter::VisitBinaryOperator(BinaryOperator *E) {
5598 QualType T = Importer.Import(E->getType());
5599 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005600 return nullptr;
Douglas Gregorc74247e2010-02-19 01:07:06 +00005601
5602 Expr *LHS = Importer.Import(E->getLHS());
5603 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005604 return nullptr;
5605
Douglas Gregorc74247e2010-02-19 01:07:06 +00005606 Expr *RHS = Importer.Import(E->getRHS());
5607 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005608 return nullptr;
5609
Douglas Gregorc74247e2010-02-19 01:07:06 +00005610 return new (Importer.getToContext()) BinaryOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005611 T, E->getValueKind(),
5612 E->getObjectKind(),
Lang Hames5de91cc2012-10-02 04:45:10 +00005613 Importer.Import(E->getOperatorLoc()),
5614 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005615}
5616
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005617Expr *ASTNodeImporter::VisitConditionalOperator(ConditionalOperator *E) {
5618 QualType T = Importer.Import(E->getType());
5619 if (T.isNull())
5620 return nullptr;
5621
5622 Expr *ToLHS = Importer.Import(E->getLHS());
5623 if (!ToLHS)
5624 return nullptr;
5625
5626 Expr *ToRHS = Importer.Import(E->getRHS());
5627 if (!ToRHS)
5628 return nullptr;
5629
5630 Expr *ToCond = Importer.Import(E->getCond());
5631 if (!ToCond)
5632 return nullptr;
5633
5634 return new (Importer.getToContext()) ConditionalOperator(
5635 ToCond, Importer.Import(E->getQuestionLoc()),
5636 ToLHS, Importer.Import(E->getColonLoc()),
5637 ToRHS, T, E->getValueKind(), E->getObjectKind());
5638}
5639
5640Expr *ASTNodeImporter::VisitBinaryConditionalOperator(
5641 BinaryConditionalOperator *E) {
5642 QualType T = Importer.Import(E->getType());
5643 if (T.isNull())
5644 return nullptr;
5645
5646 Expr *Common = Importer.Import(E->getCommon());
5647 if (!Common)
5648 return nullptr;
5649
5650 Expr *Cond = Importer.Import(E->getCond());
5651 if (!Cond)
5652 return nullptr;
5653
5654 OpaqueValueExpr *OpaqueValue = cast_or_null<OpaqueValueExpr>(
5655 Importer.Import(E->getOpaqueValue()));
5656 if (!OpaqueValue)
5657 return nullptr;
5658
5659 Expr *TrueExpr = Importer.Import(E->getTrueExpr());
5660 if (!TrueExpr)
5661 return nullptr;
5662
5663 Expr *FalseExpr = Importer.Import(E->getFalseExpr());
5664 if (!FalseExpr)
5665 return nullptr;
5666
5667 return new (Importer.getToContext()) BinaryConditionalOperator(
5668 Common, OpaqueValue, Cond, TrueExpr, FalseExpr,
5669 Importer.Import(E->getQuestionLoc()), Importer.Import(E->getColonLoc()),
5670 T, E->getValueKind(), E->getObjectKind());
5671}
5672
5673Expr *ASTNodeImporter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
5674 QualType T = Importer.Import(E->getType());
5675 if (T.isNull())
5676 return nullptr;
5677
5678 Expr *SourceExpr = Importer.Import(E->getSourceExpr());
5679 if (!SourceExpr && E->getSourceExpr())
5680 return nullptr;
5681
5682 return new (Importer.getToContext()) OpaqueValueExpr(
5683 Importer.Import(E->getExprLoc()), T, E->getValueKind(),
5684 E->getObjectKind(), SourceExpr);
5685}
5686
Douglas Gregorc74247e2010-02-19 01:07:06 +00005687Expr *ASTNodeImporter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
5688 QualType T = Importer.Import(E->getType());
5689 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005690 return nullptr;
5691
Douglas Gregorc74247e2010-02-19 01:07:06 +00005692 QualType CompLHSType = Importer.Import(E->getComputationLHSType());
5693 if (CompLHSType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005694 return nullptr;
5695
Douglas Gregorc74247e2010-02-19 01:07:06 +00005696 QualType CompResultType = Importer.Import(E->getComputationResultType());
5697 if (CompResultType.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005698 return nullptr;
5699
Douglas Gregorc74247e2010-02-19 01:07:06 +00005700 Expr *LHS = Importer.Import(E->getLHS());
5701 if (!LHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005702 return nullptr;
5703
Douglas Gregorc74247e2010-02-19 01:07:06 +00005704 Expr *RHS = Importer.Import(E->getRHS());
5705 if (!RHS)
Craig Topper36250ad2014-05-12 05:36:57 +00005706 return nullptr;
5707
Douglas Gregorc74247e2010-02-19 01:07:06 +00005708 return new (Importer.getToContext())
5709 CompoundAssignOperator(LHS, RHS, E->getOpcode(),
John McCall7decc9e2010-11-18 06:31:45 +00005710 T, E->getValueKind(),
5711 E->getObjectKind(),
5712 CompLHSType, CompResultType,
Lang Hames5de91cc2012-10-02 04:45:10 +00005713 Importer.Import(E->getOperatorLoc()),
5714 E->isFPContractable());
Douglas Gregorc74247e2010-02-19 01:07:06 +00005715}
5716
Benjamin Kramer8aef5962011-03-26 12:38:21 +00005717static bool ImportCastPath(CastExpr *E, CXXCastPath &Path) {
John McCallcf142162010-08-07 06:22:56 +00005718 if (E->path_empty()) return false;
5719
5720 // TODO: import cast paths
5721 return true;
5722}
5723
Douglas Gregor98c10182010-02-12 22:17:39 +00005724Expr *ASTNodeImporter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
5725 QualType T = Importer.Import(E->getType());
5726 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005727 return nullptr;
Douglas Gregor98c10182010-02-12 22:17:39 +00005728
5729 Expr *SubExpr = Importer.Import(E->getSubExpr());
5730 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005731 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005732
5733 CXXCastPath BasePath;
5734 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005735 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005736
5737 return ImplicitCastExpr::Create(Importer.getToContext(), T, E->getCastKind(),
John McCall2536c6d2010-08-25 10:28:54 +00005738 SubExpr, &BasePath, E->getValueKind());
Douglas Gregor98c10182010-02-12 22:17:39 +00005739}
5740
Douglas Gregor5481d322010-02-19 01:32:14 +00005741Expr *ASTNodeImporter::VisitCStyleCastExpr(CStyleCastExpr *E) {
5742 QualType T = Importer.Import(E->getType());
5743 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005744 return nullptr;
5745
Douglas Gregor5481d322010-02-19 01:32:14 +00005746 Expr *SubExpr = Importer.Import(E->getSubExpr());
5747 if (!SubExpr)
Craig Topper36250ad2014-05-12 05:36:57 +00005748 return nullptr;
Douglas Gregor5481d322010-02-19 01:32:14 +00005749
5750 TypeSourceInfo *TInfo = Importer.Import(E->getTypeInfoAsWritten());
5751 if (!TInfo && E->getTypeInfoAsWritten())
Craig Topper36250ad2014-05-12 05:36:57 +00005752 return nullptr;
5753
John McCallcf142162010-08-07 06:22:56 +00005754 CXXCastPath BasePath;
5755 if (ImportCastPath(E, BasePath))
Craig Topper36250ad2014-05-12 05:36:57 +00005756 return nullptr;
John McCallcf142162010-08-07 06:22:56 +00005757
John McCall7decc9e2010-11-18 06:31:45 +00005758 return CStyleCastExpr::Create(Importer.getToContext(), T,
5759 E->getValueKind(), E->getCastKind(),
John McCallcf142162010-08-07 06:22:56 +00005760 SubExpr, &BasePath, TInfo,
5761 Importer.Import(E->getLParenLoc()),
5762 Importer.Import(E->getRParenLoc()));
Douglas Gregor5481d322010-02-19 01:32:14 +00005763}
5764
Sean Callanan59721b32015-04-28 18:41:46 +00005765Expr *ASTNodeImporter::VisitCXXConstructExpr(CXXConstructExpr *E) {
5766 QualType T = Importer.Import(E->getType());
5767 if (T.isNull())
5768 return nullptr;
5769
Richard Smithc2bebe92016-05-11 20:37:46 +00005770 NamedDecl *ToFound =
5771 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl()));
5772 if (!ToFound)
5773 return nullptr;
5774
Sean Callanan59721b32015-04-28 18:41:46 +00005775 CXXConstructorDecl *ToCCD =
5776 dyn_cast<CXXConstructorDecl>(Importer.Import(E->getConstructor()));
Richard Smithc2bebe92016-05-11 20:37:46 +00005777 if (!ToCCD)
Sean Callanan59721b32015-04-28 18:41:46 +00005778 return nullptr;
5779
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005780 SmallVector<Expr *, 6> ToArgs(E->getNumArgs());
5781 if (ImportArrayChecked(E->getArgs(), E->getArgs() + E->getNumArgs(),
5782 ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005783 return nullptr;
Sean Callanan59721b32015-04-28 18:41:46 +00005784
5785 return CXXConstructExpr::Create(Importer.getToContext(), T,
5786 Importer.Import(E->getLocation()),
Richard Smithc2bebe92016-05-11 20:37:46 +00005787 ToFound, ToCCD, E->isElidable(),
Sean Callanan59721b32015-04-28 18:41:46 +00005788 ToArgs, E->hadMultipleCandidates(),
5789 E->isListInitialization(),
5790 E->isStdInitListInitialization(),
5791 E->requiresZeroInitialization(),
5792 E->getConstructionKind(),
5793 Importer.Import(E->getParenOrBraceRange()));
5794}
5795
Sean Callanan8bca9962016-03-28 21:43:01 +00005796Expr *ASTNodeImporter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
5797 QualType T = Importer.Import(E->getType());
5798 if (T.isNull())
5799 return nullptr;
5800
5801 Expr *ToFn = Importer.Import(E->getCallee());
5802 if (!ToFn)
5803 return nullptr;
5804
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005805 SmallVector<Expr *, 4> ToArgs(E->getNumArgs());
Sean Callanan8bca9962016-03-28 21:43:01 +00005806
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005807 if (ImportArrayChecked(E->arg_begin(), E->arg_end(), ToArgs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005808 return nullptr;
5809
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005810 return new (Importer.getToContext()) CXXMemberCallExpr(
5811 Importer.getToContext(), ToFn, ToArgs, T, E->getValueKind(),
5812 Importer.Import(E->getRParenLoc()));
Sean Callanan8bca9962016-03-28 21:43:01 +00005813}
5814
5815Expr *ASTNodeImporter::VisitCXXThisExpr(CXXThisExpr *E) {
5816 QualType T = Importer.Import(E->getType());
5817 if (T.isNull())
5818 return nullptr;
5819
5820 return new (Importer.getToContext())
5821 CXXThisExpr(Importer.Import(E->getLocation()), T, E->isImplicit());
5822}
5823
5824Expr *ASTNodeImporter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
5825 QualType T = Importer.Import(E->getType());
5826 if (T.isNull())
5827 return nullptr;
5828
5829 return new (Importer.getToContext())
5830 CXXBoolLiteralExpr(E->getValue(), T, Importer.Import(E->getLocation()));
5831}
5832
5833
Sean Callanan59721b32015-04-28 18:41:46 +00005834Expr *ASTNodeImporter::VisitMemberExpr(MemberExpr *E) {
5835 QualType T = Importer.Import(E->getType());
5836 if (T.isNull())
5837 return nullptr;
5838
5839 Expr *ToBase = Importer.Import(E->getBase());
5840 if (!ToBase && E->getBase())
5841 return nullptr;
5842
5843 ValueDecl *ToMember = dyn_cast<ValueDecl>(Importer.Import(E->getMemberDecl()));
5844 if (!ToMember && E->getMemberDecl())
5845 return nullptr;
5846
5847 DeclAccessPair ToFoundDecl = DeclAccessPair::make(
5848 dyn_cast<NamedDecl>(Importer.Import(E->getFoundDecl().getDecl())),
5849 E->getFoundDecl().getAccess());
5850
5851 DeclarationNameInfo ToMemberNameInfo(
5852 Importer.Import(E->getMemberNameInfo().getName()),
5853 Importer.Import(E->getMemberNameInfo().getLoc()));
5854
5855 if (E->hasExplicitTemplateArgs()) {
5856 return nullptr; // FIXME: handle template arguments
5857 }
5858
5859 return MemberExpr::Create(Importer.getToContext(), ToBase,
5860 E->isArrow(),
5861 Importer.Import(E->getOperatorLoc()),
5862 Importer.Import(E->getQualifierLoc()),
5863 Importer.Import(E->getTemplateKeywordLoc()),
5864 ToMember, ToFoundDecl, ToMemberNameInfo,
5865 nullptr, T, E->getValueKind(),
5866 E->getObjectKind());
5867}
5868
5869Expr *ASTNodeImporter::VisitCallExpr(CallExpr *E) {
5870 QualType T = Importer.Import(E->getType());
5871 if (T.isNull())
5872 return nullptr;
5873
5874 Expr *ToCallee = Importer.Import(E->getCallee());
5875 if (!ToCallee && E->getCallee())
5876 return nullptr;
5877
5878 unsigned NumArgs = E->getNumArgs();
5879
5880 llvm::SmallVector<Expr *, 2> ToArgs(NumArgs);
5881
5882 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai) {
5883 Expr *FromArg = E->getArg(ai);
5884 Expr *ToArg = Importer.Import(FromArg);
5885 if (!ToArg)
5886 return nullptr;
5887 ToArgs[ai] = ToArg;
5888 }
5889
5890 Expr **ToArgs_Copied = new (Importer.getToContext())
5891 Expr*[NumArgs];
5892
5893 for (unsigned ai = 0, ae = NumArgs; ai != ae; ++ai)
5894 ToArgs_Copied[ai] = ToArgs[ai];
5895
5896 return new (Importer.getToContext())
5897 CallExpr(Importer.getToContext(), ToCallee,
Craig Topperc005cc02015-09-27 03:44:08 +00005898 llvm::makeArrayRef(ToArgs_Copied, NumArgs), T, E->getValueKind(),
Sean Callanan59721b32015-04-28 18:41:46 +00005899 Importer.Import(E->getRParenLoc()));
5900}
5901
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005902Expr *ASTNodeImporter::VisitInitListExpr(InitListExpr *ILE) {
5903 QualType T = Importer.Import(ILE->getType());
Sean Callanan8bca9962016-03-28 21:43:01 +00005904 if (T.isNull())
5905 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005906
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005907 llvm::SmallVector<Expr *, 4> Exprs(ILE->getNumInits());
5908 if (ImportArrayChecked(
5909 ILE->getInits(), ILE->getInits() + ILE->getNumInits(), Exprs.begin()))
Sean Callanan8bca9962016-03-28 21:43:01 +00005910 return nullptr;
Sean Callanan8bca9962016-03-28 21:43:01 +00005911
Artem Dergachev4e7c6fd2016-04-14 11:51:27 +00005912 ASTContext &ToCtx = Importer.getToContext();
5913 InitListExpr *To = new (ToCtx) InitListExpr(
5914 ToCtx, Importer.Import(ILE->getLBraceLoc()),
5915 Exprs, Importer.Import(ILE->getLBraceLoc()));
5916 To->setType(T);
5917
5918 if (ILE->hasArrayFiller()) {
5919 Expr *Filler = Importer.Import(ILE->getArrayFiller());
5920 if (!Filler)
5921 return nullptr;
5922 To->setArrayFiller(Filler);
5923 }
5924
5925 if (FieldDecl *FromFD = ILE->getInitializedFieldInUnion()) {
5926 FieldDecl *ToFD = cast_or_null<FieldDecl>(Importer.Import(FromFD));
5927 if (!ToFD)
5928 return nullptr;
5929 To->setInitializedFieldInUnion(ToFD);
5930 }
5931
5932 if (InitListExpr *SyntForm = ILE->getSyntacticForm()) {
5933 InitListExpr *ToSyntForm = cast_or_null<InitListExpr>(
5934 Importer.Import(SyntForm));
5935 if (!ToSyntForm)
5936 return nullptr;
5937 To->setSyntacticForm(ToSyntForm);
5938 }
5939
5940 To->sawArrayRangeDesignator(ILE->hadArrayRangeDesignator());
5941 To->setValueDependent(ILE->isValueDependent());
5942 To->setInstantiationDependent(ILE->isInstantiationDependent());
5943
5944 return To;
Sean Callanan8bca9962016-03-28 21:43:01 +00005945}
5946
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00005947ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
Douglas Gregor0a791672011-01-18 03:11:38 +00005948 ASTContext &FromContext, FileManager &FromFileManager,
5949 bool MinimalImport)
Douglas Gregor96e578d2010-02-05 17:54:41 +00005950 : ToContext(ToContext), FromContext(FromContext),
Douglas Gregor0a791672011-01-18 03:11:38 +00005951 ToFileManager(ToFileManager), FromFileManager(FromFileManager),
Richard Smith5bb4cdf2012-12-20 02:22:15 +00005952 Minimal(MinimalImport), LastDiagFromFrom(false)
Douglas Gregor0a791672011-01-18 03:11:38 +00005953{
Douglas Gregor62d311f2010-02-09 19:21:46 +00005954 ImportedDecls[FromContext.getTranslationUnitDecl()]
5955 = ToContext.getTranslationUnitDecl();
5956}
5957
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +00005958ASTImporter::~ASTImporter() { }
Douglas Gregor96e578d2010-02-05 17:54:41 +00005959
5960QualType ASTImporter::Import(QualType FromT) {
5961 if (FromT.isNull())
5962 return QualType();
John McCall424cec92011-01-19 06:33:43 +00005963
5964 const Type *fromTy = FromT.getTypePtr();
Douglas Gregor96e578d2010-02-05 17:54:41 +00005965
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005966 // Check whether we've already imported this type.
John McCall424cec92011-01-19 06:33:43 +00005967 llvm::DenseMap<const Type *, const Type *>::iterator Pos
5968 = ImportedTypes.find(fromTy);
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005969 if (Pos != ImportedTypes.end())
John McCall424cec92011-01-19 06:33:43 +00005970 return ToContext.getQualifiedType(Pos->second, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005971
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005972 // Import the type
Douglas Gregor96e578d2010-02-05 17:54:41 +00005973 ASTNodeImporter Importer(*this);
John McCall424cec92011-01-19 06:33:43 +00005974 QualType ToT = Importer.Visit(fromTy);
Douglas Gregor96e578d2010-02-05 17:54:41 +00005975 if (ToT.isNull())
5976 return ToT;
5977
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005978 // Record the imported type.
John McCall424cec92011-01-19 06:33:43 +00005979 ImportedTypes[fromTy] = ToT.getTypePtr();
Douglas Gregorf65bbb32010-02-08 15:18:58 +00005980
John McCall424cec92011-01-19 06:33:43 +00005981 return ToContext.getQualifiedType(ToT, FromT.getLocalQualifiers());
Douglas Gregor96e578d2010-02-05 17:54:41 +00005982}
5983
Douglas Gregor62d311f2010-02-09 19:21:46 +00005984TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005985 if (!FromTSI)
5986 return FromTSI;
5987
5988 // FIXME: For now we just create a "trivial" type source info based
Nick Lewycky19b9f952010-07-26 16:56:01 +00005989 // on the type and a single location. Implement a real version of this.
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005990 QualType T = Import(FromTSI->getType());
5991 if (T.isNull())
Craig Topper36250ad2014-05-12 05:36:57 +00005992 return nullptr;
Douglas Gregorfa7a0e52010-02-10 17:47:19 +00005993
5994 return ToContext.getTrivialTypeSourceInfo(T,
Douglas Gregore9d95f12015-07-07 03:57:35 +00005995 Import(FromTSI->getTypeLoc().getLocStart()));
Douglas Gregor62d311f2010-02-09 19:21:46 +00005996}
5997
Sean Callanan59721b32015-04-28 18:41:46 +00005998Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
5999 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
6000 if (Pos != ImportedDecls.end()) {
6001 Decl *ToD = Pos->second;
6002 ASTNodeImporter(*this).ImportDefinitionIfNeeded(FromD, ToD);
6003 return ToD;
6004 } else {
6005 return nullptr;
6006 }
6007}
6008
Douglas Gregor62d311f2010-02-09 19:21:46 +00006009Decl *ASTImporter::Import(Decl *FromD) {
6010 if (!FromD)
Craig Topper36250ad2014-05-12 05:36:57 +00006011 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006012
Douglas Gregord451ea92011-07-29 23:31:30 +00006013 ASTNodeImporter Importer(*this);
6014
Douglas Gregor62d311f2010-02-09 19:21:46 +00006015 // Check whether we've already imported this declaration.
6016 llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
Douglas Gregord451ea92011-07-29 23:31:30 +00006017 if (Pos != ImportedDecls.end()) {
6018 Decl *ToD = Pos->second;
6019 Importer.ImportDefinitionIfNeeded(FromD, ToD);
6020 return ToD;
6021 }
Douglas Gregor62d311f2010-02-09 19:21:46 +00006022
6023 // Import the type
Douglas Gregor62d311f2010-02-09 19:21:46 +00006024 Decl *ToD = Importer.Visit(FromD);
6025 if (!ToD)
Craig Topper36250ad2014-05-12 05:36:57 +00006026 return nullptr;
6027
Douglas Gregor62d311f2010-02-09 19:21:46 +00006028 // Record the imported declaration.
6029 ImportedDecls[FromD] = ToD;
Douglas Gregorb4964f72010-02-15 23:54:17 +00006030
6031 if (TagDecl *FromTag = dyn_cast<TagDecl>(FromD)) {
6032 // Keep track of anonymous tags that have an associated typedef.
Richard Smithdda56e42011-04-15 14:24:37 +00006033 if (FromTag->getTypedefNameForAnonDecl())
Douglas Gregorb4964f72010-02-15 23:54:17 +00006034 AnonTagsWithPendingTypedefs.push_back(FromTag);
Richard Smithdda56e42011-04-15 14:24:37 +00006035 } else if (TypedefNameDecl *FromTypedef = dyn_cast<TypedefNameDecl>(FromD)) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006036 // When we've finished transforming a typedef, see whether it was the
6037 // typedef for an anonymous tag.
Craig Topper2341c0d2013-07-04 03:08:24 +00006038 for (SmallVectorImpl<TagDecl *>::iterator
Douglas Gregorb4964f72010-02-15 23:54:17 +00006039 FromTag = AnonTagsWithPendingTypedefs.begin(),
6040 FromTagEnd = AnonTagsWithPendingTypedefs.end();
6041 FromTag != FromTagEnd; ++FromTag) {
Richard Smithdda56e42011-04-15 14:24:37 +00006042 if ((*FromTag)->getTypedefNameForAnonDecl() == FromTypedef) {
Douglas Gregorb4964f72010-02-15 23:54:17 +00006043 if (TagDecl *ToTag = cast_or_null<TagDecl>(Import(*FromTag))) {
6044 // We found the typedef for an anonymous tag; link them.
Richard Smithdda56e42011-04-15 14:24:37 +00006045 ToTag->setTypedefNameForAnonDecl(cast<TypedefNameDecl>(ToD));
Douglas Gregorb4964f72010-02-15 23:54:17 +00006046 AnonTagsWithPendingTypedefs.erase(FromTag);
6047 break;
6048 }
6049 }
6050 }
6051 }
6052
Douglas Gregor62d311f2010-02-09 19:21:46 +00006053 return ToD;
6054}
6055
6056DeclContext *ASTImporter::ImportContext(DeclContext *FromDC) {
6057 if (!FromDC)
6058 return FromDC;
6059
Douglas Gregor95d82832012-01-24 18:36:04 +00006060 DeclContext *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
Douglas Gregor2e15c842012-02-01 21:00:38 +00006061 if (!ToDC)
Craig Topper36250ad2014-05-12 05:36:57 +00006062 return nullptr;
6063
Douglas Gregor2e15c842012-02-01 21:00:38 +00006064 // When we're using a record/enum/Objective-C class/protocol as a context, we
6065 // need it to have a definition.
6066 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(ToDC)) {
Douglas Gregor63db9712012-01-25 01:13:20 +00006067 RecordDecl *FromRecord = cast<RecordDecl>(FromDC);
Douglas Gregor2e15c842012-02-01 21:00:38 +00006068 if (ToRecord->isCompleteDefinition()) {
6069 // Do nothing.
6070 } else if (FromRecord->isCompleteDefinition()) {
6071 ASTNodeImporter(*this).ImportDefinition(FromRecord, ToRecord,
6072 ASTNodeImporter::IDK_Basic);
6073 } else {
6074 CompleteDecl(ToRecord);
6075 }
6076 } else if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(ToDC)) {
6077 EnumDecl *FromEnum = cast<EnumDecl>(FromDC);
6078 if (ToEnum->isCompleteDefinition()) {
6079 // Do nothing.
6080 } else if (FromEnum->isCompleteDefinition()) {
6081 ASTNodeImporter(*this).ImportDefinition(FromEnum, ToEnum,
6082 ASTNodeImporter::IDK_Basic);
6083 } else {
6084 CompleteDecl(ToEnum);
6085 }
6086 } else if (ObjCInterfaceDecl *ToClass = dyn_cast<ObjCInterfaceDecl>(ToDC)) {
6087 ObjCInterfaceDecl *FromClass = cast<ObjCInterfaceDecl>(FromDC);
6088 if (ToClass->getDefinition()) {
6089 // Do nothing.
6090 } else if (ObjCInterfaceDecl *FromDef = FromClass->getDefinition()) {
6091 ASTNodeImporter(*this).ImportDefinition(FromDef, ToClass,
6092 ASTNodeImporter::IDK_Basic);
6093 } else {
6094 CompleteDecl(ToClass);
6095 }
6096 } else if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(ToDC)) {
6097 ObjCProtocolDecl *FromProto = cast<ObjCProtocolDecl>(FromDC);
6098 if (ToProto->getDefinition()) {
6099 // Do nothing.
6100 } else if (ObjCProtocolDecl *FromDef = FromProto->getDefinition()) {
6101 ASTNodeImporter(*this).ImportDefinition(FromDef, ToProto,
6102 ASTNodeImporter::IDK_Basic);
6103 } else {
6104 CompleteDecl(ToProto);
6105 }
Douglas Gregor95d82832012-01-24 18:36:04 +00006106 }
6107
6108 return ToDC;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006109}
6110
6111Expr *ASTImporter::Import(Expr *FromE) {
6112 if (!FromE)
Craig Topper36250ad2014-05-12 05:36:57 +00006113 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006114
6115 return cast_or_null<Expr>(Import(cast<Stmt>(FromE)));
6116}
6117
6118Stmt *ASTImporter::Import(Stmt *FromS) {
6119 if (!FromS)
Craig Topper36250ad2014-05-12 05:36:57 +00006120 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006121
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006122 // Check whether we've already imported this declaration.
6123 llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
6124 if (Pos != ImportedStmts.end())
6125 return Pos->second;
6126
6127 // Import the type
6128 ASTNodeImporter Importer(*this);
6129 Stmt *ToS = Importer.Visit(FromS);
6130 if (!ToS)
Craig Topper36250ad2014-05-12 05:36:57 +00006131 return nullptr;
6132
Douglas Gregor7eeb5972010-02-11 19:21:55 +00006133 // Record the imported declaration.
6134 ImportedStmts[FromS] = ToS;
6135 return ToS;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006136}
6137
6138NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
6139 if (!FromNNS)
Craig Topper36250ad2014-05-12 05:36:57 +00006140 return nullptr;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006141
Douglas Gregor90ebf252011-04-27 16:48:40 +00006142 NestedNameSpecifier *prefix = Import(FromNNS->getPrefix());
6143
6144 switch (FromNNS->getKind()) {
6145 case NestedNameSpecifier::Identifier:
6146 if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) {
6147 return NestedNameSpecifier::Create(ToContext, prefix, II);
6148 }
Craig Topper36250ad2014-05-12 05:36:57 +00006149 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006150
6151 case NestedNameSpecifier::Namespace:
6152 if (NamespaceDecl *NS =
6153 cast<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) {
6154 return NestedNameSpecifier::Create(ToContext, prefix, NS);
6155 }
Craig Topper36250ad2014-05-12 05:36:57 +00006156 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006157
6158 case NestedNameSpecifier::NamespaceAlias:
6159 if (NamespaceAliasDecl *NSAD =
6160 cast<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) {
6161 return NestedNameSpecifier::Create(ToContext, prefix, NSAD);
6162 }
Craig Topper36250ad2014-05-12 05:36:57 +00006163 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006164
6165 case NestedNameSpecifier::Global:
6166 return NestedNameSpecifier::GlobalSpecifier(ToContext);
6167
Nikola Smiljanic67860242014-09-26 00:28:20 +00006168 case NestedNameSpecifier::Super:
6169 if (CXXRecordDecl *RD =
6170 cast<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) {
6171 return NestedNameSpecifier::SuperSpecifier(ToContext, RD);
6172 }
6173 return nullptr;
6174
Douglas Gregor90ebf252011-04-27 16:48:40 +00006175 case NestedNameSpecifier::TypeSpec:
6176 case NestedNameSpecifier::TypeSpecWithTemplate: {
6177 QualType T = Import(QualType(FromNNS->getAsType(), 0u));
6178 if (!T.isNull()) {
6179 bool bTemplate = FromNNS->getKind() ==
6180 NestedNameSpecifier::TypeSpecWithTemplate;
6181 return NestedNameSpecifier::Create(ToContext, prefix,
6182 bTemplate, T.getTypePtr());
6183 }
6184 }
Craig Topper36250ad2014-05-12 05:36:57 +00006185 return nullptr;
Douglas Gregor90ebf252011-04-27 16:48:40 +00006186 }
6187
6188 llvm_unreachable("Invalid nested name specifier kind");
Douglas Gregor62d311f2010-02-09 19:21:46 +00006189}
6190
Douglas Gregor14454802011-02-25 02:25:35 +00006191NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
6192 // FIXME: Implement!
6193 return NestedNameSpecifierLoc();
6194}
6195
Douglas Gregore2e50d332010-12-01 01:36:18 +00006196TemplateName ASTImporter::Import(TemplateName From) {
6197 switch (From.getKind()) {
6198 case TemplateName::Template:
6199 if (TemplateDecl *ToTemplate
6200 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6201 return TemplateName(ToTemplate);
6202
6203 return TemplateName();
6204
6205 case TemplateName::OverloadedTemplate: {
6206 OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
6207 UnresolvedSet<2> ToTemplates;
6208 for (OverloadedTemplateStorage::iterator I = FromStorage->begin(),
6209 E = FromStorage->end();
6210 I != E; ++I) {
6211 if (NamedDecl *To = cast_or_null<NamedDecl>(Import(*I)))
6212 ToTemplates.addDecl(To);
6213 else
6214 return TemplateName();
6215 }
6216 return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
6217 ToTemplates.end());
6218 }
6219
6220 case TemplateName::QualifiedTemplate: {
6221 QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
6222 NestedNameSpecifier *Qualifier = Import(QTN->getQualifier());
6223 if (!Qualifier)
6224 return TemplateName();
6225
6226 if (TemplateDecl *ToTemplate
6227 = cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl())))
6228 return ToContext.getQualifiedTemplateName(Qualifier,
6229 QTN->hasTemplateKeyword(),
6230 ToTemplate);
6231
6232 return TemplateName();
6233 }
6234
6235 case TemplateName::DependentTemplate: {
6236 DependentTemplateName *DTN = From.getAsDependentTemplateName();
6237 NestedNameSpecifier *Qualifier = Import(DTN->getQualifier());
6238 if (!Qualifier)
6239 return TemplateName();
6240
6241 if (DTN->isIdentifier()) {
6242 return ToContext.getDependentTemplateName(Qualifier,
6243 Import(DTN->getIdentifier()));
6244 }
6245
6246 return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator());
6247 }
John McCalld9dfe3a2011-06-30 08:33:18 +00006248
6249 case TemplateName::SubstTemplateTemplateParm: {
6250 SubstTemplateTemplateParmStorage *subst
6251 = From.getAsSubstTemplateTemplateParm();
6252 TemplateTemplateParmDecl *param
6253 = cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter()));
6254 if (!param)
6255 return TemplateName();
6256
6257 TemplateName replacement = Import(subst->getReplacement());
6258 if (replacement.isNull()) return TemplateName();
6259
6260 return ToContext.getSubstTemplateTemplateParm(param, replacement);
6261 }
Douglas Gregor5590be02011-01-15 06:45:20 +00006262
6263 case TemplateName::SubstTemplateTemplateParmPack: {
6264 SubstTemplateTemplateParmPackStorage *SubstPack
6265 = From.getAsSubstTemplateTemplateParmPack();
6266 TemplateTemplateParmDecl *Param
6267 = cast_or_null<TemplateTemplateParmDecl>(
6268 Import(SubstPack->getParameterPack()));
6269 if (!Param)
6270 return TemplateName();
6271
6272 ASTNodeImporter Importer(*this);
6273 TemplateArgument ArgPack
6274 = Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
6275 if (ArgPack.isNull())
6276 return TemplateName();
6277
6278 return ToContext.getSubstTemplateTemplateParmPack(Param, ArgPack);
6279 }
Douglas Gregore2e50d332010-12-01 01:36:18 +00006280 }
6281
6282 llvm_unreachable("Invalid template name kind");
Douglas Gregore2e50d332010-12-01 01:36:18 +00006283}
6284
Douglas Gregor62d311f2010-02-09 19:21:46 +00006285SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
6286 if (FromLoc.isInvalid())
6287 return SourceLocation();
6288
Douglas Gregor811663e2010-02-10 00:15:17 +00006289 SourceManager &FromSM = FromContext.getSourceManager();
6290
6291 // For now, map everything down to its spelling location, so that we
Chandler Carruth25366412011-07-15 00:04:35 +00006292 // don't have to import macro expansions.
6293 // FIXME: Import macro expansions!
Douglas Gregor811663e2010-02-10 00:15:17 +00006294 FromLoc = FromSM.getSpellingLoc(FromLoc);
6295 std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
6296 SourceManager &ToSM = ToContext.getSourceManager();
Sean Callanan238d8972014-12-10 01:26:39 +00006297 FileID ToFileID = Import(Decomposed.first);
6298 if (ToFileID.isInvalid())
6299 return SourceLocation();
Sean Callanan59721b32015-04-28 18:41:46 +00006300 SourceLocation ret = ToSM.getLocForStartOfFile(ToFileID)
6301 .getLocWithOffset(Decomposed.second);
6302 return ret;
Douglas Gregor62d311f2010-02-09 19:21:46 +00006303}
6304
6305SourceRange ASTImporter::Import(SourceRange FromRange) {
6306 return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd()));
6307}
6308
Douglas Gregor811663e2010-02-10 00:15:17 +00006309FileID ASTImporter::Import(FileID FromID) {
Sebastian Redl99219f12010-09-30 01:03:06 +00006310 llvm::DenseMap<FileID, FileID>::iterator Pos
6311 = ImportedFileIDs.find(FromID);
Douglas Gregor811663e2010-02-10 00:15:17 +00006312 if (Pos != ImportedFileIDs.end())
6313 return Pos->second;
6314
6315 SourceManager &FromSM = FromContext.getSourceManager();
6316 SourceManager &ToSM = ToContext.getSourceManager();
6317 const SrcMgr::SLocEntry &FromSLoc = FromSM.getSLocEntry(FromID);
Chandler Carruth25366412011-07-15 00:04:35 +00006318 assert(FromSLoc.isFile() && "Cannot handle macro expansions yet");
Douglas Gregor811663e2010-02-10 00:15:17 +00006319
6320 // Include location of this file.
6321 SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc());
6322
6323 // Map the FileID for to the "to" source manager.
6324 FileID ToID;
6325 const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
Sean Callanan25d34af2015-04-30 00:44:21 +00006326 if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
Douglas Gregor811663e2010-02-10 00:15:17 +00006327 // FIXME: We probably want to use getVirtualFile(), so we don't hit the
6328 // disk again
6329 // FIXME: We definitely want to re-use the existing MemoryBuffer, rather
6330 // than mmap the files several times.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00006331 const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName());
Sean Callanan238d8972014-12-10 01:26:39 +00006332 if (!Entry)
6333 return FileID();
Douglas Gregor811663e2010-02-10 00:15:17 +00006334 ToID = ToSM.createFileID(Entry, ToIncludeLoc,
6335 FromSLoc.getFile().getFileCharacteristic());
6336 } else {
6337 // FIXME: We want to re-use the existing MemoryBuffer!
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006338 const llvm::MemoryBuffer *
6339 FromBuf = Cache->getBuffer(FromContext.getDiagnostics(), FromSM);
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006340 std::unique_ptr<llvm::MemoryBuffer> ToBuf
Chris Lattner58c79342010-04-05 22:42:27 +00006341 = llvm::MemoryBuffer::getMemBufferCopy(FromBuf->getBuffer(),
Douglas Gregor811663e2010-02-10 00:15:17 +00006342 FromBuf->getBufferIdentifier());
David Blaikie50a5f972014-08-29 07:59:55 +00006343 ToID = ToSM.createFileID(std::move(ToBuf),
Rafael Espindolad87f8d72014-08-27 20:03:29 +00006344 FromSLoc.getFile().getFileCharacteristic());
Douglas Gregor811663e2010-02-10 00:15:17 +00006345 }
6346
6347
Sebastian Redl99219f12010-09-30 01:03:06 +00006348 ImportedFileIDs[FromID] = ToID;
Douglas Gregor811663e2010-02-10 00:15:17 +00006349 return ToID;
6350}
6351
Douglas Gregor0a791672011-01-18 03:11:38 +00006352void ASTImporter::ImportDefinition(Decl *From) {
6353 Decl *To = Import(From);
6354 if (!To)
6355 return;
6356
6357 if (DeclContext *FromDC = cast<DeclContext>(From)) {
6358 ASTNodeImporter Importer(*this);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006359
6360 if (RecordDecl *ToRecord = dyn_cast<RecordDecl>(To)) {
6361 if (!ToRecord->getDefinition()) {
6362 Importer.ImportDefinition(cast<RecordDecl>(FromDC), ToRecord,
Douglas Gregor95d82832012-01-24 18:36:04 +00006363 ASTNodeImporter::IDK_Everything);
Sean Callanan53a6bff2011-07-19 22:38:25 +00006364 return;
6365 }
6366 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006367
6368 if (EnumDecl *ToEnum = dyn_cast<EnumDecl>(To)) {
6369 if (!ToEnum->getDefinition()) {
6370 Importer.ImportDefinition(cast<EnumDecl>(FromDC), ToEnum,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006371 ASTNodeImporter::IDK_Everything);
Douglas Gregord451ea92011-07-29 23:31:30 +00006372 return;
6373 }
6374 }
Douglas Gregor2aa53772012-01-24 17:42:07 +00006375
6376 if (ObjCInterfaceDecl *ToIFace = dyn_cast<ObjCInterfaceDecl>(To)) {
6377 if (!ToIFace->getDefinition()) {
6378 Importer.ImportDefinition(cast<ObjCInterfaceDecl>(FromDC), ToIFace,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006379 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006380 return;
6381 }
6382 }
Douglas Gregord451ea92011-07-29 23:31:30 +00006383
Douglas Gregor2aa53772012-01-24 17:42:07 +00006384 if (ObjCProtocolDecl *ToProto = dyn_cast<ObjCProtocolDecl>(To)) {
6385 if (!ToProto->getDefinition()) {
6386 Importer.ImportDefinition(cast<ObjCProtocolDecl>(FromDC), ToProto,
Douglas Gregor2e15c842012-02-01 21:00:38 +00006387 ASTNodeImporter::IDK_Everything);
Douglas Gregor2aa53772012-01-24 17:42:07 +00006388 return;
6389 }
6390 }
6391
Douglas Gregor0a791672011-01-18 03:11:38 +00006392 Importer.ImportDeclContext(FromDC, true);
6393 }
6394}
6395
Douglas Gregor96e578d2010-02-05 17:54:41 +00006396DeclarationName ASTImporter::Import(DeclarationName FromName) {
6397 if (!FromName)
6398 return DeclarationName();
6399
6400 switch (FromName.getNameKind()) {
6401 case DeclarationName::Identifier:
6402 return Import(FromName.getAsIdentifierInfo());
6403
6404 case DeclarationName::ObjCZeroArgSelector:
6405 case DeclarationName::ObjCOneArgSelector:
6406 case DeclarationName::ObjCMultiArgSelector:
6407 return Import(FromName.getObjCSelector());
6408
6409 case DeclarationName::CXXConstructorName: {
6410 QualType T = Import(FromName.getCXXNameType());
6411 if (T.isNull())
6412 return DeclarationName();
6413
6414 return ToContext.DeclarationNames.getCXXConstructorName(
6415 ToContext.getCanonicalType(T));
6416 }
6417
6418 case DeclarationName::CXXDestructorName: {
6419 QualType T = Import(FromName.getCXXNameType());
6420 if (T.isNull())
6421 return DeclarationName();
6422
6423 return ToContext.DeclarationNames.getCXXDestructorName(
6424 ToContext.getCanonicalType(T));
6425 }
6426
6427 case DeclarationName::CXXConversionFunctionName: {
6428 QualType T = Import(FromName.getCXXNameType());
6429 if (T.isNull())
6430 return DeclarationName();
6431
6432 return ToContext.DeclarationNames.getCXXConversionFunctionName(
6433 ToContext.getCanonicalType(T));
6434 }
6435
6436 case DeclarationName::CXXOperatorName:
6437 return ToContext.DeclarationNames.getCXXOperatorName(
6438 FromName.getCXXOverloadedOperator());
6439
6440 case DeclarationName::CXXLiteralOperatorName:
6441 return ToContext.DeclarationNames.getCXXLiteralOperatorName(
6442 Import(FromName.getCXXLiteralIdentifier()));
6443
6444 case DeclarationName::CXXUsingDirective:
6445 // FIXME: STATICS!
6446 return DeclarationName::getUsingDirectiveName();
6447 }
6448
David Blaikiee4d798f2012-01-20 21:50:17 +00006449 llvm_unreachable("Invalid DeclarationName Kind!");
Douglas Gregor96e578d2010-02-05 17:54:41 +00006450}
6451
Douglas Gregore2e50d332010-12-01 01:36:18 +00006452IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
Douglas Gregor96e578d2010-02-05 17:54:41 +00006453 if (!FromId)
Craig Topper36250ad2014-05-12 05:36:57 +00006454 return nullptr;
Douglas Gregor96e578d2010-02-05 17:54:41 +00006455
6456 return &ToContext.Idents.get(FromId->getName());
6457}
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006458
Douglas Gregor43f54792010-02-17 02:12:47 +00006459Selector ASTImporter::Import(Selector FromSel) {
6460 if (FromSel.isNull())
6461 return Selector();
6462
Chris Lattner0e62c1c2011-07-23 10:55:15 +00006463 SmallVector<IdentifierInfo *, 4> Idents;
Douglas Gregor43f54792010-02-17 02:12:47 +00006464 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
6465 for (unsigned I = 1, N = FromSel.getNumArgs(); I < N; ++I)
6466 Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(I)));
6467 return ToContext.Selectors.getSelector(FromSel.getNumArgs(), Idents.data());
6468}
6469
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006470DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
6471 DeclContext *DC,
6472 unsigned IDNS,
6473 NamedDecl **Decls,
6474 unsigned NumDecls) {
6475 return Name;
6476}
6477
6478DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006479 if (LastDiagFromFrom)
6480 ToContext.getDiagnostics().notePriorDiagnosticFrom(
6481 FromContext.getDiagnostics());
6482 LastDiagFromFrom = false;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006483 return ToContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006484}
6485
6486DiagnosticBuilder ASTImporter::FromDiag(SourceLocation Loc, unsigned DiagID) {
Richard Smith5bb4cdf2012-12-20 02:22:15 +00006487 if (!LastDiagFromFrom)
6488 FromContext.getDiagnostics().notePriorDiagnosticFrom(
6489 ToContext.getDiagnostics());
6490 LastDiagFromFrom = true;
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +00006491 return FromContext.getDiagnostics().Report(Loc, DiagID);
Douglas Gregor3aed6cd2010-02-08 21:09:39 +00006492}
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006493
Douglas Gregor2e15c842012-02-01 21:00:38 +00006494void ASTImporter::CompleteDecl (Decl *D) {
6495 if (ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
6496 if (!ID->getDefinition())
6497 ID->startDefinition();
6498 }
6499 else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) {
6500 if (!PD->getDefinition())
6501 PD->startDefinition();
6502 }
6503 else if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
6504 if (!TD->getDefinition() && !TD->isBeingDefined()) {
6505 TD->startDefinition();
6506 TD->setCompleteDefinition(true);
6507 }
6508 }
6509 else {
6510 assert (0 && "CompleteDecl called on a Decl that can't be completed");
6511 }
6512}
6513
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006514Decl *ASTImporter::Imported(Decl *From, Decl *To) {
Sean Callanan8bca9962016-03-28 21:43:01 +00006515 if (From->hasAttrs()) {
6516 for (Attr *FromAttr : From->getAttrs())
6517 To->addAttr(FromAttr->clone(To->getASTContext()));
6518 }
6519 if (From->isUsed()) {
6520 To->setIsUsed();
6521 }
Douglas Gregor8cdbe642010-02-12 23:44:20 +00006522 ImportedDecls[From] = To;
6523 return To;
Daniel Dunbar9ced5422010-02-13 20:24:39 +00006524}
Douglas Gregorb4964f72010-02-15 23:54:17 +00006525
Douglas Gregordd6006f2012-07-17 21:16:27 +00006526bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
6527 bool Complain) {
John McCall424cec92011-01-19 06:33:43 +00006528 llvm::DenseMap<const Type *, const Type *>::iterator Pos
Douglas Gregorb4964f72010-02-15 23:54:17 +00006529 = ImportedTypes.find(From.getTypePtr());
6530 if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
6531 return true;
6532
Douglas Gregordd6006f2012-07-17 21:16:27 +00006533 StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
6534 false, Complain);
Benjamin Kramer26d19c52010-02-18 13:02:13 +00006535 return Ctx.IsStructurallyEquivalent(From, To);
Douglas Gregorb4964f72010-02-15 23:54:17 +00006536}